import javax.swing.ImageIcon; import java.util.*; public class Enemy extends Collidable{ int xVelo, yVelo; boolean alive; public Enemy(int x, int y, int w, int h, int level){ super(x,y,w,h,new ImageIcon("Sprites/Enemies/" + level + ".png")); xVelo = 2; yVelo = 0; alive = true; } public void moveX(int moveX){ this.x += moveX; this.rect.x = this.x; } public void moveY(int moveY){ this.y += moveY; this.rect.y = this.y; } public void patrol(ArrayList collidables){ moveX(xVelo); for (Collidable c : collidables){ if (this.collidesWith(c)){ xVelo = -xVelo; moveX(xVelo * 2); break; } } // check edge detection - is there ground below next step? boolean edgeAhead = true; int nextX = this.x + xVelo; for (Collidable c : collidables) { if (c instanceof Tile) { Tile t = (Tile) c; // check if tile is below enemy's next position if (nextX + this.width > t.x && nextX < t.x + t.width && t.y == this.y + this.height) { edgeAhead = false; break; } } } if (edgeAhead) { xVelo = -xVelo; } } }