Skip to content

Commit

Permalink
fix: [#2419] Child entities inherit scene from parent
Browse files Browse the repository at this point in the history
  • Loading branch information
eonarheim committed Jul 22, 2022
1 parent 245426d commit 169bddb
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
5 changes: 0 additions & 5 deletions src/engine/Actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,6 @@ export class Actor extends Entity implements Eventable, PointerEvents, CanInitia
*/
public logger: Logger = Logger.getInstance();

/**
* The scene that the actor is in
*/
public scene: Scene = null;

/**
* Draggable helper
*/
Expand Down
7 changes: 6 additions & 1 deletion src/engine/EntityComponentSystem/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { OnInitialize, OnPreUpdate, OnPostUpdate } from '../Interfaces/Lifecycle
import { Engine } from '../Engine';
import { InitializeEvent, PreUpdateEvent, PostUpdateEvent } from '../Events';
import { EventDispatcher } from '../EventDispatcher';
import { Util } from '..';
import { Scene, Util } from '..';

/**
* Interface holding an entity component pair
Expand Down Expand Up @@ -75,6 +75,11 @@ export class Entity extends Class implements OnInitialize, OnPreUpdate, OnPostUp
*/
public id: number = Entity._ID++;

/**
* The scene that the entity is in, if any
*/
public scene: Scene = null;

private _name: string = 'anonymous';
protected _setName(name: string) {
if (name) {
Expand Down
12 changes: 10 additions & 2 deletions src/engine/EntityComponentSystem/EntityManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class EntityManager<ContextType = any> implements Observer<RemovedCompone
*/
public addEntity(entity: Entity): void {
entity.active = true;
entity.scene = (this._world.context as any);
if (entity && !this._entityIndex[entity.id]) {
this._entityIndex[entity.id] = entity;
this.entities.push(entity);
Expand All @@ -62,7 +63,10 @@ export class EntityManager<ContextType = any> implements Observer<RemovedCompone
entity.componentRemoved$.register(this);

// if entity has children
entity.children.forEach((c) => this.addEntity(c));
entity.children.forEach((c) => {
c.scene = entity.scene;
this.addEntity(c);
});
entity.childrenAdded$.register({
notify: (e) => {
this.addEntity(e);
Expand Down Expand Up @@ -97,13 +101,17 @@ export class EntityManager<ContextType = any> implements Observer<RemovedCompone

delete this._entityIndex[id];
if (entity) {
entity.scene = null;
Util.removeItemFromArray(entity, this.entities);
this._world.queryManager.removeEntity(entity);
entity.componentAdded$.unregister(this);
entity.componentRemoved$.unregister(this);

// if entity has children
entity.children.forEach((c) => this.removeEntity(c, deferred));
entity.children.forEach((c) => {
c.scene = null;
this.removeEntity(c, deferred);
});
entity.childrenAdded$.clear();
entity.childrenRemoved$.clear();

Expand Down
37 changes: 37 additions & 0 deletions src/spec/EntitySpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,4 +340,41 @@ describe('An entity', () => {

expect(world.entityManager.entities.length).toBe(2);
});

it('will inherit scene from a parent entity', () => {
const e = new ex.Entity([], 'e');
const child = new ex.Entity([], 'child');
const grandchild = new ex.Entity([], 'grandchild');

const scene = new ex.Scene();
e.addChild(child.addChild(grandchild));
scene.add(e);
expect(e.scene).toBe(scene);
expect(child.scene).toBe(scene);
expect(grandchild.scene).toBe(scene);

e.removeChild(child);
expect(e.scene).toBe(scene);
expect(child.scene).toBe(null);
expect(child.parent).toBe(null);
expect(grandchild.scene).toBe(null);
expect(grandchild.parent).toBe(child);

e.addChild(child);
expect(e.scene).toBe(scene);
expect(child.scene).toBe(scene);
expect(grandchild.scene).toBe(scene);

// deferred removal
scene.remove(e);
expect(e.scene).not.toBe(null);
expect(child.scene).not.toBe(null);
expect(grandchild.scene).not.toBe(null);

scene.world.entityManager.processEntityRemovals();
expect(e.scene).toBe(null);
expect(child.scene).toBe(null);
expect(grandchild.scene).toBe(null);

});
});

0 comments on commit 169bddb

Please sign in to comment.