Files
2026-03-26 14:46:39 -05:00

120 lines
4.2 KiB
C

#include "player.h"
#include "_internal/private_player.h"
#include "chunk.h"
#include "world.h"
#include "hashmap.h"
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
uint8_t create_player(struct player_struct** player)
{
*player = (player_struct *) calloc(1, sizeof(player_struct));
if(*player == NULL)
{
return EXIT_FAILURE;
}
_initialize_player(*player);
return EXIT_SUCCESS;
}
void destroy_player(struct player_struct** player)
{
if (*player == NULL)
{
return;
}
_free_player(*player);
free(*player);
*player = NULL;
}
void update_player_controller(struct player_struct *player)
{
if(player == NULL)
{
fprintf(stderr,
"Error: update_player_controller, argument player is NULL\n");
return;
}
float seconds_since_last_frame = GetFrameTime();
_update_player_chunk_occupancy_state(player);
Vector3 camera_movement_this_frame =
_update_player_movement(player,
seconds_since_last_frame);
Vector2 change_in_mouse_position_since_last_call = GetMouseDelta();
Vector3 change_in_cameras_rotation =
{
change_in_mouse_position_since_last_call.x * player->mouse_sensitivity,
change_in_mouse_position_since_last_call.y * player->mouse_sensitivity,
0.0f // Mice only rotate x and y so we leave it 0.0f.
};
UpdateCameraPro(&player->camera,
camera_movement_this_frame,
change_in_cameras_rotation,
0.0f); // We do not zoom the camera this way so we leave it 0.0f.
_update_player_targeted_block(player);
}
void update_players_known_chunks(struct player_struct* player,
struct world_struct* world)
{
if (player == NULL
|| world == NULL)
{
fprintf(stderr,
"Error: update_players_known_chunks, arguement player,"
" or world, or both are NULL\n");
return;
}
pthread_mutex_lock(&world->mutex);
for (int8_t offset_x = -1; offset_x <= 1; offset_x++)
{
for (int8_t offset_y = -1; offset_y <= 1; offset_y++)
{
for (int8_t offset_z = -1; offset_z <= 1; offset_z++)
{
worlds_chunk_hashmap_entry query_entry =
{
.chunk_position =
&player->occupied_chunks_position,
};
worlds_chunk_hashmap_entry* search_result =
(worlds_chunk_hashmap_entry*)
hashmap_get(world->active_chunks,
&query_entry);
int array_index_x = offset_x + 1;
int array_index_y = offset_y + 1;
int array_index_z = offset_z + 1;
if (search_result == NULL)
{
player->nearby_chunks[array_index_x]
[array_index_y]
[array_index_z] = NULL;
continue;
}
player->nearby_chunks[array_index_x]
[array_index_y]
[array_index_z] =
(chunk_struct*)
search_result->chunk_pointer;
}
}
}
pthread_mutex_unlock(&world->mutex);
}