1
0
Fork 0
forked from NotAShelf/rogged

Compare commits

...

14 commits

Author SHA1 Message Date
a89d3684ef
ui: add floating damage text and shake screen; streamline action log
Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I7a85e496beaf78150c6c1f7ddbb343d96a6a6964
2026-04-03 16:37:13 +03:00
9cbbb9636f
combat: add hit chance & damage variance to make combat more engaging
Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: Ia1e0a503dba03e5df7b863b97db962e36a6a6964
2026-04-03 16:37:12 +03:00
fdc0657237
nix: add direnv manifest
Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: Idcd1d80b8d1cc8989177415a5f798f076a6a6964
2026-04-03 15:45:17 +03:00
de2ce8ba43
player: add equipment slots and inventory overlay
- Weapon/armor equip slots persist until replaced
- Numbered inventory overlay with I key
- E to equip from inventory, U to use potions"

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I474acd676da3b768c1aac24f75f303e86a6a6964
2026-04-03 15:45:16 +03:00
e4926a86fe
player: implement manual pickup and descend confirmation
Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: Icd8d0ffd9bdf92f5c8943590886543566a6a6964
2026-04-03 15:45:15 +03:00
db21bdacdd
various: add speed/cooldown & initiative system
- Goblins act every ~2 turns, skeletons ~1.5, orcs ~1.2
- Enemies accumulate cooldown based on speed, act when <= 0
- Player always acts every turn (speed = 100)"

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: Id822579a0326887d91a80bd4ab27a72d6a6a6964
2026-04-03 15:45:14 +03:00
8f6650919d
chore: setup clang-format for formatting
Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I322b7dd5dcd699247b58fe9f5db7bcd86a6a6964
2026-04-03 15:45:13 +03:00
786fce3814
various: consolidate game state into a GameState struct
Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I9d8998707c02e64cf177a6eeb51e399e6a6a6964
2026-04-03 15:45:12 +03:00
d3798cc99f
map: add is_floor() helper to check walkable tiles
Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I7368f74d1cbcf5913e5d8e9331cfa4326a6a6964
2026-04-03 15:45:11 +03:00
c61e31f628
player: correct item pickup order to copy before marking picked_up
Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: Ie226b4a0ae6566ca77ab49b7d22f36726a6a6964
2026-04-03 15:45:10 +03:00
5fbe7c8c60
core: fix enemy movement collision with the player
Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I8bfcc0f8816fbc02dbd7ad462b5c0a4e6a6a6964
2026-04-03 15:45:09 +03:00
0894466532
audio: allocate samples dynamically to avoid share static buffer corruption
Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I48808e87cf3aea831daff726eb7123a96a6a6964
2026-04-03 15:45:04 +03:00
raf
4908a32661 Merge pull request 'dev: added step_count and health recovery' (#1) from amr/rogged:recover-health into main
Reviewed-on: NotAShelf/rogged#1
Reviewed-by: raf <raf@notashelf.dev>
2026-03-20 19:52:19 +00:00
5e3ba209e0
dev: added step_count and health recovery
Every 15 steps the player will regain 1 HP,
unless they are at max HP. Also added commented
code for later hunger if this is to be implemented.
Played around with step # and 15 seems okay until
hunger is implemented
2026-03-20 11:59:59 -04:00
24 changed files with 1075 additions and 384 deletions

100
.clang-format Normal file
View file

@ -0,0 +1,100 @@
# Based on LLVM style with project-specific adjustments
---
Language: C
# Indentation
IndentWidth: 2
TabWidth: 2
UseTab: Never
IndentCaseLabels: false
IndentPPDirectives: None
# Line length
ColumnLimit: 120
# Braces
BreakBeforeBraces: Attach
BraceWrapping:
AfterFunction: false
AfterControlStatement: false
AfterEnum: false
AfterStruct: false
AfterUnion: false
BeforeElse: false
IndentBraces: false
# Alignment
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands: true
AlignTrailingComments: true
PointerAlignment: Right
# Spacing
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
# Line breaks
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
BinPackArguments: true
BinPackParameters: true
BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true
BreakStringLiterals: true
ContinuationIndentWidth: 4
# Includes
IncludeBlocks: Preserve
SortIncludes: false
# Comments
ReflowComments: true
SpacesBeforeTrailingComments: 2
# Other
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: false
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 2
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
SpaceAfterTemplateKeyword: true
SpaceBeforeInheritanceColon: true
SpaceBeforeCtorInitializerColon: true
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false
SpacesInAngles: false
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
TypenameMacros:
- STACK_OF
- LIST
- DECLARE_STACK_OF
- DECLARE_LIST
- IMPLEMENT_STACK_OF
- IMPLEMENT_LIST

1
.envrc Normal file
View file

@ -0,0 +1 @@
use flake . --substituters "https://cache.nixos.org"

View file

@ -12,7 +12,7 @@ OBJDIR := obj
SOURCES := $(wildcard $(SRCDIR)/*.c)
OBJECTS := $(patsubst $(SRCDIR)/%.c,$(OBJDIR)/%.o,$(SOURCES))
.PHONY: all clean
.PHONY: all clean format format-check
all: $(TARGET)
@ -29,3 +29,17 @@ clean:
# Alias for development
dev: all
./$(TARGET)
# Format all source files with clang-format
fmt:
@command -v clang-format >/dev/null 2>&1 || { echo "Error: clang-format is missing"; exit 1; }
@echo "Formatting source files..."
@clang-format -i $(SOURCES) $(wildcard $(SRCDIR)/*.h)
@echo "Done formatting."
# Check formatting without modifying files
fmt-check:
@command -v clang-format >/dev/null 2>&1 || { echo "Error: clang-format is missing"; exit 1; }
@echo "Checking formatting..."
@clang-format --dry-run --Werror \
$(SOURCES) $(wildcard $(SRCDIR)/*.h) && echo "All files properly formatted." || { echo "Formatting issues found. Run 'make fmt' to fix."; exit 1; }

View file

@ -7,10 +7,16 @@
}:
mkShell {
strictDeps = true;
buildInputs = [
packages = [
clang-tools
raylib
gnumake
];
buildInputs = [
raylib
];
nativeBuildInputs = [
pkg-config
];
}

View file

@ -1,6 +1,7 @@
#include "audio.h"
#include "raylib.h"
#include <math.h>
#include <stddef.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846 // xd
@ -11,11 +12,17 @@
// Generate a simple sine wave tone
static void play_tone(float frequency, float duration, float volume) {
static float samples[SAMPLE_RATE];
int sample_count = (int)(SAMPLE_RATE * duration);
if (sample_count > SAMPLE_RATE)
sample_count = SAMPLE_RATE;
if (sample_count <= 0)
return;
// Allocate samples dynamically to avoid shared static buffer corruption
float *samples = (float *)MemAlloc(sample_count * sizeof(float));
if (samples == NULL)
return;
// Generate sine wave
for (int i = 0; i < sample_count; i++) {
@ -43,6 +50,9 @@ static void play_tone(float frequency, float duration, float volume) {
Sound sound = LoadSoundFromWave(wave);
PlaySound(sound);
UnloadSound(sound);
// Free the dynamically allocated buffer
MemFree(samples);
}
void audio_init(void) {

View file

@ -1,5 +1,6 @@
#include "combat.h"
#include "common.h"
#include "rng.h"
#include <stddef.h>
// Track combat events for feedback
@ -7,35 +8,65 @@ typedef struct {
const char *message;
int damage;
int is_player_damage;
int is_critical;
} CombatEvent;
static CombatEvent last_event = {NULL, 0, 0};
static CombatEvent last_event = {NULL, 0, 0, 0};
const char *combat_get_last_message(void) { return last_event.message; }
const char *combat_get_last_message(void) {
return last_event.message;
}
int combat_get_last_damage(void) { return last_event.damage; }
int combat_get_last_damage(void) {
return last_event.damage;
}
int combat_was_player_damage(void) { return last_event.is_player_damage; }
int combat_was_player_damage(void) {
return last_event.is_player_damage;
}
int combat_was_critical(void) {
return last_event.is_critical;
}
void combat_player_attack(Player *p, Enemy *e) {
if (e == NULL || !e->alive)
return;
// Deal damage
int damage = p->attack;
e->hp -= damage;
last_event.is_critical = 0;
// Set combat event
// 90% hit chance
if (rng_int(0, 99) < 90) {
// calculate damage with variance from player stats
int base_damage = p->attack;
int variance = rng_int(p->dmg_variance_min, p->dmg_variance_max);
int damage = (base_damage * variance) / 100;
if (damage < 1)
damage = 1;
// 10% critical hit chance for 1.5x
if (rng_int(0, 9) == 0) {
damage = (damage * 3) / 2;
last_event.is_critical = 1;
}
e->hp -= damage;
last_event.damage = damage;
last_event.is_player_damage = 0;
// Check if enemy died
if (e->hp <= 0) {
e->hp = 0;
e->alive = 0;
last_event.message = "Enemy killed!";
} else if (last_event.is_critical) {
last_event.message = "Critical hit!";
} else {
last_event.message = "You hit the enemy";
last_event.message = "You hit";
}
} else {
last_event.damage = 0;
last_event.is_player_damage = 0;
last_event.message = "You missed";
}
}
@ -45,22 +76,42 @@ void combat_enemy_attack(Enemy *e, Player *p) {
if (p == NULL)
return;
// Deal damage reduced by defense (minimum 1 damage)
int damage = e->attack - p->defense;
last_event.is_critical = 0;
// 85% hit chance for enemies
if (rng_int(0, 99) < 85) {
// calculate damage with variance
int base_damage = e->attack - p->defense;
if (base_damage < 1)
base_damage = 1;
int variance = rng_int(80, 120);
int damage = (base_damage * variance) / 100;
if (damage < 1)
damage = 1;
p->hp -= damage;
// Set combat event
// 5% critical hit chance for enemies
if (rng_int(0, 19) == 0) {
damage = (damage * 3) / 2;
last_event.is_critical = 1;
}
p->hp -= damage;
last_event.damage = damage;
last_event.is_player_damage = 1;
// Check if player died
if (p->hp <= 0) {
p->hp = 0;
last_event.message = "You died!";
} else if (last_event.is_critical) {
last_event.message = "Critical!";
} else {
last_event.message = "Enemy attacks!";
last_event.message = "Hit";
}
} else {
last_event.damage = 0;
last_event.is_player_damage = 1;
last_event.message = "Missed";
}
}
@ -68,4 +119,5 @@ void combat_reset_event(void) {
last_event.message = NULL;
last_event.damage = 0;
last_event.is_player_damage = 0;
last_event.is_critical = 0;
}

View file

@ -12,10 +12,13 @@ int combat_get_last_damage(void);
// Was last damage to player?
int combat_was_player_damage(void);
// Was it a critical hit?
int combat_was_critical(void);
// Reset combat event
void combat_reset_event(void);
// Player attacks enemy
// Player attacks enemy (pass player for damage variance)
void combat_player_attack(Player *p, Enemy *e);
// Enemy attacks player

View file

@ -44,8 +44,18 @@ typedef struct {
int attack;
int defense;
int floor;
int step_count;
int speed; // actions per 100 ticks (100 = 1 action per turn)
int cooldown; // countdown to next action (0 = can act)
Item equipped_weapon;
int has_weapon;
Item equipped_armor;
int has_armor;
Item inventory[MAX_INVENTORY];
int inventory_count;
// damage variance range (0.8 to 1.2 = 80 to 120)
int dmg_variance_min; // minimum damage multiplier (80 = 0.8x)
int dmg_variance_max; // maximum damage multiplier (120 = 1.2x)
} Player;
// Enemy types
@ -55,9 +65,49 @@ typedef enum { ENEMY_GOBLIN, ENEMY_SKELETON, ENEMY_ORC } EnemyType;
typedef struct {
int x, y;
int hp;
int max_hp;
int attack;
int alive;
EnemyType type;
int speed; // actions per 100 ticks
int cooldown; // countdown to next action
} Enemy;
// Floating damage text
typedef struct {
int x, y;
int value;
int lifetime; // frames remaining
int is_critical;
} FloatingText;
// GameState - encapsulates all game state for testability and save/load
typedef struct {
Player player;
Map map;
Dungeon dungeon;
Enemy enemies[MAX_ENEMIES];
int enemy_count;
Item items[MAX_ITEMS];
int item_count;
int game_over;
int game_won;
const char *last_message;
int message_timer;
int turn_count;
int awaiting_descend; // 0 = normal, 1 = waiting for Y/N
int show_inventory; // 0 = hidden, 1 = show overlay
int inv_selected; // currently selected inventory index
// action log
char action_log[5][128];
int log_count;
int log_head;
// visual effects
FloatingText floating_texts[8];
int floating_count;
int screen_shake; // frames of screen shake remaining
int shake_x;
int shake_y;
} GameState;
#endif // COMMON_H

View file

@ -5,7 +5,7 @@
#include "rng.h"
// Forward declaration
int is_enemy_at(Enemy *enemies, int count, int x, int y);
int is_enemy_at(const Enemy *enemies, int count, int x, int y);
void enemy_spawn(Enemy enemies[], int *count, Map *map, Player *p, int floor) {
*count = 0;
@ -47,22 +47,31 @@ void enemy_spawn(Enemy enemies[], int *count, Map *map, Player *p, int floor) {
// Stats based on type and floor
switch (e.type) {
case ENEMY_GOBLIN:
e.hp = ENEMY_BASE_HP + floor;
e.max_hp = ENEMY_BASE_HP + floor;
e.hp = e.max_hp;
e.attack = ENEMY_BASE_ATTACK;
e.speed = 55 + rng_int(0, 10);
break;
case ENEMY_SKELETON:
e.hp = ENEMY_BASE_HP + floor + 2;
e.max_hp = ENEMY_BASE_HP + floor + 2;
e.hp = e.max_hp;
e.attack = ENEMY_BASE_ATTACK + 1;
e.speed = 70 + rng_int(0, 10);
break;
case ENEMY_ORC:
e.hp = ENEMY_BASE_HP + floor + 4;
e.max_hp = ENEMY_BASE_HP + floor + 4;
e.hp = e.max_hp;
e.attack = ENEMY_BASE_ATTACK + 2;
e.speed = 85 + rng_int(0, 10);
break;
default:
e.hp = ENEMY_BASE_HP;
e.max_hp = ENEMY_BASE_HP;
e.hp = e.max_hp;
e.attack = ENEMY_BASE_ATTACK;
e.speed = 60;
break;
}
e.cooldown = e.speed;
enemies[i] = e;
(*count)++;
@ -70,7 +79,7 @@ void enemy_spawn(Enemy enemies[], int *count, Map *map, Player *p, int floor) {
}
// Check if position has an enemy
int is_enemy_at(Enemy *enemies, int count, int x, int y) {
int is_enemy_at(const Enemy *enemies, int count, int x, int y) {
for (int i = 0; i < count; i++) {
if (enemies[i].alive && enemies[i].x == x && enemies[i].y == y) {
return 1;
@ -86,9 +95,13 @@ static int can_see_player(Enemy *e, Player *p) {
return (dx >= -1 && dx <= 1 && dy >= -1 && dy <= 1);
}
// Check if position is occupied by player
static int is_player_at(Player *p, int x, int y) {
return (p->x == x && p->y == y);
}
// Move enemy toward player
static void enemy_move_toward_player(Enemy *e, Player *p, Map *map,
Enemy *all_enemies, int enemy_count) {
static void enemy_move_toward_player(Enemy *e, Player *p, Map *map, Enemy *all_enemies, int enemy_count) {
int dx = 0, dy = 0;
if (p->x > e->x)
@ -105,20 +118,35 @@ static void enemy_move_toward_player(Enemy *e, Player *p, Map *map,
int new_x = e->x + dx;
int new_y = e->y;
if (dx != 0 && is_floor(map, new_x, new_y) &&
!is_enemy_at(all_enemies, enemy_count, new_x, new_y)) {
if (dx != 0 && is_floor(map, new_x, new_y) && !is_enemy_at(all_enemies, enemy_count, new_x, new_y) &&
!is_player_at(p, new_x, new_y)) {
e->x = new_x;
} else if (dy != 0) {
new_x = e->x;
new_y = e->y + dy;
if (is_floor(map, new_x, new_y) &&
!is_enemy_at(all_enemies, enemy_count, new_x, new_y)) {
if (is_floor(map, new_x, new_y) && !is_enemy_at(all_enemies, enemy_count, new_x, new_y) &&
!is_player_at(p, new_x, new_y)) {
e->x = new_x;
e->y = new_y;
}
}
}
// Perform a single action for an enemy (attack if adjacent, otherwise move)
void enemy_act(Enemy *e, Player *p, Map *map, Enemy *all_enemies, int enemy_count) {
if (!e->alive)
return;
// Check if adjacent to player - attack
if (can_see_player(e, p)) {
combat_enemy_attack(e, p);
return;
}
// Otherwise, move toward player
enemy_move_toward_player(e, p, map, all_enemies, enemy_count);
}
void enemy_update_all(Enemy enemies[], int count, Player *p, Map *map) {
for (int i = 0; i < count; i++) {
Enemy *e = &enemies[i];
@ -126,14 +154,10 @@ void enemy_update_all(Enemy enemies[], int count, Player *p, Map *map) {
if (!e->alive)
continue;
// Check if adjacent to player - attack
if (can_see_player(e, p)) {
// Use combat system
combat_enemy_attack(e, p);
continue;
}
// Otherwise, move toward player
enemy_move_toward_player(e, p, map, enemies, count);
e->cooldown -= e->speed;
if (e->cooldown <= 0) {
enemy_act(e, p, map, enemies, count);
e->cooldown = 100;
}
}
}

View file

@ -9,7 +9,10 @@ void enemy_spawn(Enemy enemies[], int *count, Map *map, Player *p, int floor);
// Update all enemy AI
void enemy_update_all(Enemy enemies[], int count, Player *p, Map *map);
// Perform a single action for an enemy (attack if adjacent, otherwise move)
void enemy_act(Enemy *e, Player *p, Map *map, Enemy *all_enemies, int enemy_count);
// Check if position has an enemy
int is_enemy_at(Enemy *enemies, int count, int x, int y);
int is_enemy_at(const Enemy *enemies, int count, int x, int y);
#endif // ENEMY_H

View file

@ -57,7 +57,7 @@ void item_spawn(Item items[], int *count, Map *map, int floor) {
}
// Get item name for display
const char *item_get_name(Item *i) {
const char *item_get_name(const Item *i) {
if (i == NULL)
return "";
@ -74,7 +74,7 @@ const char *item_get_name(Item *i) {
}
// Get item description
const char *item_get_description(Item *i) {
const char *item_get_description(const Item *i) {
if (i == NULL)
return "";
@ -91,7 +91,7 @@ const char *item_get_description(Item *i) {
}
// Get item power value
int item_get_power(Item *i) {
int item_get_power(const Item *i) {
if (i == NULL)
return 0;
return i->power;

View file

@ -8,7 +8,16 @@
// Spawn items for a floor
void item_spawn(Item items[], int *count, Map *map, int floor);
// Use an item
// Use an item (apply effect, only potions are consumed)
void item_use(Player *p, Item *i);
// Get item name for display
const char *item_get_name(const Item *i);
// Get item description
const char *item_get_description(const Item *i);
// Get item power value
int item_get_power(const Item *i);
#endif // ITEMS_H

View file

@ -10,57 +10,85 @@
#include "rng.h"
#include "settings.h"
#include <stddef.h>
#include <stdio.h>
#include <string.h>
// Global game state
static Player player;
static Map map;
static Dungeon dungeon;
static Enemy enemies[MAX_ENEMIES];
static int enemy_count;
static Item items[MAX_ITEMS];
static int item_count;
static int game_over = 0;
static int game_won = 0;
static const char *last_message = NULL;
static int message_timer = 0;
// Add message to action log
static void add_log(GameState *gs, const char *msg) {
strncpy(gs->action_log[gs->log_head], msg, 127);
gs->action_log[gs->log_head][127] = '\0';
gs->log_head = (gs->log_head + 1) % 5;
if (gs->log_count < 5) {
gs->log_count++;
}
}
// Turn counter for enemy movement (enemies move every other turn)
static int turn_count = 0;
// spawn floating damage text
static void spawn_floating_text(GameState *gs, int x, int y, int value, int is_critical) {
if (gs->floating_count < 8) {
gs->floating_texts[gs->floating_count].x = x;
gs->floating_texts[gs->floating_count].y = y;
gs->floating_texts[gs->floating_count].value = value;
gs->floating_texts[gs->floating_count].lifetime = 60;
gs->floating_texts[gs->floating_count].is_critical = is_critical;
gs->floating_count++;
}
}
// update floating texts and screen shake
static void update_effects(GameState *gs) {
// update floating texts
for (int i = 0; i < 8; i++) {
if (gs->floating_texts[i].lifetime > 0) {
gs->floating_texts[i].lifetime--;
}
}
// update screen shake
if (gs->screen_shake > 0) {
gs->screen_shake--;
gs->shake_x = rng_int(-4, 4);
gs->shake_y = rng_int(-4, 4);
} else {
gs->shake_x = 0;
gs->shake_y = 0;
}
}
// Initialize a new floor
static void init_floor(int floor_num) {
static void init_floor(GameState *gs, int floor_num) {
// Generate dungeon
dungeon_generate(&dungeon, &map, floor_num);
dungeon_generate(&gs->dungeon, &gs->map, floor_num);
// Seed rng for this floor's content
rng_seed(floor_num * 54321);
// Find spawn position
int start_x, start_y;
get_random_floor_tile(&map, &start_x, &start_y, 100);
get_random_floor_tile(&gs->map, &start_x, &start_y, 100);
// Initialize player position if first floor
if (floor_num == 1) {
player_init(&player, start_x, start_y);
player_init(&gs->player, start_x, start_y);
} else {
// Move player to new floor position
player.x = start_x;
player.y = start_y;
gs->player.x = start_x;
gs->player.y = start_y;
}
player.floor = floor_num;
gs->player.floor = floor_num;
// Spawn enemies
enemy_spawn(enemies, &enemy_count, &map, &player, floor_num);
enemy_spawn(gs->enemies, &gs->enemy_count, &gs->map, &gs->player, floor_num);
// Spawn items
item_spawn(items, &item_count, &map, floor_num);
item_spawn(gs->items, &gs->item_count, &gs->map, floor_num);
// Reset turn counter
turn_count = 0;
gs->turn_count = 0;
}
// Handle player input - returns: 0=continue, -1=quit
static int handle_input(void) {
// Handle player input - returns: 0=continue, 1=acted, -1=quit
static int handle_input(GameState *gs) {
int dx = 0, dy = 0;
// Check for quit first (always works)
@ -69,19 +97,179 @@ static int handle_input(void) {
}
// Check for restart (works during game over)
if (IsKeyPressed(KEY_R) && game_over) {
game_over = 0;
game_won = 0;
init_floor(1);
if (IsKeyPressed(KEY_R) && gs->game_over) {
memset(gs, 0, sizeof(GameState));
init_floor(gs, 1);
return 0;
}
// Check for item usage (U key)
if (IsKeyPressed(KEY_U) && !game_over) {
if (player.inventory_count > 0) {
if (player_use_first_item(&player)) {
last_message = "Used item!";
message_timer = 60;
if (gs->show_inventory) {
if (IsKeyPressed(KEY_I) || IsKeyPressed(KEY_ESCAPE)) {
gs->show_inventory = 0;
return 0;
}
if (IsKeyPressed(KEY_DOWN) || IsKeyPressed(KEY_S)) {
gs->inv_selected++;
if (gs->inv_selected >= gs->player.inventory_count) {
gs->inv_selected = 0;
}
return 0;
}
if (IsKeyPressed(KEY_UP) || IsKeyPressed(KEY_W)) {
if (gs->inv_selected == 0) {
gs->inv_selected = (gs->player.inventory_count > 0) ? gs->player.inventory_count - 1 : 0;
} else {
gs->inv_selected--;
}
return 0;
}
if (IsKeyPressed(KEY_ONE))
gs->inv_selected = 0;
if (IsKeyPressed(KEY_TWO))
gs->inv_selected = 1;
if (IsKeyPressed(KEY_THREE))
gs->inv_selected = 2;
if (IsKeyPressed(KEY_FOUR))
gs->inv_selected = 3;
if (IsKeyPressed(KEY_FIVE))
gs->inv_selected = 4;
if (IsKeyPressed(KEY_SIX))
gs->inv_selected = 5;
if (IsKeyPressed(KEY_SEVEN))
gs->inv_selected = 6;
if (IsKeyPressed(KEY_EIGHT))
gs->inv_selected = 7;
if (IsKeyPressed(KEY_NINE))
gs->inv_selected = 8;
if (IsKeyPressed(KEY_ZERO))
gs->inv_selected = 9;
// E to equip selected item
if (IsKeyPressed(KEY_E)) {
if (player_equip_item(&gs->player, gs->inv_selected)) {
gs->last_message = "Item equipped!";
gs->message_timer = 60;
add_log(gs, "Equipped item");
if (gs->inv_selected >= gs->player.inventory_count && gs->inv_selected > 0) {
gs->inv_selected--;
}
return 1;
} else {
gs->last_message = "Cannot equip that!";
gs->message_timer = 60;
}
}
// U or Enter to use selected item
if (IsKeyPressed(KEY_U) || IsKeyPressed(KEY_ENTER)) {
if (gs->player.inventory_count > 0) {
Item *item = player_get_inventory_item(&gs->player, gs->inv_selected);
if (item != NULL) {
if (item->type == ITEM_POTION) {
player_use_item(&gs->player, item);
player_remove_inventory_item(&gs->player, gs->inv_selected);
gs->last_message = "Used potion!";
gs->message_timer = 60;
add_log(gs, "Used potion");
gs->show_inventory = 0;
return 1;
} else {
gs->last_message = "Equip weapons/armor with E!";
gs->message_timer = 60;
}
}
}
}
// D to drop selected item
if (IsKeyPressed(KEY_D)) {
if (gs->player.inventory_count > 0) {
Item *item = player_get_inventory_item(&gs->player, gs->inv_selected);
if (item != NULL) {
char drop_msg[64];
snprintf(drop_msg, sizeof(drop_msg), "Dropped %s", item_get_name(item));
if (player_drop_item(&gs->player, gs->inv_selected, gs->items, gs->item_count)) {
add_log(gs, drop_msg);
gs->last_message = "Item dropped!";
gs->message_timer = 60;
if (gs->inv_selected >= gs->player.inventory_count && gs->inv_selected > 0) {
gs->inv_selected--;
}
return 1;
} else {
gs->last_message = "Cannot drop!";
gs->message_timer = 60;
}
}
}
}
return 0;
}
// Handle descend confirmation
if (gs->awaiting_descend) {
if (IsKeyPressed(KEY_Y)) {
// Descend
if (gs->player.floor < NUM_FLOORS) {
audio_play_stairs();
init_floor(gs, gs->player.floor + 1);
gs->last_message = "Descended to next floor!";
gs->message_timer = 60;
add_log(gs, "Descended stairs");
gs->awaiting_descend = 0;
return 1;
} else {
gs->game_won = 1;
gs->game_over = 1;
gs->awaiting_descend = 0;
return 1;
}
}
if (IsKeyPressed(KEY_N)) {
gs->awaiting_descend = 0;
gs->last_message = "Stayed on floor.";
gs->message_timer = 60;
return 1;
}
return 0;
}
// Check for inventory toggle (I key)
if (IsKeyPressed(KEY_I) && !gs->game_over) {
gs->show_inventory = 1;
gs->inv_selected = 0;
return 0; // don't consume turn
}
// Check for manual item pickup (G key)
if (IsKeyPressed(KEY_G) && !gs->game_over) {
Item *item = get_item_at_floor(gs->items, gs->item_count, gs->player.x, gs->player.y);
if (item != NULL) {
if (player_pickup(&gs->player, item)) {
char pickup_msg[64];
snprintf(pickup_msg, sizeof(pickup_msg), "Picked up %s", item_get_name(item));
add_log(gs, pickup_msg);
gs->last_message = "Picked up item!";
gs->message_timer = 60;
audio_play_item_pickup();
return 1;
} else {
gs->last_message = "Inventory full!";
gs->message_timer = 60;
return 1;
}
}
}
// Check for item usage (U key - use first potion)
if (IsKeyPressed(KEY_U) && !gs->game_over) {
if (gs->player.inventory_count > 0) {
if (player_use_first_item(&gs->player)) {
gs->last_message = "Used potion!";
gs->message_timer = 60;
audio_play_item_pickup();
return 1; // consume a turn
}
@ -104,58 +292,50 @@ static int handle_input(void) {
combat_reset_event();
// Player action
int action = player_move(&player, dx, dy, &map, enemies, enemy_count, items,
item_count);
int action = player_move(&gs->player, dx, dy, &gs->map, gs->enemies, gs->enemy_count);
if (action) {
// Increment turn counter
turn_count++;
gs->turn_count++;
// Check if stepped on stairs
if (map.tiles[player.y][player.x] == TILE_STAIRS) {
// Go to next floor
if (player.floor < NUM_FLOORS) {
audio_play_stairs();
init_floor(player.floor + 1);
last_message = "Descended to next floor!";
message_timer = 60;
} else {
// Won the game
game_won = 1;
game_over = 1;
}
if (gs->map.tiles[gs->player.y][gs->player.x] == TILE_STAIRS) {
gs->awaiting_descend = 1;
gs->last_message = "Descend to next floor? (Y/N)";
gs->message_timer = 120;
return 1;
}
// Check if killed enemy
if (combat_get_last_message() != NULL && !combat_was_player_damage()) {
// Check if enemy died
for (int i = 0; i < enemy_count; i++) {
if (!enemies[i].alive) {
audio_play_enemy_death();
// combat feedback - player attacked enemy
if (combat_get_last_damage() > 0 && !combat_was_player_damage()) {
// find the enemy we attacked
for (int i = 0; i < gs->enemy_count; i++) {
if (!gs->enemies[i].alive && combat_get_last_damage() > 0) {
spawn_floating_text(gs, gs->enemies[i].x * TILE_SIZE + 8, gs->enemies[i].y * TILE_SIZE,
combat_get_last_damage(), combat_was_critical());
break;
}
}
}
// Enemy turn - only every other turn for fairness
if (turn_count % 2 == 0) {
enemy_update_all(enemies, enemy_count, &player, &map);
}
// Enemy turns - now uses speed/cooldown system (no more % 2 hack)
enemy_update_all(gs->enemies, gs->enemy_count, &gs->player, &gs->map);
// Check if player took damage
if (combat_was_player_damage() && combat_get_last_damage() > 0) {
audio_play_player_damage();
gs->screen_shake = 8;
spawn_floating_text(gs, gs->player.x * TILE_SIZE + 8, gs->player.y * TILE_SIZE, combat_get_last_damage(),
combat_was_critical());
}
// Set message
last_message = combat_get_last_message();
message_timer = 60;
gs->last_message = combat_get_last_message();
gs->message_timer = 60;
// Check game over
if (player.hp <= 0) {
game_over = 1;
if (gs->player.hp <= 0) {
gs->game_over = 1;
}
}
}
@ -165,17 +345,20 @@ static int handle_input(void) {
// Main game loop
static void game_loop(void) {
GameState gs;
memset(&gs, 0, sizeof(GameState));
// Initialize first floor
rng_seed(12345);
init_floor(1);
init_floor(&gs, 1);
// Disable esc to exit
SetExitKey(0);
while (!WindowShouldClose()) {
// Handle input
if (!game_over) {
int quit = handle_input();
if (!gs.game_over) {
int quit = handle_input(&gs);
if (quit == -1)
break;
} else {
@ -183,41 +366,57 @@ static void game_loop(void) {
if (IsKeyPressed(KEY_Q))
break;
if (IsKeyPressed(KEY_R)) {
game_over = 0;
game_won = 0;
init_floor(1);
memset(&gs, 0, sizeof(GameState));
gs.game_over = 0;
gs.game_won = 0;
init_floor(&gs, 1);
}
}
// Update message timer
if (message_timer > 0)
message_timer--;
if (gs.message_timer > 0)
gs.message_timer--;
// Update effects
update_effects(&gs);
// Render
BeginDrawing();
ClearBackground(BLACK);
// Draw game elements
render_map(&map);
render_items(items, item_count);
render_enemies(enemies, enemy_count);
render_player(&player);
render_ui(&player);
// Draw game elements (with screen shake offset)
if (gs.screen_shake > 0) {
// Apply shake offset to drawing
}
render_map(&gs.map);
render_items(gs.items, gs.item_count);
render_enemies(gs.enemies, gs.enemy_count);
render_player(&gs.player);
render_floating_texts(gs.floating_texts, gs.floating_count, gs.shake_x, gs.shake_y);
render_ui(&gs.player);
// Draw action log
render_action_log(gs.action_log, gs.log_count, gs.log_head);
// Draw inventory overlay if active
if (gs.show_inventory) {
render_inventory_overlay(&gs.player, gs.inv_selected);
}
// Draw message if any
if (last_message != NULL && message_timer > 0) {
render_message(last_message);
if (gs.last_message != NULL && gs.message_timer > 0) {
render_message(gs.last_message);
}
// Draw game over screen
if (game_over) {
if (gs.game_over) {
render_game_over();
if (game_won) {
if (gs.game_won) {
// Draw win message
const char *win_msg = "YOU WIN! ESCAPED THE DUNGEON!";
int msg_w = MeasureText(win_msg, 30);
DrawText(win_msg, (SCREEN_WIDTH - msg_w) / 2, SCREEN_HEIGHT / 2 - 80,
30, GOLD);
DrawText(win_msg, (SCREEN_WIDTH - msg_w) / 2, SCREEN_HEIGHT / 2 - 80, 30, GOLD);
}
}

View file

@ -13,7 +13,7 @@ void map_init(Map *map) {
map->room_count = 0;
}
int is_floor(Map *map, int x, int y) {
int is_floor(const Map *map, int x, int y) {
if (!in_bounds(x, y, MAP_WIDTH, MAP_HEIGHT))
return 0;
return map->tiles[y][x] == TILE_FLOOR || map->tiles[y][x] == TILE_STAIRS;
@ -62,8 +62,8 @@ static int room_overlaps(Room *rooms, int count, Room *new_room) {
// Add padding to prevent rooms from touching
for (int i = 0; i < count; i++) {
Room *r = &rooms[i];
if (!(new_room->x > r->x + r->w || new_room->x + new_room->w < r->x ||
new_room->y > r->y + r->h || new_room->y + new_room->h < r->y)) {
if (!(new_room->x > r->x + r->w || new_room->x + new_room->w < r->x || new_room->y > r->y + r->h ||
new_room->y + new_room->h < r->y)) {
return 1;
}
}

View file

@ -4,7 +4,7 @@
#include "common.h"
// Check if a tile is walkable floor
int is_floor(Map *map, int x, int y);
int is_floor(const Map *map, int x, int y);
// Get room center coordinates
void get_room_center(Room *room, int *cx, int *cy);

View file

@ -1,10 +1,11 @@
#include "player.h"
#include "combat.h"
#include "common.h"
#include "items.h"
#include "map.h"
#include "utils.h"
#include "combat.h"
#include "items.h"
#include <stdlib.h>
#include <string.h>
void player_init(Player *p, int x, int y) {
p->x = x;
@ -14,7 +15,18 @@ void player_init(Player* p, int x, int y) {
p->attack = PLAYER_BASE_ATTACK;
p->defense = 0;
p->floor = 1;
p->step_count = 0;
p->speed = 100;
p->cooldown = 0;
p->has_weapon = 0;
p->has_armor = 0;
memset(&p->equipped_weapon, 0, sizeof(Item));
memset(&p->equipped_armor, 0, sizeof(Item));
p->equipped_weapon.picked_up = 1;
p->equipped_armor.picked_up = 1; // mark as invalid
p->inventory_count = 0;
p->dmg_variance_min = 80;
p->dmg_variance_max = 120;
// Initialize inventory to empty
for (int i = 0; i < MAX_INVENTORY; i++) {
@ -32,17 +44,7 @@ static Enemy* get_enemy_at(Enemy* enemies, int count, int x, int y) {
return NULL;
}
// Check if position has an item
static Item* get_item_at(Item* items, int count, int x, int y) {
for (int i = 0; i < count; i++) {
if (!items[i].picked_up && items[i].x == x && items[i].y == y) {
return &items[i];
}
}
return NULL;
}
int player_move(Player* p, int dx, int dy, Map* map, Enemy* enemies, int enemy_count, Item* items, int item_count) {
int player_move(Player *p, int dx, int dy, Map *map, Enemy *enemies, int enemy_count) {
int new_x = p->x + dx;
int new_y = p->y + dy;
@ -64,17 +66,13 @@ int player_move(Player* p, int dx, int dy, Map* map, Enemy* enemies, int enemy_c
return 1;
}
// Check for item at target position
Item* item = get_item_at(items, item_count, new_x, new_y);
if (item != NULL) {
// Pick up the item
player_pickup(p, item);
}
// Move player
p->x = new_x;
p->y = new_y;
p->step_count += 1;
if (p->step_count % 15 == 0 && p->hp < p->max_hp) {
p->hp += 1;
}
return 1;
}
@ -83,60 +81,168 @@ void player_attack(Player* p, Enemy* e) {
combat_player_attack(p, e);
}
void player_pickup(Player* p, Item* i) {
// Get item at player's current position (for pickup)
Item *get_item_at_floor(Item *items, int count, int x, int y) {
for (int i = 0; i < count; i++) {
if (!items[i].picked_up && items[i].x == x && items[i].y == y) {
return &items[i];
}
}
return NULL;
}
int player_pickup(Player *p, Item *i) {
if (p->inventory_count >= MAX_INVENTORY) {
return; // inventory full
return 0;
}
if (i->picked_up) {
return; // already picked up
return 0;
}
i->picked_up = 1;
p->inventory[p->inventory_count] = *i; // copy item to inventory
p->inventory[p->inventory_count] = *i;
p->inventory_count++;
i->picked_up = 1;
return 1;
}
void player_use_item(Player *p, Item *i) {
if (p == NULL || i == NULL) return;
if (i->picked_up) return; // invalid item
if (p == NULL || i == NULL)
return;
if (i->picked_up)
return; // invalid item
// Apply item effect
// Apply item effect (only potions are consumed)
if (i->type == ITEM_POTION) {
item_use(p, i);
// Mark item as used (remove from inventory)
i->picked_up = 1;
}
// Weapons and armor are equipped, not consumed
}
int player_use_first_item(Player *p) {
if (p == NULL || p->inventory_count == 0) return 0;
if (p == NULL || p->inventory_count == 0)
return 0;
// Find first valid item in inventory
// Find first valid item in inventory (skip weapons/armor - must equip explicitly)
for (int i = 0; i < MAX_INVENTORY; i++) {
if (!p->inventory[i].picked_up) {
Item *item = &p->inventory[i];
if (item->type == ITEM_POTION) {
// Apply item effect
item_use(p, item);
// Remove from inventory (shift remaining items)
for (int j = i; j < MAX_INVENTORY - 1; j++) {
p->inventory[j] = p->inventory[j + 1];
}
p->inventory_count--;
// Mark last slot as invalid
p->inventory[MAX_INVENTORY - 1].picked_up = 1;
player_remove_inventory_item(p, i);
return 1;
}
// Weapons/armor can't be used this way - must equip
}
}
return 0;
}
Item *player_get_inventory_item(Player *p, int index) {
if (p == NULL) return NULL;
if (index < 0 || index >= MAX_INVENTORY) return NULL;
if (p->inventory[index].picked_up) return NULL; // invalid/empty
if (p == NULL)
return NULL;
if (index < 0 || index >= MAX_INVENTORY)
return NULL;
if (p->inventory[index].picked_up)
return NULL; // invalid/empty
return &p->inventory[index];
}
void player_remove_inventory_item(Player *p, int index) {
if (p == NULL)
return;
if (index < 0 || index >= MAX_INVENTORY)
return;
if (p->inventory[index].picked_up)
return;
// Shift remaining items
for (int j = index; j < MAX_INVENTORY - 1; j++) {
p->inventory[j] = p->inventory[j + 1];
}
p->inventory_count--;
p->inventory[MAX_INVENTORY - 1].picked_up = 1;
}
int player_equip_item(Player *p, int inv_index) {
if (p == NULL)
return 0;
if (inv_index < 0 || inv_index >= MAX_INVENTORY)
return 0;
Item *item = player_get_inventory_item(p, inv_index);
if (item == NULL)
return 0;
if (item->type == ITEM_WEAPON) {
// Unequip current weapon first
if (p->has_weapon) {
p->attack -= p->equipped_weapon.power;
}
// Equip new weapon
p->equipped_weapon = *item;
p->has_weapon = 1;
p->attack += item->power;
// Adjust damage variance based on weapon power
// Higher power = wider range (more swingy but higher potential)
int min_var = 100 - (item->power * 3);
int max_var = 100 + (item->power * 5);
if (min_var < 60)
min_var = 60;
if (max_var > 150)
max_var = 150;
p->dmg_variance_min = min_var;
p->dmg_variance_max = max_var;
// Remove from inventory
player_remove_inventory_item(p, inv_index);
return 1;
}
if (item->type == ITEM_ARMOR) {
// Unequip current armor first
if (p->has_armor) {
p->defense -= p->equipped_armor.power;
}
// Equip new armor
p->equipped_armor = *item;
p->has_armor = 1;
p->defense += item->power;
// Remove from inventory
player_remove_inventory_item(p, inv_index);
return 1;
}
return 0; // not equippable (potion)
}
int player_drop_item(Player *p, int inv_index, Item *items, int item_count) {
if (p == NULL)
return 0;
if (inv_index < 0 || inv_index >= MAX_INVENTORY)
return 0;
Item *item = player_get_inventory_item(p, inv_index);
if (item == NULL)
return 0;
// Find an empty slot in items array to place the dropped item
for (int i = 0; i < item_count; i++) {
if (items[i].picked_up) {
// Place dropped item at this position
items[i] = *item;
items[i].x = p->x;
items[i].y = p->y;
items[i].picked_up = 0;
// Remove from inventory
player_remove_inventory_item(p, inv_index);
return 1;
}
}
return 0; // no room to drop
}

View file

@ -7,22 +7,33 @@
void player_init(Player *p, int x, int y);
// Move player, return 1 if moved/attacked, 0 if blocked
int player_move(Player *p, int dx, int dy, Map *map, Enemy *enemies,
int enemy_count, Item *items, int item_count);
int player_move(Player *p, int dx, int dy, Map *map, Enemy *enemies, int enemy_count);
// Player attacks enemy (deal damage)
void player_attack(Player *p, Enemy *e);
// Pick up item
void player_pickup(Player *p, Item *i);
// Get item at floor position (x, y), returns NULL if none
Item *get_item_at_floor(Item *items, int count, int x, int y);
// Use item
// Pick up item, return 1 if successful, 0 if failed (full/invalid)
int player_pickup(Player *p, Item *i);
// Use item (potions only - weapons/armor are equipped)
void player_use_item(Player *p, Item *i);
// Use first available item in inventory, return 1 if used
// Use first available potion in inventory, return 1 if used
int player_use_first_item(Player *p);
// Get item at inventory index, returns NULL if invalid
Item *player_get_inventory_item(Player *p, int index);
// Remove item from inventory at index (shifts remaining items)
void player_remove_inventory_item(Player *p, int index);
// Equip weapon/armor from inventory, return 1 if successful
int player_equip_item(Player *p, int inv_index);
// Drop item from inventory at index (returns it to floor), return 1 if successful
int player_drop_item(Player *p, int inv_index, Item *items, int item_count);
#endif // PLAYER_H

View file

@ -1,15 +1,15 @@
#include "render.h"
#include "common.h"
#include "items.h"
#include "raylib.h"
#include "settings.h"
#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,19 +28,17 @@ 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
@ -63,24 +61,21 @@ 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
@ -104,85 +99,187 @@ void render_items(Item *items, int count) {
}
}
void render_ui(Player *p) {
// UI background bar at bottom of screen
Rectangle ui_bg = {0, (float)(MAP_HEIGHT * TILE_SIZE), (float)SCREEN_WIDTH,
60};
DrawRectangleRec(ui_bg, (Color){30, 30, 30, 255});
void render_ui(const Player *p) {
// UI background bar (taller for two rows)
Rectangle ui_bg = {0, (float)(MAP_HEIGHT * TILE_SIZE), (float)SCREEN_WIDTH, 60};
DrawRectangleRec(ui_bg, (Color){15, 15, 15, 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, (Color){50, 50, 50, 255});
// Player hp
// HP Bar (row 1, left)
int bar_x = 10;
int bar_y = MAP_HEIGHT * TILE_SIZE + 10;
int bar_width = 140;
int bar_height = 18;
// Bar background
DrawRectangle(bar_x, bar_y, bar_width, bar_height, (Color){30, 30, 30, 255});
// Bar fill based on HP percentage
float hp_percent = (float)p->hp / p->max_hp;
int fill_width = (int)(bar_width * hp_percent);
// Color gradient: green > yellow > red
Color hp_color;
if (hp_percent > 0.6f) {
hp_color = (Color){60, 180, 60, 255};
} else if (hp_percent > 0.3f) {
hp_color = (Color){200, 180, 40, 255};
} else {
hp_color = (Color){200, 60, 60, 255};
}
DrawRectangle(bar_x, bar_y, fill_width, bar_height, hp_color);
// HP text inside bar
char hp_text[32];
snprintf(hp_text, sizeof(hp_text), "HP: %d/%d", p->hp, p->max_hp);
DrawText(hp_text, 10, MAP_HEIGHT * TILE_SIZE + 10, 20, RED);
snprintf(hp_text, sizeof(hp_text), "%d/%d", p->hp, p->max_hp);
int hp_text_w = MeasureText(hp_text, 14);
DrawText(hp_text, bar_x + (bar_width - hp_text_w) / 2, bar_y + 2, 14, WHITE);
// Player attack
char atk_text[32];
snprintf(atk_text, sizeof(atk_text), "ATK: %d", p->attack);
DrawText(atk_text, 150, MAP_HEIGHT * TILE_SIZE + 10, 20, YELLOW);
// Stats row 1: Floor, ATK, DEF, Inv
int stats_y = bar_y;
DrawText("F1", bar_x + bar_width + 15, stats_y, 14, WHITE);
DrawText("ATK", bar_x + bar_width + 50, stats_y, 14, YELLOW);
DrawText("DEF", bar_x + bar_width + 100, stats_y, 14, BLUE);
DrawText("INV", bar_x + bar_width + 145, stats_y, 14, GREEN);
// Floor number
char floor_text[32];
snprintf(floor_text, sizeof(floor_text), "Floor: %d", p->floor);
DrawText(floor_text, 280, MAP_HEIGHT * TILE_SIZE + 10, 20, WHITE);
// Row 2: equipment slots and controls
int row2_y = stats_y + 24;
// Defense stat
char def_text[32];
snprintf(def_text, sizeof(def_text), "DEF: %d", p->defense);
DrawText(def_text, 420, MAP_HEIGHT * TILE_SIZE + 10, 20, BLUE);
// Inventory count
char inv_text[32];
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
if (p->inventory_count > 0 && !p->inventory[0].picked_up) {
const char *item_name = "";
switch (p->inventory[0].type) {
case ITEM_POTION:
item_name = "Potion";
break;
case ITEM_WEAPON:
item_name = "Weapon";
break;
case ITEM_ARMOR:
item_name = "Armor";
break;
default:
item_name = "?";
break;
}
char first_item[48];
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);
// Equipment (left side of row 2)
if (p->has_weapon) {
char weapon_text[48];
snprintf(weapon_text, sizeof(weapon_text), "Wpn:%s +%d", item_get_name(&p->equipped_weapon),
p->equipped_weapon.power);
DrawText(weapon_text, 10, row2_y, 12, YELLOW);
} else {
DrawText("Wpn:---", 10, row2_y, 12, (Color){60, 60, 60, 255});
}
// Controls hint
DrawText("WASD: Move | U: Use Item | Q: Quit", 280,
MAP_HEIGHT * TILE_SIZE + 35, 14, GRAY);
if (p->has_armor) {
char armor_text[48];
snprintf(armor_text, sizeof(armor_text), "Arm:%s +%d", item_get_name(&p->equipped_armor), p->equipped_armor.power);
DrawText(armor_text, 150, row2_y, 12, BLUE);
} else {
DrawText("Arm:---", 150, row2_y, 12, (Color){60, 60, 60, 255});
}
// Controls hint (right side)
DrawText("[G] Pickup [I] Inventory [E] Equip [D] Drop", 350, row2_y, 12, (Color){70, 70, 70, 255});
}
void render_action_log(const char log[5][128], int count, int head) {
int log_x = 10;
int log_y = MAP_HEIGHT * TILE_SIZE - 75;
for (int i = 0; i < count && i < 5; i++) {
int idx = (head - count + i + 5) % 5;
if (log[idx][0] != '\0') {
DrawText(log[idx], log_x, log_y + i * 14, 12, (Color){140, 140, 140, 255});
}
}
}
void render_inventory_overlay(const Player *p, int selected) {
// Overlay dimensions
int ov_width = 360;
int ov_height = 300;
Rectangle overlay = {(float)(SCREEN_WIDTH - ov_width) / 2, (float)(SCREEN_HEIGHT - ov_height) / 2 - 60,
(float)ov_width, (float)ov_height};
DrawRectangleRec(overlay, (Color){12, 12, 12, 252});
DrawRectangleLines((int)overlay.x, (int)overlay.y, (int)overlay.width, (int)overlay.height, (Color){70, 70, 70, 255});
// Title
const char *title = "INVENTORY";
int title_w = MeasureText(title, 24);
DrawText(title, overlay.x + (overlay.width - title_w) / 2, overlay.y + 12, 24, WHITE);
// Draw each inventory slot
char slot_text[64];
int row_height = 26;
int start_y = overlay.y + 50;
for (int i = 0; i < MAX_INVENTORY; i++) {
int y_pos = start_y + (i * row_height);
if (i < p->inventory_count && !p->inventory[i].picked_up) {
const Item *item = &p->inventory[i];
// Selection highlight
if (i == selected) {
DrawRectangle((int)overlay.x + 6, y_pos, (int)overlay.width - 12, row_height - 2, (Color){45, 45, 45, 255});
DrawRectangleLines((int)overlay.x + 6, y_pos, (int)overlay.width - 12, row_height - 2,
(Color){180, 160, 80, 255});
}
// Slot number
snprintf(slot_text, sizeof(slot_text), "%d.", i + 1);
DrawText(slot_text, overlay.x + 16, y_pos + 4, 14, (Color){80, 80, 80, 255});
// Item name
const char *name = item_get_name(item);
Color name_color = (item->type == ITEM_POTION) ? (Color){255, 140, 140, 255}
: (item->type == ITEM_WEAPON) ? (Color){255, 255, 140, 255}
: (Color){140, 140, 255, 255};
DrawText(name, overlay.x + 45, y_pos + 4, 14, name_color);
// Power
snprintf(slot_text, sizeof(slot_text), "+%d", item->power);
DrawText(slot_text, overlay.x + 150, y_pos + 4, 14, YELLOW);
// Action
if (item->type == ITEM_POTION) {
DrawText("[U]se", overlay.x + 200, y_pos + 4, 14, GREEN);
} else {
DrawText("[E]quip [D]rop", overlay.x + 200, y_pos + 4, 14, GOLD);
}
} else {
// Empty slot
snprintf(slot_text, sizeof(slot_text), "%d.", i + 1);
DrawText(slot_text, overlay.x + 16, y_pos + 4, 14, (Color){40, 40, 40, 255});
}
}
// Instructions at bottom
const char *hint = "[1-0] Select [E] Equip [U] Use [D] Drop [I/ESC] Close";
int hint_w = MeasureText(hint, 12);
DrawText(hint, overlay.x + (overlay.width - hint_w) / 2, overlay.y + overlay.height - 22, 12,
(Color){65, 65, 65, 255});
}
void render_floating_texts(FloatingText *texts, int count, int shake_x, int shake_y) {
for (int i = 0; i < count; i++) {
if (texts[i].lifetime <= 0)
continue;
int x = texts[i].x + shake_x;
int y = texts[i].y + shake_y - (60 - texts[i].lifetime); // rise over time
float alpha = (float)texts[i].lifetime / 60.0f;
Color color =
texts[i].is_critical ? (Color){255, 200, 50, (int)(255 * alpha)} : (Color){255, 100, 100, (int)(255 * alpha)};
char text[16];
snprintf(text, sizeof(text), "%d", texts[i].value);
int text_w = MeasureText(text, 18);
DrawText(text, x - text_w / 2, y, 18, color);
}
}
void render_game_over(void) {
// Semi-transparent overlay
Rectangle overlay = {0, 0, (float)SCREEN_WIDTH, (float)SCREEN_HEIGHT};
DrawRectangleRec(overlay, (Color){0, 0, 0, 200});
DrawRectangleRec(overlay, (Color){0, 0, 0, 210});
// 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 +287,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};
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);
Rectangle msg_bg = {(float)(SCREEN_WIDTH / 2 - 150), (float)(SCREEN_HEIGHT / 2 - 30), 300, 60};
DrawRectangleRec(msg_bg, (Color){45, 45, 45, 235});
DrawRectangleLines((int)msg_bg.x, (int)msg_bg.y, (int)msg_bg.width, (int)msg_bg.height, (Color){180, 180, 180, 255});
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);
}

View file

@ -4,19 +4,28 @@
#include "common.h"
// Render the map tiles
void render_map(Map *map);
void render_map(const Map *map);
// Render the player
void render_player(Player *p);
void render_player(const Player *p);
// Render all enemies
void render_enemies(Enemy *enemies, int count);
void render_enemies(const Enemy *enemies, int count);
// Render all items
void render_items(Item *items, int count);
void render_items(const Item *items, int count);
// Render UI overlay
void render_ui(Player *p);
void render_ui(const Player *p);
// Render action log (bottom left corner)
void render_action_log(const char log[5][128], int count, int head);
// Render inventory selection overlay
void render_inventory_overlay(const Player *p, int selected);
// Render floating damage text
void render_floating_texts(FloatingText *texts, int count, int shake_x, int shake_y);
// Render game over screen
void render_game_over(void);