2020-06-08 11:49:45 +08:00
|
|
|
class Entity {
|
|
|
|
|
public name: string;
|
|
|
|
|
/** 当前实体所属的场景 */
|
|
|
|
|
public scene: Scene;
|
|
|
|
|
/** 封装实体的位置/旋转/缩放,并允许设置一个高层结构 */
|
|
|
|
|
public readonly transform: Transform;
|
2020-06-08 16:23:48 +08:00
|
|
|
/** 当前附加到此实体的所有组件的列表 */
|
|
|
|
|
public readonly components: Component[];
|
|
|
|
|
private _updateOrder: number = 0;
|
2020-06-08 11:49:45 +08:00
|
|
|
|
|
|
|
|
constructor(name: string){
|
|
|
|
|
this.name = name;
|
|
|
|
|
this.transform = new Transform(this);
|
2020-06-08 16:23:48 +08:00
|
|
|
this.components = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get updateOrder(){
|
|
|
|
|
return this._updateOrder;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public set updateOrder(value: number){
|
|
|
|
|
this.setUpdateOrder(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public setUpdateOrder(updateOrder: number){
|
|
|
|
|
if (this._updateOrder != updateOrder){
|
|
|
|
|
this._updateOrder = updateOrder;
|
|
|
|
|
if (this.scene){
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2020-06-08 11:49:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public attachToScene(newScene: Scene){
|
|
|
|
|
this.scene = newScene;
|
|
|
|
|
newScene.entities.push(this);
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < this.transform.childCount; i ++){
|
|
|
|
|
this.transform.getChild(i).entity.attachToScene(newScene);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-08 16:23:48 +08:00
|
|
|
public addComponent<T extends Component>(component: T): T{
|
|
|
|
|
component.entity = this;
|
|
|
|
|
this.components.push(component);
|
|
|
|
|
component.initialize();
|
|
|
|
|
return component;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public update(){
|
|
|
|
|
this.components.forEach(component => component.update());
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-08 11:49:45 +08:00
|
|
|
public destory(){
|
|
|
|
|
this.scene.entities.remove(this);
|
|
|
|
|
this.transform.parent = null;
|
|
|
|
|
|
|
|
|
|
for (let i = this.transform.childCount - 1; i >= 0; i --){
|
|
|
|
|
let child = this.transform.getChild(i);
|
|
|
|
|
child.entity.destory();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|