66 lines
2.1 KiB
C
66 lines
2.1 KiB
C
// src/game/collision.h
|
|
#pragma once
|
|
#include "../render/player_positioning.h"
|
|
#include "../assets/character_walk_frames.h"
|
|
#include "../assets/character_run_frames.h"
|
|
#include "../assets/character_crawl_frames.h"
|
|
|
|
// Obstacle representation
|
|
struct Obstacle {
|
|
int x;
|
|
int y;
|
|
int width;
|
|
int height;
|
|
};
|
|
|
|
inline bool check_collision(const CharacterPosition& player,
|
|
MovementType movement,
|
|
const Obstacle& obs) {
|
|
|
|
// Player is given in bottom-based world coordinates (0 = bottom line)
|
|
const int player_x = player.x;
|
|
const int player_bottom = player.y;
|
|
int box_x = 0;
|
|
int box_w = 0;
|
|
int box_h = 0;
|
|
|
|
switch (movement) {
|
|
case MovementType::Walk:
|
|
box_x = player_x + CHARACTER_WALK_COLLISION_LEFT_OFFSET;
|
|
box_w = CHARACTER_WALK_COLLISION_WIDTH;
|
|
box_h = CHARACTER_WALK_FRAME_HEIGHT;
|
|
break;
|
|
case MovementType::Run:
|
|
box_x = player_x + CHARACTER_RUN_COLLISION_LEFT_OFFSET;
|
|
box_w = CHARACTER_RUN_COLLISION_WIDTH;
|
|
box_h = CHARACTER_RUN_COLLISION_HEIGHT;
|
|
break;
|
|
case MovementType::Crawl1:
|
|
box_x = player_x + CHARACTER_CRAWL1_COLLISION_LEFT_OFFSET;
|
|
box_w = CHARACTER_CRAWL1_COLLISION_WIDTH;
|
|
box_h = CHARACTER_CRAWL1_COLLISION_HEIGHT;
|
|
break;
|
|
case MovementType::Crawl2:
|
|
box_x = player_x + CHARACTER_CRAWL2_COLLISION_LEFT_OFFSET;
|
|
box_w = CHARACTER_CRAWL2_COLLISION_WIDTH;
|
|
box_h = CHARACTER_CRAWL2_COLLISION_HEIGHT;
|
|
break;
|
|
}
|
|
|
|
// Player bounding box
|
|
const int player_left = box_x;
|
|
const int player_right = player_left + box_w;
|
|
const int player_top = player_bottom + box_h;
|
|
|
|
// Obstacle bounding box
|
|
const int obs_left = obs.x;
|
|
const int obs_right = obs.x + obs.width;
|
|
const int obs_bottom = obs.y;
|
|
const int obs_top = obs_bottom + obs.height;
|
|
|
|
// Overlap check
|
|
bool horizontal = player_left < obs_right && player_right > obs_left;
|
|
bool vertical = player_bottom < obs_top && player_top > obs_bottom;
|
|
return horizontal && vertical;
|
|
}
|