From 1c22edaac1dec9daa84d9236698d4fbaba9b9a34 Mon Sep 17 00:00:00 2001 From: Pabloader Date: Sat, 2 May 2026 20:19:47 +0000 Subject: [PATCH] Stabilize resources --- src/common/rpg/utils/resources.ts | 9 ++------ src/games/text-dungeon/images.ts | 36 ++++++++++++------------------- 2 files changed, 16 insertions(+), 29 deletions(-) diff --git a/src/common/rpg/utils/resources.ts b/src/common/rpg/utils/resources.ts index b34cffb..7017d9b 100644 --- a/src/common/rpg/utils/resources.ts +++ b/src/common/rpg/utils/resources.ts @@ -2,7 +2,6 @@ import type { Class } from "@common/types"; export namespace Resources { const resources = new Map>(); - let resourceId = 0; export function get(id: string): T | undefined; export function get(ctor: Class, id: string): T | undefined; @@ -22,16 +21,12 @@ export namespace Resources { return resources.get(ctor)?.get(id) as T; } - export function set(id: string, value: NonNullable): void { + export function add(id: string, value: NonNullable): string { const ctor = value.constructor; const namespace: Map = resources.get(ctor) ?? new Map(); + if (namespace.has(id)) throw new Error(`Resource "${id}" is already registered`); namespace.set(id, value); resources.set(ctor, namespace); - } - - export function add(value: NonNullable): string { - const id = `__resource_${resourceId++}`; - Resources.set(id, value); return id; } } \ No newline at end of file diff --git a/src/games/text-dungeon/images.ts b/src/games/text-dungeon/images.ts index 3331279..6c1e532 100644 --- a/src/games/text-dungeon/images.ts +++ b/src/games/text-dungeon/images.ts @@ -2,29 +2,21 @@ import { Color, TextRegion } from "@common/display/text" import { Resources } from "@common/rpg/utils/resources"; export const PLAYER_SPRITE = [ - new TextRegion('@', Color.YELLOW) -].map(Resources.add); + Resources.add("player", new TextRegion('@', Color.YELLOW)), +]; export const DOOR_SPRITE = [ - new TextRegion('┘ └'), - new TextRegion([ - '└', - ' ', - '┌', - ]), - new TextRegion('┐ ┌'), - new TextRegion([ - '┘', - ' ', - '┐', - ]), - new TextRegion('^', Color.CYAN), - new TextRegion('_', Color.GREEN), -].map(Resources.add); + Resources.add("door_n", new TextRegion('┘ └')), + Resources.add("door_e", new TextRegion(['└', ' ', '┌'])), + Resources.add("door_s", new TextRegion('┐ ┌')), + Resources.add("door_w", new TextRegion(['┘', ' ', '┐'])), + Resources.add("door_u", new TextRegion('^', Color.CYAN)), + Resources.add("door_d", new TextRegion('_', Color.GREEN)), +]; export const ITEM_SPRITE = [ - new TextRegion('ъ', Color.MAGENTA), - new TextRegion('ъ', Color.YELLOW), - new TextRegion('ъ', Color.CYAN), - new TextRegion('ъ', Color.GREEN), -].map(Resources.add); + Resources.add("key_m", new TextRegion('ъ', Color.MAGENTA)), + Resources.add("key_y", new TextRegion('ъ', Color.YELLOW)), + Resources.add("key_c", new TextRegion('ъ', Color.CYAN)), + Resources.add("key_g", new TextRegion('ъ', Color.GREEN)), +];