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

275 lines
10 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[] = [];
/** 记录component的快速读取列表 */
public fastComponentsMap = new Map<new (...args: any[]) => Component, es.Component[]>();
public fastComponentsToAddMap = new Map<new (...args: any[]) => Component, es.Component[]>();
/**
*
*/
public _updatableComponents: IUpdatable[] = [];
2020-07-22 20:07:14 +08:00
/**
*
*/
2021-04-20 15:46:18 +08:00
public _componentsToAdd: {[index: number]: Component} = {};
2020-07-22 20:07:14 +08:00
/**
*
*/
2021-04-20 15:46:18 +08:00
public _componentsToRemove: {[index: number]: Component} = {};
2020-07-22 20:07:14 +08:00
public _tempBufferList: Component[] = [];
/**
*
*/
public _isComponentListUnsorted: boolean;
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;
this.addFastComponentToAdd(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
Debug.warnIf(!!this._componentsToRemove[component.id], `您正在尝试删除一个您已经删除的组件(${component})`);
//
2020-07-22 20:07:14 +08:00
// 这可能不是一个活动的组件,所以我们必须注意它是否还没有被处理,它可能正在同一帧中被删除
2021-04-20 15:46:18 +08:00
if (this._componentsToAdd[component.id]) {
delete this._componentsToAdd[component.id];
this.removeFastComponentToAdd(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;
2020-06-08 23:04:57 +08:00
}
2020-07-22 20:07:14 +08:00
/**
*
*/
public removeAllComponents() {
for (let i = 0; i < this._components.length; i++) {
this.handleRemove(this._components[i]);
2020-06-08 23:04:57 +08:00
}
this.fastComponentsMap.clear();
this.fastComponentsToAddMap.clear();
this._components.length = 0;
this._updatableComponents.length = 0;
2021-04-20 15:46:18 +08:00
this._componentsToAdd = {};
this._componentsToRemove = {};
2020-06-08 23:04:57 +08:00
}
2020-07-22 20:07:14 +08:00
public deregisterAllComponents() {
for (let component of this._components) {
if (!component) continue;
// 处理IUpdatable
if (isIUpdatable(component))
2021-03-29 15:28:18 +08:00
new es.List(this._updatableComponents).remove(component);
2020-07-22 20:07:14 +08:00
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(TypeUtils.getType(component)), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
2020-07-22 20:07:14 +08:00
}
}
public registerAllComponents() {
for (let component of this._components) {
if (isIUpdatable(component))
this._updatableComponents.push(component);
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(TypeUtils.getType(component)));
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
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 updateLists() {
2021-04-20 15:46:18 +08:00
for (let i in this._componentsToRemove) {
let component = this._componentsToRemove[i];
this.handleRemove(component);
for (let index = 0; index < this._components.length; index ++) {
let searchComponent = this._components[index];
if (searchComponent.id == component.id) {
2021-04-20 15:46:18 +08:00
this._components.splice(index, 1);
break;
}
2020-07-22 20:07:14 +08:00
}
this.removeFastComponent(component);
2020-06-08 23:04:57 +08:00
}
2021-04-20 15:46:18 +08:00
this._componentsToRemove = {};
2021-04-20 15:46:18 +08:00
for (let i in this._componentsToAdd) {
let component = this._componentsToAdd[i];
2021-04-20 15:46:18 +08:00
if (isIUpdatable(component))
this._updatableComponents.push(component);
2020-06-08 23:04:57 +08:00
2021-04-20 15:46:18 +08:00
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(TypeUtils.getType(component)));
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
this.addFastComponent(component);
2020-06-08 23:04:57 +08:00
2021-04-20 15:46:18 +08:00
this._components.push(component);
this._tempBufferList.push(component);
}
2020-06-08 23:04:57 +08:00
2021-04-20 15:46:18 +08:00
// 在调用onAddedToEntity之前清除以防添加更多组件
this._componentsToAdd = {};
this.fastComponentsToAddMap.clear();
2021-04-20 15:46:18 +08:00
this._isComponentListUnsorted = true;
2020-07-22 20:07:14 +08:00
2021-04-20 15:46:18 +08:00
// 现在所有的组件都添加到了场景中我们再次循环并调用onAddedToEntity/onEnabled
for (let i = 0; i < this._tempBufferList.length; i++) {
let component = this._tempBufferList[i];
component.onAddedToEntity();
2020-07-08 18:12:17 +08:00
2021-04-20 15:46:18 +08:00
// enabled检查实体和组件
if (component.enabled) {
component.onEnabled();
}
}
2021-04-20 15:46:18 +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) {
if (isIUpdatable(component))
2021-03-29 15:28:18 +08:00
new es.List(this._updatableComponents).remove(component);
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(TypeUtils.getType(component)), false);
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
private removeFastComponent(component: Component) {
let fastList = this.fastComponentsMap.get(TypeUtils.getType(component));
let fastIndex = fastList.findIndex(c => c.id == component.id);
if (fastIndex != -1)
fastList.splice(fastIndex, 1);
}
private addFastComponent(component: Component) {
let fastList = this.fastComponentsMap.get(TypeUtils.getType(component));
if (!fastList)
fastList = [];
fastList.push(component);
this.fastComponentsMap.set(TypeUtils.getType(component), fastList);
}
private removeFastComponentToAdd(component: Component) {
let fastList = this.fastComponentsToAddMap.get(TypeUtils.getType(component));
let fastIndex = fastList.findIndex(c => c.id == component.id);
if (fastIndex != -1)
fastList.splice(fastIndex, 1);
}
private addFastComponentToAdd(component: Component) {
let fastList = this.fastComponentsToAddMap.get(TypeUtils.getType(component));
if (!fastList)
fastList = [];
fastList.push(component);
this.fastComponentsToAddMap.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 {
let fastList = this.fastComponentsMap.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) {
let fastToAddList = this.fastComponentsToAddMap.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 = [];
let fastList = this.fastComponentsMap.get(typeName);
if (fastList)
components.concat(fastList);
2020-07-22 20:07:14 +08:00
let fastToAddList = this.fastComponentsToAddMap.get(typeName);
if (fastToAddList)
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();
for (let i = 0; i < this._updatableComponents.length; i++) {
if (this._updatableComponents[i].enabled)
this._updatableComponents[i].update();
2020-07-22 20:07:14 +08:00
}
}
public onEntityTransformChanged(comp: transform.Component) {
2020-07-22 20:07:14 +08:00
for (let i = 0; i < this._components.length; i++) {
if (this._components[i].enabled)
this._components[i].onEntityTransformChanged(comp);
2020-07-22 20:07:14 +08:00
}
2021-04-20 15:46:18 +08:00
for (let i in this._componentsToAdd) {
let component = this._componentsToAdd[i];
if (component.enabled)
component.onEntityTransformChanged(comp);
2020-07-22 20:07:14 +08:00
}
}
public onEntityEnabled() {
for (let i = 0; i < this._components.length; i++)
this._components[i].onEnabled();
2020-07-22 20:07:14 +08:00
}
2020-07-22 20:07:14 +08:00
public onEntityDisabled() {
for (let i = 0; i < this._components.length; i++)
this._components[i].onDisabled();
}
2020-06-08 23:04:57 +08:00
}
2020-07-22 20:07:14 +08:00
}