forked from NotAShelf/rogged
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: Id81f32d86f70a7df99c2ad3d478646416a6a6964
826 lines
25 KiB
C
826 lines
25 KiB
C
#include "audio.h"
|
|
#include "combat.h"
|
|
#include "game_state.h"
|
|
#include "enemy.h"
|
|
#include "items.h"
|
|
#include "map/map.h"
|
|
#include "map/utils.h"
|
|
#include "movement.h"
|
|
#include "player.h"
|
|
#include "render.h"
|
|
#include "rng/rng.h"
|
|
#include "settings.h"
|
|
#include "tileset/tileset.h"
|
|
#include "tileset/tileset_paint.h"
|
|
#include <ctype.h>
|
|
#include <stddef.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
|
|
// 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++;
|
|
}
|
|
}
|
|
|
|
// Reuse an expired float slot, or claim the next free one
|
|
static int float_slot(GameState *gs) {
|
|
if (gs->floating_count < 8)
|
|
return gs->floating_count++;
|
|
for (int i = 0; i < 8; i++) {
|
|
if (gs->floating_texts[i].lifetime <= 0)
|
|
return i;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
// spawn floating damage text
|
|
static void spawn_floating_text(GameState *gs, int x, int y, int value, int is_critical) {
|
|
int slot = float_slot(gs);
|
|
if (slot < 0)
|
|
return;
|
|
gs->floating_texts[slot].x = x;
|
|
gs->floating_texts[slot].y = y;
|
|
gs->floating_texts[slot].value = value;
|
|
gs->floating_texts[slot].lifetime = FLOATING_TEXT_LIFETIME;
|
|
gs->floating_texts[slot].is_critical = is_critical;
|
|
gs->floating_texts[slot].label = LABEL_NONE;
|
|
gs->floating_texts[slot].effect_type = EFFECT_NONE;
|
|
}
|
|
|
|
// spawn floating label text (DODGE, BLOCK, CRIT!, proc names, SLAIN)
|
|
static void spawn_floating_label(GameState *gs, int x, int y, FloatingLabel label, StatusEffectType effect_type) {
|
|
int slot = float_slot(gs);
|
|
if (slot < 0)
|
|
return;
|
|
gs->floating_texts[slot].x = x;
|
|
gs->floating_texts[slot].y = y;
|
|
gs->floating_texts[slot].value = 0;
|
|
gs->floating_texts[slot].lifetime = 60;
|
|
gs->floating_texts[slot].is_critical = 0;
|
|
gs->floating_texts[slot].label = label;
|
|
gs->floating_texts[slot].effect_type = effect_type;
|
|
}
|
|
|
|
static FloatingLabel proc_label_for(StatusEffectType effect) {
|
|
switch (effect) {
|
|
case EFFECT_POISON:
|
|
return LABEL_PROC;
|
|
case EFFECT_BLEED:
|
|
return LABEL_PROC;
|
|
case EFFECT_BURN:
|
|
return LABEL_PROC;
|
|
case EFFECT_STUN:
|
|
return LABEL_PROC;
|
|
case EFFECT_WEAKEN:
|
|
return LABEL_PROC;
|
|
default:
|
|
return LABEL_NONE;
|
|
}
|
|
}
|
|
|
|
// 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(-SHAKE_MAX_OFFSET, SHAKE_MAX_OFFSET);
|
|
gs->shake_y = rng_int(-SHAKE_MAX_OFFSET, SHAKE_MAX_OFFSET);
|
|
} else {
|
|
gs->shake_x = 0;
|
|
gs->shake_y = 0;
|
|
}
|
|
}
|
|
|
|
// Initialize a new floor
|
|
static void init_floor(GameState *gs, int floor_num) {
|
|
// Seed RNG with run seed combined with floor number for deterministic generation
|
|
rng_seed(gs->run_seed + floor_num * 54321);
|
|
|
|
// Generate dungeon
|
|
dungeon_generate(&gs->dungeon, &gs->map, floor_num);
|
|
|
|
// Seed rng for this floor's content
|
|
rng_seed(gs->run_seed + floor_num * 98765);
|
|
|
|
// Find spawn position
|
|
int start_x, start_y;
|
|
get_random_floor_tile(&gs->map, &start_x, &start_y, 100);
|
|
|
|
// Initialize player position if first floor
|
|
if (floor_num == 1) {
|
|
player_init(&gs->player, start_x, start_y);
|
|
gs->floors_reached = 1;
|
|
} else {
|
|
// Move player to new floor position
|
|
gs->player.position.x = start_x;
|
|
gs->player.position.y = start_y;
|
|
}
|
|
gs->player.floor = floor_num;
|
|
|
|
// Set initial player light and compute visibility
|
|
LightSource player_light = {gs->player.position.x, gs->player.position.y, PLAYER_LIGHT_INTENSITY, PLAYER_LIGHT_RANGE};
|
|
LightSource sources[1 + 32];
|
|
sources[0] = player_light;
|
|
memcpy(sources + 1, gs->static_lights, gs->static_light_count * sizeof(LightSource));
|
|
compute_lighting(&gs->map, sources, 1 + gs->static_light_count);
|
|
|
|
// Spawn enemies
|
|
enemy_spawn(gs->enemies, &gs->enemy_count, &gs->map, &gs->player, floor_num);
|
|
|
|
// Spawn items
|
|
item_spawn(gs->items, &gs->item_count, &gs->map, floor_num);
|
|
|
|
// Reset turn counter
|
|
gs->turn_count = 0;
|
|
}
|
|
|
|
// Tick all status effects at the start of a turn
|
|
static void tick_all_effects(GameState *gs) {
|
|
// Player effects
|
|
int player_effect_dmg = combat_tick_effects(&gs->player);
|
|
if (player_effect_dmg > 0) {
|
|
spawn_floating_text(gs, gs->player.position.x * TILE_SIZE + 8, gs->player.position.y * TILE_SIZE, player_effect_dmg,
|
|
0);
|
|
gs->screen_shake = SHAKE_EFFECT_DURATION;
|
|
}
|
|
|
|
// Check if player died from effects
|
|
if (gs->player.hp <= 0) {
|
|
gs->player.hp = 0;
|
|
gs->game_over = 1;
|
|
return;
|
|
}
|
|
|
|
// Enemy effects
|
|
for (int i = 0; i < gs->enemy_count; i++) {
|
|
Enemy *e = &gs->enemies[i];
|
|
if (!e->alive)
|
|
continue;
|
|
int enemy_effect_dmg = combat_tick_enemy_effects(e);
|
|
if (enemy_effect_dmg > 0) {
|
|
spawn_floating_text(gs, e->position.x * TILE_SIZE + 8, e->position.y * TILE_SIZE, enemy_effect_dmg, 0);
|
|
}
|
|
if (!e->alive) {
|
|
add_log(gs, "Enemy died from effects!");
|
|
}
|
|
}
|
|
}
|
|
|
|
// attacked_enemy: the enemy the player attacked this turn, or NULL if player only moved
|
|
static void post_action(GameState *gs, Enemy *attacked_enemy) {
|
|
gs->turn_count++;
|
|
|
|
// Tick status effects at the start of this turn
|
|
tick_all_effects(gs);
|
|
if (gs->game_over)
|
|
return;
|
|
|
|
// combat feedback - player attacked an enemy this turn
|
|
if (attacked_enemy != NULL) {
|
|
int ex = attacked_enemy->position.x * TILE_SIZE + 8;
|
|
int ey = attacked_enemy->position.y * TILE_SIZE;
|
|
|
|
// Trigger slash effect
|
|
gs->slash_timer = 8;
|
|
gs->slash_x = attacked_enemy->position.x;
|
|
gs->slash_y = attacked_enemy->position.y;
|
|
// Use player's equipped weapon damage class, or default to slash
|
|
gs->slash_dmg_class = gs->player.has_weapon ? gs->player.equipped_weapon.dmg_class : DMG_SLASH;
|
|
|
|
if (combat_was_dodged()) {
|
|
spawn_floating_label(gs, ex, ey, LABEL_DODGE, EFFECT_NONE);
|
|
audio_play_dodge(gs);
|
|
} else {
|
|
if (combat_get_last_damage() > 0)
|
|
gs->damage_dealt += combat_get_last_damage();
|
|
spawn_floating_text(gs, ex, ey, combat_get_last_damage(), combat_was_critical());
|
|
audio_play_attack(gs);
|
|
if (combat_was_blocked()) {
|
|
spawn_floating_label(gs, ex, ey - 10, LABEL_BLOCK, EFFECT_NONE);
|
|
audio_play_block(gs);
|
|
}
|
|
if (combat_was_critical()) {
|
|
spawn_floating_label(gs, ex + 8, ey - 10, LABEL_CRIT, EFFECT_NONE);
|
|
audio_play_crit(gs);
|
|
gs->crits_landed++;
|
|
}
|
|
StatusEffectType applied = combat_get_applied_effect();
|
|
if (applied != EFFECT_NONE) {
|
|
spawn_floating_label(gs, ex, ey - 20, LABEL_PROC, applied);
|
|
audio_play_proc();
|
|
}
|
|
if (!attacked_enemy->alive) {
|
|
spawn_floating_label(gs, ex, ey - 20, LABEL_SLAIN, EFFECT_NONE);
|
|
audio_play_enemy_death(gs);
|
|
gs->total_kills++;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Close doors that the player moved away from before recomputing lighting.
|
|
for (int dy = 0; dy < MAP_HEIGHT; dy++) {
|
|
for (int dx = 0; dx < MAP_WIDTH; dx++) {
|
|
if (gs->map.tiles[dy][dx] == TILE_DOOR_OPEN) {
|
|
if (gs->player.position.x != dx || gs->player.position.y != dy) {
|
|
gs->map.tiles[dy][dx] = TILE_DOOR_CLOSED;
|
|
gs->map.door_anim_target[dy][dx] = 0;
|
|
gs->map.door_anim_timer[dy][dx] = DOOR_ANIM_FRAMES;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Update visibility based on player's new position
|
|
LightSource p_light = {gs->player.position.x, gs->player.position.y, PLAYER_LIGHT_INTENSITY, PLAYER_LIGHT_RANGE};
|
|
LightSource srcs[1 + 32];
|
|
srcs[0] = p_light;
|
|
memcpy(srcs + 1, gs->static_lights, gs->static_light_count * sizeof(LightSource));
|
|
compute_lighting(&gs->map, srcs, 1 + gs->static_light_count);
|
|
|
|
// Enemy turns - uses speed/cooldown system
|
|
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);
|
|
gs->screen_shake = SHAKE_PLAYER_DAMAGE_DURATION;
|
|
gs->damage_taken += combat_get_last_damage();
|
|
gs->times_hit++;
|
|
gs->player.flash_timer = 4; // Trigger damage flash
|
|
spawn_floating_text(gs, gs->player.position.x * TILE_SIZE + 8, gs->player.position.y * TILE_SIZE,
|
|
combat_get_last_damage(), combat_was_critical());
|
|
}
|
|
|
|
// Set message and check game over
|
|
gs->last_message = combat_get_last_message();
|
|
gs->message_timer = 60;
|
|
|
|
if (gs->player.hp <= 0)
|
|
gs->game_over = 1;
|
|
|
|
// Check if stepped on stairs AFTER enemy turns
|
|
if (gs->map.tiles[gs->player.position.y][gs->player.position.x] == TILE_STAIRS) {
|
|
gs->awaiting_descend = 1;
|
|
gs->last_message = "Descend to next floor? (Y/N)";
|
|
gs->message_timer = 120;
|
|
}
|
|
}
|
|
|
|
// If player is stunned, wait for any key then consume the turn
|
|
static int handle_stun_turn(GameState *gs) {
|
|
if (!(IsKeyDown(KEY_W) || IsKeyDown(KEY_UP) || IsKeyDown(KEY_S) || IsKeyDown(KEY_DOWN) || IsKeyDown(KEY_A) ||
|
|
IsKeyDown(KEY_LEFT) || IsKeyDown(KEY_D) || IsKeyDown(KEY_RIGHT)))
|
|
return 0;
|
|
gs->turn_count++;
|
|
tick_all_effects(gs);
|
|
if (gs->game_over)
|
|
return 1;
|
|
enemy_update_all(gs->enemies, gs->enemy_count, &gs->player, &gs->map);
|
|
// Close doors that the player moved away from before recomputing lighting.
|
|
for (int dy = 0; dy < MAP_HEIGHT; dy++) {
|
|
for (int dx = 0; dx < MAP_WIDTH; dx++) {
|
|
if (gs->map.tiles[dy][dx] == TILE_DOOR_OPEN) {
|
|
if (gs->player.position.x != dx || gs->player.position.y != dy) {
|
|
gs->map.tiles[dy][dx] = TILE_DOOR_CLOSED;
|
|
gs->map.door_anim_target[dy][dx] = 0;
|
|
gs->map.door_anim_timer[dy][dx] = DOOR_ANIM_FRAMES;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
{
|
|
LightSource l = {gs->player.position.x, gs->player.position.y, PLAYER_LIGHT_INTENSITY, PLAYER_LIGHT_RANGE};
|
|
LightSource s[1 + 32];
|
|
s[0] = l;
|
|
memcpy(s + 1, gs->static_lights, gs->static_light_count * sizeof(LightSource));
|
|
compute_lighting(&gs->map, s, 1 + gs->static_light_count);
|
|
}
|
|
if (gs->player.hp <= 0)
|
|
gs->game_over = 1;
|
|
gs->last_message = "You are stunned!";
|
|
gs->message_timer = 60;
|
|
add_log(gs, "Stunned! Lost a turn.");
|
|
return 1;
|
|
}
|
|
|
|
static int handle_inventory_input(GameState *gs) {
|
|
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)) {
|
|
gs->inv_selected = (gs->inv_selected == 0) ? (gs->player.inventory_count > 0 ? gs->player.inventory_count - 1 : 0)
|
|
: gs->inv_selected - 1;
|
|
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;
|
|
}
|
|
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);
|
|
gs->potions_used++;
|
|
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;
|
|
}
|
|
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;
|
|
}
|
|
gs->last_message = "Cannot drop!";
|
|
gs->message_timer = 60;
|
|
}
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int handle_descend_input(GameState *gs) {
|
|
if (IsKeyPressed(KEY_Y)) {
|
|
if (gs->player.floor < NUM_FLOORS) {
|
|
audio_play_stairs(gs);
|
|
if (gs->player.floor + 1 > gs->floors_reached)
|
|
gs->floors_reached = gs->player.floor + 1;
|
|
init_floor(gs, gs->player.floor + 1);
|
|
gs->last_message = "Descended to next floor!";
|
|
gs->message_timer = 60;
|
|
add_log(gs, "Descended stairs");
|
|
} 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;
|
|
}
|
|
|
|
static int handle_movement_input(GameState *gs) {
|
|
// Check for inventory toggle (I key)
|
|
if (IsKeyPressed(KEY_I)) {
|
|
gs->show_inventory = 1;
|
|
gs->inv_selected = 0;
|
|
return 0;
|
|
}
|
|
|
|
// Check for manual item pickup (G key)
|
|
if (IsKeyPressed(KEY_G)) {
|
|
Item *item = get_item_at_floor(gs->items, gs->item_count, gs->player.position.x, gs->player.position.y);
|
|
if (item != NULL) {
|
|
if (player_pickup(&gs->player, item)) {
|
|
gs->items_collected++;
|
|
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(gs);
|
|
} 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)) {
|
|
if (gs->player.inventory_count > 0 && player_use_first_item(&gs->player)) {
|
|
gs->potions_used++;
|
|
gs->last_message = "Used potion!";
|
|
gs->message_timer = 60;
|
|
audio_play_item_pickup(gs);
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
Vec2 direction = {0, 0};
|
|
if (IsKeyDown(KEY_W) || IsKeyDown(KEY_UP))
|
|
direction.y = -1;
|
|
else if (IsKeyDown(KEY_S) || IsKeyDown(KEY_DOWN))
|
|
direction.y = 1;
|
|
else if (IsKeyDown(KEY_A) || IsKeyDown(KEY_LEFT))
|
|
direction.x = -1;
|
|
else if (IsKeyDown(KEY_D) || IsKeyDown(KEY_RIGHT))
|
|
direction.x = 1;
|
|
|
|
if (direction.x == 0 && direction.y == 0)
|
|
return 0;
|
|
|
|
// Reset combat event before player acts
|
|
combat_reset_event();
|
|
|
|
int new_x = gs->player.position.x + direction.x;
|
|
int new_y = gs->player.position.y + direction.y;
|
|
|
|
Enemy *target = NULL;
|
|
int action = 0;
|
|
|
|
MoveResult result =
|
|
try_move_entity(&gs->player.position, direction, &gs->map, &gs->player, gs->enemies, gs->enemy_count, true);
|
|
if (result == MOVE_RESULT_MOVED) {
|
|
player_on_move(&gs->player);
|
|
// Set walk animation
|
|
gs->player.anim_state = PLAYER_ANIM_WALK;
|
|
gs->player.anim_frame = 0;
|
|
gs->player.anim_timer = 8; // frames to show each walk frame
|
|
// Update facing direction
|
|
if (direction.x != 0)
|
|
gs->player.facing_right = (direction.x > 0);
|
|
action = 1;
|
|
} else if (result == MOVE_RESULT_BLOCKED_ENEMY) {
|
|
target = player_find_enemy_at(gs->enemies, gs->enemy_count, new_x, new_y);
|
|
if (target != NULL) {
|
|
player_attack(&gs->player, target);
|
|
// Set attack animation
|
|
gs->player.anim_state = PLAYER_ANIM_ATTACK;
|
|
gs->player.anim_frame = 0;
|
|
gs->player.anim_timer = 12; // frames to show attack
|
|
// Face the enemy
|
|
if (target->position.x > gs->player.position.x)
|
|
gs->player.facing_right = 1;
|
|
else if (target->position.x < gs->player.position.x)
|
|
gs->player.facing_right = 0;
|
|
action = 1;
|
|
}
|
|
}
|
|
|
|
if (action)
|
|
post_action(gs, target);
|
|
|
|
return action;
|
|
}
|
|
|
|
// Handle player input - returns: 0=continue, 1=acted, -1=quit
|
|
static int handle_input(GameState *gs) {
|
|
// Check for quit first (always works)
|
|
if (IsKeyPressed(KEY_Q))
|
|
return -1;
|
|
|
|
// Check for restart (works during game over)
|
|
if (IsKeyPressed(KEY_R) && gs->game_over) {
|
|
memset(gs, 0, sizeof(GameState));
|
|
// Generate a new random seed for the new run
|
|
gs->run_seed = (unsigned int)time(NULL);
|
|
init_floor(gs, 1);
|
|
// Update window title with new seed
|
|
char title[128];
|
|
snprintf(title, sizeof(title), "Roguelike - Seed: %u", gs->run_seed);
|
|
SetWindowTitle(title);
|
|
return 0;
|
|
}
|
|
|
|
if (combat_has_effect(gs->player.effects, gs->player.effect_count, EFFECT_STUN))
|
|
return handle_stun_turn(gs);
|
|
|
|
if (gs->show_inventory)
|
|
return handle_inventory_input(gs);
|
|
|
|
if (gs->awaiting_descend)
|
|
return handle_descend_input(gs);
|
|
|
|
return handle_movement_input(gs);
|
|
}
|
|
|
|
void load_audio_assets(GameState *gs) {
|
|
gs->sounds.attack1 = LoadSound("./assets/sounds/sword1.wav");
|
|
gs->sounds.attack2 = LoadSound("./assets/sounds/sword2.wav");
|
|
gs->sounds.attack3 = LoadSound("./assets/sounds/sword3.wav");
|
|
gs->sounds.pickup = LoadSound("./assets/sounds/itempickup.wav");
|
|
gs->sounds.staircase = LoadSound("./assets/sounds/levelcomplete.wav");
|
|
gs->sounds.dodge1 = LoadSound("./assets/sounds/dodge1.wav");
|
|
gs->sounds.dodge2 = LoadSound("./assets/sounds/dodge2.wav");
|
|
gs->sounds.dodge3 = LoadSound("./assets/sounds/dodge3.wav");
|
|
gs->sounds.crit = LoadSound("./assets/sounds/crit.wav");
|
|
return;
|
|
}
|
|
|
|
// Main game loop
|
|
static void game_loop(unsigned int run_seed, FontManager *fm) {
|
|
GameState gs;
|
|
memset(&gs, 0, sizeof(GameState));
|
|
gs.run_seed = run_seed;
|
|
// load external assets
|
|
// sound
|
|
load_audio_assets(&gs);
|
|
// font
|
|
init_fonts(fm);
|
|
// Initialize tileset atlas
|
|
if (!tileset_init(&gs.tileset, TILE_SIZE, TILE_SIZE)) {
|
|
fprintf(stderr, "Failed to initialize tileset\n");
|
|
destroy_fonts(fm);
|
|
return;
|
|
}
|
|
if (!tileset_paint_all(&gs.tileset)) {
|
|
fprintf(stderr, "Failed to paint tiles\n");
|
|
tileset_destroy(&gs.tileset);
|
|
destroy_fonts(fm);
|
|
return;
|
|
}
|
|
if (!tileset_finalize(&gs.tileset)) {
|
|
fprintf(stderr, "Failed to finalize tileset\n");
|
|
tileset_destroy(&gs.tileset);
|
|
destroy_fonts(fm);
|
|
return;
|
|
}
|
|
|
|
// Initialize first floor
|
|
init_floor(&gs, 1);
|
|
|
|
// Disable esc to exit
|
|
SetExitKey(0);
|
|
|
|
int frame_counter = 0;
|
|
while (!WindowShouldClose()) {
|
|
frame_counter++;
|
|
|
|
// Handle input
|
|
if (!gs.game_over) {
|
|
// Tick status effects at the start of each frame where input is checked
|
|
// (effects tick once per player action via the acted flag below)
|
|
int quit = handle_input(&gs);
|
|
if (quit == -1)
|
|
break;
|
|
} else {
|
|
// Even during game over, check for q/r
|
|
if (IsKeyPressed(KEY_Q))
|
|
break;
|
|
if (IsKeyPressed(KEY_R)) {
|
|
memset(&gs, 0, sizeof(GameState));
|
|
// Generate a new random seed for the new run
|
|
gs.run_seed = (unsigned int)time(NULL);
|
|
gs.game_over = 0;
|
|
gs.game_won = 0;
|
|
load_audio_assets(&gs);
|
|
init_fonts(fm);
|
|
// Re-initialize tileset for new run
|
|
if (!tileset_init(&gs.tileset, TILE_SIZE, TILE_SIZE) || !tileset_paint_all(&gs.tileset) ||
|
|
!tileset_finalize(&gs.tileset)) {
|
|
fprintf(stderr, "Failed to re-initialize tileset\n");
|
|
break;
|
|
}
|
|
init_floor(&gs, 1);
|
|
// Update window title with new seed
|
|
char title[128];
|
|
snprintf(title, sizeof(title), "Roguelike - Seed: %u", gs.run_seed);
|
|
SetWindowTitle(title);
|
|
}
|
|
}
|
|
|
|
// Update message timer
|
|
if (gs.message_timer > 0)
|
|
gs.message_timer--;
|
|
|
|
// Update effects
|
|
update_effects(&gs);
|
|
|
|
// Update slash effect timer
|
|
if (gs.slash_timer > 0)
|
|
gs.slash_timer--;
|
|
|
|
// Door animations are visual, so they tick every rendered frame.
|
|
for (int y = 0; y < MAP_HEIGHT; y++) {
|
|
for (int x = 0; x < MAP_WIDTH; x++) {
|
|
if (gs.map.door_anim_timer[y][x] > 0) {
|
|
gs.map.door_anim_timer[y][x]--;
|
|
if (gs.map.door_anim_timer[y][x] == 0 && gs.map.door_anim_target[y][x] == 0)
|
|
gs.map.door_open_from[y][x] = 255;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Update player animation
|
|
if (gs.player.anim_timer > 0) {
|
|
gs.player.anim_timer--;
|
|
if (gs.player.anim_timer <= 0) {
|
|
// Animation finished, return to idle
|
|
gs.player.anim_state = PLAYER_ANIM_IDLE;
|
|
gs.player.anim_frame = 0;
|
|
} else if (gs.player.anim_state == PLAYER_ANIM_WALK) {
|
|
// Toggle walk frame every 4 frames
|
|
gs.player.anim_frame = (gs.player.anim_timer / 4) % 2;
|
|
}
|
|
}
|
|
|
|
// Update player damage flash
|
|
if (gs.player.flash_timer > 0)
|
|
gs.player.flash_timer--;
|
|
|
|
// Render
|
|
BeginDrawing();
|
|
ClearBackground(BLACK);
|
|
|
|
// Draw game world with screen shake applied via camera offset
|
|
Camera2D cam = {0};
|
|
cam.zoom = 1.0f;
|
|
cam.offset = (Vector2){(float)gs.shake_x, (float)gs.shake_y};
|
|
BeginMode2D(cam);
|
|
render_map(&gs.map, &gs.tileset);
|
|
render_items(gs.items, gs.item_count, &gs.map, &gs.tileset);
|
|
render_enemies(gs.enemies, gs.enemy_count, &gs.map, &gs.tileset, frame_counter);
|
|
render_player(&gs.player, &gs.tileset, frame_counter);
|
|
// Draw slash effect on top of entities
|
|
if (gs.slash_timer > 0) {
|
|
render_slash_effect(gs.slash_x, gs.slash_y, gs.slash_dmg_class, gs.slash_timer);
|
|
}
|
|
EndMode2D();
|
|
|
|
// Floating texts follow world shake
|
|
render_floating_texts(gs.floating_texts, gs.floating_count, gs.shake_x, gs.shake_y, fm);
|
|
render_ui(&gs.player, &gs.tileset, fm);
|
|
|
|
// Draw action log
|
|
render_action_log(gs.action_log, gs.log_count, gs.log_head, fm);
|
|
|
|
// Draw inventory overlay if active
|
|
if (gs.show_inventory) {
|
|
render_inventory_overlay(&gs.player, gs.inv_selected, fm);
|
|
}
|
|
|
|
// Draw message if any
|
|
if (gs.last_message != NULL && gs.message_timer > 0) {
|
|
render_message(gs.last_message, fm);
|
|
}
|
|
|
|
// Draw persistent seed display in top right
|
|
render_seed_display(gs.run_seed);
|
|
|
|
// Draw game over screen
|
|
if (gs.game_over) {
|
|
// Compute final score
|
|
gs.final_score = gs.total_kills * 100 + gs.items_collected * 30 + gs.floors_reached * 200 + gs.crits_landed * 25 +
|
|
gs.damage_dealt * 2 - gs.damage_taken * 2 - gs.times_hit * 15;
|
|
if (gs.game_won) {
|
|
gs.final_score = (gs.final_score * 3) / 2;
|
|
}
|
|
render_end_screen(gs.game_won, gs.total_kills, gs.items_collected, gs.damage_dealt, gs.damage_taken,
|
|
gs.crits_landed, gs.times_hit, gs.potions_used, gs.floors_reached, gs.turn_count,
|
|
gs.final_score, gs.run_seed, fm);
|
|
}
|
|
|
|
EndDrawing();
|
|
|
|
// small delay for key repeat control
|
|
WaitTime(0.08);
|
|
}
|
|
|
|
// Cleanup
|
|
destroy_fonts(fm);
|
|
}
|
|
|
|
// Check if a string is a valid unsigned integer
|
|
static int is_valid_uint(const char *str) {
|
|
if (str == NULL || *str == '\0')
|
|
return 0;
|
|
// Check for optional leading +
|
|
if (*str == '+')
|
|
str++;
|
|
// Must have at least one digit
|
|
if (*str == '\0')
|
|
return 0;
|
|
// All characters must be digits
|
|
for (const char *p = str; *p != '\0'; p++) {
|
|
if (!isdigit((unsigned char)*p))
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
// Parse command-line arguments
|
|
unsigned int run_seed = 0;
|
|
int seed_provided = 0;
|
|
|
|
for (int i = 1; i < argc; i++) {
|
|
if (strcmp(argv[i], "--seed") == 0) {
|
|
if (i + 1 >= argc) {
|
|
fprintf(stderr, "Error: --seed requires a value\n");
|
|
fprintf(stderr, "Usage: %s [--seed <number>]\n", argv[0]);
|
|
return 1;
|
|
}
|
|
const char *seed_str = argv[i + 1];
|
|
if (!is_valid_uint(seed_str)) {
|
|
fprintf(stderr, "Error: Invalid seed value: %s\n", seed_str);
|
|
fprintf(stderr, "Seed must be a non-negative integer\n");
|
|
return 1;
|
|
}
|
|
run_seed = (unsigned int)strtoul(seed_str, NULL, 10);
|
|
seed_provided = 1;
|
|
i++; // Skip the value
|
|
}
|
|
}
|
|
|
|
// If no seed provided, generate random seed from time
|
|
if (!seed_provided) {
|
|
run_seed = (unsigned int)time(NULL);
|
|
}
|
|
|
|
printf("Starting game with seed: %u\n", run_seed);
|
|
|
|
// Initialize audio
|
|
audio_init();
|
|
// Initialize random number generator
|
|
SetRandomSeed(88435);
|
|
// Initialize window with seed in title
|
|
char title[128];
|
|
snprintf(title, sizeof(title), "Roguelike - Seed: %u", run_seed);
|
|
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT + 60, title);
|
|
SetTargetFPS(60);
|
|
|
|
// Run game
|
|
FontManager fm;
|
|
init_fonts(&fm);
|
|
game_loop(run_seed, &fm);
|
|
|
|
// Cleanup
|
|
CloseWindow();
|
|
audio_close();
|
|
|
|
return 0;
|
|
}
|