Refactor codebase

This commit is contained in:
Peter Stockings
2026-01-04 15:56:18 +11:00
parent 3785885abe
commit bfe5ebae8c
18 changed files with 380 additions and 191 deletions

View File

@@ -0,0 +1,62 @@
export const GAME_CONFIG = {
player: {
initialStats: { maxHp: 20, hp: 20, attack: 5, defense: 2 },
speed: 100,
viewRadius: 8
},
map: {
width: 60,
height: 40,
minRooms: 8,
maxRooms: 13,
roomMinWidth: 5,
roomMaxWidth: 12,
roomMinHeight: 4,
roomMaxHeight: 10
},
enemy: {
baseHpPerLevel: 2,
baseHp: 8,
baseAttack: 3,
attackPerTwoLevels: 1,
minSpeed: 80,
maxSpeed: 130,
maxDefense: 2,
baseCountPerLevel: 1,
baseCount: 3,
randomBonus: 4
},
rendering: {
tileSize: 24,
cameraZoom: 2,
wallColor: 0x2b2b2b,
floorColor: 0x161616,
exitColor: 0xffd166,
playerColor: 0x66ff66,
enemyColor: 0xff6666,
pathPreviewColor: 0x3355ff,
fogAlphaFloor: 0.15,
fogAlphaWall: 0.35,
visibleMinAlpha: 0.35,
visibleMaxAlpha: 1.0,
visibleStrengthFactor: 0.65
},
ui: {
minimapPanelWidth: 340,
minimapPanelHeight: 220,
minimapPadding: 20,
menuPanelWidth: 340,
menuPanelHeight: 220
},
gameplay: {
energyThreshold: 100,
actionCost: 100
}
} as const;
export type GameConfig = typeof GAME_CONFIG;

5
src/core/constants.ts Normal file
View File

@@ -0,0 +1,5 @@
import { GAME_CONFIG } from "./config/GameConfig";
export const TILE_SIZE = GAME_CONFIG.rendering.tileSize;
export const ENERGY_THRESHOLD = GAME_CONFIG.gameplay.energyThreshold;
export const ACTION_COST = GAME_CONFIG.gameplay.actionCost;

17
src/core/math.ts Normal file
View File

@@ -0,0 +1,17 @@
import type { Vec2 } from "./types";
export function seededRandom(seed: number): () => number {
let state = seed;
return () => {
state = (state * 1103515245 + 12345) & 0x7fffffff;
return state / 0x7fffffff;
};
}
export function manhattan(a: Vec2, b: Vec2): number {
return Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
}
export function lerp(a: number, b: number, t: number): number {
return a + (b - a) * t;
}

54
src/core/types.ts Normal file
View File

@@ -0,0 +1,54 @@
export type EntityId = number;
export type Vec2 = { x: number; y: number };
export type Tile = 0 | 1; // 0 = floor, 1 = wall
export type Action =
| { type: "move"; dx: number; dy: number }
| { type: "attack"; targetId: EntityId }
| { type: "wait" };
export type SimEvent =
| { type: "moved"; actorId: EntityId; from: Vec2; to: Vec2 }
| { type: "attacked"; attackerId: EntityId; targetId: EntityId }
| { type: "damaged"; targetId: EntityId; amount: number; hp: number; x: number; y: number }
| { type: "killed"; targetId: EntityId; killerId: EntityId; x: number; y: number; victimType?: "player" | "rat" | "bat" }
| { type: "waited"; actorId: EntityId };
export type Stats = {
maxHp: number;
hp: number;
attack: number;
defense: number;
};
export type Inventory = {
gold: number;
items: string[];
};
export type RunState = {
stats: Stats;
inventory: Inventory;
};
export type Actor = {
id: EntityId;
isPlayer: boolean;
type?: "player" | "rat" | "bat";
pos: Vec2;
speed: number;
energy: number;
stats?: Stats;
inventory?: Inventory;
};
export type World = {
width: number;
height: number;
tiles: Tile[];
actors: Map<EntityId, Actor>;
exit: Vec2;
};

7
src/core/utils.ts Normal file
View File

@@ -0,0 +1,7 @@
export function key(x: number, y: number): string {
return `${x},${y}`;
}
export function sleep(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}