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 {
|
2020-07-22 23:30:31 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 组件列表的全局updateOrder排序
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static compareUpdatableOrder: IUpdatableComparer = new IUpdatableComparer();
|
2020-07-22 20:07:14 +08:00
|
|
|
|
public _entity: Entity;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 添加到实体的组件列表
|
|
|
|
|
|
*/
|
2020-12-14 11:34:23 +08:00
|
|
|
|
public _components: Component[] = [];
|
2020-11-23 16:05:06 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 所有需要更新的组件列表
|
|
|
|
|
|
*/
|
2020-12-14 11:34:23 +08:00
|
|
|
|
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 } = {};
|
2020-07-22 20:07:14 +08:00
|
|
|
|
public _tempBufferList: Component[] = [];
|
2020-07-22 23:30:31 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 用于确定是否需要对该框架中的组件进行排序的标志
|
|
|
|
|
|
*/
|
|
|
|
|
|
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() {
|
2020-12-14 11:34:23 +08:00
|
|
|
|
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() {
|
2020-07-22 23:30:31 +08:00
|
|
|
|
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-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]) {
|
|
|
|
|
|
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;
|
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
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-07 16:23:15 +08:00
|
|
|
|
this.componentsByType.clear();
|
|
|
|
|
|
this.componentsToAddByType.clear();
|
2020-12-14 11:34:23 +08:00
|
|
|
|
this._components.length = 0;
|
|
|
|
|
|
this._updatableComponents.length = 0;
|
2021-04-20 15:46:18 +08:00
|
|
|
|
this._componentsToAdd = {};
|
2021-05-07 16:23:15 +08:00
|
|
|
|
this._componentsToRemove = {};
|
2020-06-08 23:04:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 20:07:14 +08:00
|
|
|
|
public deregisterAllComponents() {
|
2020-12-14 11:34:23 +08:00
|
|
|
|
for (let component of this._components) {
|
2020-10-30 20:08:26 +08:00
|
|
|
|
if (!component) continue;
|
2020-07-27 16:10:36 +08:00
|
|
|
|
|
2020-11-23 16:05:06 +08:00
|
|
|
|
// 处理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
|
|
|
|
|
2021-05-07 16:23:15 +08:00
|
|
|
|
this.decreaseBits(component);
|
2020-12-30 16:28:07 +08:00
|
|
|
|
this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
|
2020-07-22 20:07:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public registerAllComponents() {
|
2020-12-14 11:34:23 +08:00
|
|
|
|
for (let component of this._components) {
|
2020-11-23 16:05:06 +08:00
|
|
|
|
if (isIUpdatable(component))
|
2020-12-14 11:34:23 +08:00
|
|
|
|
this._updatableComponents.push(component);
|
2020-07-10 11:24:42 +08:00
|
|
|
|
|
2021-05-07 16:23:15 +08:00
|
|
|
|
this.addBits(component);
|
2020-12-30 16:28:07 +08:00
|
|
|
|
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-04-20 15:46:18 +08:00
|
|
|
|
for (let i in this._componentsToRemove) {
|
|
|
|
|
|
let component = this._componentsToRemove[i];
|
|
|
|
|
|
this.handleRemove(component);
|
2021-05-07 16:23:15 +08:00
|
|
|
|
for (let index = 0; index < this._components.length; index++) {
|
2021-04-26 15:23:16 +08:00
|
|
|
|
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
|
|
|
|
}
|
2021-05-07 16:23:15 +08:00
|
|
|
|
this.removeComponentsByType(component);
|
2020-06-08 23:04:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-20 15:46:18 +08:00
|
|
|
|
this._componentsToRemove = {};
|
2020-07-27 16:10:36 +08:00
|
|
|
|
|
2021-04-20 15:46:18 +08:00
|
|
|
|
for (let i in this._componentsToAdd) {
|
|
|
|
|
|
let component = this._componentsToAdd[i];
|
2020-11-23 16:05:06 +08:00
|
|
|
|
|
2021-04-20 15:46:18 +08:00
|
|
|
|
if (isIUpdatable(component))
|
|
|
|
|
|
this._updatableComponents.push(component);
|
2020-06-08 23:04:57 +08:00
|
|
|
|
|
2021-05-07 16:23:15 +08:00
|
|
|
|
this.addBits(component);
|
2021-04-20 15:46:18 +08:00
|
|
|
|
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
|
2020-06-08 23:04:57 +08:00
|
|
|
|
|
2021-05-07 16:23:15 +08:00
|
|
|
|
this.addComponentsByType(component);
|
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-05-07 16:23:15 +08:00
|
|
|
|
// 在调用onAddedToEntity之前清除,以防添加更多组件
|
|
|
|
|
|
this._componentsToAdd = {};
|
|
|
|
|
|
this.componentsToAddByType.clear();
|
|
|
|
|
|
this._isComponentListUnsorted = true;
|
2020-07-22 20:07:14 +08:00
|
|
|
|
|
2021-05-07 16:23:15 +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-05-07 16:23:15 +08:00
|
|
|
|
// enabled检查实体和组件
|
|
|
|
|
|
if (component.enabled) {
|
|
|
|
|
|
component.onEnabled();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-07-22 23:30:31 +08:00
|
|
|
|
|
2021-05-07 16:23:15 +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) {
|
2020-11-23 16:05:06 +08:00
|
|
|
|
if (isIUpdatable(component))
|
2021-03-29 15:28:18 +08:00
|
|
|
|
new es.List(this._updatableComponents).remove(component);
|
2020-11-23 16:05:06 +08:00
|
|
|
|
|
2021-05-07 16:23:15 +08:00
|
|
|
|
this.decreaseBits(component);
|
2020-12-30 16:28:07 +08:00
|
|
|
|
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));
|
2021-04-26 15:23:16 +08:00
|
|
|
|
let fastIndex = fastList.findIndex(c => c.id == component.id);
|
2021-05-07 16:23:15 +08:00
|
|
|
|
if (fastIndex != -1) {
|
2021-04-26 15:23:16 +08:00
|
|
|
|
fastList.splice(fastIndex, 1);
|
2021-05-07 16:23:15 +08:00
|
|
|
|
}
|
2021-04-26 15:23:16 +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 = [];
|
2021-04-26 15:23:16 +08:00
|
|
|
|
fastList.push(component);
|
2021-05-07 16:23:15 +08:00
|
|
|
|
this.componentsByType.set(TypeUtils.getType(component), fastList);
|
2021-04-26 15:23:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-07 16:23:15 +08:00
|
|
|
|
private removeComponentsToAddByType(component: Component) {
|
|
|
|
|
|
let fastList = this.componentsToAddByType.get(TypeUtils.getType(component));
|
2021-04-26 15:23:16 +08:00
|
|
|
|
let fastIndex = fastList.findIndex(c => c.id == component.id);
|
2021-05-07 16:23:15 +08:00
|
|
|
|
if (fastIndex != -1) {
|
2021-04-26 15:23:16 +08:00
|
|
|
|
fastList.splice(fastIndex, 1);
|
2021-05-07 16:23:15 +08:00
|
|
|
|
}
|
2021-04-26 15:23:16 +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 = [];
|
2021-04-26 15:23:16 +08:00
|
|
|
|
fastList.push(component);
|
2021-05-07 16:23:15 +08:00
|
|
|
|
this.componentsToAddByType.set(TypeUtils.getType(component), fastList);
|
2021-04-26 15:23:16 +08:00
|
|
|
|
}
|
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);
|
2021-04-26 15:23:16 +08:00
|
|
|
|
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);
|
2021-04-26 15:23:16 +08:00
|
|
|
|
if (fastToAddList && fastToAddList.length > 0)
|
|
|
|
|
|
return fastToAddList[0] as T;
|
2020-06-16 11:22:37 +08:00
|
|
|
|
}
|
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
|
|
|
|
|
|
*/
|
2021-04-26 15:23:16 +08:00
|
|
|
|
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);
|
2021-04-26 15:23:16 +08:00
|
|
|
|
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);
|
2021-04-26 15:23:16 +08:00
|
|
|
|
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();
|
2020-11-23 16:05:06 +08:00
|
|
|
|
for (let i = 0; i < this._updatableComponents.length; i++) {
|
2020-12-14 11:34:23 +08:00
|
|
|
|
if (this._updatableComponents[i].enabled)
|
|
|
|
|
|
this._updatableComponents[i].update();
|
2020-07-22 20:07:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 23:30:31 +08:00
|
|
|
|
public onEntityTransformChanged(comp: transform.Component) {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
for (let i = 0; i < this._components.length; i++) {
|
2020-12-14 11:34:23 +08:00
|
|
|
|
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++)
|
2020-12-14 11:34:23 +08:00
|
|
|
|
this._components[i].onEnabled();
|
2020-07-22 20:07:14 +08:00
|
|
|
|
}
|
2020-07-10 11:24:42 +08:00
|
|
|
|
|
2020-07-22 20:07:14 +08:00
|
|
|
|
public onEntityDisabled() {
|
|
|
|
|
|
for (let i = 0; i < this._components.length; i++)
|
2020-12-14 11:34:23 +08:00
|
|
|
|
this._components[i].onDisabled();
|
2020-06-11 00:03:26 +08:00
|
|
|
|
}
|
2020-06-08 23:04:57 +08:00
|
|
|
|
}
|
2020-07-22 20:07:14 +08:00
|
|
|
|
}
|