core: fix enemy movement collision with the player

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I8bfcc0f8816fbc02dbd7ad462b5c0a4e6a6a6964
This commit is contained in:
raf 2026-03-24 17:09:47 +03:00
commit 5fbe7c8c60
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF
3 changed files with 66 additions and 57 deletions

View file

@ -5,11 +5,10 @@
#include <stddef.h>
#include <stdio.h>
void render_map(Map *map) {
void render_map(const Map *map) {
for (int y = 0; y < MAP_HEIGHT; y++) {
for (int x = 0; x < MAP_WIDTH; x++) {
Rectangle rect = {(float)(x * TILE_SIZE), (float)(y * TILE_SIZE),
(float)TILE_SIZE, (float)TILE_SIZE};
Rectangle rect = {(float)(x * TILE_SIZE), (float)(y * TILE_SIZE), (float)TILE_SIZE, (float)TILE_SIZE};
switch (map->tiles[y][x]) {
case TILE_WALL:
@ -28,32 +27,30 @@ void render_map(Map *map) {
}
}
void render_player(Player *p) {
Rectangle rect = {(float)(p->x * TILE_SIZE), (float)(p->y * TILE_SIZE),
(float)TILE_SIZE, (float)TILE_SIZE};
void render_player(const Player *p) {
Rectangle rect = {(float)(p->x * TILE_SIZE), (float)(p->y * TILE_SIZE), (float)TILE_SIZE, (float)TILE_SIZE};
DrawRectangleRec(rect, BLUE);
}
void render_enemies(Enemy *enemies, int count) {
void render_enemies(const Enemy *enemies, int count) {
for (int i = 0; i < count; i++) {
if (!enemies[i].alive)
continue;
Rectangle rect = {(float)(enemies[i].x * TILE_SIZE),
(float)(enemies[i].y * TILE_SIZE), (float)TILE_SIZE,
Rectangle rect = {(float)(enemies[i].x * TILE_SIZE), (float)(enemies[i].y * TILE_SIZE), (float)TILE_SIZE,
(float)TILE_SIZE};
// Different colors based on enemy type
Color enemy_color;
switch (enemies[i].type) {
case ENEMY_GOBLIN:
enemy_color = (Color){150, 50, 50, 255}; // dark red
enemy_color = (Color){150, 50, 50, 255}; // dark red
break;
case ENEMY_SKELETON:
enemy_color = (Color){200, 200, 200, 255}; // light gray
enemy_color = (Color){200, 200, 200, 255}; // light gray
break;
case ENEMY_ORC:
enemy_color = (Color){50, 150, 50, 255}; // dark green
enemy_color = (Color){50, 150, 50, 255}; // dark green
break;
default:
enemy_color = RED;
@ -63,37 +60,34 @@ void render_enemies(Enemy *enemies, int count) {
DrawRectangleRec(rect, enemy_color);
// Draw hp bar above enemy
int hp_percent =
(enemies[i].hp * TILE_SIZE) / 10; // FIXME: assuming max 10 hp, for now
int hp_percent = (enemies[i].hp * TILE_SIZE) / enemies[i].max_hp;
if (hp_percent > 0) {
Rectangle hp_bar = {(float)(enemies[i].x * TILE_SIZE),
(float)(enemies[i].y * TILE_SIZE - 4),
(float)hp_percent, 3};
Rectangle hp_bar = {(float)(enemies[i].x * TILE_SIZE), (float)(enemies[i].y * TILE_SIZE - 4), (float)hp_percent,
3};
DrawRectangleRec(hp_bar, GREEN);
}
}
}
void render_items(Item *items, int count) {
void render_items(const Item *items, int count) {
for (int i = 0; i < count; i++) {
if (items[i].picked_up)
continue;
Rectangle rect = {(float)(items[i].x * TILE_SIZE),
(float)(items[i].y * TILE_SIZE), (float)TILE_SIZE,
Rectangle rect = {(float)(items[i].x * TILE_SIZE), (float)(items[i].y * TILE_SIZE), (float)TILE_SIZE,
(float)TILE_SIZE};
// Different colors based on item type
Color item_color;
switch (items[i].type) {
case ITEM_POTION:
item_color = (Color){255, 100, 100, 255}; // red/pink
item_color = (Color){255, 100, 100, 255}; // red/pink
break;
case ITEM_WEAPON:
item_color = (Color){255, 255, 100, 255}; // yellow
item_color = (Color){255, 255, 100, 255}; // yellow
break;
case ITEM_ARMOR:
item_color = (Color){100, 100, 255, 255}; // blue
item_color = (Color){100, 100, 255, 255}; // blue
break;
default:
item_color = GREEN;
@ -104,15 +98,13 @@ void render_items(Item *items, int count) {
}
}
void render_ui(Player *p) {
void render_ui(const Player *p) {
// UI background bar at bottom of screen
Rectangle ui_bg = {0, (float)(MAP_HEIGHT * TILE_SIZE), (float)SCREEN_WIDTH,
60};
Rectangle ui_bg = {0, (float)(MAP_HEIGHT * TILE_SIZE), (float)SCREEN_WIDTH, 60};
DrawRectangleRec(ui_bg, (Color){30, 30, 30, 255});
// Draw dividing line
DrawLine(0, MAP_HEIGHT * TILE_SIZE, SCREEN_WIDTH, MAP_HEIGHT * TILE_SIZE,
GRAY);
DrawLine(0, MAP_HEIGHT * TILE_SIZE, SCREEN_WIDTH, MAP_HEIGHT * TILE_SIZE, GRAY);
// Player hp
char hp_text[32];
@ -136,8 +128,7 @@ void render_ui(Player *p) {
// Inventory count
char inv_text[32];
snprintf(inv_text, sizeof(inv_text), "Inv: %d/%d", p->inventory_count,
MAX_INVENTORY);
snprintf(inv_text, sizeof(inv_text), "Inv: %d/%d", p->inventory_count, MAX_INVENTORY);
DrawText(inv_text, 530, MAP_HEIGHT * TILE_SIZE + 10, 16, GREEN);
// Show first item in inventory if any
@ -158,14 +149,12 @@ void render_ui(Player *p) {
break;
}
char first_item[48];
snprintf(first_item, sizeof(first_item), "[%s +%d]", item_name,
p->inventory[0].power);
snprintf(first_item, sizeof(first_item), "[%s +%d]", item_name, p->inventory[0].power);
DrawText(first_item, 10, MAP_HEIGHT * TILE_SIZE + 35, 14, LIGHTGRAY);
}
// Controls hint
DrawText("WASD: Move | U: Use Item | Q: Quit", 280,
MAP_HEIGHT * TILE_SIZE + 35, 14, GRAY);
DrawText("WASD: Move | U: Use Item | Q: Quit", 280, MAP_HEIGHT * TILE_SIZE + 35, 14, GRAY);
}
void render_game_over(void) {
@ -176,13 +165,11 @@ void render_game_over(void) {
// Game over text
const char *title = "GAME OVER";
int title_width = MeasureText(title, 60);
DrawText(title, (SCREEN_WIDTH - title_width) / 2, SCREEN_HEIGHT / 2 - 30, 60,
RED);
DrawText(title, (SCREEN_WIDTH - title_width) / 2, SCREEN_HEIGHT / 2 - 30, 60, RED);
const char *subtitle = "Press R to restart or Q to quit";
int sub_width = MeasureText(subtitle, 20);
DrawText(subtitle, (SCREEN_WIDTH - sub_width) / 2, SCREEN_HEIGHT / 2 + 40, 20,
WHITE);
DrawText(subtitle, (SCREEN_WIDTH - sub_width) / 2, SCREEN_HEIGHT / 2 + 40, 20, WHITE);
}
void render_message(const char *message) {
@ -190,13 +177,10 @@ void render_message(const char *message) {
return;
// Draw message box
Rectangle msg_bg = {(float)(SCREEN_WIDTH / 2 - 150),
(float)(SCREEN_HEIGHT / 2 - 30), 300, 60};
Rectangle msg_bg = {(float)(SCREEN_WIDTH / 2 - 150), (float)(SCREEN_HEIGHT / 2 - 30), 300, 60};
DrawRectangleRec(msg_bg, (Color){50, 50, 50, 230});
DrawRectangleLines((int)msg_bg.x, (int)msg_bg.y, (int)msg_bg.width,
(int)msg_bg.height, WHITE);
DrawRectangleLines((int)msg_bg.x, (int)msg_bg.y, (int)msg_bg.width, (int)msg_bg.height, WHITE);
int msg_width = MeasureText(message, 20);
DrawText(message, (SCREEN_WIDTH - msg_width) / 2, SCREEN_HEIGHT / 2 - 10, 20,
WHITE);
DrawText(message, (SCREEN_WIDTH - msg_width) / 2, SCREEN_HEIGHT / 2 - 10, 20, WHITE);
}