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
|
|
|
|
/**
|
|
|
|
|
|
* 添加到此框架的组件列表。用来对组件进行分组,这样我们就可以同时进行加工
|
|
|
|
|
|
*/
|
|
|
|
|
|
public _componentsToAdd: Component[] = [];
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 标记要删除此框架的组件列表。用来对组件进行分组,这样我们就可以同时进行加工
|
|
|
|
|
|
*/
|
|
|
|
|
|
public _componentsToRemove: Component[] = [];
|
|
|
|
|
|
public _tempBufferList: Component[] = [];
|
2020-07-22 23:30:31 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 用于确定是否需要对该框架中的组件进行排序的标志
|
|
|
|
|
|
*/
|
|
|
|
|
|
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() {
|
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) {
|
|
|
|
|
|
this._componentsToAdd.push(component);
|
2020-06-08 23:04:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 20:07:14 +08:00
|
|
|
|
public remove(component: Component) {
|
2021-03-29 15:28:18 +08:00
|
|
|
|
let componentToRemove = new es.List(this._componentsToRemove);
|
|
|
|
|
|
let componentToAdd = new es.List(this._componentsToAdd);
|
2021-01-18 19:54:41 +08:00
|
|
|
|
Debug.warnIf(componentToRemove.contains(component), `您正在尝试删除一个您已经删除的组件(${component})`);
|
2020-06-08 23:04:57 +08:00
|
|
|
|
|
2020-07-22 20:07:14 +08:00
|
|
|
|
// 这可能不是一个活动的组件,所以我们必须注意它是否还没有被处理,它可能正在同一帧中被删除
|
2020-11-30 13:50:18 +08:00
|
|
|
|
if (componentToAdd.contains(component)) {
|
|
|
|
|
|
componentToAdd.remove(component);
|
2020-07-22 20:07:14 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2020-06-18 23:22:54 +08:00
|
|
|
|
|
2020-11-30 13:50:18 +08:00
|
|
|
|
componentToRemove.add(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
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-14 11:34:23 +08:00
|
|
|
|
this._components.length = 0;
|
|
|
|
|
|
this._updatableComponents.length = 0;
|
2020-07-22 20:07:14 +08:00
|
|
|
|
this._componentsToAdd.length = 0;
|
2020-06-08 23:04:57 +08:00
|
|
|
|
this._componentsToRemove.length = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-12-30 16:28:07 +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() {
|
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
|
|
|
|
|
2020-12-30 16:28:07 +08:00
|
|
|
|
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() {
|
|
|
|
|
|
if (this._componentsToRemove.length > 0) {
|
|
|
|
|
|
for (let i = 0; i < this._componentsToRemove.length; i++) {
|
|
|
|
|
|
this.handleRemove(this._componentsToRemove[i]);
|
2021-03-29 15:28:18 +08:00
|
|
|
|
new es.List(this._components).remove(this._componentsToRemove[i]);
|
2020-07-22 20:07:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this._componentsToRemove.length = 0;
|
2020-06-08 23:04:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 20:07:14 +08:00
|
|
|
|
if (this._componentsToAdd.length > 0) {
|
|
|
|
|
|
for (let i = 0, count = this._componentsToAdd.length; i < count; i++) {
|
|
|
|
|
|
let component = this._componentsToAdd[i];
|
2020-07-27 16:10:36 +08:00
|
|
|
|
|
2020-11-23 16:05:06 +08:00
|
|
|
|
if (isIUpdatable(component))
|
2020-12-14 11:34:23 +08:00
|
|
|
|
this._updatableComponents.push(component);
|
2020-11-23 16:05:06 +08:00
|
|
|
|
|
2020-12-30 16:28:07 +08:00
|
|
|
|
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(TypeUtils.getType(component)));
|
|
|
|
|
|
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
|
2020-06-08 23:04:57 +08:00
|
|
|
|
|
2020-12-14 11:34:23 +08:00
|
|
|
|
this._components.push(component);
|
2020-07-22 20:07:14 +08:00
|
|
|
|
this._tempBufferList.push(component);
|
2020-06-08 23:04:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 20:07:14 +08:00
|
|
|
|
// 在调用onAddedToEntity之前清除,以防添加更多组件
|
|
|
|
|
|
this._componentsToAdd.length = 0;
|
2020-07-22 23:30:31 +08:00
|
|
|
|
this._isComponentListUnsorted = true;
|
2020-06-08 23:04:57 +08:00
|
|
|
|
|
2020-07-22 20:07:14 +08:00
|
|
|
|
// 现在所有的组件都添加到了场景中,我们再次循环并调用onAddedToEntity/onEnabled
|
|
|
|
|
|
for (let i = 0; i < this._tempBufferList.length; i++) {
|
|
|
|
|
|
let component = this._tempBufferList[i];
|
|
|
|
|
|
component.onAddedToEntity();
|
|
|
|
|
|
|
|
|
|
|
|
// enabled检查实体和组件
|
|
|
|
|
|
if (component.enabled) {
|
|
|
|
|
|
component.onEnabled();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-07-08 18:12:17 +08:00
|
|
|
|
|
2020-07-22 20:07:14 +08:00
|
|
|
|
this._tempBufferList.length = 0;
|
|
|
|
|
|
}
|
2020-07-22 23:30:31 +08:00
|
|
|
|
|
2021-04-16 17:48:13 +08:00
|
|
|
|
// if (this._isComponentListUnsorted) {
|
|
|
|
|
|
// this._updatableComponents.sort(ComponentList.compareUpdatableOrder.compare);
|
|
|
|
|
|
// this._isComponentListUnsorted = false;
|
|
|
|
|
|
// }
|
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
|
|
|
|
|
2020-12-30 16:28:07 +08:00
|
|
|
|
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
|
|
|
|
|
2020-07-22 20:07:14 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取类型T的第一个组件并返回它
|
|
|
|
|
|
* 可以选择跳过检查未初始化的组件(尚未调用onAddedToEntity方法的组件)
|
|
|
|
|
|
* 如果没有找到组件,则返回null。
|
|
|
|
|
|
* @param type
|
|
|
|
|
|
* @param onlyReturnInitializedComponents
|
|
|
|
|
|
*/
|
|
|
|
|
|
public getComponent<T extends Component>(type, onlyReturnInitializedComponents: boolean): T {
|
2020-12-14 11:34:23 +08:00
|
|
|
|
for (let component of this._components) {
|
2020-06-08 23:04:57 +08:00
|
|
|
|
if (component instanceof type)
|
|
|
|
|
|
return component as T;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 20:07:14 +08:00
|
|
|
|
// 我们可以选择检查挂起的组件,以防addComponent和getComponent在同一个框架中被调用
|
|
|
|
|
|
if (!onlyReturnInitializedComponents) {
|
2020-12-14 11:34:23 +08:00
|
|
|
|
for (let component of this._componentsToAdd) {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
if (component instanceof type)
|
|
|
|
|
|
return component as T;
|
2020-06-16 16:35:17 +08:00
|
|
|
|
}
|
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
|
|
|
|
|
|
*/
|
2020-11-23 16:05:06 +08:00
|
|
|
|
public getComponents(typeName: any, components?) {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
if (!components)
|
|
|
|
|
|
components = [];
|
|
|
|
|
|
|
2020-12-14 11:34:23 +08:00
|
|
|
|
for (let component of this._components) {
|
2020-11-23 16:05:06 +08:00
|
|
|
|
if (component instanceof typeName) {
|
|
|
|
|
|
components.push(component);
|
2020-06-16 16:35:17 +08:00
|
|
|
|
}
|
2020-07-22 20:07:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-23 16:05:06 +08:00
|
|
|
|
// 我们还检查了待处理的组件,以防在同一帧中调用addComponent和getComponent
|
2020-12-14 11:34:23 +08:00
|
|
|
|
for (let component of this._componentsToAdd) {
|
2020-11-23 16:05:06 +08:00
|
|
|
|
if (component instanceof typeName) {
|
|
|
|
|
|
components.push(component);
|
2020-06-16 16:35:17 +08:00
|
|
|
|
}
|
2020-06-16 11:22:37 +08:00
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < this._componentsToAdd.length; i++) {
|
|
|
|
|
|
if (this._componentsToAdd[i].enabled)
|
|
|
|
|
|
this._componentsToAdd[i].onEntityTransformChanged(comp);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|