diff options
Diffstat (limited to 'src/player.c')
| -rw-r--r-- | src/player.c | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/player.c b/src/player.c index 2b72477..11bc307 100644 --- a/src/player.c +++ b/src/player.c @@ -7,6 +7,12 @@ Player createPlayer() .position = Vector3Zero(), .direction = (Vector3){0.0, 0.0, 0.0}, .velocity = Vector3Zero(), + .relativeBox = (BoundingBox){ + .min = (Vector3){-PLAYER_WIDTH / 2.0, -PLAYER_HEIGHT / 2.0, + -PLAYER_WIDTH / 2.0}, + .max = (Vector3){PLAYER_WIDTH / 2.0, PLAYER_HEIGHT / 2.0, + PLAYER_WIDTH / 2.0} + }, .camera = (Camera){ .position = (Vector3){0.0, 0.0, 0.0}, .target = Vector3Zero(), @@ -48,6 +54,63 @@ void updatePlayerLookingAround(Player* player, Game* game) player->direction = (Vector3){matrix.m2, matrix.m6, matrix.m10}; } +WorldUID playerCheckCollisionWithBuildings(Player* player, Game* game) +{ + for (int index = 0; index < WORLD_PLACE_COUNT; ++index) + { + WorldUID uid = game->world.places[index]; + Entity* place = &game->world.entities[uid]; + + if (entityIsBuilding(place->id) + && CheckCollisionBoxes(place->box, player->box)) + { + return uid; + } + } + + return ENTITY_NONE; +} + +// TODO: Make this fucker speedy quick +void playerHandleCollisionWithBuilding(Player* player, Game* game, + WorldUID uid) +{ + Entity* place = &game->world.entities[uid]; + EntityBuilding* building = (EntityBuilding*)place->data; + + BoundingBox wallBox = (BoundingBox){ + .min = (Vector3){-ENTITY_BUILDING_CUBE_SIZE.x / 2.0, + -ENTITY_BUILDING_CUBE_SIZE.y / 2.0, + -ENTITY_BUILDING_CUBE_SIZE.z / 2.0}, + .max = (Vector3){ENTITY_BUILDING_CUBE_SIZE.x / 2.0, + ENTITY_BUILDING_CUBE_SIZE.y / 2.0, + ENTITY_BUILDING_CUBE_SIZE.z / 2.0} + }; + + for (int y = 0; y < building->height; ++y) + { + for (int x = 0; x < building->width; ++x) + { + Color color = building->pixelMap[y * building->width + x]; + + if (color.r == 0) + { + continue; + } + + Vector3 wallPosition = (Vector3){ + place->position.x + x * ENTITY_BUILDING_CUBE_SIZE.x, + place->position.y + ENTITY_BUILDING_CUBE_SIZE.y / 2.0, + place->position.z + y * ENTITY_BUILDING_CUBE_SIZE.z + }; + + BoundingBox box = wallBox; + box.min = Vector3Add(box.min, place->position); + box.max = Vector3Add(box.max, place->position); + } + } +} + void updatePlayerMovement(Player* player, Game* game) { Camera* camera = &player->camera; @@ -85,10 +148,18 @@ void updatePlayerMovement(Player* player, Game* game) player->velocity = Vector3Scale(player->velocity, PLAYER_SPEED); + printf("%d\n", playerCheckCollisionWithBuildings(player, game)); + // Apply velocity. player->position = Vector3Add( player->position, Vector3Scale(player->velocity, GetFrameTime())); + // Update box. + player->box = player->relativeBox; + player->box.min = Vector3Add(player->box.min, player->position); + player->box.max = Vector3Add(player->box.max, player->position); + //DrawBoundingBox(player->box, YELLOW); + updatePlayerHeight(player, game); // Apply camera. |
