From 967b377e76be19511bd7151e1640f42cf70395ec Mon Sep 17 00:00:00 2001 From: Priec Date: Tue, 18 Nov 2025 00:19:17 +0100 Subject: [PATCH] proper placement of the obstacles --- .../src/assets/obstacle_crawl_frames.h | 32 ++++++++++--------- semestralka1/src/game/collision.h | 2 +- semestralka1/src/game/obstacle_manager.h | 14 ++++---- semestralka1/src/render/loop.cpp | 17 ++++++++-- semestralka1/src/render/obstacle.cpp | 4 +-- 5 files changed, 40 insertions(+), 29 deletions(-) diff --git a/semestralka1/src/assets/obstacle_crawl_frames.h b/semestralka1/src/assets/obstacle_crawl_frames.h index 71d039a..80c7bcb 100644 --- a/semestralka1/src/assets/obstacle_crawl_frames.h +++ b/semestralka1/src/assets/obstacle_crawl_frames.h @@ -4,24 +4,24 @@ // Crawl obstacle for CRAWL1 (2 lines tall) static const char *OBSTACLE_CRAWL1_FRAME[] = { - "#######", - "#######", - "#######", - "#######", - "#######", - "#######" + "#########", + ".######", + "..#####", + "..####", + "...###", + "...###" }; // Crawl obstacle for CRAWL2 (4 lines tall) static const char *OBSTACLE_CRAWL2_FRAME[] = { - "#######", - "#######", - "#######", - "#######", - "#######", - "#######", - "#######", - "#######" + "###", + "###", + "###", + "###", + "###", + "###", + "###", + "###" }; enum class CrawlObstacleType { Crawl1 = 0, Crawl2, COUNT }; @@ -34,7 +34,9 @@ static const int OBSTACLE_CRAWL1_FRAME_WIDTH = std::strlen(OBSTACLE_CRAWL1_FRAME static const int OBSTACLE_CRAWL1_COLLISION_LEFT_OFFSET = 0; static const int OBSTACLE_CRAWL1_COLLISION_RIGHT_OFFSET = 0; static const int OBSTACLE_CRAWL1_COLLISION_WIDTH = OBSTACLE_CRAWL1_FRAME_WIDTH - (OBSTACLE_CRAWL1_COLLISION_LEFT_OFFSET + OBSTACLE_CRAWL1_COLLISION_RIGHT_OFFSET); -static const int OBSTACLE_CRAWL1_COLLISION_HEIGHT = OBSTACLE_CRAWL1_FRAME_HEIGHT; +static const int OBSTACLE_CRAWL1_COLLISION_TOP_OFFSET = 4; // first 4 rows are just empty ('.') +// collision box height = bottom 2 rows +static const int OBSTACLE_CRAWL1_COLLISION_HEIGHT = 2; // Crawl2 dimensions and collision static const int OBSTACLE_CRAWL2_FRAME_HEIGHT = sizeof(OBSTACLE_CRAWL2_FRAME) / sizeof(OBSTACLE_CRAWL2_FRAME[0]); diff --git a/semestralka1/src/game/collision.h b/semestralka1/src/game/collision.h index ac5c712..badc88f 100644 --- a/semestralka1/src/game/collision.h +++ b/semestralka1/src/game/collision.h @@ -19,7 +19,7 @@ inline bool check_collision(const CharacterPosition& player, MovementType movement, const Obstacle& obs) { int player_x = player.x; - int player_y = player.y; + int player_y = (VIEW_HEIGHT - player.y) - CHARACTER_WALK_FRAME_HEIGHT; int box_x = 0, box_w = 0, box_h = 0; switch (movement) { diff --git a/semestralka1/src/game/obstacle_manager.h b/semestralka1/src/game/obstacle_manager.h index 747a8e9..c3ef84a 100644 --- a/semestralka1/src/game/obstacle_manager.h +++ b/semestralka1/src/game/obstacle_manager.h @@ -2,6 +2,7 @@ #pragma once #include "../game/collision.h" #include "../assets/obstacle_crawl_frames.h" +#include "../assets/background_frame.h" // One movable obstacle struct MovingObstacle { @@ -20,15 +21,12 @@ inline int spawn_obstacle(CrawlObstacleType type, int x_start) { if (!obstacle_pool[i].active) { obstacle_pool[i].active = true; obstacle_pool[i].type = type; - obstacle_pool[i].data.x = x_start; + obstacle_pool[i].data.x = x_start; + + obstacle_pool[i].data.width = (type == CrawlObstacleType::Crawl1) ? OBSTACLE_CRAWL1_COLLISION_WIDTH : OBSTACLE_CRAWL2_COLLISION_WIDTH; + obstacle_pool[i].data.height = (type == CrawlObstacleType::Crawl1) ? OBSTACLE_CRAWL1_COLLISION_HEIGHT : OBSTACLE_CRAWL2_COLLISION_HEIGHT; obstacle_pool[i].data.y = 0; - if (type == CrawlObstacleType::Crawl1) { - obstacle_pool[i].data.width = OBSTACLE_CRAWL1_COLLISION_WIDTH; - obstacle_pool[i].data.height = OBSTACLE_CRAWL1_COLLISION_HEIGHT; - } else { - obstacle_pool[i].data.width = OBSTACLE_CRAWL2_COLLISION_WIDTH; - obstacle_pool[i].data.height = OBSTACLE_CRAWL2_COLLISION_HEIGHT; - } + return i; } } diff --git a/semestralka1/src/render/loop.cpp b/semestralka1/src/render/loop.cpp index 5c2d079..cd58a2c 100644 --- a/semestralka1/src/render/loop.cpp +++ b/semestralka1/src/render/loop.cpp @@ -115,12 +115,23 @@ void render_loop(int speed) { draw_character(draw_pos.x, draw_pos.y, frame.movement, frame.frame_index); for (int i = 0; i < MAX_OBSTACLES; i++) { - if (!obstacle_pool[i].active) continue; - draw_obstacle(obstacle_pool[i].data.x, obstacle_pool[i].data.y, obstacle_pool[i].type); + if (!obstacle_pool[i].active) + continue; + + draw_obstacle(obstacle_pool[i].data.x, + obstacle_pool[i].data.y, + obstacle_pool[i].type); + + if (check_collision(draw_pos, frame.movement, obstacle_pool[i].data)) { + printf("\033[2J\033[H"); // clear screen & home cursor + printf("GAME OVER\r\n"); + game_over = true; + break; + } } if (game_over) - break; + break; ThisThread::sleep_for(50ms); } diff --git a/semestralka1/src/render/obstacle.cpp b/semestralka1/src/render/obstacle.cpp index 806c737..04879d4 100644 --- a/semestralka1/src/render/obstacle.cpp +++ b/semestralka1/src/render/obstacle.cpp @@ -1,9 +1,10 @@ // src/render/obstacle.cpp #include "obstacle.h" #include "../assets/obstacle_crawl_frames.h" +#include "../assets/background_frame.h" #include -// Draw a single obstacle ASCII sprite at (x, y) +// Draw a single obstacle ASCII sprite at (x, y) where y is ground‑based (0 = bottom) void draw_obstacle(int x, int y, CrawlObstacleType type) { const char **obstacle = nullptr; int obstacle_height = 0; @@ -23,7 +24,6 @@ void draw_obstacle(int x, int y, CrawlObstacleType type) { return; // in case of invalid type } - // Each obstacle row is printed using ANSI cursor positioning for (int i = 0; i < obstacle_height; i++) { printf("\033[%d;%dH%s", y + i + 1, x + 1, obstacle[i]); }