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

398 lines
12 KiB
TypeScript
Raw Normal View History

2020-07-22 20:07:14 +08:00
module es {
export class Entity {
public static _idGenerator: number = 0;
2020-07-22 20:07:14 +08:00
/**
*
*/
public scene: Scene;
/**
*
*/
public name: string;
/**
*
*/
public readonly id: number;
/**
* //
*/
public readonly transform: Transform;
/**
*
*/
public readonly components: ComponentList;
2020-07-28 16:25:20 +08:00
/**
* entity update方法的频率12
*/
public updateInterval: number = 1;
public componentBits: BitSet;
constructor(name: string) {
this.components = new ComponentList(this);
this.transform = new Transform(this);
this.name = name;
this.id = Entity._idGenerator++;
if (Core.entitySystemsEnabled)
this.componentBits = new BitSet();
2020-07-28 16:25:20 +08:00
}
public _isDestroyed: boolean;
/**
* destroytrue
*/
public get isDestroyed() {
return this._isDestroyed;
}
private _tag: number = 0;
2020-07-22 20:07:14 +08:00
/**
* 使使
*/
public get tag(): number {
return this._tag;
}
2020-07-22 20:07:14 +08:00
/**
* 使使
* @param value
*/
public set tag(value: number) {
this.setTag(value);
}
2020-07-28 16:25:20 +08:00
private _enabled: boolean = true;
2020-07-22 20:07:14 +08:00
/**
* /
*/
public get enabled() {
return this._enabled;
}
2020-07-22 20:07:14 +08:00
/**
* /
* @param value
*/
public set enabled(value: boolean) {
this.setEnabled(value);
}
2020-07-08 18:12:17 +08:00
2020-07-28 16:25:20 +08:00
private _updateOrder: number = 0;
2020-07-22 20:07:14 +08:00
/**
* updateOrder还用于对scene.entities上的标签列表进行排序
*/
public get updateOrder() {
return this._updateOrder;
}
2020-06-08 23:04:57 +08:00
2020-07-22 20:07:14 +08:00
/**
* updateOrder还用于对scene.entities上的标签列表进行排序
* @param value
*/
public set updateOrder(value: number) {
this.setUpdateOrder(value);
}
2020-07-09 14:16:10 +08:00
2020-07-22 20:07:14 +08:00
public get parent(): Transform {
return this.transform.parent;
2020-06-08 18:26:05 +08:00
}
2020-07-22 20:07:14 +08:00
public set parent(value: Transform) {
this.transform.setParent(value);
}
2020-06-08 11:49:45 +08:00
2020-07-22 20:07:14 +08:00
public get childCount() {
return this.transform.childCount;
}
2020-06-09 22:32:18 +08:00
2020-07-22 20:07:14 +08:00
public get position(): Vector2 {
return this.transform.position;
}
2020-06-09 22:32:18 +08:00
2020-07-22 20:07:14 +08:00
public set position(value: Vector2) {
this.transform.setPosition(value.x, value.y);
}
2020-06-19 18:16:42 +08:00
public get localPosition(): Vector2 {
return this.transform.localPosition;
}
2020-07-28 16:25:20 +08:00
public set localPosition(value: Vector2) {
this.transform.setLocalPosition(value);
}
2020-07-22 20:07:14 +08:00
public get rotation(): number {
return this.transform.rotation;
}
2020-06-09 22:32:18 +08:00
2020-07-22 20:07:14 +08:00
public set rotation(value: number) {
this.transform.setRotation(value);
}
2020-07-09 14:16:10 +08:00
public get rotationDegrees(): number {
return this.transform.rotationDegrees;
}
2020-07-28 16:25:20 +08:00
public set rotationDegrees(value: number) {
this.transform.setRotationDegrees(value);
}
public get localRotation(): number {
return this.transform.localRotation;
}
2020-07-28 16:25:20 +08:00
public set localRotation(value: number) {
this.transform.setLocalRotation(value);
}
public get localRotationDegrees(): number {
return this.transform.localRotationDegrees;
}
2020-07-28 16:25:20 +08:00
public set localRotationDegrees(value: number) {
this.transform.setLocalRotationDegrees(value);
}
2020-06-08 16:23:48 +08:00
2020-07-22 20:07:14 +08:00
public get scale(): Vector2 {
return this.transform.scale;
}
2020-06-08 16:23:48 +08:00
2020-07-22 20:07:14 +08:00
public set scale(value: Vector2) {
this.transform.setScale(value);
}
2020-06-08 16:23:48 +08:00
public get localScale(): Vector2 {
return this.transform.localScale;
}
2020-07-28 16:25:20 +08:00
public set localScale(value: Vector2) {
this.transform.setLocalScale(value);
}
public get worldInverseTransform(): Matrix2D {
return this.transform.worldInverseTransform;
}
public get localToWorldTransform(): Matrix2D {
return this.transform.localToWorldTransform;
}
public get worldToLocalTransform(): Matrix2D {
return this.transform.worldToLocalTransform;
}
public onTransformChanged(comp: transform.Component) {
2020-07-22 20:07:14 +08:00
// 通知我们的子项改变了位置
this.components.onEntityTransformChanged(comp);
2020-06-08 16:23:48 +08:00
}
2020-06-08 11:49:45 +08:00
2020-07-22 20:07:14 +08:00
/**
*
* @param tag
*/
public setTag(tag: number): Entity {
if (this._tag != tag) {
// 我们只有在已经有场景的情况下才会调用entityTagList。如果我们还没有场景我们会被添加到entityTagList
if (this.scene)
this.scene.entities.removeFromTagList(this);
this._tag = tag;
if (this.scene)
this.scene.entities.addToTagList(this);
2020-06-09 22:32:18 +08:00
}
2020-07-22 20:07:14 +08:00
return this;
}
2020-06-09 22:32:18 +08:00
2020-07-22 20:07:14 +08:00
/**
*
* @param isEnabled
*/
public setEnabled(isEnabled: boolean) {
if (this._enabled != isEnabled) {
this._enabled = isEnabled;
if (this._enabled)
this.components.onEntityEnabled();
else
this.components.onEntityDisabled();
}
2020-06-08 11:49:45 +08:00
2020-07-22 20:07:14 +08:00
return this;
2020-06-08 11:49:45 +08:00
}
2020-07-22 20:07:14 +08:00
/**
* updateOrder还用于对scene.entities上的标签列表进行排序
* @param updateOrder
*/
public setUpdateOrder(updateOrder: number) {
if (this._updateOrder != updateOrder) {
this._updateOrder = updateOrder;
if (this.scene) {
this.scene.entities.markEntityListUnsorted();
this.scene.entities.markTagUnsorted(this.tag);
2020-07-22 20:07:14 +08:00
}
return this;
}
}
2020-06-08 23:04:57 +08:00
2020-07-22 20:07:14 +08:00
/**
*
*/
public destroy() {
this._isDestroyed = true;
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.destroy();
}
}
2020-06-08 23:04:57 +08:00
2020-07-22 20:07:14 +08:00
/**
* 下面的生命周期方法将被调用在组件上:OnRemovedFromEntity
*/
public detachFromScene() {
this.scene.entities.remove(this);
this.components.deregisterAllComponents();
2020-06-08 16:23:48 +08:00
2020-07-22 20:07:14 +08:00
for (let i = 0; i < this.transform.childCount; i++)
this.transform.getChild(i).entity.detachFromScene();
}
2020-07-22 20:07:14 +08:00
/**
*
* @param newScene
*/
public attachToScene(newScene: Scene) {
this.scene = newScene;
newScene.entities.add(this);
this.components.registerAllComponents();
for (let i = 0; i < this.transform.childCount; i++) {
this.transform.getChild(i).entity.attachToScene(newScene);
}
}
2020-07-22 20:07:14 +08:00
/**
*
*/
public onAddedToScene() {
}
2020-06-16 00:04:28 +08:00
2020-07-22 20:07:14 +08:00
/**
*
*/
public onRemovedFromScene() {
// 如果已经被销毁了,移走我们的组件。如果我们只是分离,我们需要保持我们的组件在实体上。
if (this._isDestroyed)
this.components.removeAllComponents();
}
2020-07-08 18:12:17 +08:00
2020-07-22 20:07:14 +08:00
/**
*
*/
public update() {
this.components.update();
}
2020-07-22 20:07:14 +08:00
/**
*
* @param component
*/
public addComponent<T extends Component>(component: T): T {
component.entity = this;
this.components.add(component);
component.initialize();
return component;
}
2020-07-22 20:07:14 +08:00
/**
* T的第一个组件并返回它null
* @param type
*/
public getComponent<T extends Component>(type): T {
return this.components.getComponent(type, false) as T;
}
2020-07-22 20:07:14 +08:00
/**
*
* @param type
*/
public hasComponent<T extends Component>(type) {
return this.components.getComponent<T>(type, false) != null;
}
2020-07-22 20:07:14 +08:00
/**
* T的第一个组件并返回它
* @param type
*/
public getOrCreateComponent<T extends Component>(type: T) {
2020-11-26 17:26:49 +08:00
let comp = this.components.getComponent<T>(TypeUtils.getType(type), true);
2020-07-22 20:07:14 +08:00
if (!comp) {
comp = this.addComponent<T>(type);
}
2020-06-08 16:23:48 +08:00
2020-07-22 20:07:14 +08:00
return comp;
}
2020-06-08 23:04:57 +08:00
2020-07-22 20:07:14 +08:00
/**
* typeName类型的所有组件使
* @param typeName
* @param componentList
*/
public getComponents(typeName: any, componentList?) {
2020-07-22 20:07:14 +08:00
return this.components.getComponents(typeName, componentList);
}
2020-06-08 23:04:57 +08:00
2020-07-22 20:07:14 +08:00
/**
*
* @param component
*/
public removeComponent(component: Component) {
this.components.remove(component);
}
2020-06-08 23:04:57 +08:00
2020-07-22 20:07:14 +08:00
/**
* T的第一个组件
* @param type
*/
public removeComponentForType<T extends Component>(type) {
let comp = this.getComponent<T>(type);
if (comp) {
this.removeComponent(comp);
return true;
}
2020-07-09 14:16:10 +08:00
2020-07-22 20:07:14 +08:00
return false;
}
2020-06-08 11:49:45 +08:00
2020-07-22 20:07:14 +08:00
/**
*
*/
public removeAllComponents() {
for (let i = 0; i < this.components.count; i++) {
this.removeComponent(this.components.buffer[i]);
}
}
2020-07-09 14:16:10 +08:00
public compareTo(other: Entity): number {
let compare = this._updateOrder - other._updateOrder;
if (compare == 0)
compare = this.id - other.id;
return compare;
}
2020-07-22 20:07:14 +08:00
public toString(): string {
return `[Entity: name: ${this.name}, tag: ${this.tag}, enabled: ${this.enabled}, depth: ${this.updateOrder}]`;
2020-06-08 11:49:45 +08:00
}
}
2020-07-08 18:12:17 +08:00
}