2020-06-29 15:41:02 +08:00
|
|
|
class Entity extends egret.DisplayObjectContainer {
|
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;
|
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-22 15:27:58 +08:00
|
|
|
public _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;
|
|
|
|
|
|
2020-06-29 15:41:02 +08:00
|
|
|
public get isDestoryed(){
|
|
|
|
|
return this._isDestoryed;
|
2020-06-09 11:09:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get position(){
|
2020-07-08 18:12:17 +08:00
|
|
|
return new Vector2(this.x, this.y);
|
2020-06-09 11:09:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public set position(value: Vector2){
|
2020-07-08 18:12:17 +08:00
|
|
|
this.$setX(value.x);
|
|
|
|
|
this.$setY(value.y);
|
|
|
|
|
this.onEntityTransformChanged(TransformComponent.position);
|
2020-06-09 11:09:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get scale(){
|
2020-06-29 15:41:02 +08:00
|
|
|
return new Vector2(this.scaleX, this.scaleY);
|
2020-06-09 11:09:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public set scale(value: Vector2){
|
2020-07-08 18:12:17 +08:00
|
|
|
this.$setScaleX(value.x);
|
|
|
|
|
this.$setScaleY(value.y);
|
|
|
|
|
this.onEntityTransformChanged(TransformComponent.scale);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public set rotation(value: number){
|
|
|
|
|
this.$setRotation(value);
|
|
|
|
|
this.onEntityTransformChanged(TransformComponent.rotation);
|
2020-06-08 23:04:57 +08:00
|
|
|
}
|
|
|
|
|
|
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-19 18:16:42 +08:00
|
|
|
public get stage(){
|
|
|
|
|
if (!this.scene)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return this.scene.stage;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-08 11:49:45 +08:00
|
|
|
constructor(name: string){
|
2020-06-29 15:41:02 +08:00
|
|
|
super();
|
2020-06-08 11:49:45 +08:00
|
|
|
this.name = name;
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-29 15:41:02 +08:00
|
|
|
public roundPosition(){
|
|
|
|
|
this.position = Vector2Ext.round(this.position);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-08 16:23:48 +08:00
|
|
|
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
|
|
|
|
2020-06-29 15:41:02 +08:00
|
|
|
for (let i = 0; i < this.numChildren; i ++){
|
|
|
|
|
(this.getChildAt(i) as Component).entity.attachToScene(newScene);
|
2020-06-08 11:49:45 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-08 23:04:57 +08:00
|
|
|
public detachFromScene(){
|
|
|
|
|
this.scene.entities.remove(this);
|
|
|
|
|
this.components.deregisterAllComponents();
|
|
|
|
|
|
2020-06-29 15:41:02 +08:00
|
|
|
for (let i = 0; i < this.numChildren; i ++)
|
|
|
|
|
(this.getChildAt(i) as Component).entity.detachFromScene();
|
2020-06-08 23:04:57 +08:00
|
|
|
}
|
|
|
|
|
|
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-29 15:41:02 +08:00
|
|
|
this.addChild(component);
|
2020-06-08 16:23:48 +08:00
|
|
|
component.initialize();
|
|
|
|
|
return component;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-09 11:09:26 +08:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
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-16 11:22:37 +08:00
|
|
|
public getComponents(typeName: string | any, componentList?){
|
2020-06-16 09:10:09 +08:00
|
|
|
return this.components.getComponents(typeName, componentList);
|
2020-06-16 00:04:28 +08:00
|
|
|
}
|
|
|
|
|
|
2020-07-08 18:12:17 +08:00
|
|
|
private onEntityTransformChanged(comp: TransformComponent){
|
|
|
|
|
this.components.onEntityTransformChanged(comp);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-09 11:09:26 +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();
|
2020-06-08 16:23:48 +08:00
|
|
|
}
|
|
|
|
|
|
2020-06-08 23:04:57 +08:00
|
|
|
public onAddedToScene(){
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public onRemovedFromScene(){
|
|
|
|
|
if (this._isDestoryed)
|
2020-06-22 15:27:58 +08:00
|
|
|
this.components.removeAllComponents();
|
2020-06-08 23:04:57 +08:00
|
|
|
}
|
|
|
|
|
|
2020-06-29 15:41:02 +08:00
|
|
|
public destroy(){
|
2020-06-08 23:04:57 +08:00
|
|
|
this._isDestoryed = true;
|
2020-06-08 11:49:45 +08:00
|
|
|
this.scene.entities.remove(this);
|
2020-06-29 15:41:02 +08:00
|
|
|
this.removeChildren();
|
2020-06-08 11:49:45 +08:00
|
|
|
|
2020-06-29 15:41:02 +08:00
|
|
|
for (let i = this.numChildren - 1; i >= 0; i --){
|
|
|
|
|
let child = this.getChildAt(i);
|
|
|
|
|
(child as Component).entity.destroy();
|
2020-06-08 11:49:45 +08:00
|
|
|
}
|
|
|
|
|
}
|
2020-07-08 18:12:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum TransformComponent {
|
|
|
|
|
rotation,
|
|
|
|
|
scale,
|
|
|
|
|
position
|
2020-06-08 11:49:45 +08:00
|
|
|
}
|