新增componentlist管理组件列表
This commit is contained in:
@@ -5,12 +5,17 @@ class Entity {
|
||||
/** 封装实体的位置/旋转/缩放,并允许设置一个高层结构 */
|
||||
public readonly transform: Transform;
|
||||
/** 当前附加到此实体的所有组件的列表 */
|
||||
public readonly components: Component[];
|
||||
public readonly components: ComponentList;
|
||||
private _updateOrder: number = 0;
|
||||
private _enabled: boolean = true;
|
||||
private _isDestoryed: boolean;
|
||||
|
||||
public componentBits: BitSet;
|
||||
|
||||
public get isDestoryed(){
|
||||
return this._isDestoryed;
|
||||
}
|
||||
|
||||
public get enabled(){
|
||||
return this._enabled;
|
||||
}
|
||||
@@ -30,7 +35,7 @@ class Entity {
|
||||
constructor(name: string){
|
||||
this.name = name;
|
||||
this.transform = new Transform(this);
|
||||
this.components = [];
|
||||
this.components = new ComponentList(this);
|
||||
this.componentBits = new BitSet();
|
||||
}
|
||||
|
||||
@@ -56,30 +61,52 @@ class Entity {
|
||||
public attachToScene(newScene: Scene){
|
||||
this.scene = newScene;
|
||||
newScene.entities.add(this);
|
||||
this.components.forEach(component => component.registerComponent());
|
||||
this.components.registerAllComponents();
|
||||
|
||||
for (let i = 0; i < this.transform.childCount; i ++){
|
||||
this.transform.getChild(i).entity.attachToScene(newScene);
|
||||
}
|
||||
}
|
||||
|
||||
public detachFromScene(){
|
||||
this.scene.entities.remove(this);
|
||||
this.components.deregisterAllComponents();
|
||||
|
||||
for (let i = 0; i < this.transform.childCount; i ++)
|
||||
this.transform.getChild(i).entity.detachFromScene();
|
||||
}
|
||||
|
||||
public addComponent<T extends Component>(component: T): T{
|
||||
component.entity = this;
|
||||
this.components.push(component);
|
||||
this.components.add(component);
|
||||
component.initialize();
|
||||
return component;
|
||||
}
|
||||
|
||||
public getComponent<T extends Component>(type): T{
|
||||
return this.components.firstOrDefault(component => component instanceof type) as T;
|
||||
return this.components.getComponent(type, false) as T;
|
||||
}
|
||||
|
||||
public update(){
|
||||
this.components.forEach(component => component.update());
|
||||
this.components.update();
|
||||
this.transform.updateTransform();
|
||||
}
|
||||
|
||||
public onAddedToScene(){
|
||||
|
||||
}
|
||||
|
||||
public onRemovedFromScene(){
|
||||
if (this._isDestoryed)
|
||||
this.components.remove
|
||||
}
|
||||
|
||||
public onTransformChanged(comp: ComponentTransform){
|
||||
this.components.onEntityTransformChanged(comp);
|
||||
}
|
||||
|
||||
public destory(){
|
||||
this._isDestoryed = true;
|
||||
this.scene.entities.remove(this);
|
||||
this.transform.parent = null;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user