init_player - 1Fr3aK2/Cub3d GitHub Wiki
π init_player
Initializes the player's position and direction based on the map data.
The function searches the map for the player's starting point, defined by one of the direction characters ('N', 'S', 'E', 'W'), and sets the player's coordinates and facing direction accordingly.
βοΈ Parameters
| Parameter | Type | Description |
|---|---|---|
data |
t_data * |
Pointer to the main game data structure containing the map and player information. Must be properly initialized before calling this function. |
π Returns
| Return value | Description |
|---|---|
void |
This function does not return a value. It directly modifies the player and map fields within the data structure. |
π Description
The init_player function scans the 2D array data->map.map to locate the playerβs initial spawn point and facing direction.
When it finds a directional character:
'N'β Player faces north'S'β Player faces south'E'β Player faces east'W'β Player faces west
The function performs the following steps:
- Sets the player's position coordinates (
player.xandplayer.y) to the center of the map tile by adding0.5to both thexandyindices. - Calls
set_player_direction(&data->player, dir)to configure the player's facing direction vector. - Replaces the direction character on the map with
'0', marking that position as walkable ground. - Immediately exits after initializing the player to prevent multiple spawns.
If no player character ('N', 'S', 'E', 'W') is found in the map, the function terminates the program by calling exit_error(data, "ERROR:\nplayer not found in map").
π‘ Example Usage
t_data data;
// Assume data.map.map is already loaded with level data
init_player(&data);
printf("Player initialized at position (%.2f, %.2f)\n", data.player.x, data.player.y);