Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: Ie3b66d17f6f660c9b9a719210bd86f9f6a6a6964
247 lines
5.5 KiB
C
247 lines
5.5 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>
|
|
|
|
// 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;
|
|
|
|
// Turn counter for enemy movement (enemies move every other turn)
|
|
static int turn_count = 0;
|
|
|
|
// Initialize a new floor
|
|
static void init_floor(int floor_num) {
|
|
// Generate dungeon
|
|
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(&map, &start_x, &start_y, 100);
|
|
|
|
// Initialize player position if first floor
|
|
if (floor_num == 1) {
|
|
player_init(&player, start_x, start_y);
|
|
} else {
|
|
// Move player to new floor position
|
|
player.x = start_x;
|
|
player.y = start_y;
|
|
}
|
|
player.floor = floor_num;
|
|
|
|
// Spawn enemies
|
|
enemy_spawn(enemies, &enemy_count, &map, &player, floor_num);
|
|
|
|
// Spawn items
|
|
item_spawn(items, &item_count, &map, floor_num);
|
|
|
|
// Reset turn counter
|
|
turn_count = 0;
|
|
}
|
|
|
|
// Handle player input - returns: 0=continue, -1=quit
|
|
static int handle_input(void) {
|
|
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) && game_over) {
|
|
game_over = 0;
|
|
game_won = 0;
|
|
init_floor(1);
|
|
return 0;
|
|
}
|
|
|
|
// Check for item usage (U key)
|
|
if (IsKeyPressed(KEY_U) && !game_over) {
|
|
if (player.inventory_count > 0) {
|
|
if (player_use_first_item(&player)) {
|
|
last_message = "Used item!";
|
|
message_timer = 60;
|
|
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(&player, dx, dy, &map, enemies, enemy_count, items,
|
|
item_count);
|
|
|
|
if (action) {
|
|
// Increment turn counter
|
|
turn_count++;
|
|
|
|
// Check if stepped on stairs
|
|
|
|
if (map.tiles[player.y][player.x] == TILE_STAIRS) {
|
|
// Go to next floor
|
|
if (player.floor < NUM_FLOORS) {
|
|
audio_play_stairs();
|
|
init_floor(player.floor + 1);
|
|
last_message = "Descended to next floor!";
|
|
message_timer = 60;
|
|
} else {
|
|
// Won the game
|
|
game_won = 1;
|
|
game_over = 1;
|
|
}
|
|
}
|
|
|
|
// 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 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();
|
|
}
|
|
|
|
// Set message
|
|
last_message = combat_get_last_message();
|
|
message_timer = 60;
|
|
|
|
// Check game over
|
|
if (player.hp <= 0) {
|
|
game_over = 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
// Main game loop
|
|
static void game_loop(void) {
|
|
// Initialize first floor
|
|
rng_seed(12345);
|
|
init_floor(1);
|
|
|
|
// Disable esc to exit
|
|
SetExitKey(0);
|
|
|
|
while (!WindowShouldClose()) {
|
|
// Handle input
|
|
if (!game_over) {
|
|
int quit = handle_input();
|
|
if (quit == -1)
|
|
break;
|
|
} else {
|
|
// Even during game over, check for q/r
|
|
if (IsKeyPressed(KEY_Q))
|
|
break;
|
|
if (IsKeyPressed(KEY_R)) {
|
|
game_over = 0;
|
|
game_won = 0;
|
|
init_floor(1);
|
|
}
|
|
}
|
|
|
|
// Update message timer
|
|
if (message_timer > 0)
|
|
message_timer--;
|
|
|
|
// Render
|
|
BeginDrawing();
|
|
ClearBackground(BLACK);
|
|
|
|
// Draw game elements
|
|
render_map(&map);
|
|
render_items(items, item_count);
|
|
render_enemies(enemies, enemy_count);
|
|
render_player(&player);
|
|
render_ui(&player);
|
|
|
|
// Draw message if any
|
|
if (last_message != NULL && message_timer > 0) {
|
|
render_message(last_message);
|
|
}
|
|
|
|
// Draw game over screen
|
|
if (game_over) {
|
|
render_game_over();
|
|
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);
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|