I am working on a basic AI system to get an entity to follow a player. I created both the player
and the ghost
entity, but it seems as if the ghost only wants to increment both x
and y
, it never wants to do the opposite, or choose only x
or y
to use. Here is where I implemented the ghost
and player
in my constructor:
public MansionAzazel() {
...
TileCoordinate ghostSpawn = new TileCoordinate(52, 50);
TileCoordinate playerSpawn = new TileCoordinate(54, 52);
player = new Player(playerSpawn.x(), playerSpawn.y(), key);
player.init(level);
ghost = new Ghost(ghostSpawn.x(), ghostSpawn.y(), player);
ghost.init(level);
...
}
Here is the update() function for the ghost:
public void update() {
int xa = 0, ya = 0;
if (anim < 1000000)
anim++;
else
anim = 0;
if (player.getX() > xa)
xa++;
if (player.getX() < xa)
xa--;
if (player.getY() > ya)
ya++;
if (player.getY() < ya)
ya--;
if (xa != 0 || ya != 0) {
move(xa, ya);
walking = true;
} else
walking = false;
}
In which to sum it all up, the ghost
entity will only and always move in one direction because the coordinates are 0 and I don't know why.
System.out.println(xa + ", " + ya);
toSystem.out.println(xa + ", " + ya + " " + player.getX() + ", " + player.getY());
\$\endgroup\$1
Also, I divided theplayer.getX()
andplayer.getY()
by 16 (tile size) it prints out the actual location, just not ghost. \$\endgroup\$