class Enemy {
float x, y;
float speed = 1.5;
int health;
int maxHealth = 100;
float respawnTime;
float respawnTimer = respawnTime;
List path;
int pathIndex;
int updatePathInterval = 60; // Update path every 60 frames
code
Assistant
int frameCount = 0;
boolean active;
PImage spriteSheet;
int spriteWidth = 90;
int spriteHeight = 90;
int currentFrame = 0;
int frameDelay = 5;
int frameCounter = 0;
int stateTimer = 0;
int damagedDuration = 30; // Duration for damaged state in frames
int dyingDuration = 60; // Duration for dying animation in frames
AnimationState currentState = AnimationState.IDLE;
HashMap<AnimationState, Animation> animations = new HashMap<>();
Enemy(float respawnTime) {
this.respawnTime = respawnTime;
this.maxHealth = 100;
this.health = maxHealth;
this.spriteSheet = random(1) < 0.5 ? enemySpriteSheet1 : enemySpriteSheet2;
animations.put(AnimationState.ATTACKING, new Animation(0, 11, 5, false));
animations.put(AnimationState.DYING, new Animation(1, 13, 5, false));
animations.put(AnimationState.DAMAGED, new Animation(2, 4, 5, false));
animations.put(AnimationState.IDLE, new Animation(3, 8, 5, true));
animations.put(AnimationState.WALKING, new Animation(4, 10, 5, true));
respawn(); //this takes care of initital position
}
void update(Tile[][] map) {
if (!active) { //downtime after being killed
respawnTimer -= 1.0 / frameRate;
if (respawnTimer <= 0) {
active = true;
respawn();
}
return;
}
if (currentState == AnimationState.DAMAGED || currentState == AnimationState.DYING) {
stateTimer--;
if (stateTimer <= 0) {
if (currentState == AnimationState.DYING) {
active = false;
respawnTimer = respawnTime;
} else {
setState(AnimationState.WALKING);
}
}
// Don't update position or path while in DAMAGED or DYING state
updateAnimation();
return;
}
if (!isWithinMapBounds(x, y)) {
PVector safePosition = findSafeSpawnPosition();
x = safePosition.x;
y = safePosition.y;
}
frameCount++;
if (frameCount >= updatePathInterval) {
updatePath(map);
frameCount = 0;
}
if (path!= null && !path.isEmpty()) {
PVector target = path.get(pathIndex);
float dx = target.x - x;
float dy = target.y - y;
float distance = sqrt(dx * dx + dy * dy);
if (distance < speed) {
pathIndex++;
if (pathIndex >= path.size()) {
path = null;
setState(AnimationState.IDLE);
}
} else {
float ratio = speed / distance;
x += dx * ratio;
y += dy * ratio;
setState(AnimationState.WALKING);
}
} else {
setState(AnimationState.IDLE);
}
frameCounter++;
updateAnimation();
}
void updateAnimation() {
frameCounter++;
if (frameCounter >= frameDelay) {
frameCounter = 0;
currentFrame++;
if (currentFrame >= animations.get(currentState)[1]) {
currentFrame = 0;
}
}
}
void setState(AnimationState newState) {
if (currentState != newState) {
print("setting state from: " + currentState + " to " + newState);
currentState = newState;
currentFrame = 0;
if (newState == AnimationState.DAMAGED) {
stateTimer = damagedDuration;
} else if (newState == AnimationState.DYING) {
stateTimer = dyingDuration;
}
}
}
void updatePath(Tile[][] map) {
int startX = floor(x / TILE_SIZE);
int startY = floor(y / TILE_SIZE);
int endX = floor(player.x / TILE_SIZE);
int endY = floor(player.y / TILE_SIZE);
path = aStar(map,startX, startY, endX, endY);
pathIndex = 0;
}
List<PVector> aStar(Tile[][] map, int startX, int startY, int goalX, int goalY) {
PriorityQueue<Node> openSet = new PriorityQueue<>();
boolean[][] closedSet = new boolean[map.length][map[0].length];
Node[][] nodeMap = new Node[map.length][map[0].length];
for (int i = 0; i < map.length; i++) {
for (int j = 0;j < map[0].length;j++) {
nodeMap[i][j] = new Node(i, j);
}
}
Node start = nodeMap[startX][startY];
Node goal = nodeMap[goalX][goalY];
start.g = 0;
start.f = heuristic(start, goal);
openSet.add(start);
while(!openSet.isEmpty()) {
Node current = openSet.poll();
if (current == goal) {
return reconstructPath(current);
}
closedSet[current.x][current.y] = true;
for (int[] dir : new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}) {
int neighborX = current.x + dir[0];
int neighborY = current.y + dir[1];
if (neighborX < 0 || neighborX >= map.length || neighborY < 0 || neighborY >= map[0].length) {
continue;
}
if (closedSet[neighborX][neighborY] || map[neighborX][neighborY].type != TileType.EMPTY) {
continue;
}
Node neighbor = nodeMap[neighborX][neighborY];
float tentativeG = current.g + 1;
if (!openSet.contains(neighbor) || tentativeG < neighbor.g) {
neighbor.parent = current;
neighbor.g = tentativeG;
neighbor.f = neighbor.g + heuristic(neighbor, goal);
if (!openSet.contains(neighbor)) {
openSet.add(neighbor);
}
}
}
}
return null; // No path found
}
float heuristic(Node a, Node b) {
return abs(a.x - b.x) + abs(a.y - b.y);
}
List<PVector> reconstructPath(Node goal) {
List<PVector> path = new ArrayList<>();
Node current = goal;
while(current != null) {
path.add(new PVector(current.x * TILE_SIZE + TILE_SIZE / 2, current.y * TILE_SIZE + TILE_SIZE / 2));
current = current.parent;
}
Collections.reverse(path);
return path;
}
void takeDamage(int damage) {
health -= damage;
if (!isAlive()) {
setState(AnimationState.DYING);
}
else {setState(AnimationState.DAMAGED);}
}
void respawn() {
active = true;
health = maxHealth;
PVector safePosition = findSafeSpawnPosition();
x = safePosition.x;
y = safePosition.y;
currentState = AnimationState.IDLE;
currentFrame = 0;
frameCounter = 0;
frameCount = 0;
stateTimer = 0;
path = null;
pathIndex = 0;
}
boolean isAlive() {
return health > 0;
}
void display() {
if (!active) return;
int[] animationInfo = animations.get(currentState);
int rowIndex = animationInfo[0] / 11; // 11 frames per row
int colIndex = (animationInfo[0] + currentFrame) % 11;
pushMatrix();
translate(x, y);
boolean flipHorizontally = currentState == AnimationState.WALKING && path != null && !path.isEmpty() && path.get(pathIndex).x < x;
// Flip the sprite horizontally if moving left
if (flipHorizontally) {
scale(-1, 1);
}
image(spriteSheet, -spriteWidth/2, -spriteHeight/2, spriteWidth, spriteHeight,
colIndex * spriteWidth,
rowIndex * spriteHeight,
(colIndex + 1) * spriteWidth,
(rowIndex + 1) * spriteHeight);
popMatrix();
// Draw health bar
if (health < maxHealth && currentState != AnimationState.DYING) {
drawHealthBar();
}
}
void drawHealthBar() {
float barWidth = TILE_SIZE;
float healthRatio = (float)health / maxHealth;
float barHeight = 5;
fill(255, 0, 0);
rect(x - barWidth / 2, y - TILE_SIZE / 2 - 10,barWidth * healthRatio, barHeight);
noFill();
}
}
class Node implements Comparable<Node> {
int x, y;
float g, f;
Node parent;
Node(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Node other) {
return Float.compare(this.f, other.f);
}
}
animation.pde:
class Animation {
int row; // The row in the sprite sheet
int frameCount; // Total frames in this animation
int frameDelay; //controls animation speed
boolean loop; // Should the animation loop?
Certainly! To optimize your Enemy class using the Animation class from animation.pde, we'll refactor the animation handling to utilize the Animation objects effectively. This will make your animation system more modular, maintainable, and efficient.
Overview of Changes
Use Animation Objects Properly: Replace the current animation handling with the Animation class properties such as row, frameCount, frameDelay, and loop.
Remove Redundant Fields: Since Animation handles frame delays and counts, we can remove or repurpose some fields in the Enemy class.
Update updateAnimation Method: Leverage the Animation object's frameDelay for controlling animation speed.
Modify display Method: Use the Animation object's row and frameCount to determine which sprite to display.
Ensure Correct Animation State Transitions: Properly handle transitions between different animation states based on game logic.
Updated Enemy Class
Below is the refactored Enemy class incorporating the Animation manager:
java
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.PriorityQueue;
enum AnimationState {
IDLE, WALKING, ATTACKING, DAMAGED, DYING
}
class Enemy {
// Position and Movement
float x, y;
float speed = 1.5;
List<PVector> path;
int pathIndex;
int updatePathInterval = 60; // Update path every 60 frames
int frameCount = 0;
boolean active;
// Health Management
int health;
int maxHealth = 100;
float respawnTime;
float respawnTimer;
// Animation Management
PImage spriteSheet;
int spriteWidth = 90;
int spriteHeight = 90;
int frameCounter = 0;
AnimationState currentState = AnimationState.IDLE;
HashMap<AnimationState, Animation> animations = new HashMap<>();
// State Timers
int stateTimer = 0;
int damagedDuration = 30; // Duration for damaged state in frames
int dyingDuration = 60; // Duration for dying animation in frames
Enemy(float respawnTime) {
this.respawnTime = respawnTime;
this.respawnTimer = respawnTime;
this.maxHealth = 100;
this.health = maxHealth;
// Initialize sprite sheet randomly
this.spriteSheet = random(1) < 0.5 ? enemySpriteSheet1 : enemySpriteSheet2;
// Initialize animations
animations.put(AnimationState.ATTACKING, new Animation(0, 11, 5, false));
animations.put(AnimationState.DYING, new Animation(1, 13, 5, false));
animations.put(AnimationState.DAMAGED, new Animation(2, 4, 5, false));
animations.put(AnimationState.IDLE, new Animation(3, 8, 5, true));
animations.put(AnimationState.WALKING, new Animation(4, 10, 5, true));
respawn(); // Initialize position and state
}
void update(Tile[][] map) {
if (!active) { // Downtime after being killed
respawnTimer -= 1.0 / frameRate;
if (respawnTimer <= 0) {
active = true;
respawn();
}
return;
}
// Handle DAMAGED and DYING states
if (currentState == AnimationState.DAMAGED || currentState == AnimationState.DYING) {
stateTimer--;
if (stateTimer <= 0) {
if (currentState == AnimationState.DYING) {
active = false;
respawnTimer = respawnTime;
} else {
setState(AnimationState.WALKING);
}
}
updateAnimation();
return; // Skip movement updates
}
// Ensure enemy is within map bounds
if (!isWithinMapBounds(x, y)) {
PVector safePosition = findSafeSpawnPosition();
x = safePosition.x;
y = safePosition.y;
}
frameCount++;
if (frameCount >= updatePathInterval) {
updatePath(map);
frameCount = 0;
}
// Movement Logic
if (path != null && !path.isEmpty()) {
PVector target = path.get(pathIndex);
float dx = target.x - x;
float dy = target.y - y;
float distance = sqrt(dx * dx + dy * dy);
if (distance < speed) {
pathIndex++;
if (pathIndex >= path.size()) {
path = null;
setState(AnimationState.IDLE);
}
} else {
float ratio = speed / distance;
x += dx * ratio;
y += dy * ratio;
setState(AnimationState.WALKING);
}
} else {
setState(AnimationState.IDLE);
}
updateAnimation();
}
void updateAnimation() {
Animation animation = animations.get(currentState);
if (animation == null) return;
frameCounter++;
if (frameCounter >= animation.frameDelay) {
frameCounter = 0;
// Advance frame
if (currentState == AnimationState.DYING || currentState == AnimationState.DAMAGED) {
currentFrameInternal(animation);
} else {
if (animation.loop) {
currentFrameInternal(animation);
} else {
// Non-looping animations stop at the last frame
if (currentFrameInternal(animation)) {
// Animation ended, handle if necessary
}
}
}
}
}
/**
* Advances the current frame and returns true if the animation has completed (for non-looping).
*/
boolean currentFrameInternal(Animation animation) {
currentFrame++;
if (currentFrame >= animation.frameCount) {
currentFrame = animation.loop ? 0 : animation.frameCount - 1;
return !animation.loop;
}
return false;
}
void setState(AnimationState newState) {
if (currentState != newState) {
println("Setting state from: " + currentState + " to " + newState);
currentState = newState;
currentFrame = 0;
frameCounter = 0;
if (newState == AnimationState.DAMAGED) {
stateTimer = damagedDuration;
} else if (newState == AnimationState.DYING) {
stateTimer = dyingDuration;
}
}
}
void updatePath(Tile[][] map) {
int startX = floor(x / TILE_SIZE);
int startY = floor(y / TILE_SIZE);
int endX = floor(player.x / TILE_SIZE);
int endY = floor(player.y / TILE_SIZE);
path = aStar(map, startX, startY, endX, endY);
pathIndex = 0;
}
List<PVector> aStar(Tile[][] map, int startX, int startY, int goalX, int goalY) {
PriorityQueue<Node> openSet = new PriorityQueue<>();
boolean[][] closedSet = new boolean[map.length][map[0].length];
Node[][] nodeMap = new Node[map.length][map[0].length];
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[0].length; j++) {
nodeMap[i][j] = new Node(i, j);
}
}
Node start = nodeMap[startX][startY];
Node goal = nodeMap[goalX][goalY];
start.g = 0;
start.f = heuristic(start, goal);
openSet.add(start);
while (!openSet.isEmpty()) {
Node current = openSet.poll();
if (current == goal) {
return reconstructPath(current);
}
closedSet[current.x][current.y] = true;
for (int[] dir : new int[][] { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } }) {
int neighborX = current.x + dir[0];
int neighborY = current.y + dir[1];
if (neighborX < 0 || neighborX >= map.length || neighborY < 0 || neighborY >= map[0].length) {
continue;
}
if (closedSet[neighborX][neighborY] || map[neighborX][neighborY].type != TileType.EMPTY) {
continue;
}
Node neighbor = nodeMap[neighborX][neighborY];
float tentativeG = current.g + 1;
if (!openSet.contains(neighbor) || tentativeG < neighbor.g) {
neighbor.parent = current;
neighbor.g = tentativeG;
neighbor.f = neighbor.g + heuristic(neighbor, goal);
if (!openSet.contains(neighbor)) {
openSet.add(neighbor);
}
}
}
}
return null; // No path found
}
float heuristic(Node a, Node b) {
return abs(a.x - b.x) + abs(a.y - b.y);
}
List<PVector> reconstructPath(Node goal) {
List<PVector> path = new ArrayList<>();
Node current = goal;
while (current != null) {
path.add(new PVector(current.x * TILE_SIZE + TILE_SIZE / 2, current.y * TILE_SIZE + TILE_SIZE / 2));
current = current.parent;
}
Collections.reverse(path);
return path;
}
void takeDamage(int damage) {
if (!active) return;
health -= damage;
if (!isAlive()) {
setState(AnimationState.DYING);
} else {
setState(AnimationState.DAMAGED);
}
}
void respawn() {
active = true;
health = maxHealth;
PVector safePosition = findSafeSpawnPosition();
x = safePosition.x;
y = safePosition.y;
setState(AnimationState.IDLE);
path = null;
pathIndex = 0;
}
boolean isAlive() {
return health > 0;
}
void display() {
if (!active) return;
Animation animation = animations.get(currentState);
if (animation == null) return;
int rowIndex = animation.row;
int colIndex = currentFrame;
pushMatrix();
translate(x, y);
// Determine if sprite should be flipped
boolean flipHorizontally = currentState == AnimationState.WALKING
&& path != null
&& !path.isEmpty()
&& path.get(pathIndex).x < x;
if (flipHorizontally) {
scale(-1, 1); // Flip the sprite
}
// Calculate the source rectangle
int sx1 = colIndex * spriteWidth;
int sy1 = rowIndex * spriteHeight;
int sx2 = sx1 + spriteWidth;
int sy2 = sy1 + spriteHeight;
// Draw the sprite
image(spriteSheet, -spriteWidth / 2, -spriteHeight / 2, spriteWidth, spriteHeight, sx1, sy1, sx2, sy2);
popMatrix();
// Draw health bar
if (health < maxHealth && currentState != AnimationState.DYING) {
drawHealthBar();
}
}
void drawHealthBar() {
float barWidth = spriteWidth;
float healthRatio = (float) health / maxHealth;
float barHeight = 5;
fill(255, 0, 0);
rect(x - barWidth / 2, y - spriteHeight / 2 - 10, barWidth * healthRatio, barHeight);
noFill();
}
// Placeholder methods (implement these based on your game logic)
boolean isWithinMapBounds(float x, float y) {
// Implement boundary check
return true;
}
PVector findSafeSpawnPosition() {
// Implement safe spawn logic
return new PVector(width / 2, height / 2);
}
// AnimationState to keep track
int currentFrame = 0;
}
Key Changes Explained
Animation Object Usage:
Initialization: The animations HashMap now holds Animation objects mapped to each AnimationState.
Frame Control: The updateAnimation method uses the frameDelay from the current Animation object to control the speed of frame changes.
updateAnimation Method:
This method now retrieves the current Animation based on currentState and updates currentFrame accordingly.
For looping animations (like IDLE and WALKING), frames reset after reaching frameCount.
For non-looping animations (DAMAGED, DYING, ATTACKING), frames stop advancing once the last frame is reached.
display Method:
Utilizes the Animation object's row property to determine the vertical position (sy1) on the sprite sheet.
Uses currentFrame to calculate the horizontal position (sx1) on the sprite sheet.
Handles sprite flipping based on movement direction.
State Management:
The setState method resets currentFrame and frameCounter when changing states.
Timers (stateTimer) manage the duration of non-looping states like DAMAGED and DYING.
Health Bar Adjustments:
Adjusted the health bar to align with the sprite's dimensions (spriteWidth and spriteHeight).
Removed Redundant Fields:
Removed speed related frame handling as it's now managed through the Animation class.
Additional Recommendations
Animation Looping: Ensure that the loop property in the Animation objects is correctly set based on whether the animation should loop or not.
Resource Management: Verify that enemySpriteSheet1 and enemySpriteSheet2 are properly loaded PImage objects before creating Enemy instances.
Boundary and Spawn Logic: Implement the isWithinMapBounds and findSafeSpawnPosition methods based on your game's map and spawning logic.
Performance Optimization: If you have numerous enemies, consider optimizing pathfinding (aStar) and animation updates, possibly using spatial partitioning or animation pooling.
Example Animation Class (animation.pde)
Ensure that your Animation class in animation.pde is properly defined as follows:
java
class Animation {
int row; // The row in the sprite sheet
int frameCount; // Total frames in this animation
int frameDelay; // Controls animation speed (frames per sprite frame)
boolean loop; // Should the animation loop?
Animation(int row, int frameCount, int frameDelay, boolean loop) {
this.row = row;
this.frameCount = frameCount;
this.frameDelay = frameDelay;
this.loop = loop;
}
}
Conclusion
By integrating the Animation class into your Enemy class, you achieve a more structured and efficient animation system. This setup allows for easy adjustments and scalability, especially as your game grows in complexity. Ensure to test each animation state thoroughly to confirm that transitions and frame updates behave as expected.
If you have an AnimationManager class or additional animation handling logic in animation.pde, feel free to share it for more tailored integration guidance.