新增IUpdatable接口 用于减少update所带来的的性能损耗

This commit is contained in:
yhh
2020-07-10 11:24:42 +08:00
parent 877fc4c9bf
commit f6c2d81a83
14 changed files with 238 additions and 79 deletions

View File

@@ -193,15 +193,17 @@ declare abstract class Component extends egret.DisplayObjectContainer {
private _enabled; private _enabled;
updateInterval: number; updateInterval: number;
userData: any; userData: any;
private _updateOrder;
enabled: boolean; enabled: boolean;
readonly localPosition: Vector2; readonly localPosition: Vector2;
setEnabled(isEnabled: boolean): this; setEnabled(isEnabled: boolean): this;
updateOrder: number;
setUpdateOrder(updateOrder: number): this;
initialize(): void; initialize(): void;
onAddedToEntity(): void; onAddedToEntity(): void;
onRemovedFromEntity(): void; onRemovedFromEntity(): void;
onEnabled(): void; onEnabled(): void;
onDisabled(): void; onDisabled(): void;
update(): void;
debugRender(): void; debugRender(): void;
onEntityTransformChanged(comp: TransformComponent): void; onEntityTransformChanged(comp: TransformComponent): void;
registerComponent(): void; registerComponent(): void;
@@ -253,6 +255,11 @@ declare enum TransformComponent {
scale = 1, scale = 1,
position = 2 position = 2
} }
interface IUpdatable {
enabled: boolean;
updateOrder: number;
update(): any;
}
declare class Scene extends egret.DisplayObjectContainer { declare class Scene extends egret.DisplayObjectContainer {
camera: Camera; camera: Camera;
readonly entities: EntityList; readonly entities: EntityList;
@@ -297,7 +304,7 @@ declare class SceneManager {
static render(): void; static render(): void;
static startSceneTransition<T extends SceneTransition>(sceneTransition: T): T; static startSceneTransition<T extends SceneTransition>(sceneTransition: T): T;
} }
declare class Camera extends Component { declare class Camera extends Component implements IUpdatable {
private _zoom; private _zoom;
private _origin; private _origin;
private _minimumZoom; private _minimumZoom;
@@ -396,7 +403,7 @@ declare class SpriteRenderer extends RenderableComponent {
onRemovedFromEntity(): void; onRemovedFromEntity(): void;
reset(): void; reset(): void;
} }
declare class SpriteAnimator extends SpriteRenderer { declare class SpriteAnimator extends SpriteRenderer implements IUpdatable {
onAnimationCompletedEvent: Function; onAnimationCompletedEvent: Function;
speed: number; speed: number;
animationState: State; animationState: State;
@@ -450,7 +457,7 @@ declare class Mover extends Component {
applyMovement(motion: Vector2): void; applyMovement(motion: Vector2): void;
move(motion: Vector2): CollisionResult; move(motion: Vector2): CollisionResult;
} }
declare abstract class Collider extends Component { declare abstract class Collider extends Component implements IUpdatable {
shape: Shape; shape: Shape;
physicsLayer: number; physicsLayer: number;
isTrigger: boolean; isTrigger: boolean;
@@ -548,6 +555,7 @@ declare class ComponentList {
private _components; private _components;
private _componentsToAdd; private _componentsToAdd;
private _componentsToRemove; private _componentsToRemove;
private _updatableComponents;
private _tempBufferList; private _tempBufferList;
constructor(entity: Entity); constructor(entity: Entity);
readonly count: number; readonly count: number;

View File

@@ -300,6 +300,7 @@ var AStarPathfinder = (function () {
return "break"; return "break";
} }
graph.getNeighbors(current.data).forEach(function (next) { graph.getNeighbors(current.data).forEach(function (next) {
console.log(next);
var newCost = costSoFar.get(current.data) + graph.cost(current.data, next); var newCost = costSoFar.get(current.data) + graph.cost(current.data, next);
if (!_this.hasKey(costSoFar, next) || newCost < costSoFar.get(next)) { if (!_this.hasKey(costSoFar, next) || newCost < costSoFar.get(next)) {
costSoFar.set(next, newCost); costSoFar.set(next, newCost);
@@ -950,6 +951,7 @@ var Component = (function (_super) {
var _this = _super !== null && _super.apply(this, arguments) || this; var _this = _super !== null && _super.apply(this, arguments) || this;
_this._enabled = true; _this._enabled = true;
_this.updateInterval = 1; _this.updateInterval = 1;
_this._updateOrder = 0;
return _this; return _this;
} }
Object.defineProperty(Component.prototype, "enabled", { Object.defineProperty(Component.prototype, "enabled", {
@@ -981,6 +983,22 @@ var Component = (function (_super) {
} }
return this; return this;
}; };
Object.defineProperty(Component.prototype, "updateOrder", {
get: function () {
return this._updateOrder;
},
set: function (value) {
this.setUpdateOrder(value);
},
enumerable: true,
configurable: true
});
Component.prototype.setUpdateOrder = function (updateOrder) {
if (this._updateOrder != updateOrder) {
this._updateOrder = updateOrder;
}
return this;
};
Component.prototype.initialize = function () { Component.prototype.initialize = function () {
}; };
Component.prototype.onAddedToEntity = function () { Component.prototype.onAddedToEntity = function () {
@@ -991,8 +1009,6 @@ var Component = (function (_super) {
}; };
Component.prototype.onDisabled = function () { Component.prototype.onDisabled = function () {
}; };
Component.prototype.update = function () {
};
Component.prototype.debugRender = function () { Component.prototype.debugRender = function () {
}; };
Component.prototype.onEntityTransformChanged = function (comp) { Component.prototype.onEntityTransformChanged = function (comp) {
@@ -2492,6 +2508,7 @@ var ComponentList = (function () {
this._components = []; this._components = [];
this._componentsToAdd = []; this._componentsToAdd = [];
this._componentsToRemove = []; this._componentsToRemove = [];
this._updatableComponents = [];
this._tempBufferList = []; this._tempBufferList = [];
this._entity = entity; this._entity = entity;
} }
@@ -2513,6 +2530,8 @@ var ComponentList = (function () {
this._componentsToAdd.push(component); this._componentsToAdd.push(component);
}; };
ComponentList.prototype.remove = function (component) { ComponentList.prototype.remove = function (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)) { if (this._componentsToAdd.contains(component)) {
this._componentsToAdd.remove(component); this._componentsToAdd.remove(component);
return; return;
@@ -2524,6 +2543,7 @@ var ComponentList = (function () {
this.handleRemove(this._components[i]); this.handleRemove(this._components[i]);
} }
this._components.length = 0; this._components.length = 0;
this._updatableComponents.length = 0;
this._componentsToAdd.length = 0; this._componentsToAdd.length = 0;
this._componentsToRemove.length = 0; this._componentsToRemove.length = 0;
}; };
@@ -2532,6 +2552,8 @@ var ComponentList = (function () {
var component = this._components[i]; var component = this._components[i];
if (component instanceof RenderableComponent) if (component instanceof RenderableComponent)
this._entity.scene.renderableComponents.remove(component); 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.componentBits.set(ComponentTypeManager.getIndexFor(component), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity); this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
} }
@@ -2541,6 +2563,8 @@ var ComponentList = (function () {
var component = this._components[i]; var component = this._components[i];
if (component instanceof RenderableComponent) if (component instanceof RenderableComponent)
this._entity.scene.renderableComponents.add(component); this._entity.scene.renderableComponents.add(component);
if (egret.is(component, "IUpdatable"))
this._updatableComponents.push(component);
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component)); this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component));
this._entity.scene.entityProcessors.onComponentAdded(this._entity); this._entity.scene.entityProcessors.onComponentAdded(this._entity);
} }
@@ -2558,6 +2582,8 @@ var ComponentList = (function () {
var component = this._componentsToAdd[i]; var component = this._componentsToAdd[i];
if (component instanceof RenderableComponent) if (component instanceof RenderableComponent)
this._entity.scene.renderableComponents.add(component); this._entity.scene.renderableComponents.add(component);
if (egret.is(component, "IUpdatable"))
this._updatableComponents.push(component);
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component)); this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component));
this._entity.scene.entityProcessors.onComponentAdded(this._entity); this._entity.scene.entityProcessors.onComponentAdded(this._entity);
this._components.push(component); this._components.push(component);
@@ -2587,6 +2613,8 @@ var ComponentList = (function () {
ComponentList.prototype.handleRemove = function (component) { ComponentList.prototype.handleRemove = function (component) {
if (component instanceof RenderableComponent) if (component instanceof RenderableComponent)
this._entity.scene.renderableComponents.remove(component); 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.componentBits.set(ComponentTypeManager.getIndexFor(component), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity); this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
component.onRemovedFromEntity(); component.onRemovedFromEntity();
@@ -2640,10 +2668,16 @@ var ComponentList = (function () {
}; };
ComponentList.prototype.update = function () { ComponentList.prototype.update = function () {
this.updateLists(); this.updateLists();
for (var i = 0; i < this._components.length; i++) { for (var i = 0; i < this._updatableComponents.length; i++) {
var component = this._components[i]; var updatable = this._updatableComponents[i];
if (component.enabled && (component.updateInterval == 1 || Time.frameCount % component.updateInterval == 0)) var updateableComponent = void 0;
component.update(); if (updatable instanceof Component)
updateableComponent = updatable;
if (updatable.enabled &&
updateableComponent.enabled &&
(updateableComponent.updateInterval == 1 ||
Time.frameCount % updateableComponent.updateInterval == 0))
updatable.update();
} }
}; };
return ComponentList; return ComponentList;

File diff suppressed because one or more lines are too long

View File

@@ -73,14 +73,15 @@ class MainScene extends Scene {
} }
public astarTest() { public astarTest() {
let graph = new AstarGridGraph(20, 20); let graph = new AstarGridGraph(30, 30);
graph.weightedNodes.push(new Vector2(3, 3)); // graph.weightedNodes.push(new Vector2(3, 3));
graph.weightedNodes.push(new Vector2(3, 4)); // graph.weightedNodes.push(new Vector2(3, 4));
graph.weightedNodes.push(new Vector2(4, 3)); // graph.weightedNodes.push(new Vector2(4, 3));
graph.weightedNodes.push(new Vector2(4, 4)); // graph.weightedNodes.push(new Vector2(4, 4));
let path = graph.search(new Vector2(3, 4), new Vector2(15, 17)); let startTime = egret.getTimer();
console.log(path); let path = graph.search(new Vector2(1, 1), new Vector2(29, 29));
console.log(egret.getTimer() - startTime);
} }
} }

View File

@@ -193,15 +193,17 @@ declare abstract class Component extends egret.DisplayObjectContainer {
private _enabled; private _enabled;
updateInterval: number; updateInterval: number;
userData: any; userData: any;
private _updateOrder;
enabled: boolean; enabled: boolean;
readonly localPosition: Vector2; readonly localPosition: Vector2;
setEnabled(isEnabled: boolean): this; setEnabled(isEnabled: boolean): this;
updateOrder: number;
setUpdateOrder(updateOrder: number): this;
initialize(): void; initialize(): void;
onAddedToEntity(): void; onAddedToEntity(): void;
onRemovedFromEntity(): void; onRemovedFromEntity(): void;
onEnabled(): void; onEnabled(): void;
onDisabled(): void; onDisabled(): void;
update(): void;
debugRender(): void; debugRender(): void;
onEntityTransformChanged(comp: TransformComponent): void; onEntityTransformChanged(comp: TransformComponent): void;
registerComponent(): void; registerComponent(): void;
@@ -253,6 +255,11 @@ declare enum TransformComponent {
scale = 1, scale = 1,
position = 2 position = 2
} }
interface IUpdatable {
enabled: boolean;
updateOrder: number;
update(): any;
}
declare class Scene extends egret.DisplayObjectContainer { declare class Scene extends egret.DisplayObjectContainer {
camera: Camera; camera: Camera;
readonly entities: EntityList; readonly entities: EntityList;
@@ -297,7 +304,7 @@ declare class SceneManager {
static render(): void; static render(): void;
static startSceneTransition<T extends SceneTransition>(sceneTransition: T): T; static startSceneTransition<T extends SceneTransition>(sceneTransition: T): T;
} }
declare class Camera extends Component { declare class Camera extends Component implements IUpdatable {
private _zoom; private _zoom;
private _origin; private _origin;
private _minimumZoom; private _minimumZoom;
@@ -396,7 +403,7 @@ declare class SpriteRenderer extends RenderableComponent {
onRemovedFromEntity(): void; onRemovedFromEntity(): void;
reset(): void; reset(): void;
} }
declare class SpriteAnimator extends SpriteRenderer { declare class SpriteAnimator extends SpriteRenderer implements IUpdatable {
onAnimationCompletedEvent: Function; onAnimationCompletedEvent: Function;
speed: number; speed: number;
animationState: State; animationState: State;
@@ -450,7 +457,7 @@ declare class Mover extends Component {
applyMovement(motion: Vector2): void; applyMovement(motion: Vector2): void;
move(motion: Vector2): CollisionResult; move(motion: Vector2): CollisionResult;
} }
declare abstract class Collider extends Component { declare abstract class Collider extends Component implements IUpdatable {
shape: Shape; shape: Shape;
physicsLayer: number; physicsLayer: number;
isTrigger: boolean; isTrigger: boolean;
@@ -548,6 +555,7 @@ declare class ComponentList {
private _components; private _components;
private _componentsToAdd; private _componentsToAdd;
private _componentsToRemove; private _componentsToRemove;
private _updatableComponents;
private _tempBufferList; private _tempBufferList;
constructor(entity: Entity); constructor(entity: Entity);
readonly count: number; readonly count: number;

View File

@@ -300,6 +300,7 @@ var AStarPathfinder = (function () {
return "break"; return "break";
} }
graph.getNeighbors(current.data).forEach(function (next) { graph.getNeighbors(current.data).forEach(function (next) {
console.log(next);
var newCost = costSoFar.get(current.data) + graph.cost(current.data, next); var newCost = costSoFar.get(current.data) + graph.cost(current.data, next);
if (!_this.hasKey(costSoFar, next) || newCost < costSoFar.get(next)) { if (!_this.hasKey(costSoFar, next) || newCost < costSoFar.get(next)) {
costSoFar.set(next, newCost); costSoFar.set(next, newCost);
@@ -950,6 +951,7 @@ var Component = (function (_super) {
var _this = _super !== null && _super.apply(this, arguments) || this; var _this = _super !== null && _super.apply(this, arguments) || this;
_this._enabled = true; _this._enabled = true;
_this.updateInterval = 1; _this.updateInterval = 1;
_this._updateOrder = 0;
return _this; return _this;
} }
Object.defineProperty(Component.prototype, "enabled", { Object.defineProperty(Component.prototype, "enabled", {
@@ -981,6 +983,22 @@ var Component = (function (_super) {
} }
return this; return this;
}; };
Object.defineProperty(Component.prototype, "updateOrder", {
get: function () {
return this._updateOrder;
},
set: function (value) {
this.setUpdateOrder(value);
},
enumerable: true,
configurable: true
});
Component.prototype.setUpdateOrder = function (updateOrder) {
if (this._updateOrder != updateOrder) {
this._updateOrder = updateOrder;
}
return this;
};
Component.prototype.initialize = function () { Component.prototype.initialize = function () {
}; };
Component.prototype.onAddedToEntity = function () { Component.prototype.onAddedToEntity = function () {
@@ -991,8 +1009,6 @@ var Component = (function (_super) {
}; };
Component.prototype.onDisabled = function () { Component.prototype.onDisabled = function () {
}; };
Component.prototype.update = function () {
};
Component.prototype.debugRender = function () { Component.prototype.debugRender = function () {
}; };
Component.prototype.onEntityTransformChanged = function (comp) { Component.prototype.onEntityTransformChanged = function (comp) {
@@ -2492,6 +2508,7 @@ var ComponentList = (function () {
this._components = []; this._components = [];
this._componentsToAdd = []; this._componentsToAdd = [];
this._componentsToRemove = []; this._componentsToRemove = [];
this._updatableComponents = [];
this._tempBufferList = []; this._tempBufferList = [];
this._entity = entity; this._entity = entity;
} }
@@ -2513,6 +2530,8 @@ var ComponentList = (function () {
this._componentsToAdd.push(component); this._componentsToAdd.push(component);
}; };
ComponentList.prototype.remove = function (component) { ComponentList.prototype.remove = function (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)) { if (this._componentsToAdd.contains(component)) {
this._componentsToAdd.remove(component); this._componentsToAdd.remove(component);
return; return;
@@ -2524,6 +2543,7 @@ var ComponentList = (function () {
this.handleRemove(this._components[i]); this.handleRemove(this._components[i]);
} }
this._components.length = 0; this._components.length = 0;
this._updatableComponents.length = 0;
this._componentsToAdd.length = 0; this._componentsToAdd.length = 0;
this._componentsToRemove.length = 0; this._componentsToRemove.length = 0;
}; };
@@ -2532,6 +2552,8 @@ var ComponentList = (function () {
var component = this._components[i]; var component = this._components[i];
if (component instanceof RenderableComponent) if (component instanceof RenderableComponent)
this._entity.scene.renderableComponents.remove(component); 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.componentBits.set(ComponentTypeManager.getIndexFor(component), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity); this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
} }
@@ -2541,6 +2563,8 @@ var ComponentList = (function () {
var component = this._components[i]; var component = this._components[i];
if (component instanceof RenderableComponent) if (component instanceof RenderableComponent)
this._entity.scene.renderableComponents.add(component); this._entity.scene.renderableComponents.add(component);
if (egret.is(component, "IUpdatable"))
this._updatableComponents.push(component);
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component)); this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component));
this._entity.scene.entityProcessors.onComponentAdded(this._entity); this._entity.scene.entityProcessors.onComponentAdded(this._entity);
} }
@@ -2558,6 +2582,8 @@ var ComponentList = (function () {
var component = this._componentsToAdd[i]; var component = this._componentsToAdd[i];
if (component instanceof RenderableComponent) if (component instanceof RenderableComponent)
this._entity.scene.renderableComponents.add(component); this._entity.scene.renderableComponents.add(component);
if (egret.is(component, "IUpdatable"))
this._updatableComponents.push(component);
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component)); this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component));
this._entity.scene.entityProcessors.onComponentAdded(this._entity); this._entity.scene.entityProcessors.onComponentAdded(this._entity);
this._components.push(component); this._components.push(component);
@@ -2587,6 +2613,8 @@ var ComponentList = (function () {
ComponentList.prototype.handleRemove = function (component) { ComponentList.prototype.handleRemove = function (component) {
if (component instanceof RenderableComponent) if (component instanceof RenderableComponent)
this._entity.scene.renderableComponents.remove(component); 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.componentBits.set(ComponentTypeManager.getIndexFor(component), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity); this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
component.onRemovedFromEntity(); component.onRemovedFromEntity();
@@ -2640,10 +2668,16 @@ var ComponentList = (function () {
}; };
ComponentList.prototype.update = function () { ComponentList.prototype.update = function () {
this.updateLists(); this.updateLists();
for (var i = 0; i < this._components.length; i++) { for (var i = 0; i < this._updatableComponents.length; i++) {
var component = this._components[i]; var updatable = this._updatableComponents[i];
if (component.enabled && (component.updateInterval == 1 || Time.frameCount % component.updateInterval == 0)) var updateableComponent = void 0;
component.update(); if (updatable instanceof Component)
updateableComponent = updatable;
if (updatable.enabled &&
updateableComponent.enabled &&
(updateableComponent.updateInterval == 1 ||
Time.frameCount % updateableComponent.updateInterval == 0))
updatable.update();
} }
}; };
return ComponentList; return ComponentList;

File diff suppressed because one or more lines are too long

View File

@@ -29,6 +29,7 @@ class AStarPathfinder {
} }
graph.getNeighbors(current.data).forEach(next => { graph.getNeighbors(current.data).forEach(next => {
console.log(next);
let newCost = costSoFar.get(current.data) + graph.cost(current.data, next); let newCost = costSoFar.get(current.data) + graph.cost(current.data, next);
if (!this.hasKey(costSoFar, next) || newCost < costSoFar.get(next)){ if (!this.hasKey(costSoFar, next) || newCost < costSoFar.get(next)){
costSoFar.set(next, newCost); costSoFar.set(next, newCost);

View File

@@ -4,6 +4,7 @@ abstract class Component extends egret.DisplayObjectContainer {
public updateInterval: number = 1; public updateInterval: number = 1;
/** 允许用户为实体存入信息 */ /** 允许用户为实体存入信息 */
public userData: any; public userData: any;
private _updateOrder = 0;
public get enabled(){ public get enabled(){
return this.entity ? this.entity.enabled && this._enabled : this._enabled; return this.entity ? this.entity.enabled && this._enabled : this._enabled;
@@ -31,6 +32,22 @@ abstract class Component extends egret.DisplayObjectContainer {
return this; return this;
} }
/** 更新此实体上组件的顺序 */
public get updateOrder(){
return this._updateOrder;
}
/** 更新此实体上组件的顺序 */
public set updateOrder(value: number){
this.setUpdateOrder(value);
}
public setUpdateOrder(updateOrder: number){
if (this._updateOrder != updateOrder){
this._updateOrder = updateOrder;
}
return this;
}
public initialize(){ public initialize(){
} }
@@ -50,10 +67,6 @@ abstract class Component extends egret.DisplayObjectContainer {
} }
public update(){
}
public debugRender(){ public debugRender(){
} }

View File

@@ -1,5 +1,5 @@
///<reference path="../Component.ts"/> ///<reference path="../Component.ts"/>
class Camera extends Component { class Camera extends Component implements IUpdatable {
private _zoom; private _zoom;
private _origin: Vector2 = Vector2.zero; private _origin: Vector2 = Vector2.zero;

View File

@@ -1,4 +1,4 @@
abstract class Collider extends Component { abstract class Collider extends Component implements IUpdatable {
/** 对撞机的基本形状 */ /** 对撞机的基本形状 */
public shape: Shape; public shape: Shape;
/** 在处理冲突时physicsLayer可以用作过滤器。Flags类有帮助位掩码的方法。 */ /** 在处理冲突时physicsLayer可以用作过滤器。Flags类有帮助位掩码的方法。 */

View File

@@ -1,5 +1,5 @@
///<reference path="./SpriteRenderer.ts" /> ///<reference path="./SpriteRenderer.ts" />
class SpriteAnimator extends SpriteRenderer { class SpriteAnimator extends SpriteRenderer implements IUpdatable {
/** 在动画完成时触发,包括动画名称; */ /** 在动画完成时触发,包括动画名称; */
public onAnimationCompletedEvent: Function; public onAnimationCompletedEvent: Function;
/** 动画播放速度 */ /** 动画播放速度 */

View File

@@ -0,0 +1,6 @@
/** 当将该接口添加到组件时,只要启用了组件和实体,它就需要调用每帧的更新方法。 */
interface IUpdatable {
enabled: boolean;
updateOrder: number;
update();
}

View File

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