8000 Fix `isRoot` was not set correctly when set `entity.parent` by cptbtptpbcptdtptp · Pull Request #2597 · galacean/engine · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix isRoot was not set correctly when set entity.parent #2597

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
268 changes: 126 additions & 142 deletions packages/core/src/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,111 @@
* Entity, be used as components container.
*/
export class Entity extends EngineObject {
/**
* @internal
*/
static _moveEntityTo(entity: Entity, parent: Entity = null, scene: Scene = null, index?: number): void {
const { _parent: oldParent, _scene: oldScene } = entity;
if (oldScene !== scene) {
if (oldParent) {
Entity._removeFromChildren(oldParent._children, entity);
oldParent._dispatchModify(EntityModifyFlags.Child, oldParent);

Check warning on line 30 in packages/core/src/Entity.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/Entity.ts#L29-L30

Added lines #L29 - L30 were not covered by tests
} else if (oldScene) {
Entity._removeFromChildren(oldScene._rootEntities, entity);
}

Check warning on line 33 in packages/core/src/Entity.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/Entity.ts#L32-L33

Added lines #L32 - L33 were not covered by tests
const isActiveInHierarchy = entity._isActiveInHierarchy;
const isActiveInScene = entity._isActiveInScene;
// Cross scene should inActive first and then active
let inActiveChangeFlag = isActiveInScene ? ActiveChangeFlag.Scene : ActiveChangeFlag.None;
let activeChangeFlag = ActiveChangeFlag.None;
if (parent) {
entity._isRoot = false;
Entity._addToChildren(parent._children, entity, index);
parent._dispatchModify(EntityModifyFlags.Child, parent);
if (isActiveInHierarchy && !parent._isActiveInHierarchy) {
inActiveChangeFlag |= ActiveChangeFlag.Hierarchy;
}

Check warning on line 45 in packages/core/src/Entity.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/Entity.ts#L44-L45

Added lines #L44 - L45 were not covered by tests
if (entity._isActive) {
if (!isActiveInHierarchy && parent._isActiveInHierarchy) {
activeChangeFlag |= ActiveChangeFlag.Hierarchy;
}
if (parent._isActiveInScene) {
activeChangeFlag |= ActiveChangeFlag.Scene;
}
}
} else if (scene) {
entity._isRoot = true;
Entity._addToChildren(scene._rootEntities, entity, index);
if (isActiveInHierarchy && !scene._isActiveInEngine) {
inActiveChangeFlag |= ActiveChangeFlag.Hierarchy;
}

Check warning on line 59 in packages/core/src/Entity.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/Entity.ts#L58-L59

Added lines #L58 - L59 were not covered by tests
if (entity._isActive) {
if (!isActiveInHierarchy && scene._isActiveInEngine) {
activeChangeFlag |= ActiveChangeFlag.Hierarchy;
}
activeChangeFlag |= ActiveChangeFlag.Scene;
}
} else {
entity._isRoot = false;
if (isActiveInHierarchy) {
inActiveChangeFlag |= ActiveChangeFlag.Hierarchy;
}
}

Check warning on line 71 in packages/core/src/Entity.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/Entity.ts#L67-L71

Added lines #L67 - L71 were not covered by tests
inActiveChangeFlag && entity._processInActive(inActiveChangeFlag);
Entity._traverseSetOwnerScene(entity, scene);
entity._parent = parent;
entity._onParentChange();
activeChangeFlag && entity._processActive(activeChangeFlag);
} else {
if (oldParent !== parent) {
if (oldParent) {
Entity._removeFromChildren(oldParent._children, entity);
oldParent._dispatchModify(EntityModifyFlags.Child, oldParent);
}
const isActiveInHierarchy = entity._isActiveInHierarchy;
const isActiveInScene = entity._isActiveInScene;
let inActiveChangeFlag = ActiveChangeFlag.None;
let activeChangeFlag = ActiveChangeFlag.None;
if (parent) {
entity._isRoot = false;
Entity._addToChildren(parent._children, entity, index);
parent._dispatchModify(EntityModifyFlags.Child, parent);
if (isActiveInHierarchy && !parent._isActiveInHierarchy) {
inActiveChangeFlag |= ActiveChangeFlag.Hierarchy;
}
if (isActiveInScene && !parent._isActiveInScene) {
inActiveChangeFlag |= ActiveChangeFlag.Scene;
}
if (entity._isActive) {
if (!isActiveInHierarchy && parent._isActiveInHierarchy) {
activeChangeFlag |= ActiveChangeFlag.Hierarchy;
}
if (!isActiveInScene && parent._isActiveInScene) {
activeChangeFlag |= ActiveChangeFlag.Scene;
}
}
} else if (scene) {
entity._isRoot = true;
Entity._addToChildren(scene._rootEntities, entity, index);
if (isActiveInHierarchy && !scene._isActiveInEngine) {
inActiveChangeFlag |= ActiveChangeFlag.Hierarchy;
}
if (entity._isActive && !isActiveInHierarchy && scene._isActiveInEngine) {
activeChangeFlag |= ActiveChangeFlag.Hierarchy;
}
} else {
entity._isRoot = false;
}
inActiveChangeFlag && entity._processInActive(inActiveChangeFlag);
entity._parent = parent;
entity._onParentChange();
activeChangeFlag && entity._processActive(activeChangeFlag);
} else {
!!parent && index !== undefined && (entity.siblingIndex = index);
}
}

Check warning on line 124 in packages/core/src/Entity.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/Entity.ts#L78-L124

Added lines #L78 - L124 were not covered by tests
}

/**
* @internal
*/
Expand Down Expand Up @@ -203,7 +308,7 @@
}

set parent(value: Entity) {
this._setParent(value);
this.parent !== value && Entity._moveEntityTo(this, value, value?._scene);
}

/**
Expand Down Expand Up @@ -239,8 +344,13 @@
if (this._siblingIndex === -1) {
throw `The entity ${this.name} is not in the hierarchy`;
}

this._setSiblingIndex(this._isRoot ? this._scene._rootEntities : this._parent._children, value);
if (this._isRoot) {
this._setSiblingIndex(this._scene._rootEntities, value);

Check warning on line 348 in packages/core/src/Entity.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/Entity.ts#L348

Added line #L348 was not covered by tests
} else {
const parent = this._parent;
this._setSiblingIndex(parent._children, value);
parent._dispatchModify(EntityModifyFlags.Child, parent);
}
}

/**
Expand Down Expand Up @@ -343,57 +453,10 @@
addChild(index: number, child: Entity): void;

addChild(indexOrChild: number | Entity, child?: Entity): void {
let index: number;
if (typeof indexOrChild === "number") {
index = indexOrChild;
} else {
index = undefined;
child = indexOrChild;
}

if (child._isRoot) {
const oldScene = child._scene;
Entity._removeFromChildren(oldScene._rootEntities, child);
child._isRoot = false;

this._addToChildrenList(index, child);
child._parent = this;

const newScene = this._scene;

let inActiveChangeFlag = ActiveChangeFlag.None;
if (!this._isActiveInHierarchy) {
child._isActiveInHierarchy && (inActiveChangeFlag |= ActiveChangeFlag.Hierarchy);
}
if (child._isActiveInScene) {
if (this._isActiveInScene) {
// Cross scene should inActive first and then active
oldScene !== newScene && (inActiveChangeFlag |= ActiveChangeFlag.Scene);
} else {
inActiveChangeFlag |= ActiveChangeFlag.Scene;
}
}

inActiveChangeFlag && child._processInActive(inActiveChangeFlag);

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

let activeChangeFlag = ActiveChangeFlag.None;
if (child._isActive) {
if (this._isActiveInHierarchy) {
!child._isActiveInHierarchy && (activeChangeFlag |= ActiveChangeFlag.Hierarchy);
}
if (this._isActiveInScene) {
(!child._isActiveInScene || oldScene !== newScene) && (activeChangeFlag |= ActiveChangeFlag.Scene);
}
}
activeChangeFlag && child._processActive(activeChangeFlag);

child._setParentChange();
Entity._moveEntityTo(child, this, this._scene, indexOrChild);

Check warning on line 457 in packages/core/src/Entity.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/Entity.ts#L457

Added line #L457 was not covered by tests
} else {
child._setParent(this, index);
Entity._moveEntityTo(indexOrChild, this, this._scene);
}
}

Expand All @@ -402,7 +465,8 @@
* @param child - The child entity which want to be removed
*/
removeChild(child: Entity): void {
child._setParent(null);
if (child._parent !== this) return;
Entity._moveEntityTo(child);
}

/**
Expand Down Expand Up @@ -459,7 +523,7 @@
? new Entity(this.engine, name, transform.constructor as ComponentConstructor)
: new Entity(this.engine, name);
child.layer = this.layer;
child.parent = this;
Entity._moveEntityTo(child, this, this._scene);
return child;
}

Expand All @@ -471,13 +535,12 @@
for (let i = children.length - 1; i >= 0; i--) {
const child = children[i];
child._parent = null;

let activeChangeFlag = ActiveChangeFlag.None;
child._isActiveInHierarchy && (activeChangeFlag |= ActiveChangeFlag.Hierarchy);
child._isActiveInScene && (activeChangeFlag |= ActiveChangeFlag.Scene);
activeChangeFlag && child._processInActive(activeChangeFlag);

Entity._traverseSetOwnerScene(child, null); // Must after child._processInActive().
let inActiveChangeFlag = ActiveChangeFlag.None;
child._isActiveInHierarchy && (inActiveChangeFlag |= ActiveChangeFlag.Hierarchy);
child._isActiveInScene && (inActiveChangeFlag |= ActiveChangeFlag.Scene);
inActiveChangeFlag && child._processInActive(inActiveChangeFlag);
// Must after child._processInActive().
Entity._traverseSetOwnerScene(child, null);
}
children.length = 0;
}
Expand Down Expand Up @@ -578,12 +641,7 @@
children[0].destroy();
}

if (this._isRoot) {
this._scene.removeRootEntity(this);
} else {
this._setParent(null);
}

Entity._moveEntityTo(this);
this.isActive = false;
}

Expand Down Expand Up @@ -613,18 +671,6 @@
script._entityScriptsIndex = -1;
}

/**
* @internal
*/
_removeFromParent(): void {
const oldParent = this._parent;
if (oldParent != null) {
Entity._removeFromChildren(oldParent._children, this);
this._parent = null;
this._dispatchModify(EntityModifyFlags.Child, oldParent);
}
}

/* 9E81 *
* @internal
*/
Expand Down Expand Up @@ -652,7 +698,7 @@
/**
* @internal
*/
_setParentChange() {
_onParentChange() {
this._transform._parentChange();
this._dispatchModify(EntityModifyFlags.Parent, this);
}
Expand All @@ -675,67 +721,6 @@
this._modifyFlagManager?.dispatch(flag, param);
}

private _addToChildrenList(index: number, child: Entity): void {
Entity._addToChildren(this._children, child, index);
this._dispatchModify(EntityModifyFlags.Child, this);
}

private _setParent(parent: Entity, siblingIndex?: number): void {
const oldParent = this._parent;
if (parent !== oldParent) {
this._removeFromParent();
this._parent = parent;
if (parent) {
parent._addToChildrenList(siblingIndex, this);

const oldScene = this._scene;
const parentScene = parent._scene;

let inActiveChangeFlag = ActiveChangeFlag.None;
if (!parent._isActiveInHierarchy) {
this._isActiveInHierarchy && (inActiveChangeFlag |= ActiveChangeFlag.Hierarchy);
}
if (parent._isActiveInScene) {
// cross scene should inActive first and then active
this._isActiveInScene && oldScene !== parentScene && (inActiveChangeFlag |= ActiveChangeFlag.Scene);
} else {
this._isActiveInScene && (inActiveChangeFlag |= ActiveChangeFlag.Scene);
}
inActiveChangeFlag && this._processInActive(inActiveChangeFlag);

if (oldScene !== parentScene) {
Entity._traverseSetOwnerScene(this, parentScene);
}

let activeChangeFlag = ActiveChangeFlag.None;

if (this._isActive) {
if (parent._isActiveInHierarchy) {
!this._isActiveInHierarchy && (activeChangeFlag |= ActiveChangeFlag.Hierarchy);
}
if (parent._isActiveInScene) {
(!this._isActiveInScene || oldScene !== parentScene) && (activeChangeFlag |= ActiveChangeFlag.Scene);
}
}

activeChangeFlag && this._processActive(activeChangeFlag);
} else {
let inActiveChangeFlag = ActiveChangeFlag.None;
this._isActiveInHierarchy && (inActiveChangeFlag |= ActiveChangeFlag.Hierarchy);
this._isActiveInScene && (inActiveChangeFlag |= ActiveChangeFlag.Scene);
inActiveChangeFlag && this._processInActive(inActiveChangeFlag);
if (oldParent) {
Entity._traverseSetOwnerScene(this, null);
}
}
this._setParentChange();
} else {
if (parent && siblingIndex !== undefined) {
this.siblingIndex = siblingIndex;
}
}
}

private _getComponentsInChildren<T extends Component>(type: new (entity: Entity) => T, results: T[]): void {
for (let i = this._components.length - 1; i >= 0; i--) {
const component = this._components[i];
Expand Down Expand Up @@ -808,7 +793,6 @@
}
}
}
this._dispatchModify(EntityModifyFlags.Child, this);
}

//--------------------------------------------------------------deprecated----------------------------------------------------------------
Expand Down
Loading
0