proper placement of the obstacles
This commit is contained in:
@@ -4,24 +4,24 @@
|
|||||||
|
|
||||||
// Crawl obstacle for CRAWL1 (2 lines tall)
|
// Crawl obstacle for CRAWL1 (2 lines tall)
|
||||||
static const char *OBSTACLE_CRAWL1_FRAME[] = {
|
static const char *OBSTACLE_CRAWL1_FRAME[] = {
|
||||||
"#######",
|
"#########",
|
||||||
"#######",
|
".######",
|
||||||
"#######",
|
"..#####",
|
||||||
"#######",
|
"..####",
|
||||||
"#######",
|
"...###",
|
||||||
"#######"
|
"...###"
|
||||||
};
|
};
|
||||||
|
|
||||||
// Crawl obstacle for CRAWL2 (4 lines tall)
|
// Crawl obstacle for CRAWL2 (4 lines tall)
|
||||||
static const char *OBSTACLE_CRAWL2_FRAME[] = {
|
static const char *OBSTACLE_CRAWL2_FRAME[] = {
|
||||||
"#######",
|
"###",
|
||||||
"#######",
|
"###",
|
||||||
"#######",
|
"###",
|
||||||
"#######",
|
"###",
|
||||||
"#######",
|
"###",
|
||||||
"#######",
|
"###",
|
||||||
"#######",
|
"###",
|
||||||
"#######"
|
"###"
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class CrawlObstacleType { Crawl1 = 0, Crawl2, COUNT };
|
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_LEFT_OFFSET = 0;
|
||||||
static const int OBSTACLE_CRAWL1_COLLISION_RIGHT_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_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
|
// Crawl2 dimensions and collision
|
||||||
static const int OBSTACLE_CRAWL2_FRAME_HEIGHT = sizeof(OBSTACLE_CRAWL2_FRAME) / sizeof(OBSTACLE_CRAWL2_FRAME[0]);
|
static const int OBSTACLE_CRAWL2_FRAME_HEIGHT = sizeof(OBSTACLE_CRAWL2_FRAME) / sizeof(OBSTACLE_CRAWL2_FRAME[0]);
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ inline bool check_collision(const CharacterPosition& player,
|
|||||||
MovementType movement,
|
MovementType movement,
|
||||||
const Obstacle& obs) {
|
const Obstacle& obs) {
|
||||||
int player_x = player.x;
|
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;
|
int box_x = 0, box_w = 0, box_h = 0;
|
||||||
|
|
||||||
switch (movement) {
|
switch (movement) {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "../game/collision.h"
|
#include "../game/collision.h"
|
||||||
#include "../assets/obstacle_crawl_frames.h"
|
#include "../assets/obstacle_crawl_frames.h"
|
||||||
|
#include "../assets/background_frame.h"
|
||||||
|
|
||||||
// One movable obstacle
|
// One movable obstacle
|
||||||
struct MovingObstacle {
|
struct MovingObstacle {
|
||||||
@@ -21,14 +22,11 @@ inline int spawn_obstacle(CrawlObstacleType type, int x_start) {
|
|||||||
obstacle_pool[i].active = true;
|
obstacle_pool[i].active = true;
|
||||||
obstacle_pool[i].type = type;
|
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;
|
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;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -115,8 +115,19 @@ void render_loop(int speed) {
|
|||||||
draw_character(draw_pos.x, draw_pos.y, frame.movement, frame.frame_index);
|
draw_character(draw_pos.x, draw_pos.y, frame.movement, frame.frame_index);
|
||||||
|
|
||||||
for (int i = 0; i < MAX_OBSTACLES; i++) {
|
for (int i = 0; i < MAX_OBSTACLES; i++) {
|
||||||
if (!obstacle_pool[i].active) continue;
|
if (!obstacle_pool[i].active)
|
||||||
draw_obstacle(obstacle_pool[i].data.x, obstacle_pool[i].data.y, obstacle_pool[i].type);
|
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)
|
if (game_over)
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
// src/render/obstacle.cpp
|
// src/render/obstacle.cpp
|
||||||
#include "obstacle.h"
|
#include "obstacle.h"
|
||||||
#include "../assets/obstacle_crawl_frames.h"
|
#include "../assets/obstacle_crawl_frames.h"
|
||||||
|
#include "../assets/background_frame.h"
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
// 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) {
|
void draw_obstacle(int x, int y, CrawlObstacleType type) {
|
||||||
const char **obstacle = nullptr;
|
const char **obstacle = nullptr;
|
||||||
int obstacle_height = 0;
|
int obstacle_height = 0;
|
||||||
@@ -23,7 +24,6 @@ void draw_obstacle(int x, int y, CrawlObstacleType type) {
|
|||||||
return; // in case of invalid type
|
return; // in case of invalid type
|
||||||
}
|
}
|
||||||
|
|
||||||
// Each obstacle row is printed using ANSI cursor positioning
|
|
||||||
for (int i = 0; i < obstacle_height; i++) {
|
for (int i = 0; i < obstacle_height; i++) {
|
||||||
printf("\033[%d;%dH%s", y + i + 1, x + 1, obstacle[i]);
|
printf("\033[%d;%dH%s", y + i + 1, x + 1, obstacle[i]);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user