refactor(core): 重构Scene和Entity的代码质量,消除所有Lint警告 (#209)

This commit is contained in:
YHH
2025-11-01 17:18:12 +08:00
committed by GitHub
parent 3ad5dc9ca3
commit 4355538d8d
2 changed files with 125 additions and 66 deletions

View File

@@ -6,6 +6,13 @@ import { createLogger } from '../Utils/Logger';
import { getComponentInstanceTypeName, getComponentTypeName } from './Decorators';
import type { IScene } from './IScene';
/**
* 组件活跃状态变化接口
*/
interface IActiveChangeable {
onActiveChanged(): void;
}
/**
* 实体比较器
*
@@ -69,19 +76,6 @@ export class Entity {
*/
public static eventBus: EventBus | null = null;
/**
* 通知Scene中的QuerySystem实体组件发生变动
*
* @param entity 发生组件变动的实体
*/
private static notifyQuerySystems(entity: Entity): void {
// 只通知Scene中的QuerySystem
if (entity.scene && entity.scene.querySystem) {
entity.scene.querySystem.updateEntity(entity);
entity.scene.clearSystemEntityCaches();
}
}
/**
* 实体名称
*/
@@ -352,7 +346,11 @@ export class Entity {
* const health = entity.createComponent(Health, 100);
* ```
*/
public createComponent<T extends Component>(componentType: ComponentType<T>, ...args: any[]): T {
public createComponent<T extends Component>(
componentType: ComponentType<T>,
...args: ConstructorParameters<ComponentType<T>>
): T {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const component = new componentType(...args);
return this.addComponent(component);
}
@@ -380,6 +378,16 @@ export class Entity {
return component;
}
/**
* 通知Scene中的QuerySystem实体组件发生变动
*/
private notifyQuerySystems(): void {
if (this.scene && this.scene.querySystem) {
this.scene.querySystem.updateEntity(this);
this.scene.clearSystemEntityCaches();
}
}
/**
* 添加组件到实体
*
@@ -432,8 +440,7 @@ export class Entity {
});
}
// 通知所有相关的QuerySystem组件已变动
Entity.notifyQuerySystems(this);
this.notifyQuerySystems();
return component;
}
@@ -507,9 +514,13 @@ export class Entity {
* position.x = 100;
* ```
*/
public getOrCreateComponent<T extends Component>(type: ComponentType<T>, ...args: any[]): T {
public getOrCreateComponent<T extends Component>(
type: ComponentType<T>,
...args: ConstructorParameters<ComponentType<T>>
): T {
let component = this.getComponent(type);
if (!component) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
component = this.createComponent(type, ...args);
}
return component;
@@ -562,8 +573,7 @@ export class Entity {
});
}
// 通知所有相关的QuerySystem组件已变动
Entity.notifyQuerySystems(this);
this.notifyQuerySystems();
}
/**
@@ -603,8 +613,7 @@ export class Entity {
component.onRemovedFromEntity();
}
// 通知所有相关的QuerySystem组件已全部移除
Entity.notifyQuerySystems(this);
this.notifyQuerySystems();
}
/**
@@ -804,11 +813,10 @@ export class Entity {
* @returns 层次结构的根实体
*/
public getRoot(): Entity {
let root: Entity = this;
while (root._parent) {
root = root._parent;
if (!this._parent) {
return this;
}
return root;
return this._parent.getRoot();
}
/**
@@ -874,7 +882,7 @@ export class Entity {
private onActiveChanged(): void {
for (const component of this.components) {
if ('onActiveChanged' in component && typeof component.onActiveChanged === 'function') {
(component as any).onActiveChanged();
(component as IActiveChangeable).onActiveChanged();
}
}
@@ -1002,7 +1010,7 @@ export class Entity {
childIds: number[];
depth: number;
cacheBuilt: boolean;
} {
} {
return {
name: this.name,
id: this.id,