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
|
|
|
/** 当前附加到此实体的所有组件的列表 */
|
2020-06-08 23:04:57 +08:00
|
|
|
public readonly components: ComponentList;
|
2020-06-08 16:23:48 +08:00
|
|
|
private _updateOrder: number = 0;
|
2020-06-08 18:26:05 +08:00
|
|
|
private _enabled: boolean = true;
|
2020-06-08 23:04:57 +08:00
|
|
|
private _isDestoryed: boolean;
|
2020-06-08 18:26:05 +08:00
|
|
|
|
2020-06-08 20:11:58 +08:00
|
|
|
public componentBits: BitSet;
|
|
|
|
|
|
2020-06-08 23:04:57 +08:00
|
|
|
public get isDestoryed(){
|
|
|
|
|
return this._isDestoryed;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-08 18:26:05 +08:00
|
|
|
public get enabled(){
|
|
|
|
|
return this._enabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public set enabled(value: boolean){
|
|
|
|
|
this.setEnabled(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public setEnabled(isEnabled: boolean){
|
|
|
|
|
if (this._enabled != isEnabled){
|
|
|
|
|
this._enabled = isEnabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2020-06-08 11:49:45 +08:00
|
|
|
|
|
|
|
|
constructor(name: string){
|
|
|
|
|
this.name = name;
|
|
|
|
|
this.transform = new Transform(this);
|
2020-06-08 23:04:57 +08:00
|
|
|
this.components = new ComponentList(this);
|
2020-06-08 20:11:58 +08:00
|
|
|
this.componentBits = new BitSet();
|
2020-06-08 16:23:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2020-06-08 20:11:58 +08:00
|
|
|
newScene.entities.add(this);
|
2020-06-08 23:04:57 +08:00
|
|
|
this.components.registerAllComponents();
|
2020-06-08 11:49:45 +08:00
|
|
|
|
|
|
|
|
for (let i = 0; i < this.transform.childCount; i ++){
|
|
|
|
|
this.transform.getChild(i).entity.attachToScene(newScene);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-08 23:04:57 +08:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-08 16:23:48 +08:00
|
|
|
public addComponent<T extends Component>(component: T): T{
|
|
|
|
|
component.entity = this;
|
2020-06-08 23:04:57 +08:00
|
|
|
this.components.add(component);
|
2020-06-08 16:23:48 +08:00
|
|
|
component.initialize();
|
|
|
|
|
return component;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-08 21:53:09 +08:00
|
|
|
public getComponent<T extends Component>(type): T{
|
2020-06-08 23:04:57 +08:00
|
|
|
return this.components.getComponent(type, false) as T;
|
2020-06-08 18:26:05 +08:00
|
|
|
}
|
|
|
|
|
|
2020-06-08 16:23:48 +08:00
|
|
|
public update(){
|
2020-06-08 23:04:57 +08:00
|
|
|
this.components.update();
|
2020-06-08 17:18:21 +08:00
|
|
|
this.transform.updateTransform();
|
2020-06-08 16:23:48 +08:00
|
|
|
}
|
|
|
|
|
|
2020-06-08 23:04:57 +08:00
|
|
|
public onAddedToScene(){
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public onRemovedFromScene(){
|
|
|
|
|
if (this._isDestoryed)
|
|
|
|
|
this.components.remove
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public onTransformChanged(comp: ComponentTransform){
|
|
|
|
|
this.components.onEntityTransformChanged(comp);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-08 11:49:45 +08:00
|
|
|
public destory(){
|
2020-06-08 23:04:57 +08:00
|
|
|
this._isDestoryed = true;
|
2020-06-08 11:49:45 +08:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|