proper placement of the obstacles

This commit is contained in:
Priec
2025-11-18 00:19:17 +01:00
parent cf30946c0d
commit 967b377e76
5 changed files with 40 additions and 29 deletions

View File

@@ -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]);

View File

@@ -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) {

View File

@@ -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;
}
}

View File

@@ -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);
}

View File

@@ -1,9 +1,10 @@
// src/render/obstacle.cpp
#include "obstacle.h"
#include "../assets/obstacle_crawl_frames.h"
#include "../assets/background_frame.h"
#include <cstdio>
// Draw a single obstacle ASCII sprite at (x, y)
// Draw a single obstacle ASCII sprite at (x, y) where y is groundbased (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]);
}