The Enemy
class represents an enemy entity within the game. This class encapsulates the properties and behaviors of enemies, including rendering, updating, collision handling, and response to projectile hits.
Constructor(spawn): Initializes the enemy entity with spawn coordinates and sets default values for various properties.
render(context): Renders the enemy entity on the canvas using the Renderer
class.
update(game): Updates the enemy entity’s position, handles enemy-enemy collisions, checks for projectile hits, and updates the entity’s health and state.
pushAlong(vectorX, vectorY): Sets the push velocity to push enemies when bumped by entities.
hitByProjectile(projectile, dps, enemies): Sets the enemy’s velocity and handles damage per second (DPS) when enemies are hit by projectiles.
The render
method delegates the rendering responsibility to the Renderer
class, ensuring a separation of concerns and maintaining a modular structure.
The update
method is responsible for updating the enemy entity’s position, handling enemy-enemy collisions, responding to projectile hits, and updating health and state. It ensures that the enemy’s state is updated based on various interactions within the game.
In the update
method, there is a section that checks for enemy-enemy collisions. If the distance between two enemies is within a specified range, one enemy pushes the other, simulating a collision response.
The update
method also checks for projectile hits by iterating over projectiles in the game. If a projectile intersects with the enemy’s bounds, the hitByProjectile
method is called, applying damage and potentially marking the enemy as dead.