Skip to content
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

Optimize the logic of remove and add entity. #1930

Open
wants to merge 1 commit 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
24 changes: 15 additions & 9 deletions packages/core/src/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,11 +516,13 @@ export class Entity extends EngineObject {
const oldParent = this._parent;
if (oldParent != null) {
const oldSibling = oldParent._children;
let index = this._siblingIndex;
oldSibling.splice(index, 1);
for (let n = oldSibling.length; index < n; index++) {
oldSibling[index]._siblingIndex--;
const count = oldSibling.length - 1;
for (let i = this._siblingIndex; i < count; i++) {
const child = oldSibling[i + 1];
oldSibling[i] = child;
child._siblingIndex = i;
}
oldSibling.length = count;
this._parent = null;
this._siblingIndex = -1;
}
Expand Down Expand Up @@ -554,17 +556,21 @@ export class Entity extends EngineObject {
const children = this._children;
const childCount = children.length;
if (index === undefined) {
children.length = childCount + 1;
child._siblingIndex = childCount;
children.push(child);
children[childCount] = child;
} else {
if (index < 0 || index > childCount) {
throw `The index ${index} is out of child list bounds ${childCount}`;
}
child._siblingIndex = index;
children.splice(index, 0, child);
for (let i = index + 1, n = childCount + 1; i < n; i++) {
children[i]._siblingIndex++;
children.length = childCount + 1;
for (let i = childCount; i > index; i--) {
const swapChild = children[i - 1];
swapChild._siblingIndex = i;
children[i] = swapChild;
}
child._siblingIndex = index;
children[index] = child;
}
}

Expand Down
24 changes: 15 additions & 9 deletions packages/core/src/Scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,11 +455,13 @@ export class Scene extends EngineObject {
*/
_removeFromEntityList(entity: Entity): void {
const rootEntities = this._rootEntities;
let index = entity._siblingIndex;
rootEntities.splice(index, 1);
for (let n = rootEntities.length; index < n; index++) {
rootEntities[index]._siblingIndex--;
const count = rootEntities.length - 1;
for (let i = entity._siblingIndex; i < count; i++) {
const child = rootEntities[i + 1];
rootEntities[i] = child;
child._siblingIndex = i;
}
rootEntities.length = count;
entity._siblingIndex = -1;
}

Expand Down Expand Up @@ -489,17 +491,21 @@ export class Scene extends EngineObject {
const rootEntities = this._rootEntities;
const rootEntityCount = rootEntities.length;
if (index === undefined) {
rootEntities.length = rootEntityCount + 1;
rootEntity._siblingIndex = rootEntityCount;
rootEntities.push(rootEntity);
rootEntities[rootEntityCount] = rootEntity;
} else {
if (index < 0 || index > rootEntityCount) {
throw `The index ${index} is out of child list bounds ${rootEntityCount}`;
}
rootEntity._siblingIndex = index;
rootEntities.splice(index, 0, rootEntity);
for (let i = index + 1, n = rootEntityCount + 1; i < n; i++) {
rootEntities[i]._siblingIndex++;
rootEntities.length = rootEntityCount + 1;
for (let i = rootEntityCount; i > index; i--) {
const swapRoot = rootEntities[i - 1];
swapRoot._siblingIndex = i;
rootEntities[i] = swapRoot;
}
rootEntity._siblingIndex = index;
rootEntities[index] = rootEntity;
}
}

Expand Down