forked from NotAShelf/rogged
590 lines
16 KiB
C
590 lines
16 KiB
C
#include "audio.h"
|
|
#include "combat.h"
|
|
#include "common.h"
|
|
#include "enemy.h"
|
|
#include "items.h"
|
|
#include "map.h"
|
|
#include "player.h"
|
|
#include "raylib.h"
|
|
#include "render.h"
|
|
#include "rng.h"
|
|
#include "settings.h"
|
|
#include <stddef.h>
|
|
#include <stdio.h>
|
|
#include <string.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 = 60;
|
|
gs->floating_texts[slot].is_critical = is_critical;
|
|
gs->floating_texts[slot].label[0] = '\0'; // numeric, no label
|
|
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, const char *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].effect_type = effect_type;
|
|
strncpy(gs->floating_texts[slot].label, label, 7);
|
|
gs->floating_texts[slot].label[7] = '\0';
|
|
}
|
|
|
|
static const char *proc_label_for(StatusEffectType effect) {
|
|
switch (effect) {
|
|
case EFFECT_POISON:
|
|
return "POISON!";
|
|
case EFFECT_BLEED:
|
|
return "BLEED!";
|
|
case EFFECT_BURN:
|
|
return "BURN!";
|
|
case EFFECT_STUN:
|
|
return "STUN!";
|
|
case EFFECT_WEAKEN:
|
|
return "WEAKEN!";
|
|
default:
|
|
return "";
|
|
}
|
|
}
|
|
|
|
// 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(GameState *gs, int floor_num) {
|
|
// Generate dungeon
|
|
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(&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);
|
|
} else {
|
|
// Move player to new floor position
|
|
gs->player.x = start_x;
|
|
gs->player.y = start_y;
|
|
}
|
|
gs->player.floor = floor_num;
|
|
|
|
// 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.x * TILE_SIZE + 8, gs->player.y * TILE_SIZE, player_effect_dmg, 0);
|
|
gs->screen_shake = 4;
|
|
}
|
|
|
|
// 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->x * TILE_SIZE + 8, e->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;
|
|
|
|
// 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;
|
|
}
|
|
|
|
// combat feedback - player attacked an enemy this turn
|
|
if (attacked_enemy != NULL) {
|
|
int ex = attacked_enemy->x * TILE_SIZE + 8;
|
|
int ey = attacked_enemy->y * TILE_SIZE;
|
|
|
|
if (combat_was_dodged()) {
|
|
spawn_floating_label(gs, ex, ey, "DODGE", EFFECT_NONE);
|
|
audio_play_dodge(gs);
|
|
} else {
|
|
if (combat_get_last_damage() > 0)
|
|
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, "BLOCK", EFFECT_NONE);
|
|
audio_play_block(gs);
|
|
}
|
|
if (combat_was_critical()) {
|
|
spawn_floating_label(gs, ex + 8, ey - 10, "CRIT!", EFFECT_NONE);
|
|
audio_play_crit(gs);
|
|
}
|
|
StatusEffectType applied = combat_get_applied_effect();
|
|
if (applied != EFFECT_NONE) {
|
|
spawn_floating_label(gs, ex, ey - 20, proc_label_for(applied), applied);
|
|
audio_play_proc();
|
|
}
|
|
if (!attacked_enemy->alive) {
|
|
spawn_floating_label(gs, ex, ey - 20, "SLAIN", EFFECT_NONE);
|
|
audio_play_enemy_death(gs);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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 = 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 and check game over
|
|
gs->last_message = combat_get_last_message();
|
|
gs->message_timer = 60;
|
|
|
|
if (gs->player.hp <= 0)
|
|
gs->game_over = 1;
|
|
}
|
|
|
|
// 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);
|
|
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);
|
|
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);
|
|
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.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(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->last_message = "Used potion!";
|
|
gs->message_timer = 60;
|
|
audio_play_item_pickup(gs);
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
// Movement: use IsKeyDown for held-key repeat
|
|
int dx = 0, dy = 0;
|
|
if (IsKeyDown(KEY_W) || IsKeyDown(KEY_UP))
|
|
dy = -1;
|
|
else if (IsKeyDown(KEY_S) || IsKeyDown(KEY_DOWN))
|
|
dy = 1;
|
|
else if (IsKeyDown(KEY_A) || IsKeyDown(KEY_LEFT))
|
|
dx = -1;
|
|
else if (IsKeyDown(KEY_D) || IsKeyDown(KEY_RIGHT))
|
|
dx = 1;
|
|
|
|
if (dx == 0 && dy == 0)
|
|
return 0;
|
|
|
|
// Reset combat event before player acts
|
|
combat_reset_event();
|
|
|
|
int new_x = gs->player.x + dx;
|
|
int new_y = gs->player.y + dy;
|
|
int action = 0;
|
|
|
|
// Attack enemy at target tile, or move into it
|
|
Enemy *target = player_find_enemy_at(gs->enemies, gs->enemy_count, new_x, new_y);
|
|
if (target != NULL) {
|
|
player_attack(&gs->player, target);
|
|
action = 1;
|
|
} else {
|
|
action = player_move(&gs->player, dx, dy, &gs->map);
|
|
}
|
|
|
|
if (action)
|
|
post_action(gs, target); // target is NULL on move, enemy ptr on attack
|
|
|
|
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));
|
|
init_floor(gs, 1);
|
|
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(void) {
|
|
GameState gs;
|
|
memset(&gs, 0, sizeof(GameState));
|
|
load_audio_assets(&gs);
|
|
// Initialize first floor
|
|
rng_seed(12345);
|
|
init_floor(&gs, 1);
|
|
|
|
// Disable esc to exit
|
|
SetExitKey(0);
|
|
|
|
while (!WindowShouldClose()) {
|
|
// 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));
|
|
gs.game_over = 0;
|
|
gs.game_won = 0;
|
|
init_floor(&gs, 1);
|
|
}
|
|
}
|
|
|
|
// Update message timer
|
|
if (gs.message_timer > 0)
|
|
gs.message_timer--;
|
|
|
|
// Update effects
|
|
update_effects(&gs);
|
|
|
|
// 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);
|
|
render_items(gs.items, gs.item_count);
|
|
render_enemies(gs.enemies, gs.enemy_count);
|
|
render_player(&gs.player);
|
|
EndMode2D();
|
|
|
|
// Floating texts follow world shake
|
|
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 (gs.last_message != NULL && gs.message_timer > 0) {
|
|
render_message(gs.last_message);
|
|
}
|
|
|
|
// Draw game over screen
|
|
if (gs.game_over) {
|
|
render_game_over();
|
|
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);
|
|
}
|
|
}
|
|
|
|
EndDrawing();
|
|
|
|
// small delay for key repeat control
|
|
WaitTime(0.08);
|
|
}
|
|
}
|
|
|
|
int main(void) {
|
|
// Initialize audio
|
|
audio_init();
|
|
// Initialize random number generator
|
|
SetRandomSeed(88435);
|
|
// Initialize window
|
|
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT + 60, "Roguelike");
|
|
SetTargetFPS(60);
|
|
|
|
// Run game
|
|
game_loop();
|
|
|
|
// Cleanup
|
|
CloseWindow();
|
|
audio_close();
|
|
|
|
return 0;
|
|
}
|