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

View File

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