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

242 lines
8.8 KiB
TypeScript
Raw Normal View History

2020-07-22 20:07:14 +08:00
module es {
export class ComponentList {
public _entity: Entity;
/**
*
*/
public _components: Component[] = [];
/**
*
*/
public _componentsToAdd: Component[] = [];
/**
*
*/
public _componentsToRemove: Component[] = [];
public _tempBufferList: Component[] = [];
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-06-18 23:22:54 +08:00
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) {
if (this._componentsToRemove.contains(component))
console.warn(`You are trying to remove a Component (${component}) that you already removed`)
2020-06-08 23:04:57 +08:00
2020-07-22 20:07:14 +08:00
// 这可能不是一个活动的组件,所以我们必须注意它是否还没有被处理,它可能正在同一帧中被删除
if (this._componentsToAdd.contains(component)) {
this._componentsToAdd.remove(component);
return;
}
2020-06-18 23:22:54 +08:00
2020-07-22 20:07:14 +08:00
this._componentsToRemove.push(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-07-22 20:07:14 +08:00
this._components.length = 0;
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() {
for (let i = 0; i < this._components.length; i++) {
let component = this._components[i];
// 处理渲染层列表
if (component instanceof RenderableComponent)
this._entity.scene.renderableComponents.remove(component);
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
}
}
public registerAllComponents() {
for (let i = 0; i < this._components.length; i++) {
let component = this._components[i];
2020-06-18 23:22:54 +08:00
if (component instanceof RenderableComponent)
this._entity.scene.renderableComponents.add(component);
2020-06-08 23:04:57 +08:00
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(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]);
this._components.remove(this._componentsToRemove[i]);
}
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];
if (component instanceof RenderableComponent)
this._entity.scene.renderableComponents.add(component);
2020-06-08 23:04:57 +08:00
2020-07-22 20:07:14 +08:00
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component));
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
2020-06-08 23:04:57 +08:00
2020-07-22 20:07:14 +08:00
this._components.push(component);
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-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-08 18:12:17 +08:00
}
2020-07-22 20:07:14 +08:00
public handleRemove(component: Component) {
// 处理渲染层列表
if (component instanceof RenderableComponent)
this._entity.scene.renderableComponents.remove(component);
2020-06-29 15:41:02 +08:00
2020-07-22 20:07:14 +08:00
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(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 {
for (let i = 0; i < this._components.length; i++) {
let component = this._components[i];
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) {
for (let i = 0; i < this._componentsToAdd.length; i++) {
let component = this._componentsToAdd[i];
if (component instanceof type)
return component 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: string | any, components?) {
if (!components)
components = [];
for (let i = 0; i < this._components.length; i++) {
let component = this._components[i];
if (typeof (typeName) == "string") {
if (egret.is(component, typeName)) {
components.push(component);
}
} else {
if (component instanceof typeName) {
components.push(component);
}
}
2020-07-22 20:07:14 +08:00
}
for (let i = 0; i < this._componentsToAdd.length; i++) {
let component = this._componentsToAdd[i];
if (typeof (typeName) == "string") {
if (egret.is(component, typeName)) {
components.push(component);
}
} else {
if (component instanceof typeName) {
components.push(component);
}
}
}
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._components.length; i++) {
let updatableComponent = this._components[i];
2020-06-16 00:04:28 +08:00
2020-07-22 20:07:14 +08:00
if (updatableComponent.enabled &&
(updatableComponent.updateInterval == 1 ||
Time.frameCount % updatableComponent.updateInterval == 0))
updatableComponent.update();
}
}
public onEntityTransformChanged(comp: Transform.Component) {
for (let i = 0; i < this._components.length; i++) {
if (this._components[i].enabled)
this._components[i].onEntityTransformChanged(comp);
}
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++)
this._components[i].onEnabled();
}
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
}