just so much groundwork

This commit is contained in:
2025-02-05 04:00:40 -07:00
parent b3772052f5
commit 952b5dd57f
22 changed files with 1003 additions and 2081 deletions

View File

@@ -27,8 +27,11 @@ export const ctx = new Proxy(
throw new Error(`Context variable '${prop}' is not defined.`);
},
},
) as Record<string, any>;
) as Record<string, unknown>;
export function getContext() {
return ctx;
}
export function getContextItem<T>(prop: string): T {
return ctx[prop] as T;
}

View File

@@ -3,15 +3,21 @@ export class InputManager {
private mouseStates: Map<string | number, boolean> = new Map();
private mouseLocation: { x: number; y: number } = { x: 0, y: 0 };
private mouseDelta: { x: number; y: number } = { x: 0, y: 0 };
private keyEvents: Map<string | number, () => void> = new Map();
private mouseEvents: Map<string | number, () => void> = new Map();
constructor() {
document.addEventListener("keydown", (e) => {
this.keyStates.set(e.key, true);
this.keyEvents.get(e.key)?.call(e);
});
document.addEventListener("keyup", (e) => {
this.keyStates.set(e.key, false);
});
document.addEventListener("mousedown", (e) => {
this.mouseStates.set(e.button, true);
this.mouseEvents.get(e.button)?.call(e);
});
document.addEventListener("mouseup", (e) => {
this.mouseStates.set(e.button, false);
@@ -38,4 +44,18 @@ export class InputManager {
getMouseDelta() {
return this.mouseDelta;
}
onKey(key: string | number, cb: () => void) {
this.keyEvents.set(key, cb);
}
onMouse(key: string | number, cb: () => void) {
this.mouseEvents.set(key, cb);
}
offKey(key: string | number) {
this.keyEvents.delete(key);
}
offMouse(key: string | number) {
this.mouseEvents.delete(key);
}
}

38
lib/resources.ts Normal file
View File

@@ -0,0 +1,38 @@
export class ResourceManager {
private resources: Map<string, unknown> = new Map();
private statuses: Map<string, Promise<boolean>> = new Map();
get<T>(name: string): T {
if (!this.resources.has(name)) {
throw new Error(`Resource ${name} not found`);
}
return this.resources.get(name) as T;
}
set(name: string, value: unknown) {
if (typeof (value as EventSource).addEventListener === "function") {
this.statuses.set(
name,
new Promise((resolve) => {
const onload = () => {
this.resources.set(name, value);
resolve(true);
(value as EventSource).removeEventListener("load", onload);
};
(value as EventSource).addEventListener("load", onload);
}),
);
} else {
console.warn("Resource added was not a loadable resource");
}
this.resources.set(name, value);
}
delete(name: string) {
this.resources.delete(name);
}
ready() {
return Promise.all(Array.from(this.statuses.values()));
}
}