Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: inspect properties of objects #316

Merged
merged 12 commits into from
Aug 5, 2024
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ debug.log(obj.getAnim("walk"));
- (**v3001/4000**) added higher order easing functions linear, steps and
cubic-bezier
- (**v3001/4000**) added `textInput()` component
- (**v3001/4000**) now you can see custom properties in debug.inspect

## Bug fixes

Expand Down
20 changes: 18 additions & 2 deletions src/game/make.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type MakeType<T> = MakeTypeIsCLASS<T, MakeTypeIsFN<T>>;

export function make<T>(comps: CompList<T> = []): GameObj<MakeType<T>> {
const compStates = new Map<string, Comp>();
const anonymousCompStates: Comp[] = [];
const cleanups = {} as Record<string, (() => unknown)[]>;
const events = new KEventHandler();
const inputEvents: KEventController[] = [];
Expand Down Expand Up @@ -203,7 +204,7 @@ export function make<T>(comps: CompList<T> = []): GameObj<MakeType<T>> {
popTransform();
},

// use a comp, or tag
// use a comp or a tag
use(comp: Comp | Tag) {
if (!comp) {
return;
Expand Down Expand Up @@ -235,6 +236,9 @@ export function make<T>(comps: CompList<T> = []): GameObj<MakeType<T>> {
gc = cleanups[comp.id];
compStates.set(comp.id, comp);
}
else {
anonymousCompStates.push(comp);
}

for (const k in comp) {
if (COMP_DESC.has(k)) {
Expand All @@ -245,7 +249,7 @@ export function make<T>(comps: CompList<T> = []): GameObj<MakeType<T>> {
if (!prop) continue;

if (typeof prop.value === "function") {
// @ts-ignore Maybe a MAP would be better?
// @ts-ignore
comp[k] = comp[k].bind(this);
}

Expand Down Expand Up @@ -539,6 +543,18 @@ export function make<T>(comps: CompList<T> = []): GameObj<MakeType<T>> {
for (const [tag, comp] of compStates) {
info[tag] = comp.inspect?.() ?? null;
}

for (const comp of anonymousCompStates) {
for (const [key, value] of Object.entries(comp)) {
if (typeof value === "function") {
continue;
}
else {
info[key] = `${key}: ${value}`;
}
}
}

return info;
},

Expand Down