Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/tangy-ravens-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@jolly-pixel/engine": patch
---

Remove useless duplication and push of components in Actor.registerComponent + Add unique ID (index) to ActorComponent
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@changesets/cli": "^2.9.2",
"@openally/config.eslint": "^2.2.0",
"@openally/config.typescript": "1.2.1",
"@types/node": "^25.0.2",
"@types/node": "25.2.3",
"@types/three": "^0.182.0",
"c8": "^10.1.3",
"electron": "^40.0.0",
Expand Down
16 changes: 0 additions & 16 deletions packages/engine/src/actor/Actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,6 @@ export class Actor extends ActorTree {
): this {
const [options, callback] = args;
const component = new componentClass(this, options);
if (this.components.indexOf(component) === -1) {
this.components.push(component);
}

const index = this.gameInstance.scene.componentsToBeStarted.indexOf(component);
if (index === -1) {
this.gameInstance.scene.componentsToBeStarted.push(component);
}

callback?.(component as InstanceType<T>);
if (this.awoken) {
Expand All @@ -109,14 +101,6 @@ export class Actor extends ActorTree {
): InstanceType<T> {
const [options] = args;
const component = new componentClass(this, options);
if (this.components.indexOf(component) === -1) {
this.components.push(component);
}

const index = this.gameInstance.scene.componentsToBeStarted.indexOf(component);
if (index === -1) {
this.gameInstance.scene.componentsToBeStarted.push(component);
}

if (this.awoken) {
component.awake?.();
Expand Down
11 changes: 11 additions & 0 deletions packages/engine/src/actor/ActorComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ export type ActorComponentEvents = {
};

export class ActorComponent extends EventEmitter<ActorComponentEvents> implements Component {
protected static Id = 0;

static generateNextId() {
return this.Id++;
}

static clearId() {
this.Id = 0;
}

id = ActorComponent.generateNextId();
actor: Actor;
typeName: FreeComponentEnum;

Expand Down
9 changes: 8 additions & 1 deletion packages/engine/test/ActorComponent.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
// Import Node.js Dependencies
import { describe, test } from "node:test";
import { describe, test, beforeEach } from "node:test";
import assert from "node:assert/strict";

// Import Internal Dependencies
import { ActorComponent } from "../src/index.ts";
import { createActor } from "./mocks.ts";

describe("ActorComponent", () => {
beforeEach(() => {
ActorComponent.clearId();
});

test("should register component to actor and add to components to be started", () => {
const fakeActor = createActor();

Expand All @@ -19,6 +23,7 @@ describe("ActorComponent", () => {
assert.deepEqual(fakeActor.components, [component]);
assert.deepEqual(fakeActor.gameInstance.scene.componentsToBeStarted, [component]);
assert.equal(component.actor, fakeActor);
assert.equal(component.id, 0);
assert.equal(component.typeName, "TestComponent");
assert.equal(component.pendingForDestruction, false);
});
Expand All @@ -40,6 +45,8 @@ describe("ActorComponent", () => {

assert.deepEqual(fakeActor.components, [component1, component2]);
assert.deepEqual(fakeActor.gameInstance.scene.componentsToBeStarted, [component1, component2]);
assert.equal(component1.id, 0);
assert.equal(component2.id, 1);
});

test("should not be destroyed initially", () => {
Expand Down
12 changes: 12 additions & 0 deletions packages/engine/test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "..",
"noEmit": true,
"composite": false
},
"include": [
"../src",
"."
]
}
Loading