Skip to content

Commit

Permalink
Fix a entity add another scene's root entity (#882)
Browse files Browse the repository at this point in the history
* fix: another scene's entity add a root entity
* fix: onDisable bug
  • Loading branch information
GuoLei1990 authored Jul 22, 2022
1 parent 4140ab1 commit 648484a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ export abstract class Component extends EngineObject {
}
// You can do isActive = false in onAwake function.
if (this._entity._isActiveInHierarchy) {
this._enabled && this._onEnable();
this._onEnable();
}
} else {
this._enabled && this._onDisable();
this._onDisable();
}
}
}
36 changes: 29 additions & 7 deletions packages/core/src/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { Layer } from "./Layer";
import { Scene } from "./Scene";
import { Script } from "./Script";
import { Transform } from "./Transform";
import { UpdateFlag } from "./UpdateFlag";

/**
* Entity, be used as components container.
Expand Down Expand Up @@ -103,10 +102,10 @@ export class Entity extends EngineObject {
return this._parent;
}

set parent(entity: Entity) {
if (entity !== this._parent) {
set parent(value: Entity) {
if (value !== this._parent) {
const oldParent = this._removeFromParent();
const newParent = (this._parent = entity);
const newParent = (this._parent = value);
if (newParent) {
newParent._children.push(this);
const parentScene = newParent._scene;
Expand Down Expand Up @@ -224,7 +223,28 @@ export class Entity extends EngineObject {
* @param child - The child entity which want to be added.
*/
addChild(child: Entity): void {
child.parent = this;
if (child._isRoot) {
child._scene._removeEntity(child);
child._isRoot = false;

this._children.push(child);
child._parent = this;

const newScene = this._scene;
if (child._scene !== newScene) {
Entity._traverseSetOwnerScene(child, newScene);
}

if (this._isActiveInHierarchy) {
!child._isActiveInHierarchy && child._isActive && child._processActive();
} else {
child._isActiveInHierarchy && child._processInActive();
}

child._setTransformDirty();
} else {
child.parent = this;
}
}

/**
Expand Down Expand Up @@ -451,7 +471,8 @@ export class Entity extends EngineObject {
this._isActiveInHierarchy = true;
const components = this._components;
for (let i = components.length - 1; i >= 0; i--) {
activeChangedComponents.push(components[i]);
const component = components[i];
component.enabled && activeChangedComponents.push(component);
}
const children = this._children;
for (let i = children.length - 1; i >= 0; i--) {
Expand All @@ -464,7 +485,8 @@ export class Entity extends EngineObject {
this._isActiveInHierarchy = false;
const components = this._components;
for (let i = components.length - 1; i >= 0; i--) {
activeChangedComponents.push(components[i]);
const component = components[i];
component.enabled && activeChangedComponents.push(component);
}
const children = this._children;
for (let i = children.length - 1; i >= 0; i--) {
Expand Down
10 changes: 7 additions & 3 deletions packages/core/src/Scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export class Scene extends EngineObject {
removeRootEntity(entity: Entity): void {
if (entity._isRoot && entity._scene == this) {
this._removeEntity(entity);
entity._isRoot = false;
this._isActiveInEngine && entity._isActiveInHierarchy && entity._processInActive();
Entity._traverseSetOwnerScene(entity, null);
}
Expand Down Expand Up @@ -264,9 +265,12 @@ export class Scene extends EngineObject {
lightMgr._updateShaderData(this.shaderData);
}

private _removeEntity(entity: Entity): void {
const oldRootEntities = this._rootEntities;
oldRootEntities.splice(oldRootEntities.indexOf(entity), 1);
/**
* @internal
*/
_removeEntity(entity: Entity): void {
const rootEntities = this._rootEntities;
rootEntities.splice(rootEntities.indexOf(entity), 1);
}

//-----------------------------------------@deprecated-----------------------------------
Expand Down

0 comments on commit 648484a

Please sign in to comment.