|
|
|
@@ -1,28 +1,37 @@
|
|
|
|
|
class ComponentList {
|
|
|
|
|
private _entity: Entity;
|
|
|
|
|
/** 添加到实体的组件列表 */
|
|
|
|
|
private _components: Component[] = [];
|
|
|
|
|
/** 添加到此框架的组件列表。用来对组件进行分组,这样我们就可以同时进行加工 */
|
|
|
|
|
private _componentsToAdd: Component[] = [];
|
|
|
|
|
/** 标记要删除此框架的组件列表。用来对组件进行分组,这样我们就可以同时进行加工 */
|
|
|
|
|
private _componentsToRemove: Component[] = [];
|
|
|
|
|
/** 需要调用更新的所有组件的列表 */
|
|
|
|
|
private _updatableComponents: IUpdatable[] = [];
|
|
|
|
|
private _tempBufferList: Component[] = [];
|
|
|
|
|
|
|
|
|
|
constructor(entity: Entity){
|
|
|
|
|
constructor(entity: Entity) {
|
|
|
|
|
this._entity = entity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get count(){
|
|
|
|
|
public get count() {
|
|
|
|
|
return this._components.length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get buffer(){
|
|
|
|
|
public get buffer() {
|
|
|
|
|
return this._components;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public add(component: Component){
|
|
|
|
|
public add(component: Component) {
|
|
|
|
|
this._componentsToAdd.push(component);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public remove(component: Component){
|
|
|
|
|
if (this._componentsToAdd.contains(component)){
|
|
|
|
|
public remove(component: Component) {
|
|
|
|
|
if (this._componentsToRemove.contains(component))
|
|
|
|
|
console.warn(`You are trying to remove a Component (${component}) that you already removed`)
|
|
|
|
|
|
|
|
|
|
// 这可能不是一个活动的组件,所以我们必须注意它是否还没有被处理,它可能正在同一帧中被删除
|
|
|
|
|
if (this._componentsToAdd.contains(component)) {
|
|
|
|
|
this._componentsToAdd.remove(component);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
@@ -30,43 +39,58 @@ class ComponentList {
|
|
|
|
|
this._componentsToRemove.push(component);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public removeAllComponents(){
|
|
|
|
|
for (let i = 0; i < this._components.length; i ++){
|
|
|
|
|
/**
|
|
|
|
|
* 立即从组件列表中删除所有组件
|
|
|
|
|
*/
|
|
|
|
|
public removeAllComponents() {
|
|
|
|
|
for (let i = 0; i < this._components.length; i++) {
|
|
|
|
|
this.handleRemove(this._components[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._components.length = 0;
|
|
|
|
|
this._updatableComponents.length = 0;
|
|
|
|
|
this._componentsToAdd.length = 0;
|
|
|
|
|
this._componentsToRemove.length = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public deregisterAllComponents(){
|
|
|
|
|
for (let i = 0; i < this._components.length; i ++){
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
// 处理IUpdatable
|
|
|
|
|
if (egret.is(component, "IUpdatable"))
|
|
|
|
|
this._updatableComponents.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 ++){
|
|
|
|
|
public registerAllComponents() {
|
|
|
|
|
for (let i = 0; i < this._components.length; i++) {
|
|
|
|
|
let component = this._components[i];
|
|
|
|
|
|
|
|
|
|
if (component instanceof RenderableComponent)
|
|
|
|
|
this._entity.scene.renderableComponents.add(component);
|
|
|
|
|
|
|
|
|
|
if (egret.is(component, "IUpdatable"))
|
|
|
|
|
this._updatableComponents.push(component as any);
|
|
|
|
|
|
|
|
|
|
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component));
|
|
|
|
|
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public updateLists(){
|
|
|
|
|
if (this._componentsToRemove.length > 0){
|
|
|
|
|
for (let i = 0; i < this._componentsToRemove.length; i ++){
|
|
|
|
|
/**
|
|
|
|
|
* 处理任何需要删除或添加的组件
|
|
|
|
|
*/
|
|
|
|
|
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]);
|
|
|
|
|
}
|
|
|
|
@@ -74,11 +98,15 @@ class ComponentList {
|
|
|
|
|
this._componentsToRemove.length = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this._componentsToAdd.length > 0){
|
|
|
|
|
for (let i = 0, count = this._componentsToAdd.length; i < count; i ++){
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
if (egret.is(component, "IUpdatable"))
|
|
|
|
|
this._updatableComponents.push(component as any);
|
|
|
|
|
|
|
|
|
|
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component));
|
|
|
|
|
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
|
|
|
|
|
|
|
|
|
@@ -86,13 +114,16 @@ class ComponentList {
|
|
|
|
|
this._tempBufferList.push(component);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 在调用onAddedToEntity之前清除,以防添加更多组件
|
|
|
|
|
this._componentsToAdd.length = 0;
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < this._tempBufferList.length; i++){
|
|
|
|
|
// 现在所有的组件都添加到了场景中,我们再次循环并调用onAddedToEntity/onEnabled
|
|
|
|
|
for (let i = 0; i < this._tempBufferList.length; i++) {
|
|
|
|
|
let component = this._tempBufferList[i];
|
|
|
|
|
component.onAddedToEntity();
|
|
|
|
|
|
|
|
|
|
if (component.enabled){
|
|
|
|
|
// enabled检查实体和组件
|
|
|
|
|
if (component.enabled) {
|
|
|
|
|
component.onEnabled();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@@ -101,22 +132,25 @@ class ComponentList {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public onEntityTransformChanged(comp: TransformComponent){
|
|
|
|
|
for (let i = 0; i < this._components.length; i ++){
|
|
|
|
|
public onEntityTransformChanged(comp: TransformComponent) {
|
|
|
|
|
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 ++){
|
|
|
|
|
for (let i = 0; i < this._componentsToAdd.length; i++) {
|
|
|
|
|
if (this._componentsToAdd[i].enabled)
|
|
|
|
|
this._componentsToAdd[i].onEntityTransformChanged(comp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private handleRemove(component: Component){
|
|
|
|
|
private handleRemove(component: Component) {
|
|
|
|
|
if (component instanceof RenderableComponent)
|
|
|
|
|
this._entity.scene.renderableComponents.remove(component);
|
|
|
|
|
|
|
|
|
|
if (egret.is(component, "IUpdatable"))
|
|
|
|
|
this._updatableComponents.remove(component);
|
|
|
|
|
|
|
|
|
|
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component), false);
|
|
|
|
|
this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
|
|
|
|
|
|
|
|
|
@@ -124,15 +158,23 @@ class ComponentList {
|
|
|
|
|
component.entity = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public getComponent<T extends Component>(type, onlyReturnInitializedComponents: boolean): T{
|
|
|
|
|
for (let i = 0; i < this._components.length; i ++){
|
|
|
|
|
/**
|
|
|
|
|
* 获取类型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];
|
|
|
|
|
if (component instanceof type)
|
|
|
|
|
return component as T;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!onlyReturnInitializedComponents){
|
|
|
|
|
for (let i = 0; i < this._componentsToAdd.length; i ++){
|
|
|
|
|
// 我们可以选择检查挂起的组件,以防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;
|
|
|
|
@@ -142,31 +184,36 @@ class ComponentList {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public getComponents(typeName: string | any, components?){
|
|
|
|
|
/**
|
|
|
|
|
* 获取T类型的所有组件,但不使用列表分配
|
|
|
|
|
* @param typeName
|
|
|
|
|
* @param components
|
|
|
|
|
*/
|
|
|
|
|
public getComponents(typeName: string | any, components?) {
|
|
|
|
|
if (!components)
|
|
|
|
|
components = [];
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < this._components.length; i ++){
|
|
|
|
|
for (let i = 0; i < this._components.length; i++) {
|
|
|
|
|
let component = this._components[i];
|
|
|
|
|
if (typeof(typeName) == "string"){
|
|
|
|
|
if (egret.is(component, typeName)){
|
|
|
|
|
if (typeof (typeName) == "string") {
|
|
|
|
|
if (egret.is(component, typeName)) {
|
|
|
|
|
components.push(component);
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
if (component instanceof typeName){
|
|
|
|
|
} else {
|
|
|
|
|
if (component instanceof typeName) {
|
|
|
|
|
components.push(component);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < this._componentsToAdd.length; i ++){
|
|
|
|
|
for (let i = 0; i < this._componentsToAdd.length; i++) {
|
|
|
|
|
let component = this._componentsToAdd[i];
|
|
|
|
|
if (typeof(typeName) == "string"){
|
|
|
|
|
if (egret.is(component, typeName)){
|
|
|
|
|
if (typeof (typeName) == "string") {
|
|
|
|
|
if (egret.is(component, typeName)) {
|
|
|
|
|
components.push(component);
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
if (component instanceof typeName){
|
|
|
|
|
} else {
|
|
|
|
|
if (component instanceof typeName) {
|
|
|
|
|
components.push(component);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@@ -175,12 +222,19 @@ class ComponentList {
|
|
|
|
|
return components;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public update(){
|
|
|
|
|
public update() {
|
|
|
|
|
this.updateLists();
|
|
|
|
|
for (let i = 0; i < this._components.length; i ++){
|
|
|
|
|
let component = this._components[i];
|
|
|
|
|
if (component.enabled && (component.updateInterval == 1 || Time.frameCount % component.updateInterval == 0))
|
|
|
|
|
component.update();
|
|
|
|
|
for (let i = 0; i < this._updatableComponents.length; i++) {
|
|
|
|
|
let updatable = this._updatableComponents[i];
|
|
|
|
|
let updateableComponent;
|
|
|
|
|
if (updatable instanceof Component)
|
|
|
|
|
updateableComponent = updatable as Component;
|
|
|
|
|
|
|
|
|
|
if (updatable.enabled &&
|
|
|
|
|
updateableComponent.enabled &&
|
|
|
|
|
(updateableComponent.updateInterval == 1 ||
|
|
|
|
|
Time.frameCount % updateableComponent.updateInterval == 0))
|
|
|
|
|
updatable.update();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|