Skip to content

Commit

Permalink
refactor: opt code
Browse files Browse the repository at this point in the history
  • Loading branch information
GuoLei1990 committed Feb 6, 2025
1 parent c5ca418 commit dd4a609
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
13 changes: 8 additions & 5 deletions packages/core/src/Scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,15 +424,18 @@ export class Scene extends EngineObject {

const searchRootName = splits.shift();
for (let i = 0, n = this.rootEntitiesCount; i < n; i++) {
let rootEntity = this.getRootEntity(i);
if (rootEntity.name != searchRootName) {
let findEntity = this.getRootEntity(i);
if (findEntity.name !== searchRootName) {
continue;
}

const target = Entity._findChildByName(rootEntity, 0, splits, 0);
if (target) {
return target;
if (splits.length) {
findEntity = Entity._findChildByName(findEntity, 0, splits, 0);
if (!findEntity) {
continue;
}

Check warning on line 436 in packages/core/src/Scene.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/Scene.ts#L435-L436

Added lines #L435 - L436 were not covered by tests
}
return findEntity;
}
return null;
}
Expand Down
23 changes: 20 additions & 3 deletions tests/src/core/Entity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,22 @@ describe("Entity", async () => {
const child = new Entity(engine, "child");
child.parent = parent;


expect(scene.findEntityByPath("")).eq(null);

expect(scene.findEntityByPath("root")).eq(scene.getRootEntity());

expect(scene.findEntityByPath("root/parent")).eq(parent);

expect(scene.findEntityByPath("root/parent/child")).eq(child);



});
it("not found", () => {
const parent = new Entity(engine, "parent");

parent.parent = scene.getRootEntity();

const child = new Entity(engine, "child");
child.parent = parent;

Expand Down Expand Up @@ -286,10 +294,19 @@ describe("Entity", async () => {
parent.parent = scene.getRootEntity();
const child = new Entity(engine, "child");
child.parent = parent;
const child2 = new Entity(engine, "child2");
const child2 = new Entity(engine, "child");
child2.parent = parent;
const child3 = new Entity(engine, "child");
child3.parent = parent;

const grandson = new Entity(engine, "grandsonX");
grandson.parent = child;
const grandson2 = new Entity(engine, "grandson");
grandson2.parent = child2;


expect(parent.findByPath("/child")).eq(child);
expect(parent.findByPath("child2")).eq(child2);
expect(parent.findByPath("child/grandson")).eq(grandson2);
});

it("clearChildren", () => {
Expand Down

0 comments on commit dd4a609

Please # to comment.