obstacles added fully now

This commit is contained in:
Priec
2025-11-17 16:37:34 +01:00
parent 34c9d76389
commit ae2b8b91aa
3 changed files with 146 additions and 0 deletions

View File

@@ -0,0 +1,64 @@
// src/game/collision.h
#pragma once
#include "../render/player_positioning.h"
#include "../render/player.h"
#include "../assets/character_walk_frames.h"
#include "../assets/character_run_frames.h"
#include "../assets/character_crawl_frames.h"
// Simple obstacle representation
struct Obstacle {
int x;
int y;
int width;
int height;
};
// Axis-aligned bounding-box collision
inline bool check_collision(const CharacterPosition& player,
MovementType movement,
const Obstacle& obs) {
int player_x = player.x;
int player_y = player.y;
int box_x = 0, box_w = 0, 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's bounding box
int player_left = box_x;
int player_right = player_left + box_w;
int player_top = player_y;
int player_bottom = player_top + box_h;
// Obstacle bounding box
int obs_left = obs.x;
int obs_right = obs.x + obs.width;
int obs_top = obs.y;
int obs_bottom = obs.y + obs.height;
// Simple overlap check
bool horizontal = player_left < obs_right && player_right > obs_left;
bool vertical = player_top < obs_bottom && player_bottom > obs_top;
return horizontal && vertical;
}