Ciaran
Ciaran

Reputation: 695

java: Enemy player tracking AI for java game

I am creating a game for the first time in java and I'm currently trying to get my zombies to track and follow the player. below is my code. I have created a controller class, and used a linked list to create multiple zombies. Then in my zombies update method I use simple if statements for the zombie to track the player. The problem is that the zombies track to the players initial starting position, but not to where he is moved.

In the zombie update() method I used a System.println(player.getX()); and it showed that the players position was not being updated in the zombies class, so that is why they only track to its starting position. I cant figure out how to fix this. any help would be appreciated.

ZOMBIE CLASS:

public class Zombie extends Entity{

Animation dwn,up, left, right;
BufferedImage north, south, east, west;

Sprites sprites = new Sprites();
Player player = new Player(100,100);


Rectangle boundsZombie;

String direction = "";


public Zombie(int x, int y){

    super(x,y);
    sprites.create();
    boundsZombie = new Rectangle(0,0, 32, 32);

}

public void update(){

    if(player.getX() > x){
        x = x + 1;
    }

    if(player.getX() < x){
        x = x - 1;
    }

    if(player.getY() > y){
        y = y + 1;
    }

    if(player.getY() < y){
        y = y - 1;
    }

    System.out.println(player.getX());
}



public Rectangle getBounds(){
    return new Rectangle((int) x, (int) y, 32, 32);
}

public void render(Graphics g){

    g.drawImage(sprites.zombieNorth, x, y, null);
    //g.setColor(Color.red);
    //g.fillRect((int) x + boundsZombie.x, (int) y + boundsZombie.y, boundsZombie.width, boundsZombie.height);

}

public int getX() {
    return x;
}

public void setX(int x) {
    this.x = x;
}

public int getY() {
    return y;
}

public void setY(int y) {
    this.y = y;
}

}

PLAYER CLASS

public class Player extends Entity{

Animation dwn,up, left, right;
BufferedImage north, south, east, west;
Sprites sprites = new Sprites();
KeyManager keyManager = new KeyManager();
Rectangle boundsPlayer;
//Controller c;


boolean movUp, movDwn, movRight, movLeft;
String direction = "";



public Player(int x, int y){
    super(x,y);
    sprites.create();
    dwn = new Animation(100, sprites.player_down);
    up = new Animation(100, sprites.player_up);
    left = new Animation(100, sprites.player_left);
    right = new Animation(100, sprites.player_right);
    north = sprites.playerNorth;
    south = sprites.playerSouth;
    east = sprites.playerEast;
    west = sprites.playerWest;

    boundsPlayer = new Rectangle(0,0, 32, 32);
}

public void setX(int x) {
    this.x = x;
}

public void setY(int y) {
    this.y = y;
}

public Rectangle getBounds(){
    return new Rectangle((int) x, (int) y, 32, 32);
}

/**
 * player tick method, used to move the player
 */
public void update(){

    if(Game.getKeyManager().up){
        y -= 3;
        direction = "north";


    }
    if(Game.getKeyManager().down){
        y += 3;
        direction = "south";

    }
    if(Game.getKeyManager().left){
        x -= 3;
        direction = "west";

    }

    if(Game.getKeyManager().right){
        x += 3;
        direction = "east";
        //System.out.println(x);
    }

}

public int getX() {
    return (int)x;
}

public int getY() {
    return (int)y;
}

public String getDirection(){
    return direction;
}

/**
 * method used to return players facing direction sprite
 * @return
 */
public BufferedImage getPlayerDirection(){
    if(direction == "north"){
        return north;
    }

    else if(direction == "south"){
        return south;
    }

    else if(direction == "east"){
        return east;
    }

    else{
        return west;
    }

}


public void render(Graphics g){
    g.drawImage(getCurrentAnimationFrame(), (int) x, (int) y, null);

    //g.setColor(Color.red);
    //g.fillRect((int) x + boundsPlayer.x, (int) y + boundsPlayer.y, boundsPlayer.width, boundsPlayer.height);
}


/**
 * method used to return the bufferedImage of current frame
 * @return
 */
public BufferedImage getCurrentAnimationFrame(){
    if(Game.getKeyManager().right == true){
        return right.getCurrentFrame();
    }

    else if(Game.getKeyManager().left == true){
        return left.getCurrentFrame();
    }

    else if(Game.getKeyManager().up == true){
        return up.getCurrentFrame();
    }

    else if(Game.getKeyManager().down == true){
        return dwn.getCurrentFrame();
    }

    else {
        return getPlayerDirection();
    }


}

}

Upvotes: 3

Views: 2182

Answers (1)

Sak6lab
Sak6lab

Reputation: 118

This happens because your enemy class has one player object and your game class has a completely different player object. So your player position will always be the same in the enemy class. Hence, it would be more logical to do enemy AI movement in the game class. Your player or enemy class should just hold your character's position, direction or loading a image whether it is a enemy or a player.

I suggest creating a parent class for both enemy and player class because they both have similar characteristics.

Upvotes: 2

Related Questions