Files
esengine/source/src/ECS/Utils/ComponentList.ts

318 lines
12 KiB
TypeScript
Raw Normal View History

2020-10-27 18:08:49 +08:00
///<reference path="../Components/IUpdatable.ts" />
2020-07-22 20:07:14 +08:00
module es {
export class ComponentList {
/**
* updateOrder排序
*/
public static compareUpdatableOrder: IUpdatableComparer = new IUpdatableComparer();
2020-07-22 20:07:14 +08:00
public _entity: Entity;
/**
*
*/
public _components: Component[] = [];
/**
*
*/
public _updatableComponents: IUpdatable[] = [];
2020-07-22 20:07:14 +08:00
/**
*
*/
2021-05-07 16:23:15 +08:00
public _componentsToAdd: { [index: number]: Component } = {};
2020-07-22 20:07:14 +08:00
/**
*
*/
2021-05-07 16:23:15 +08:00
public _componentsToRemove: { [index: number]: Component } = {};
2021-05-12 13:08:47 +08:00
public _componentsToAddList: Component[] = [];
public _componentsToRemoveList: Component[] = [];
2020-07-22 20:07:14 +08:00
public _tempBufferList: Component[] = [];
/**
*
*/
public _isComponentListUnsorted: boolean;
2021-05-07 16:23:15 +08:00
private componentsByType = new Map<new (...args: any[]) => Component, es.Component[]>();
private componentsToAddByType = new Map<new (...args: any[]) => Component, es.Component[]>();
2020-07-22 20:07:14 +08:00
constructor(entity: Entity) {
this._entity = entity;
2020-06-08 23:04:57 +08:00
}
2020-07-22 20:07:14 +08:00
public get count() {
return this._components.length;
2020-06-08 23:04:57 +08:00
}
2020-07-22 20:07:14 +08:00
public get buffer() {
return this._components;
2020-07-22 20:07:14 +08:00
}
2020-06-18 23:22:54 +08:00
2020-07-28 16:25:20 +08:00
public markEntityListUnsorted() {
this._isComponentListUnsorted = true;
}
2020-07-22 20:07:14 +08:00
public add(component: Component) {
2021-04-20 15:46:18 +08:00
this._componentsToAdd[component.id] = component;
2021-05-12 13:08:47 +08:00
this._componentsToAddList.push(component);
2021-05-07 16:23:15 +08:00
this.addComponentsToAddByType(component);
2020-06-08 23:04:57 +08:00
}
2020-07-22 20:07:14 +08:00
public remove(component: Component) {
2021-04-20 15:46:18 +08:00
if (this._componentsToAdd[component.id]) {
2021-05-12 13:08:47 +08:00
let index = this._componentsToAddList.findIndex(c => c.id == component.id);
if (index != -1)
this._componentsToAddList.splice(index, 1);
2021-04-20 15:46:18 +08:00
delete this._componentsToAdd[component.id];
2021-05-07 16:23:15 +08:00
this.removeComponentsToAddByType(component);
2020-07-22 20:07:14 +08:00
return;
}
2020-06-18 23:22:54 +08:00
2021-04-20 15:46:18 +08:00
this._componentsToRemove[component.id] = component;
2021-05-12 13:08:47 +08:00
this._componentsToRemoveList.push(component);
2020-06-08 23:04:57 +08:00
}
2020-07-22 20:07:14 +08:00
/**
*
*/
public removeAllComponents() {
if (this._components.length > 0) {
for (let i = 0, s = this._components.length; i < s; ++ i) {
this.handleRemove(this._components[i]);
}
}
2021-05-24 17:20:27 +08:00
this.componentsByType.clear();
this.componentsToAddByType.clear();
this._components.length = 0;
this._updatableComponents.length = 0;
this._componentsToAdd = {};
this._componentsToRemove = {};
this._componentsToAddList.length = 0;
this._componentsToRemoveList.length = 0;
2020-06-08 23:04:57 +08:00
}
2020-07-22 20:07:14 +08:00
public deregisterAllComponents() {
if (this._components.length > 0) {
for (let i = 0, s = this._components.length; i < s; ++ i) {
let component = this._components[i];
if (!component) continue;
// 处理IUpdatable
if (isIUpdatable(component))
new es.List(this._updatableComponents).remove(component);
this.decreaseBits(component);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
}
2020-07-22 20:07:14 +08:00
}
}
public registerAllComponents() {
if (this._components.length > 0) {
for (let i = 0, s = this._components.length; i < s; ++ i) {
let component = this._components[i];
if (isIUpdatable(component))
this._updatableComponents.push(component);
this.addBits(component);
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
}
2020-07-22 20:07:14 +08:00
}
}
2020-06-08 23:04:57 +08:00
2021-05-07 16:23:15 +08:00
private decreaseBits(component: Component) {
let bits = this._entity.componentBits;
let typeIndex = ComponentTypeManager.getIndexFor(TypeUtils.getType(component));
bits.set(typeIndex, bits.get(typeIndex) - 1);
}
private addBits(component: Component) {
let bits = this._entity.componentBits;
let typeIndex = ComponentTypeManager.getIndexFor(TypeUtils.getType(component));
bits.set(typeIndex, bits.get(typeIndex) + 1);
}
2020-07-22 20:07:14 +08:00
/**
*
*/
public updateLists() {
2021-05-12 13:08:47 +08:00
if (this._componentsToRemoveList.length > 0) {
for (let i = 0, l = this._componentsToRemoveList.length; i < l; ++ i) {
let component = this._componentsToRemoveList[i];
this.handleRemove(component);
let index = this._components.findIndex(c => c.id == component.id);
if (index != -1)
2021-04-20 15:46:18 +08:00
this._components.splice(index, 1);
2021-05-12 13:08:47 +08:00
this.removeComponentsByType(component);
2020-07-22 20:07:14 +08:00
}
2021-05-12 13:08:47 +08:00
this._componentsToRemove = {};
this._componentsToRemoveList.length = 0;
2020-06-08 23:04:57 +08:00
}
2021-05-12 13:08:47 +08:00
if (this._componentsToAddList.length > 0) {
for (let i = 0, l = this._componentsToAddList.length; i < l; ++ i) {
let component = this._componentsToAddList[i];
if (isIUpdatable(component))
this._updatableComponents.push(component);
this.addBits(component);
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
this.addComponentsByType(component);
this._components.push(component);
this._tempBufferList.push(component);
}
// 在调用onAddedToEntity之前清除以防添加更多组件
this._componentsToAdd = {};
this._componentsToAddList.length = 0;
this.componentsToAddByType.clear();
this._isComponentListUnsorted = true;
2021-04-20 15:46:18 +08:00
}
2020-06-08 23:04:57 +08:00
2021-05-12 13:08:47 +08:00
if (this._tempBufferList.length > 0) {
// 现在所有的组件都添加到了场景中我们再次循环并调用onAddedToEntity/onEnabled
for (let i = 0, l = this._tempBufferList.length; i < l; ++ i) {
let component = this._tempBufferList[i];
component.onAddedToEntity();
2020-07-08 18:12:17 +08:00
2021-05-12 13:08:47 +08:00
// enabled检查实体和组件
if (component.enabled) {
component.onEnabled();
}
2021-05-07 16:23:15 +08:00
}
2021-05-12 13:08:47 +08:00
this._tempBufferList.length = 0;
}
2020-07-08 18:12:17 +08:00
}
2020-07-22 20:07:14 +08:00
public handleRemove(component: Component) {
2021-05-12 13:08:47 +08:00
if (isIUpdatable(component) && this._updatableComponents.length > 0) {
let index = this._updatableComponents.findIndex((c) => (<any>c as Component).id == component.id);
if (index != -1)
this._updatableComponents.splice(index, 1);
}
2021-05-07 16:23:15 +08:00
this.decreaseBits(component);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
2020-06-08 23:04:57 +08:00
2020-07-22 20:07:14 +08:00
component.onRemovedFromEntity();
component.entity = null;
}
2020-06-08 23:04:57 +08:00
2021-05-07 16:23:15 +08:00
private removeComponentsByType(component: Component) {
let fastList = this.componentsByType.get(TypeUtils.getType(component));
let fastIndex = fastList.findIndex(c => c.id == component.id);
2021-05-07 16:23:15 +08:00
if (fastIndex != -1) {
fastList.splice(fastIndex, 1);
2021-05-07 16:23:15 +08:00
}
}
2021-05-07 16:23:15 +08:00
private addComponentsByType(component: Component) {
let fastList = this.componentsByType.get(TypeUtils.getType(component));
if (!fastList) fastList = [];
fastList.push(component);
2021-05-07 16:23:15 +08:00
this.componentsByType.set(TypeUtils.getType(component), fastList);
}
2021-05-07 16:23:15 +08:00
private removeComponentsToAddByType(component: Component) {
let fastList = this.componentsToAddByType.get(TypeUtils.getType(component));
let fastIndex = fastList.findIndex(c => c.id == component.id);
2021-05-07 16:23:15 +08:00
if (fastIndex != -1) {
fastList.splice(fastIndex, 1);
2021-05-07 16:23:15 +08:00
}
}
2021-05-07 16:23:15 +08:00
private addComponentsToAddByType(component: Component) {
let fastList = this.componentsToAddByType.get(TypeUtils.getType(component));
if (!fastList) fastList = [];
fastList.push(component);
2021-05-07 16:23:15 +08:00
this.componentsToAddByType.set(TypeUtils.getType(component), fastList);
}
2020-07-22 20:07:14 +08:00
/**
* T的第一个组件并返回它
* (onAddedToEntity方法的组件)
* null
* @param type
* @param onlyReturnInitializedComponents
*/
public getComponent<T extends Component>(type, onlyReturnInitializedComponents: boolean): T {
2021-05-07 16:23:15 +08:00
let fastList = this.componentsByType.get(type);
if (fastList && fastList.length > 0)
return fastList[0] as T;
2020-06-08 23:04:57 +08:00
2020-07-22 20:07:14 +08:00
// 我们可以选择检查挂起的组件以防addComponent和getComponent在同一个框架中被调用
if (!onlyReturnInitializedComponents) {
2021-05-07 16:23:15 +08:00
let fastToAddList = this.componentsToAddByType.get(type);
if (fastToAddList && fastToAddList.length > 0)
return fastToAddList[0] as T;
}
2020-07-22 20:07:14 +08:00
return null;
2020-06-16 00:04:28 +08:00
}
2020-07-22 20:07:14 +08:00
/**
* T类型的所有组件使
* @param typeName
* @param components
*/
public getComponents(typeName: any, components?: any[]) {
2020-07-22 20:07:14 +08:00
if (!components)
components = [];
2021-05-07 16:23:15 +08:00
let fastList = this.componentsByType.get(typeName);
if (fastList)
2021-04-29 11:00:15 +08:00
components = components.concat(fastList);
2020-07-22 20:07:14 +08:00
2021-05-07 16:23:15 +08:00
let fastToAddList = this.componentsToAddByType.get(typeName);
if (fastToAddList)
2021-05-07 16:23:15 +08:00
components = components.concat(fastToAddList);
2020-07-22 20:07:14 +08:00
return components;
2020-06-16 00:04:28 +08:00
}
2020-07-22 20:07:14 +08:00
public update() {
this.updateLists();
2021-05-12 14:53:29 +08:00
if (this._updatableComponents.length > 0) {
for (let i = 0, s = this._updatableComponents.length; i < s; ++ i) {
let updateComponent = this._updatableComponents[i];
if (updateComponent.enabled)
updateComponent.update();
}
}
2020-07-22 20:07:14 +08:00
}
public onEntityTransformChanged(comp: transform.Component) {
if (this._components.length > 0 ){
for (let i = 0, s = this._components.length; i < s; ++ i) {
let component = this._components[i];
if (component.enabled)
component.onEntityTransformChanged(comp);
}
2020-07-22 20:07:14 +08:00
}
if (this._componentsToAddList.length > 0) {
for (let i = 0, s = this._componentsToAddList.length; i < s; ++ i) {
let component = this._componentsToAddList[i];
if (component.enabled)
component.onEntityTransformChanged(comp);
}
2020-07-22 20:07:14 +08:00
}
}
public onEntityEnabled() {
if (this._components.length > 0) {
for (let i = 0, s = this._components.length; i < s; i ++)
this._components[i].onEnabled();
}
2020-07-22 20:07:14 +08:00
}
2020-07-22 20:07:14 +08:00
public onEntityDisabled() {
if (this._components.length > 0) {
for (let i = 0, s = this._components.length; i < s; i ++)
this._components[i].onDisabled();
}
}
2020-06-08 23:04:57 +08:00
}
2020-07-22 20:07:14 +08:00
}