Skip to content

chore: clarify make code and rename GameObj test to add #668

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

Merged
merged 1 commit into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/game/make.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function make<const T extends CompList<unknown>>(
let _parent: GameObj;

// the game object without the event methods, added later
const obj: Omit<GameObj, keyof typeof evs> = {
const obj: Omit<GameObj, keyof typeof appEvs> = {
id: uid(),
// TODO: a nice way to hide / pause when add()-ing
hidden: false,
Expand Down Expand Up @@ -325,6 +325,8 @@ export function make<const T extends CompList<unknown>>(
}

for (const k in comp) {
// These are properties from the component data (id, require), shouldn't
// be added to the game obj prototype, that's why we continue
if (COMP_DESC.has(k)) {
continue;
}
Expand All @@ -350,7 +352,7 @@ export function make<const T extends CompList<unknown>>(
}

if (COMP_EVENTS.has(k)) {
// automatically clean up events created by components in add() stage
// Automatically clean up events created by components in add() stage
const func = k === "add"
? () => {
onCurCompCleanup = (c: any) => gc.push(c);
Expand All @@ -362,7 +364,7 @@ export function make<const T extends CompList<unknown>>(
}
else {
if (this[k] === undefined) {
// assign comp fields to game obj
// Assign comp fields to game obj
Object.defineProperty(this, k, {
get: () => comp[<keyof typeof comp> k],
set: (val) => comp[<keyof typeof comp> k] = val,
Expand Down Expand Up @@ -786,7 +788,8 @@ export function make<const T extends CompList<unknown>>(
},
};

const evs = [
// We add App Events for "attaching" it to game object (not really)
const appEvs = [
"onKeyPress",
"onKeyPressRepeat",
"onKeyDown",
Expand All @@ -810,15 +813,17 @@ export function make<const T extends CompList<unknown>>(
"onButtonRelease",
] as unknown as [keyof Pick<App, "onKeyPress">];

for (const e of evs) {
for (const e of appEvs) {
obj[e] = (...args: [any]) => {
const ev = _k.app[e]?.(...args);
inputEvents.push(ev);

obj.onDestroy(() => ev.cancel());

// This only happens if obj.has("stay");
obj.on("sceneEnter", () => {
// All app events are already canceled by changing the scene
// not neccesary -> ev.cancel();
// so we don't need to event.cancel();
inputEvents.splice(inputEvents.indexOf(ev), 1);
// create a new event with the same arguments
const newEv = _k.app[e]?.(...args);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expectTypeOf } from "vitest";
import type { CircleComp, ScaleComp } from "../../src/";
import type { CircleComp, ScaleComp } from "../../src";
import { kaplay } from "../../src/kaplay";
import type { GameObj } from "../../src/types";

Expand Down