Files
esengine/source/src/ECS/Entity.ts

267 lines
6.6 KiB
TypeScript
Raw Normal View History

2020-06-08 11:49:45 +08:00
class Entity {
2020-06-09 22:32:18 +08:00
private static _idGenerator: number;
2020-06-08 11:49:45 +08:00
public name: string;
2020-06-09 22:32:18 +08:00
public readonly id: number;
2020-06-08 11:49:45 +08:00
/** 当前实体所属的场景 */
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-09 22:32:18 +08:00
private _tag: number = 0;
2020-06-08 18:26:05 +08:00
2020-06-08 20:11:58 +08:00
public componentBits: BitSet;
public get parent(){
return this.transform.parent;
}
public set parent(value: Transform){
this.transform.setParent(value);
}
public get position(){
return this.transform.position;
}
public set position(value: Vector2){
this.transform.setPosition(value);
}
public get localPosition(){
return this.transform.localPosition;
}
public set localPosition(value: Vector2){
this.transform.setLocalPosition(value);
}
public get rotation(){
return this.transform.rotation;
}
public set rotation(value: number){
this.transform.setRotation(value);
}
public get rotationDegrees(){
return this.transform.rotationDegrees;
}
public set rotationDegrees(value: number){
this.transform.setRotationDegrees(value);
}
public get localRotation(){
return this.transform.localRotation;
}
public set localRotation(value: number){
this.transform.setLocalRotation(value);
}
public get localRotationDegrees(){
return this.transform.localRotationDegrees;
}
public set localRotationDegrees(value: number){
this.transform.setLocalRotationDegrees(value);
}
public get scale(){
return this.transform.scale;
}
public set scale(value: Vector2){
this.transform.setScale(value);
}
public get localScale(){
return this.transform.scale;
}
public set localScale(value: Vector2){
this.transform.setScale(value);
}
public get worldInverseTransform(){
return this.transform.worldInverseTransform;
}
public get localToWorldTransform(){
return this.transform.localToWorldTransform;
}
public get worldToLocalTransform(){
return this.transform.worldToLocalTransform;
}
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
2020-06-09 22:32:18 +08:00
public get tag(){
return this._tag;
}
public set tag(value: number){
this.setTag(value);
}
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-09 22:32:18 +08:00
this.id = Entity._idGenerator ++;
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
}
2020-06-09 22:32:18 +08:00
public setTag(tag: number): Entity{
if (this._tag != tag){
if (this.scene){
this.scene.entities.removeFromTagList(this);
}
this._tag = tag;
if (this.scene){
this.scene.entities.addToTagList(this);
}
}
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;
}
public hasComponent<T extends Component>(type){
return this.components.getComponent<T>(type, false) != null;
}
public getOrCreateComponent<T extends Component>(type: T){
let comp = this.components.getComponent<T>(type, true);
if (!comp){
comp = this.addComponent<T>(type);
}
return comp;
}
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
}
public getComponents(typeName: string, componentList?){
return this.components.getComponents(typeName, componentList);
2020-06-16 00:04:28 +08:00
}
public removeComponentForType<T extends Component>(type){
let comp = this.getComponent<T>(type);
if (comp){
this.removeComponent(comp);
return true;
}
return false;
}
public removeComponent(component: Component){
this.components.remove(component);
}
public removeAllComponents(){
for (let i = 0; i < this.components.count; i ++){
this.removeComponent(this.components.buffer[i]);
}
}
2020-06-08 16:23:48 +08:00
public update(){
2020-06-08 23:04:57 +08:00
this.components.update();
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();
}
}
}