forked from NotAShelf/rogged
- Goblins act every ~2 turns, skeletons ~1.5, orcs ~1.2 - Enemies accumulate cooldown based on speed, act when <= 0 - Player always acts every turn (speed = 100)" Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: Id822579a0326887d91a80bd4ab27a72d6a6a6964
359 lines
9.1 KiB
C
359 lines
9.1 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 <string.h>
|
|
|
|
// 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;
|
|
}
|
|
|
|
// Handle player input - returns: 0=continue, 1=acted, -1=quit
|
|
static int handle_input(GameState *gs) {
|
|
int dx = 0, dy = 0;
|
|
|
|
// Check for quit first (always works)
|
|
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 (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;
|
|
// Adjust selection if needed
|
|
if (gs->inv_selected >= gs->player.inventory_count && gs->inv_selected > 0) {
|
|
gs->inv_selected--;
|
|
}
|
|
return 1; // Consume turn
|
|
} 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;
|
|
gs->show_inventory = 0;
|
|
return 1; // Consume turn
|
|
} else {
|
|
gs->last_message = "Equip weapons/armor with E!";
|
|
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;
|
|
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; // Waiting for Y/N
|
|
}
|
|
|
|
// 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)) {
|
|
gs->last_message = "Picked up item!";
|
|
gs->message_timer = 60;
|
|
audio_play_item_pickup();
|
|
return 1; // Consume a turn
|
|
} 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
|
|
}
|
|
}
|
|
}
|
|
|
|
// Movement, use iskeydown for held key repeat, with delay
|
|
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) {
|
|
// Reset combat message
|
|
combat_reset_event();
|
|
|
|
// Player action
|
|
int action = player_move(&gs->player, dx, dy, &gs->map, gs->enemies, gs->enemy_count);
|
|
|
|
if (action) {
|
|
// Increment turn counter
|
|
gs->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;
|
|
}
|
|
|
|
// Check if killed enemy
|
|
if (combat_get_last_message() != NULL && !combat_was_player_damage()) {
|
|
// Check if enemy died
|
|
for (int i = 0; i < gs->enemy_count; i++) {
|
|
if (!gs->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);
|
|
|
|
// Check if player took damage
|
|
if (combat_was_player_damage() && combat_get_last_damage() > 0) {
|
|
audio_play_player_damage();
|
|
}
|
|
|
|
// Set message
|
|
gs->last_message = combat_get_last_message();
|
|
gs->message_timer = 60;
|
|
|
|
// Check game over
|
|
if (gs->player.hp <= 0) {
|
|
gs->game_over = 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
// 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);
|
|
|
|
// Disable esc to exit
|
|
SetExitKey(0);
|
|
|
|
while (!WindowShouldClose()) {
|
|
// Handle input
|
|
if (!gs.game_over) {
|
|
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--;
|
|
|
|
// Render
|
|
BeginDrawing();
|
|
ClearBackground(BLACK);
|
|
|
|
// Draw game elements
|
|
render_map(&gs.map);
|
|
render_items(gs.items, gs.item_count);
|
|
render_enemies(gs.enemies, gs.enemy_count);
|
|
render_player(&gs.player);
|
|
render_ui(&gs.player);
|
|
|
|
// 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 window
|
|
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT + 60, "Roguelike");
|
|
SetTargetFPS(60);
|
|
|
|
// Run game
|
|
game_loop();
|
|
|
|
// Cleanup
|
|
CloseWindow();
|
|
audio_close();
|
|
|
|
return 0;
|
|
}
|