1
0
mirror of https://github.com/Klagarge/PokeHES.git synced 2025-03-14 14:54:33 +00:00
PokeHES/src/Entity/Player.java

131 lines
3.3 KiB
Java
Raw Normal View History

2022-05-25 15:32:53 +02:00
package Entity;
2022-06-07 21:35:13 +02:00
import java.util.Vector;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.maps.tiled.TiledMapTile;
2022-06-01 16:47:53 +02:00
import com.badlogic.gdx.math.Vector2;
2022-06-07 21:35:13 +02:00
import Control.Controller;
2022-06-09 21:07:13 +02:00
import Main.PokeMudry;
2022-06-16 14:24:20 +02:00
import Main.Settings;
2022-06-07 21:35:13 +02:00
import Screen.ScreenMap;
2022-05-25 15:32:53 +02:00
public class Player extends Character{
2022-06-01 16:47:53 +02:00
2022-06-13 23:11:17 +02:00
private int xp = 0;
2022-06-10 18:49:02 +02:00
public Enemy lastEnemy = null;
2022-06-13 21:08:11 +02:00
public boolean onEnemy = false;
2022-06-16 15:46:34 +02:00
private static final int XP_MAX = 6000;
2022-06-01 16:47:53 +02:00
public Player(int x, int y, String map) {
super("Player", x, y, "Character_flipped", map);
2022-06-16 16:59:02 +02:00
this.pv = Settings.TIME*60;
2022-06-01 16:47:53 +02:00
}
public void addXp(int xp){
2022-06-13 23:11:17 +02:00
this.xp += xp;
2022-06-01 16:47:53 +02:00
}
2022-06-14 12:51:47 +02:00
public int getXp(){
return xp;
}
2022-06-07 21:35:13 +02:00
public void manageEntity(ScreenMap sm, Controller c) {
boolean onDoor = sm.isDoor(getPosition());
2022-06-07 21:35:13 +02:00
// Do nothing if hero is already moving
if (!isMoving()) {
// Compute direction and next cell
Vector<TiledMapTile> nextCell = new Vector<>();
Player.Direction goalDirection = Player.Direction.NULL;
2022-06-13 21:08:11 +02:00
Vector2 nextPos = new Vector2(position);
2022-06-07 21:35:13 +02:00
if (c.keyStatus.get(Input.Keys.RIGHT) && !onDoor) {
2022-06-07 21:35:13 +02:00
goalDirection = Player.Direction.RIGHT;
nextCell = sm.getTile(getPosition(), 1, 0);
2022-06-09 21:07:13 +02:00
nextPos.x+=sm.tileWidth;
} else if (c.keyStatus.get(Input.Keys.LEFT) && !onDoor) {
2022-06-07 21:35:13 +02:00
goalDirection = Player.Direction.LEFT;
nextCell = sm.getTile(getPosition(), -1, 0);
2022-06-09 21:07:13 +02:00
nextPos.x-=sm.tileWidth;
} else if (c.keyStatus.get(Input.Keys.UP) && !onDoor) {
2022-06-07 21:35:13 +02:00
goalDirection = Player.Direction.UP;
nextCell = sm.getTile(getPosition(), 0, 1);
2022-06-09 21:07:13 +02:00
nextPos.y+=sm.tileHeight;
} else if (c.keyStatus.get(Input.Keys.DOWN) && !onDoor) {
2022-06-07 21:35:13 +02:00
goalDirection = Player.Direction.DOWN;
nextCell = sm.getTile(getPosition(), 0, -1);
2022-06-09 21:07:13 +02:00
nextPos.y-=sm.tileHeight;
2022-06-07 21:35:13 +02:00
}
// Is the move valid ?
if (sm.isWalkable(nextCell)) {
2022-06-09 21:07:13 +02:00
if (enemy(sm, nextPos)) {
2022-06-12 12:35:08 +02:00
turn(goalDirection);
2022-06-09 21:07:13 +02:00
System.out.println("It's a enemy !!");
} else {
setSpeed(sm.getSpeed(nextCell)*3); //TODO remove x3
2022-06-09 21:07:13 +02:00
go(goalDirection);
}
2022-06-07 21:35:13 +02:00
} else {
// Face the wall
turn(goalDirection);
}
if(onDoor){
2022-06-16 14:24:20 +02:00
long time = System.currentTimeMillis();
while (System.currentTimeMillis()-time < Settings.SWITCHMAPTIME) { }
2022-06-07 21:35:13 +02:00
String nMap = null;
Integer x = null;
Integer y = null;
try {
nMap = ScreenMap.Door.nextMap;
x = ScreenMap.Door.nextX;
y = ScreenMap.Door.nextY;
2022-06-13 21:08:11 +02:00
goalDirection = ScreenMap.Door.nextDirection;
2022-06-07 21:35:13 +02:00
} catch (Exception e) { }
ScreenMap.Door.reset();
if (nMap == null || x == null || y == null) return;
map = nMap;
2022-06-07 21:35:13 +02:00
setPosition(x*sm.tileWidth, y*sm.tileHeight);
turn(goalDirection);
System.out.println("Go to: " + map + " in " + x + " x " + y);
2022-06-07 21:35:13 +02:00
}
}
}
2022-06-09 21:07:13 +02:00
private boolean enemy(ScreenMap sm, Vector2 nextPos) {
Vector<Enemy> enemies = PokeMudry.getEnemies();
for (Enemy enemy : enemies) {
boolean bMap = sm.map.equals(enemy.getMap());
int pX = (int) nextPos.x/sm.tileWidth;
int pY = (int) nextPos.y/sm.tileHeight;
int eX = (int) enemy.position.x/sm.tileWidth;
int eY = (int) enemy.position.y/sm.tileHeight;
2022-06-12 12:35:08 +02:00
2022-06-10 18:49:02 +02:00
if(bMap && pX==eX && pY==eY) {
lastEnemy = enemy;
2022-06-13 21:08:11 +02:00
onEnemy = true;
2022-06-10 18:49:02 +02:00
return true;
}
2022-06-09 21:07:13 +02:00
}
2022-06-08 20:08:18 +02:00
return false;
}
2022-06-01 16:47:53 +02:00
@Override
2022-06-16 16:59:02 +02:00
public void removedPv(int pv) {
this.pv -= pv;
2022-06-01 16:47:53 +02:00
}
2022-06-16 15:46:34 +02:00
public int getXpMax(){
return XP_MAX;
}
2022-05-25 15:32:53 +02:00
}