build: move map & rng logic to their own libraries

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I1802469f3baff4576f61accfb5a197d86a6a6964
This commit is contained in:
raf 2026-04-10 15:17:39 +03:00
commit 26aa295f82
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF
17 changed files with 136 additions and 93 deletions

View file

@ -4,6 +4,51 @@ pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{}); const optimize = b.standardOptimizeOption(.{});
const c_flags = [_][]const u8{
"-std=c99",
"-Wall",
"-Wextra",
"-O2",
};
// RNG library
const rng_lib = b.addLibrary(.{
.name = "rng",
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
}),
});
rng_lib.addCSourceFiles(.{
.files = &[_][]const u8{"libs/rng/rng.c"},
.flags = &c_flags,
});
rng_lib.addIncludePath(b.path("libs/rng"));
// Map library
const map_lib = b.addLibrary(.{
.name = "map",
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
}),
});
map_lib.addCSourceFiles(.{
.files = &[_][]const u8{
"libs/map/map.c",
"libs/map/utils.c",
},
.flags = &c_flags,
});
// map.h includes common.h and settings.h which live in src/
map_lib.addIncludePath(b.path("src"));
// map.c includes rng/rng.h via libs/ root
map_lib.addIncludePath(b.path("libs"));
// utils.h is co-located with map.c
map_lib.addIncludePath(b.path("libs/map"));
// Zig combat library // Zig combat library
const combat_lib = b.addLibrary(.{ const combat_lib = b.addLibrary(.{
.name = "combat", .name = "combat",
@ -14,29 +59,20 @@ pub fn build(b: *std.Build) void {
.link_libc = true, .link_libc = true,
}), }),
}); });
// common.h and settings.h live in src/; rng.h exposed bare from libs/rng
combat_lib.addIncludePath(b.path("src")); combat_lib.addIncludePath(b.path("src"));
combat_lib.linkSystemLibrary("raylib"); combat_lib.addIncludePath(b.path("libs/rng"));
// C sources (everything except combat, which is now Zig) // C sources remaining in src/
const c_sources = [_][]const u8{ const c_sources = [_][]const u8{
"src/audio.c", "src/audio.c",
"src/enemy.c", "src/enemy.c",
"src/items.c", "src/items.c",
"src/main.c", "src/main.c",
"src/map.c",
"src/player.c",
"src/movement.c", "src/movement.c",
"src/player.c",
"src/render.c", "src/render.c",
"src/rng.c",
"src/settings.c", "src/settings.c",
"src/utils.c",
};
const c_flags = [_][]const u8{
"-std=c99",
"-Wall",
"-Wextra",
"-O2",
}; };
// Main executable // Main executable
@ -54,7 +90,12 @@ pub fn build(b: *std.Build) void {
.flags = &c_flags, .flags = &c_flags,
}); });
// src/ for own headers; libs/ so "rng/rng.h" and "map/map.h" resolve
exe.addIncludePath(b.path("src")); exe.addIncludePath(b.path("src"));
exe.addIncludePath(b.path("libs"));
exe.linkLibrary(rng_lib);
exe.linkLibrary(map_lib);
exe.linkLibrary(combat_lib); exe.linkLibrary(combat_lib);
exe.linkSystemLibrary("raylib"); exe.linkSystemLibrary("raylib");
exe.linkSystemLibrary("m"); exe.linkSystemLibrary("m");

View file

@ -1,5 +1,5 @@
#include "map.h" #include "map.h"
#include "rng.h" #include "rng/rng.h"
#include "settings.h" #include "settings.h"
#include "utils.h" #include "utils.h"
#include <stdlib.h> #include <stdlib.h>

View file

@ -1,6 +1,4 @@
#include "audio.h" #include "audio.h"
#include "raylib.h"
#include "common.h"
#include <math.h> #include <math.h>
#include <stddef.h> #include <stddef.h>

View file

@ -1,6 +1,6 @@
#ifndef AUDIO_H #ifndef AUDIO_H
#define AUDIO_H #define AUDIO_H
#include "common.h" #include "game_state.h"
// Initialize audio system // Initialize audio system
void audio_init(void); void audio_init(void);

View file

@ -2,7 +2,7 @@
#define COMMON_H #define COMMON_H
#include "settings.h" #include "settings.h"
#include <raylib.h> #include <stdbool.h>
typedef struct { typedef struct {
int x, y; int x, y;
@ -114,68 +114,5 @@ typedef struct {
int effect_count; int effect_count;
} Enemy; } Enemy;
// Floating damage text
typedef enum { LABEL_NONE = 0, LABEL_DODGE, LABEL_BLOCK, LABEL_CRIT, LABEL_SLAIN, LABEL_PROC } FloatingLabel;
typedef struct {
int x, y;
int value;
int lifetime; // frames remaining
int is_critical;
FloatingLabel label; // label type instead of string
StatusEffectType effect_type; // used to pick color for proc labels
} FloatingText;
// AudioAssets
typedef struct {
Sound attack1, attack2, attack3;
Sound pickup;
Sound staircase;
Sound dodge1, dodge2, dodge3;
Sound crit;
} AudioAssets;
// 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;
AudioAssets sounds;
// Statistics
int total_kills;
int items_collected;
int damage_dealt;
int damage_taken;
int crits_landed;
int times_hit;
int potions_used;
int floors_reached;
int final_score;
// Seed for this run
unsigned int run_seed;
} GameState;
#endif // COMMON_H #endif // COMMON_H

View file

@ -1,9 +1,9 @@
#include "enemy.h" #include "enemy.h"
#include "combat.h" #include "combat.h"
#include "common.h" #include "common.h"
#include "map.h" #include "map/map.h"
#include "movement.h" #include "movement.h"
#include "rng.h" #include "rng/rng.h"
#include "settings.h" #include "settings.h"
#include <string.h> #include <string.h>

70
src/game_state.h Normal file
View file

@ -0,0 +1,70 @@
#ifndef GAME_STATE_H
#define GAME_STATE_H
#include "common.h"
#include <raylib.h>
// Floating damage text
typedef enum { LABEL_NONE = 0, LABEL_DODGE, LABEL_BLOCK, LABEL_CRIT, LABEL_SLAIN, LABEL_PROC } FloatingLabel;
typedef struct {
int x, y;
int value;
int lifetime; // frames remaining
int is_critical;
FloatingLabel label; // label type instead of string
StatusEffectType effect_type; // used to pick color for proc labels
} FloatingText;
// AudioAssets
typedef struct {
Sound attack1, attack2, attack3;
Sound pickup;
Sound staircase;
Sound dodge1, dodge2, dodge3;
Sound crit;
} AudioAssets;
// 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;
AudioAssets sounds;
// Statistics
int total_kills;
int items_collected;
int damage_dealt;
int damage_taken;
int crits_landed;
int times_hit;
int potions_used;
int floors_reached;
int final_score;
// Seed for this run
unsigned int run_seed;
} GameState;
#endif // GAME_STATE_H

View file

@ -1,6 +1,6 @@
#include "common.h" #include "common.h"
#include "map.h" #include "map/map.h"
#include "rng.h" #include "rng/rng.h"
#include "settings.h" #include "settings.h"
#include <stddef.h> #include <stddef.h>

View file

@ -1,14 +1,13 @@
#include "audio.h" #include "audio.h"
#include "combat.h" #include "combat.h"
#include "common.h" #include "game_state.h"
#include "enemy.h" #include "enemy.h"
#include "items.h" #include "items.h"
#include "map.h" #include "map/map.h"
#include "movement.h" #include "movement.h"
#include "player.h" #include "player.h"
#include "raylib.h"
#include "render.h" #include "render.h"
#include "rng.h" #include "rng/rng.h"
#include "settings.h" #include "settings.h"
#include <ctype.h> #include <ctype.h>
#include <stddef.h> #include <stddef.h>

View file

@ -1,6 +1,6 @@
#include "movement.h" #include "movement.h"
#include "enemy.h" #include "enemy.h"
#include "map.h" #include "map/map.h"
#include <stdbool.h> #include <stdbool.h>
// Check if position is occupied by player // Check if position is occupied by player

View file

@ -1,7 +1,5 @@
#include "render.h" #include "render.h"
#include "common.h"
#include "items.h" #include "items.h"
#include "raylib.h"
#include "settings.h" #include "settings.h"
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>

View file

@ -2,7 +2,7 @@
#ifndef RENDER_H #ifndef RENDER_H
#define RENDER_H #define RENDER_H
#include "common.h" #include "game_state.h"
// HUD colors // HUD colors
#define HUD_BG (Color){25, 20, 15, 255} #define HUD_BG (Color){25, 20, 15, 255}