movements - 1Fr3aK2/Cub3d GitHub Wiki
π movements
Handles all player movement and rotation actions based on the currently active key inputs stored inside the gameβs data structure.
βοΈ Parameters
| Parameter | Type | Description |
|---|---|---|
data |
t_data * |
Pointer to the main game data structure containing key states, player information, and movement handlers. |
π Returns
| Return value | Description |
|---|---|
void |
This function does not return a value; instead, it triggers the appropriate movement or rotation functions depending on active key flags. |
π Description
The movements function processes the current keyboard input flags stored in data->keys and executes the corresponding movement or rotation functions.
It checks each direction/rotation bitmask and calls the appropriate handler:
- FORWARD β
move_forward(data) - BACKWARDS β
move_backward(data) - LEFT_S β
move_left(data) - RIGHT_S β
move_right(data) - TURN_L β
rotate_left(data) - TURN_R β
rotate_right(data)
This function is typically called once per frame (e.g., inside the main loop or update loop) to ensure that player movement feels continuous and responsive.
π Input Handling
The data->keys field is a bitmask that tracks which movement keys are currently pressed.
By using bitwise checks (&), the function can process multiple simultaneous inputsβsuch as moving forward while turning.
This allows smooth, natural movement like strafing, diagonal walking, and rotating while moving.
π‘ Example Usage
// Called each frame in the main loop
void game_loop(void)
{
t_data *data = data_s();
movements(data); // Apply current movement based on key inputs
render_frame(data); // Draw frame after updating the player's position/direction
}