The Player
class represents a player entity within the game. This class encapsulates the properties and behaviors of the player, including movement, rendering, health management, and interactions with other entities such as enemies, walls and power-ups.
Constructor(spawn): Initializes the player entity with spawn coordinates and sets default values for various properties.
render(context): Renders the player entity on the canvas using the Renderer
class.
update(game): Updates the player entity’s position, speed, and angle based on user input, collisions with walls, and mouse position. It also handles animation and invincibility states.
takeDamage(enemy): Handles damage inflicted on the player by enemies, considering the player’s invincibility state.
boostSpeed(amount): Boosts the player’s speed for a short duration when a stamina pickup is acquired.
refillHealth(amount, pickup): Refills the player’s health when a health pickup is acquired or used.
The render
method delegates the rendering responsibility to the Renderer
class, which abstracts the rendering logic. This separation allows for modular rendering and enhances code readability.
The update
method is responsible for updating the player’s position, handling user input, checking collisions, managing animation, and adjusting the player’s angle based on the mouse position. The method also handles damage inflicted by enemies and updates the player’s health accordingly.
The takeDamage
method calculates the damage vector based on the player’s position relative to an enemy. If the player is not invincible, it applies damage, triggers invincibility, and updates the player’s health. If the player’s health reaches zero, the player is marked as dead.
The boostSpeed
and refillHealth
methods handle the activation of stamina and health pickups, respectively. The player’s properties are adjusted based on the type of pickup and whether it is acquired or used.