diff --git a/.clang-format b/.clang-format deleted file mode 100644 index 39ef087..0000000 --- a/.clang-format +++ /dev/null @@ -1,100 +0,0 @@ -# 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 diff --git a/.envrc b/.envrc deleted file mode 100644 index 23f9a4e..0000000 --- a/.envrc +++ /dev/null @@ -1 +0,0 @@ -use flake . --substituters "https://cache.nixos.org" diff --git a/Makefile b/Makefile index 607167e..096ca87 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ OBJDIR := obj SOURCES := $(wildcard $(SRCDIR)/*.c) OBJECTS := $(patsubst $(SRCDIR)/%.c,$(OBJDIR)/%.o,$(SOURCES)) -.PHONY: all clean format format-check +.PHONY: all clean all: $(TARGET) @@ -29,17 +29,3 @@ 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; } diff --git a/nix/shell.nix b/nix/shell.nix index e51be03..c30fba3 100644 --- a/nix/shell.nix +++ b/nix/shell.nix @@ -7,16 +7,10 @@ }: mkShell { strictDeps = true; - packages = [ - clang-tools - gnumake - ]; - buildInputs = [ + clang-tools raylib - ]; - - nativeBuildInputs = [ + gnumake pkg-config ]; } diff --git a/src/audio.c b/src/audio.c index c6bcefd..741136b 100644 --- a/src/audio.c +++ b/src/audio.c @@ -1,10 +1,9 @@ #include "audio.h" #include "raylib.h" #include -#include #ifndef M_PI -#define M_PI 3.14159265358979323846 // xd +#define M_PI 3.14159265358979323846 // xd #endif #define SAMPLE_RATE 44100 @@ -12,17 +11,11 @@ // 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++) { @@ -31,7 +24,7 @@ static void play_tone(float frequency, float duration, float volume) { // Apply simple envelope (fade in/out) float envelope = 1.0f; - int fade_samples = SAMPLE_RATE / 20; // 50ms fade + int fade_samples = SAMPLE_RATE / 20; // 50ms fade if (i < fade_samples) { envelope = (float)i / fade_samples; } else if (i > sample_count - fade_samples) { @@ -50,9 +43,6 @@ 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) { diff --git a/src/audio.h b/src/audio.h index a574a9a..8950980 100644 --- a/src/audio.h +++ b/src/audio.h @@ -25,4 +25,4 @@ void audio_play_player_damage(void); // Play stairs/level change sound void audio_play_stairs(void); -#endif // AUDIO_H +#endif // AUDIO_H diff --git a/src/combat.c b/src/combat.c index fbb66cc..2caa0e5 100644 --- a/src/combat.c +++ b/src/combat.c @@ -1,6 +1,5 @@ #include "combat.h" #include "common.h" -#include "rng.h" #include // Track combat events for feedback @@ -8,65 +7,35 @@ typedef struct { const char *message; int damage; int is_player_damage; - int is_critical; } CombatEvent; -static CombatEvent last_event = {NULL, 0, 0, 0}; +static CombatEvent last_event = {NULL, 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_critical(void) { - return last_event.is_critical; -} +int combat_was_player_damage(void) { return last_event.is_player_damage; } void combat_player_attack(Player *p, Enemy *e) { if (e == NULL || !e->alive) return; - last_event.is_critical = 0; + // Deal damage + int damage = p->attack; + e->hp -= damage; - // 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; + // Set combat event + last_event.damage = damage; + last_event.is_player_damage = 0; - // 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; - - 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"; - } + // Check if enemy died + if (e->hp <= 0) { + e->hp = 0; + e->alive = 0; + last_event.message = "Enemy killed!"; } else { - last_event.damage = 0; - last_event.is_player_damage = 0; - last_event.message = "You missed"; + last_event.message = "You hit the enemy"; } } @@ -76,42 +45,22 @@ void combat_enemy_attack(Enemy *e, Player *p) { if (p == NULL) return; - last_event.is_critical = 0; + // Deal damage reduced by defense (minimum 1 damage) + int damage = e->attack - p->defense; + if (damage < 1) + damage = 1; + p->hp -= damage; - // 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; + // Set combat event + last_event.damage = damage; + last_event.is_player_damage = 1; - int variance = rng_int(80, 120); - int damage = (base_damage * variance) / 100; - if (damage < 1) - damage = 1; - - // 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; - - 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 = "Hit"; - } + // Check if player died + if (p->hp <= 0) { + p->hp = 0; + last_event.message = "You died!"; } else { - last_event.damage = 0; - last_event.is_player_damage = 1; - last_event.message = "Missed"; + last_event.message = "Enemy attacks!"; } } @@ -119,5 +68,4 @@ void combat_reset_event(void) { last_event.message = NULL; last_event.damage = 0; last_event.is_player_damage = 0; - last_event.is_critical = 0; } diff --git a/src/combat.h b/src/combat.h index c53f6a7..cc44794 100644 --- a/src/combat.h +++ b/src/combat.h @@ -12,16 +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 (pass player for damage variance) +// Player attacks enemy void combat_player_attack(Player *p, Enemy *e); // Enemy attacks player void combat_enemy_attack(Enemy *e, Player *p); -#endif // COMBAT_H +#endif // COMBAT_H diff --git a/src/common.h b/src/common.h index b963c3b..85c6be1 100644 --- a/src/common.h +++ b/src/common.h @@ -44,18 +44,8 @@ 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 @@ -65,49 +55,9 @@ 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 +#endif // COMMON_H diff --git a/src/enemy.c b/src/enemy.c index f6ca10f..fa35de5 100644 --- a/src/enemy.c +++ b/src/enemy.c @@ -5,7 +5,7 @@ #include "rng.h" // Forward declaration -int is_enemy_at(const Enemy *enemies, int count, int x, int y); +int is_enemy_at(Enemy *enemies, int count, int x, int y); void enemy_spawn(Enemy enemies[], int *count, Map *map, Player *p, int floor) { *count = 0; @@ -47,31 +47,22 @@ 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.max_hp = ENEMY_BASE_HP + floor; - e.hp = e.max_hp; + e.hp = ENEMY_BASE_HP + floor; e.attack = ENEMY_BASE_ATTACK; - e.speed = 55 + rng_int(0, 10); break; case ENEMY_SKELETON: - e.max_hp = ENEMY_BASE_HP + floor + 2; - e.hp = e.max_hp; + e.hp = ENEMY_BASE_HP + floor + 2; e.attack = ENEMY_BASE_ATTACK + 1; - e.speed = 70 + rng_int(0, 10); break; case ENEMY_ORC: - e.max_hp = ENEMY_BASE_HP + floor + 4; - e.hp = e.max_hp; + e.hp = ENEMY_BASE_HP + floor + 4; e.attack = ENEMY_BASE_ATTACK + 2; - e.speed = 85 + rng_int(0, 10); break; default: - e.max_hp = ENEMY_BASE_HP; - e.hp = e.max_hp; + e.hp = ENEMY_BASE_HP; e.attack = ENEMY_BASE_ATTACK; - e.speed = 60; break; } - e.cooldown = e.speed; enemies[i] = e; (*count)++; @@ -79,7 +70,7 @@ void enemy_spawn(Enemy enemies[], int *count, Map *map, Player *p, int floor) { } // Check if position has an enemy -int is_enemy_at(const Enemy *enemies, int count, int x, int y) { +int is_enemy_at(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; @@ -95,13 +86,9 @@ 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) @@ -118,35 +105,20 @@ static void enemy_move_toward_player(Enemy *e, Player *p, Map *map, Enemy *all_e 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) && - !is_player_at(p, 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)) { 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) && - !is_player_at(p, new_x, new_y)) { + if (is_floor(map, new_x, new_y) && + !is_enemy_at(all_enemies, enemy_count, 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]; @@ -154,10 +126,14 @@ void enemy_update_all(Enemy enemies[], int count, Player *p, Map *map) { if (!e->alive) continue; - e->cooldown -= e->speed; - if (e->cooldown <= 0) { - enemy_act(e, p, map, enemies, count); - e->cooldown = 100; + // 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); } } diff --git a/src/enemy.h b/src/enemy.h index 8eaf852..fdd260b 100644 --- a/src/enemy.h +++ b/src/enemy.h @@ -9,10 +9,7 @@ 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(const Enemy *enemies, int count, int x, int y); +int is_enemy_at(Enemy *enemies, int count, int x, int y); -#endif // ENEMY_H +#endif // ENEMY_H diff --git a/src/items.c b/src/items.c index c758955..ebe41eb 100644 --- a/src/items.c +++ b/src/items.c @@ -40,15 +40,15 @@ void item_spawn(Item items[], int *count, Map *map, int floor) { if (type_roll < 50) { // 50% chance for potion item.type = ITEM_POTION; - item.power = 5 + rng_int(0, floor * 2); // healing: 5 + 0-2*floor + item.power = 5 + rng_int(0, floor * 2); // healing: 5 + 0-2*floor } else if (type_roll < 80) { // 30% chance for weapon item.type = ITEM_WEAPON; - item.power = 1 + rng_int(0, floor); // attack bonus: 1 + 0-floor + item.power = 1 + rng_int(0, floor); // attack bonus: 1 + 0-floor } else { // 20% chance for armor item.type = ITEM_ARMOR; - item.power = 1 + rng_int(0, floor / 2); // defense bonus + item.power = 1 + rng_int(0, floor / 2); // defense bonus } items[i] = item; @@ -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(const Item *i) { +const char *item_get_name(Item *i) { if (i == NULL) return ""; @@ -74,7 +74,7 @@ const char *item_get_name(const Item *i) { } // Get item description -const char *item_get_description(const Item *i) { +const char *item_get_description(Item *i) { if (i == NULL) return ""; @@ -91,7 +91,7 @@ const char *item_get_description(const Item *i) { } // Get item power value -int item_get_power(const Item *i) { +int item_get_power(Item *i) { if (i == NULL) return 0; return i->power; diff --git a/src/items.h b/src/items.h index fa913f2..c54e827 100644 --- a/src/items.h +++ b/src/items.h @@ -8,16 +8,7 @@ // Spawn items for a floor void item_spawn(Item items[], int *count, Map *map, int floor); -// Use an item (apply effect, only potions are consumed) +// Use an item 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 +#endif // ITEMS_H diff --git a/src/main.c b/src/main.c index cf0e9d1..e08d341 100644 --- a/src/main.c +++ b/src/main.c @@ -10,85 +10,57 @@ #include "rng.h" #include "settings.h" #include -#include -#include -// 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++; - } -} +// 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; -// 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; - } -} +// Turn counter for enemy movement (enemies move every other turn) +static int turn_count = 0; // Initialize a new floor -static void init_floor(GameState *gs, int floor_num) { +static void init_floor(int floor_num) { // Generate dungeon - dungeon_generate(&gs->dungeon, &gs->map, floor_num); + dungeon_generate(&dungeon, &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(&gs->map, &start_x, &start_y, 100); + get_random_floor_tile(&map, &start_x, &start_y, 100); // Initialize player position if first floor if (floor_num == 1) { - player_init(&gs->player, start_x, start_y); + player_init(&player, start_x, start_y); } else { // Move player to new floor position - gs->player.x = start_x; - gs->player.y = start_y; + player.x = start_x; + player.y = start_y; } - gs->player.floor = floor_num; + player.floor = floor_num; // Spawn enemies - enemy_spawn(gs->enemies, &gs->enemy_count, &gs->map, &gs->player, floor_num); + enemy_spawn(enemies, &enemy_count, &map, &player, floor_num); // Spawn items - item_spawn(gs->items, &gs->item_count, &gs->map, floor_num); + item_spawn(items, &item_count, &map, floor_num); // Reset turn counter - gs->turn_count = 0; + turn_count = 0; } -// Handle player input - returns: 0=continue, 1=acted, -1=quit -static int handle_input(GameState *gs) { +// Handle player input - returns: 0=continue, -1=quit +static int handle_input(void) { int dx = 0, dy = 0; // Check for quit first (always works) @@ -97,181 +69,21 @@ static int handle_input(GameState *gs) { } // Check for restart (works during game over) - if (IsKeyPressed(KEY_R) && gs->game_over) { - memset(gs, 0, sizeof(GameState)); - init_floor(gs, 1); + if (IsKeyPressed(KEY_R) && game_over) { + game_over = 0; + game_won = 0; + init_floor(1); return 0; } - 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; + // 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; 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 + return 1; // consume a turn } } } @@ -292,50 +104,58 @@ static int handle_input(GameState *gs) { combat_reset_event(); // Player action - int action = player_move(&gs->player, dx, dy, &gs->map, gs->enemies, gs->enemy_count); + + int action = player_move(&player, dx, dy, &map, enemies, enemy_count, items, + item_count); if (action) { // Increment turn counter - gs->turn_count++; + turn_count++; // Check if stepped on stairs - 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; + + 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; + } } - // 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; + // 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(); } } } - // Enemy turns - now uses speed/cooldown system (no more % 2 hack) - enemy_update_all(gs->enemies, gs->enemy_count, &gs->player, &gs->map); + // Enemy turn - only every other turn for fairness + if (turn_count % 2 == 0) { + enemy_update_all(enemies, enemy_count, &player, &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 - gs->last_message = combat_get_last_message(); - gs->message_timer = 60; + last_message = combat_get_last_message(); + message_timer = 60; // Check game over - if (gs->player.hp <= 0) { - gs->game_over = 1; + if (player.hp <= 0) { + game_over = 1; } } } @@ -345,20 +165,17 @@ static int handle_input(GameState *gs) { // Main game loop static void game_loop(void) { - GameState gs; - memset(&gs, 0, sizeof(GameState)); - // Initialize first floor rng_seed(12345); - init_floor(&gs, 1); + init_floor(1); // Disable esc to exit SetExitKey(0); while (!WindowShouldClose()) { // Handle input - if (!gs.game_over) { - int quit = handle_input(&gs); + if (!game_over) { + int quit = handle_input(); if (quit == -1) break; } else { @@ -366,57 +183,41 @@ static void game_loop(void) { if (IsKeyPressed(KEY_Q)) break; if (IsKeyPressed(KEY_R)) { - memset(&gs, 0, sizeof(GameState)); - gs.game_over = 0; - gs.game_won = 0; - init_floor(&gs, 1); + game_over = 0; + game_won = 0; + init_floor(1); } } // Update message timer - if (gs.message_timer > 0) - gs.message_timer--; - - // Update effects - update_effects(&gs); + if (message_timer > 0) + message_timer--; // Render BeginDrawing(); ClearBackground(BLACK); - // 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 game elements + render_map(&map); + render_items(items, item_count); + render_enemies(enemies, enemy_count); + render_player(&player); + render_ui(&player); // Draw message if any - if (gs.last_message != NULL && gs.message_timer > 0) { - render_message(gs.last_message); + if (last_message != NULL && message_timer > 0) { + render_message(last_message); } // Draw game over screen - if (gs.game_over) { + if (game_over) { render_game_over(); - if (gs.game_won) { + if (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); } } diff --git a/src/map.c b/src/map.c index 24016c9..e3559ec 100644 --- a/src/map.c +++ b/src/map.c @@ -13,7 +13,7 @@ void map_init(Map *map) { map->room_count = 0; } -int is_floor(const Map *map, int x, int y) { +int is_floor(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; } } diff --git a/src/map.h b/src/map.h index 3cf6337..4280e2b 100644 --- a/src/map.h +++ b/src/map.h @@ -4,7 +4,7 @@ #include "common.h" // Check if a tile is walkable floor -int is_floor(const Map *map, int x, int y); +int is_floor(Map *map, int x, int y); // Get room center coordinates void get_room_center(Room *room, int *cx, int *cy); @@ -18,4 +18,4 @@ void map_init(Map *map); // Get a random floor tile position void get_random_floor_tile(Map *map, int *x, int *y, int attempts); -#endif // MAP_H +#endif // MAP_H diff --git a/src/player.c b/src/player.c index 218d855..0d3e326 100644 --- a/src/player.c +++ b/src/player.c @@ -1,248 +1,142 @@ #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 -#include -void player_init(Player *p, int x, int y) { - p->x = x; - p->y = y; - p->hp = PLAYER_BASE_HP; - p->max_hp = PLAYER_BASE_HP; - 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++) { - p->inventory[i].picked_up = 1; // mark as invalid - } +void player_init(Player* p, int x, int y) { + p->x = x; + p->y = y; + p->hp = PLAYER_BASE_HP; + p->max_hp = PLAYER_BASE_HP; + p->attack = PLAYER_BASE_ATTACK; + p->defense = 0; + p->floor = 1; + p->inventory_count = 0; + + // Initialize inventory to empty + for (int i = 0; i < MAX_INVENTORY; i++) { + p->inventory[i].picked_up = 1; // mark as invalid + } } // Check if position has an enemy -static Enemy *get_enemy_at(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 &enemies[i]; +static Enemy* get_enemy_at(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 &enemies[i]; + } } - } - return NULL; + return NULL; } -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; - - // Check bounds - if (!in_bounds(new_x, new_y, MAP_WIDTH, MAP_HEIGHT)) { - return 0; - } - - // Check if walkable - if (!is_floor(map, new_x, new_y)) { - return 0; - } - - // Check for enemy at target position - Enemy *enemy = get_enemy_at(enemies, enemy_count, new_x, new_y); - if (enemy != NULL) { - // Attack the enemy - player_attack(p, enemy); - return 1; - } - - // 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; -} - -void player_attack(Player *p, Enemy *e) { - // Use combat system - combat_player_attack(p, e); -} - -// 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]; +// 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; + return NULL; } -int player_pickup(Player *p, Item *i) { - if (p->inventory_count >= MAX_INVENTORY) { - return 0; - } - - if (i->picked_up) { - return 0; - } - - 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 - - // Apply item effect (only potions are consumed) - if (i->type == ITEM_POTION) { - item_use(p, i); - } - // Weapons and armor are equipped, not consumed -} - -int player_use_first_item(Player *p) { - if (p == NULL || p->inventory_count == 0) - return 0; - - // 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) - player_remove_inventory_item(p, i); +int player_move(Player* p, int dx, int dy, Map* map, Enemy* enemies, int enemy_count, Item* items, int item_count) { + int new_x = p->x + dx; + int new_y = p->y + dy; + + // Check bounds + if (!in_bounds(new_x, new_y, MAP_WIDTH, MAP_HEIGHT)) { + return 0; + } + + // Check if walkable + if (!is_floor(map, new_x, new_y)) { + return 0; + } + + // Check for enemy at target position + Enemy* enemy = get_enemy_at(enemies, enemy_count, new_x, new_y); + if (enemy != NULL) { + // Attack the enemy + player_attack(p, enemy); 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 - 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; + + // 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); } - // 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); + + // Move player + p->x = new_x; + p->y = new_y; + 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 +void player_attack(Player* p, Enemy* e) { + // Use combat system + combat_player_attack(p, e); +} + +void player_pickup(Player* p, Item* i) { + if (p->inventory_count >= MAX_INVENTORY) { + return; // inventory full + } + + if (i->picked_up) { + return; // already picked up + } + + i->picked_up = 1; + p->inventory[p->inventory_count] = *i; // copy item to inventory + p->inventory_count++; +} + +void player_use_item(Player* p, Item* i) { + if (p == NULL || i == NULL) return; + if (i->picked_up) return; // invalid item + + // Apply item effect + item_use(p, i); + + // Mark item as used (remove from inventory) + i->picked_up = 1; +} + +int player_use_first_item(Player* p) { + if (p == NULL || p->inventory_count == 0) return 0; + + // Find first valid item in inventory + for (int i = 0; i < MAX_INVENTORY; i++) { + if (!p->inventory[i].picked_up) { + Item* item = &p->inventory[i]; + + // 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; + + return 1; + } + } + 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 + return &p->inventory[index]; } diff --git a/src/player.h b/src/player.h index 7d6c151..c7cd3fa 100644 --- a/src/player.h +++ b/src/player.h @@ -7,33 +7,22 @@ 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); +int player_move(Player *p, int dx, int dy, Map *map, Enemy *enemies, + int enemy_count, Item *items, int item_count); // Player attacks enemy (deal damage) void player_attack(Player *p, Enemy *e); -// Get item at floor position (x, y), returns NULL if none -Item *get_item_at_floor(Item *items, int count, int x, int y); +// Pick up item +void player_pickup(Player *p, Item *i); -// 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) +// Use item void player_use_item(Player *p, Item *i); -// Use first available potion in inventory, return 1 if used +// Use first available item 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 +#endif // PLAYER_H diff --git a/src/render.c b/src/render.c index 4ecacdf..80224aa 100644 --- a/src/render.c +++ b/src/render.c @@ -1,15 +1,15 @@ #include "render.h" #include "common.h" -#include "items.h" #include "raylib.h" #include "settings.h" #include #include -void render_map(const Map *map) { +void render_map(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,30 +28,32 @@ void render_map(const Map *map) { } } -void render_player(const Player *p) { - Rectangle rect = {(float)(p->x * TILE_SIZE), (float)(p->y * TILE_SIZE), (float)TILE_SIZE, (float)TILE_SIZE}; +void render_player(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(const Enemy *enemies, int count) { +void render_enemies(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; @@ -61,34 +63,37 @@ void render_enemies(const Enemy *enemies, int count) { DrawRectangleRec(rect, enemy_color); // Draw hp bar above enemy - int hp_percent = (enemies[i].hp * TILE_SIZE) / enemies[i].max_hp; + int hp_percent = + (enemies[i].hp * TILE_SIZE) / 10; // FIXME: assuming max 10 hp, for now 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(const Item *items, int count) { +void render_items(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; @@ -99,187 +104,85 @@ void render_items(const Item *items, int count) { } } -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}); +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}); // Draw dividing line - DrawLine(0, MAP_HEIGHT * TILE_SIZE, SCREEN_WIDTH, MAP_HEIGHT * TILE_SIZE, (Color){50, 50, 50, 255}); + DrawLine(0, MAP_HEIGHT * TILE_SIZE, SCREEN_WIDTH, MAP_HEIGHT * TILE_SIZE, + GRAY); - // 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 + // Player hp char hp_text[32]; - 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); + 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); - // 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); + // 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); - // Row 2: equipment slots and controls - int row2_y = stats_y + 24; + // 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); - // 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}); - } + // 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); - 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}); - } + // 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); - // 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}); + // 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); } - // 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); - } + // Controls hint + DrawText("WASD: Move | U: Use Item | Q: Quit", 280, + MAP_HEIGHT * TILE_SIZE + 35, 14, GRAY); } void render_game_over(void) { // Semi-transparent overlay Rectangle overlay = {0, 0, (float)SCREEN_WIDTH, (float)SCREEN_HEIGHT}; - DrawRectangleRec(overlay, (Color){0, 0, 0, 210}); + DrawRectangleRec(overlay, (Color){0, 0, 0, 200}); // 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) { @@ -287,10 +190,13 @@ 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){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}); + 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); 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); } diff --git a/src/render.h b/src/render.h index 850c989..32e26e7 100644 --- a/src/render.h +++ b/src/render.h @@ -4,28 +4,19 @@ #include "common.h" // Render the map tiles -void render_map(const Map *map); +void render_map(Map *map); // Render the player -void render_player(const Player *p); +void render_player(Player *p); // Render all enemies -void render_enemies(const Enemy *enemies, int count); +void render_enemies(Enemy *enemies, int count); // Render all items -void render_items(const Item *items, int count); +void render_items(Item *items, int count); // Render UI overlay -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); +void render_ui(Player *p); // Render game over screen void render_game_over(void); @@ -33,4 +24,4 @@ void render_game_over(void); // Render a message popup void render_message(const char *message); -#endif // RENDER_H +#endif // RENDER_H diff --git a/src/rng.c b/src/rng.c index 62d6fcf..2efcce7 100644 --- a/src/rng.c +++ b/src/rng.c @@ -6,7 +6,7 @@ static unsigned int g_seed = 1; // LCG parameters (from numerical recipes) #define LCG_A 1664525 #define LCG_C 1013904223 -#define LCG_MOD 4294967294 // 2^32 - 2 (avoid 0) +#define LCG_MOD 4294967294 // 2^32 - 2 (avoid 0) void rng_seed(unsigned int seed) { // Ensure seed is never 0 diff --git a/src/rng.h b/src/rng.h index a89c798..94bad67 100644 --- a/src/rng.h +++ b/src/rng.h @@ -7,4 +7,4 @@ void rng_seed(unsigned int seed); // Get a random integer in range [min, max] int rng_int(int min, int max); -#endif // RNG_H +#endif // RNG_H diff --git a/src/settings.h b/src/settings.h index 47f3c12..d5978fb 100644 --- a/src/settings.h +++ b/src/settings.h @@ -25,4 +25,4 @@ #define NUM_FLOORS 5 #define MAX_INVENTORY 10 -#endif // SETTINGS_H +#endif // SETTINGS_H diff --git a/src/utils.h b/src/utils.h index 211cf44..e638c0d 100644 --- a/src/utils.h +++ b/src/utils.h @@ -7,4 +7,4 @@ int clamp(int value, int min, int max); // Check if coordinates are within map bounds int in_bounds(int x, int y, int width, int height); -#endif // UTILS_H +#endif // UTILS_H