My AI in my processing game that I want to chase the player across the map, such that…
- User
My AI in my processing game that I want to chase the player across the map, such that they shoud be able to navigate obstacles in the way; technically works, however theres a weird behaviour of the enemy ai that makes them 'twitch' / backtrack their path at seemingly the same time intervals, could you please diagnose and suggest changes to the code in order to fix this behaviour. It is completely fine to change the code structure if needed
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.PriorityQueue;
enum AnimationState { IDLE, WALKING, ATTACKING, DAMAGED, DYING }
class Enemy {
codefloat x, y; float speed = 1.5; int health; int maxHealth = 100; float respawnTime; float respawnTimer; List<PVector> path; int pathIndex; int updatePathInterval = 60; // Update path every 60 frames int frameCount; boolean active; AnimationState currentState = AnimationState.IDLE; HashMap<AnimationState, Animation> animations = new HashMap<>(); Animation currentAnim; int stateTimer = 0; int damagedDuration = 30; int dyingDuration = 60; int attackingDuration = 55; //Duration for attacking state in frames (11 frames * 5 frame delay) boolean hasDealtDamage = false; //to ensure damage is dealt only once per attack PImage spriteSheet; int spriteWidth = 90; int spriteHeight = 64; Player player; // Reference to the player Enemy(float respawnTime, Player playerRef) { this.respawnTime = respawnTime; this.respawnTimer = respawnTime; this.health = maxHealth; this.player = playerRef; 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)); // Set the initial animation currentAnim = animations.get(currentState); currentAnim.reset(); respawn(); //this takes care of initital position } - Assistant