Files
chess/American-Identity-Project/Enemy.java
CoolGuy27 a97e122e58 Add a new platformer game with various levels and enemies
The agent has updated the game's core logic, including player movement, enemy AI, collision detection, and level loading. New sprites and sound effects have been integrated, along with the addition of a Chess game. The replit.nix file has been configured to include jdk21 and neovim.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: f6819c21-e85d-45ac-acde-604db2cfa4fe
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Event-Id: 32bc3e83-133e-4485-96b6-ff3d548bcbfd
Replit-Helium-Checkpoint-Created: true
2026-04-20 21:19:57 +00:00

54 lines
1.2 KiB
Java

import java.util.*;
import javax.swing.ImageIcon;
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<Collidable> 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;
}
}
}