0
\$\begingroup\$

Hello I have a top down survival game where you shoot bad guys(green squares). The player can collide with certain tiles and so can the mobs but I want the mobs to go around the tile if there is a path and not juts look in the direction of the player. How could I do this. Here is my mob class, collision class and block class

public class collision {


public static boolean iscolliding(Point p,block b){
    return b.contains(p);

}

}

public class block extends Rectangle {
private static final long serialVersionUID = 1L;

public static int blocksize = 40;
private int id;

private Image i;

public static Rectangle r = new Rectangle(Comp.mx, Comp.my, 1, 1);

public static boolean c = false;


public block(int x, int y, int id) {
    setBounds(x, y, blocksize, blocksize);
    this.id = id;
}

public void render(Graphics g) {
    g.setColor(Color.green);

    if (id == 0) {
        ImageIcon i62 = new ImageIcon("res/tiles/air.png");
        i = i62.getImage();
        g.drawImage(i, x - play.camx, y - play.camy, null);

    }
    if (id == 1) {
        ImageIcon i62 = new ImageIcon("res/tiles/dirt.png");
        i = i62.getImage();
        g.drawImage(i, x - play.camx, y - play.camy, null);

    }
    if (id == 2) {
        ImageIcon i62 = new ImageIcon("res/tiles/roots.png");
        i = i62.getImage();
        g.drawImage(i, x - play.camx, y - play.camy, null);

    }
    if (id == 3) {
        ImageIcon i62 = new ImageIcon("res/tiles/hellgrasses/hellsoil.png");
        i = i62.getImage();
        g.drawImage(i, x - play.camx, y - play.camy, null);

    }










}


public void setID(int id) {
    this.id = id;
}

public int getID() {
    return id;
}

}

public class blob {

public double x,y;

public boolean dead;

public Rectangle r = new Rectangle((int)x, (int)y,20,20);

public boolean canmove;

public blob (int x, int y){
    this.x = x;
    this.y = y;
}

public void tick(){
    r = new Rectangle((int)x- play.camx, (int)y-play.camy,20,20);

    if(!play.isPaused){
        if(x > play.p.x+ play.camx){
        x-=.2;
        }
        if(x < play.p.x+ play.camx){
        x+=.2;
        }
        if(y > play.p.y+ play.camy){
        y-=.2;
        }
        if(y <  play.p.y+ play.camy){
            y+=.2;
        }

        if(r.intersects(play.p.r)){
            play.p.health-=1;
        }
    }

    if(dead){
        if(new Random().nextInt(100) >= 95){
            play.hp.add(new healthPack((int)x,(int)y));
        }
    }
}

public void render(Graphics g){
    g.setColor(Color.green);
    g.fillRect((int)x - play.camx, (int)y -play.camy, 20, 20);
    g.setColor(Color.red);
    g.drawRect((int)r.x , (int)r.y, 20, 20);
}

}

if you need any other info just leave a comment

\$\endgroup\$
0
0
\$\begingroup\$

You might want to implement a 2Ds pathfinding algorithm and call it in your tick() method. There are several pathfinding algorithms, but the A* algorithm might be the best for you as it is rather simple to understand and code.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.