1
0
Fork 0

Stabilize resources

This commit is contained in:
Pabloader 2026-05-02 20:19:47 +00:00
parent 1d3882e954
commit 1c22edaac1
2 changed files with 16 additions and 29 deletions

View File

@ -2,7 +2,6 @@ import type { Class } from "@common/types";
export namespace Resources { export namespace Resources {
const resources = new Map<Function, Map<string, any>>(); const resources = new Map<Function, Map<string, any>>();
let resourceId = 0;
export function get<T>(id: string): T | undefined; export function get<T>(id: string): T | undefined;
export function get<T>(ctor: Class<T>, id: string): T | undefined; export function get<T>(ctor: Class<T>, id: string): T | undefined;
@ -22,16 +21,12 @@ export namespace Resources {
return resources.get(ctor)?.get(id) as T; return resources.get(ctor)?.get(id) as T;
} }
export function set<T>(id: string, value: NonNullable<T>): void { export function add<T>(id: string, value: NonNullable<T>): string {
const ctor = value.constructor; const ctor = value.constructor;
const namespace: Map<string, T> = resources.get(ctor) ?? new Map(); const namespace: Map<string, T> = resources.get(ctor) ?? new Map();
if (namespace.has(id)) throw new Error(`Resource "${id}" is already registered`);
namespace.set(id, value); namespace.set(id, value);
resources.set(ctor, namespace); resources.set(ctor, namespace);
}
export function add<T>(value: NonNullable<T>): string {
const id = `__resource_${resourceId++}`;
Resources.set(id, value);
return id; return id;
} }
} }

View File

@ -2,29 +2,21 @@ import { Color, TextRegion } from "@common/display/text"
import { Resources } from "@common/rpg/utils/resources"; import { Resources } from "@common/rpg/utils/resources";
export const PLAYER_SPRITE = [ export const PLAYER_SPRITE = [
new TextRegion('@', Color.YELLOW) Resources.add("player", new TextRegion('@', Color.YELLOW)),
].map(Resources.add); ];
export const DOOR_SPRITE = [ export const DOOR_SPRITE = [
new TextRegion('┘ └'), Resources.add("door_n", new TextRegion('┘ └')),
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)),
new TextRegion('┐ ┌'), ];
new TextRegion([
'┘',
' ',
'┐',
]),
new TextRegion('^', Color.CYAN),
new TextRegion('_', Color.GREEN),
].map(Resources.add);
export const ITEM_SPRITE = [ export const ITEM_SPRITE = [
new TextRegion('ъ', Color.MAGENTA), Resources.add("key_m", new TextRegion('ъ', Color.MAGENTA)),
new TextRegion('ъ', Color.YELLOW), Resources.add("key_y", new TextRegion('ъ', Color.YELLOW)),
new TextRegion('ъ', Color.CYAN), Resources.add("key_c", new TextRegion('ъ', Color.CYAN)),
new TextRegion('ъ', Color.GREEN), Resources.add("key_g", new TextRegion('ъ', Color.GREEN)),
].map(Resources.add); ];