onUpdate
and onRender
MethodsThe Game
class is responsible for managing entities within the game loop.
Two crucial methods, onUpdate
and onRender
, orchestrate the updating and rendering of entities during the game loop. These methods ensure that the player, enemies, pickups, and other entities are appropriately handled for smooth gameplay.
onUpdate
MethodThe onUpdate
method is responsible for updating entities during each iteration of the game loop, primarily focusing on the player and enemy entities. Let’s break down its functionality:
Camera Update: The method starts by updating the game camera, ensuring that it follows the player’s movements. This is crucial for keeping the player centered within the game view.
Ballistics Update: The ballistics system is updated, which handles things like projectile movements and other ballistic-related calculations within the game such as spread, velocity and drop-off.
Entity Iteration: A loop iterates over all entities in the game.
Conditional Entity Update: The update
method of each entity is called if it satisfies conditions specified in canUpdateEntity
. This method ensures that only entities requiring updates are processed.
Player State Check: A check for the player entity’s state is performed. If the player is dead, the gameover flag is set to true.
Enemy State Check: If all enemies are dead, the gameover and levelPassed flags are set to true, indicating that the player has successfully completed the level.
Pickup Removal: If a pickup entity is marked for deletion (markToDelete
is true), it is removed from the entities
array.
UI Update: The number of remaining enemies is updated in the game’s user interface.
onRender
MethodThe onRender
method handles the rendering of entities during each frame of the game loop. It sets up the scene, performs pre-render actions, renders entities, and completes post-render tasks. Let’s delve into its components:
New Scene Setup: Prepares for rendering a new scene by clearing the canvas.
Pre-render Operations: Performs pre-render operations, such as adjusting the camera based on the player’s position. This ensures that the player remains centered within the game view.
Ballistics Rendering: Renders the ballistics system, potentially displaying projectiles or other ballistic-related visual elements.
Entity Iteration for Rendering: A loop iterates over all entities, and each entity’s render
method is called. This includes rendering the player, enemies, pickups, and any other entities present in the game.
Player
Entity UpdateThe onUpdate
method specifically targets the player entity by checking its type. If the player is not dead, the update
method of the player entity is called. This ensures that the player’s position, speed, and other attributes are updated according to user input and game logic.
Player
Entity RenderIn the onRender
method, the player entity’s render
method is called, ensuring that the player is visually represented on the canvas.
The onUpdate
and onRender
methods in the Game
class play a pivotal role in coordinating the game loop’s entity updates and rendering processes.
The integration with the Player
entity ensures that the player’s state is consistently updated and visually reflected in the game.