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

170 lines
5.2 KiB
TypeScript
Raw Normal View History

2020-07-22 20:07:14 +08:00
module es {
/**
*
* - onAddedToEntity
* - OnEnabled
*
*
* - onRemovedFromEntity
*/
export abstract class Component {
2021-04-20 15:46:18 +08:00
public static _idGenerator: number = 0;
/**
*
*/
public readonly id: number;
2020-07-22 20:07:14 +08:00
/**
*
*/
public entity: Entity;
2021-04-20 15:46:18 +08:00
constructor() {
this.id = Component._idGenerator++;
}
2020-07-22 20:07:14 +08:00
/**
* 访 this.entity.transform
*/
public get transform(): Transform {
return this.entity.transform;
}
2020-07-09 14:16:10 +08:00
2020-07-28 16:25:20 +08:00
private _enabled: boolean = true;
2020-07-22 20:07:14 +08:00
/**
* onEnabled/onDisable
*/
public get enabled() {
return this.entity ? this.entity.enabled && this._enabled : this._enabled;
}
2020-07-22 20:07:14 +08:00
/**
* onEnabled/onDisable
* @param value
*/
public set enabled(value: boolean) {
this.setEnabled(value);
2020-06-08 18:26:05 +08:00
}
2020-07-28 16:25:20 +08:00
private _updateOrder = 0;
2020-07-22 20:07:14 +08:00
/** 更新此实体上组件的顺序 */
public get updateOrder() {
return this._updateOrder;
}
2020-06-08 16:23:48 +08:00
2020-07-22 20:07:14 +08:00
/** 更新此实体上组件的顺序 */
public set updateOrder(value: number) {
this.setUpdateOrder(value);
}
2020-07-22 20:07:14 +08:00
/**
* 西访
*/
public initialize() {
}
2020-06-08 23:04:57 +08:00
2020-07-22 20:07:14 +08:00
/**
*
*/
public onAddedToEntity() {
}
/**
*
*/
public onRemovedFromEntity() {
}
2020-06-08 23:04:57 +08:00
2020-07-22 20:07:14 +08:00
/**
*
* @param comp
*/
public onEntityTransformChanged(comp: ComponentTransform) {
2020-07-22 20:07:14 +08:00
}
2020-06-08 23:04:57 +08:00
2020-07-22 20:07:14 +08:00
/**
*
*/
public onEnabled() {
}
2020-06-08 23:04:57 +08:00
2020-07-22 20:07:14 +08:00
/**
*
*/
public onDisabled() {
}
2020-07-22 20:07:14 +08:00
public setEnabled(isEnabled: boolean) {
if (this._enabled != isEnabled) {
this._enabled = isEnabled;
2020-06-08 23:04:57 +08:00
2020-07-22 20:07:14 +08:00
if (this._enabled) {
this.onEnabled();
} else {
this.onDisabled();
}
}
2020-07-22 20:07:14 +08:00
return this;
}
2020-07-22 20:07:14 +08:00
public setUpdateOrder(updateOrder: number) {
if (this._updateOrder != updateOrder) {
this._updateOrder = updateOrder;
}
2020-07-08 18:12:17 +08:00
2020-07-22 20:07:14 +08:00
return this;
}
2021-08-20 19:05:40 +08:00
/**
*
* @param component
* @returns
*/
2021-08-20 19:10:20 +08:00
public addComponent<T extends Component>(component: T): T {
return this.entity.addComponent<T>(component);
2021-08-20 19:05:40 +08:00
}
/**
*
* @param type
* @returns
*/
public getComponent<T extends Component>(type: new (...args: any[]) => T): T {
2021-08-20 19:10:20 +08:00
return this.entity.getComponent<T>(type);
2021-08-20 19:05:40 +08:00
}
/**
*
* @param typeName
* @param componentList
* @returns
*/
public getComponents(typeName: any, componentList?: any[]): any[] {
2021-08-20 19:05:40 +08:00
return this.entity.getComponents(typeName, componentList);
}
/**
*
* @param type
* @returns true false
*/
public hasComponent(type: new (...args: any[]) => Component): boolean {
2021-08-20 19:05:40 +08:00
return this.entity.hasComponent(type);
}
/**
*
* @param component
*/
public removeComponent(component?: Component): void {
2021-08-20 19:05:40 +08:00
if (component) {
this.entity.removeComponent(component);
} else {
this.entity.removeComponent(this);
}
}
2020-06-08 20:11:58 +08:00
}
2020-07-22 20:07:14 +08:00
}