优化updatelist增快addEntity速度

This commit is contained in:
yhh
2020-10-09 15:39:06 +08:00
parent c9c745c730
commit 74fcfd7778
8 changed files with 137 additions and 114 deletions
+8
View File
@@ -1169,6 +1169,13 @@ declare class TimeUtils {
static secondToTime(time?: number, partition?: string, showHour?: boolean): string; static secondToTime(time?: number, partition?: string, showHour?: boolean): string;
static timeToMillisecond(time: string, partition?: string): string; static timeToMillisecond(time: string, partition?: string): string;
} }
declare module es {
class Graphics {
static Instance: Graphics;
pixelTexture: Sprite;
constructor();
}
}
declare module es { declare module es {
class GraphicsCapabilities extends egret.Capabilities { class GraphicsCapabilities extends egret.Capabilities {
initialize(device: GraphicsDevice): void; initialize(device: GraphicsDevice): void;
@@ -2229,6 +2236,7 @@ declare module es {
declare module es { declare module es {
class Vector2Ext { class Vector2Ext {
static isTriangleCCW(a: Vector2, center: Vector2, c: Vector2): boolean; static isTriangleCCW(a: Vector2, center: Vector2, c: Vector2): boolean;
static halfVector(): Vector2;
static cross(u: Vector2, v: Vector2): number; static cross(u: Vector2, v: Vector2): number;
static perpendicular(first: Vector2, second: Vector2): Vector2; static perpendicular(first: Vector2, second: Vector2): Vector2;
static normalize(vec: Vector2): void; static normalize(vec: Vector2): void;
+50 -19
View File
@@ -1118,6 +1118,7 @@ var es;
Core.emitter.emit(es.CoreEvents.GraphicsDeviceReset); Core.emitter.emit(es.CoreEvents.GraphicsDeviceReset);
}; };
Core.prototype.initialize = function () { Core.prototype.initialize = function () {
es.Graphics.Instance = new es.Graphics();
}; };
Core.prototype.update = function () { Core.prototype.update = function () {
return __awaiter(this, void 0, void 0, function () { return __awaiter(this, void 0, void 0, function () {
@@ -4845,44 +4846,53 @@ var es;
} }
}; };
EntityList.prototype.updateLists = function () { EntityList.prototype.updateLists = function () {
var _this = this;
if (this._entitiesToRemove.length > 0) { if (this._entitiesToRemove.length > 0) {
var temp = this._entitiesToRemove; var temp = this._entitiesToRemove;
this._entitiesToRemove = this._tempEntityList; this._entitiesToRemove = this._tempEntityList;
this._tempEntityList = temp; this._tempEntityList = temp;
this._tempEntityList.forEach(function (entity) { for (var _i = 0, _a = this._tempEntityList; _i < _a.length; _i++) {
_this.removeFromTagList(entity); var entity = _a[_i];
_this._entities.remove(entity); this.removeFromTagList(entity);
this._entities.remove(entity);
entity.onRemovedFromScene(); entity.onRemovedFromScene();
entity.scene = null; entity.scene = null;
_this.scene.entityProcessors.onEntityRemoved(entity); this.scene.entityProcessors.onEntityRemoved(entity);
}); }
this._tempEntityList.length = 0; this._tempEntityList.length = 0;
} }
if (this._entitiesToAdded.length > 0) { if (this._entitiesToAdded.length > 0) {
var temp = this._entitiesToAdded; var temp = this._entitiesToAdded;
this._entitiesToAdded = this._tempEntityList; this._entitiesToAdded = this._tempEntityList;
this._tempEntityList = temp; this._tempEntityList = temp;
this._tempEntityList.forEach(function (entity) { for (var _b = 0, _c = this._tempEntityList; _b < _c.length; _b++) {
if (!_this._entities.contains(entity)) { var entity = _c[_b];
_this._entities.push(entity); if (!this._entities.contains(entity)) {
entity.scene = _this.scene; this._entities.push(entity);
_this.addToTagList(entity); entity.scene = this.scene;
_this.scene.entityProcessors.onEntityAdded(entity); this.addToTagList(entity);
this.scene.entityProcessors.onEntityAdded(entity);
}
}
for (var _d = 0, _e = this._tempEntityList; _d < _e.length; _d++) {
var entity = _e[_d];
entity.onAddedToScene();
} }
});
this._tempEntityList.forEach(function (entity) { return entity.onAddedToScene(); });
this._tempEntityList.length = 0; this._tempEntityList.length = 0;
this._isEntityListUnsorted = true; this._isEntityListUnsorted = true;
} }
if (this._isEntityListUnsorted) { if (this._isEntityListUnsorted) {
this._entities.sort(); this._entities.sort(function (a, b) {
return a.compareTo(b);
});
this._isEntityListUnsorted = false; this._isEntityListUnsorted = false;
} }
if (this._unsortedTags.length > 0) { if (this._unsortedTags.length > 0) {
this._unsortedTags.forEach(function (tag) { for (var _f = 0, _g = this._unsortedTags; _f < _g.length; _f++) {
_this._entityDict.get(tag).sort(); var tag = _g[_f];
this._entityDict.get(tag).sort(function (a, b) {
return a.compareTo(b);
}); });
}
this._unsortedTags.length = 0; this._unsortedTags.length = 0;
} }
}; };
@@ -4906,10 +4916,11 @@ var es;
if (this._entities[i] instanceof type) if (this._entities[i] instanceof type)
list.push(this._entities[i]); list.push(this._entities[i]);
} }
this._entitiesToAdded.forEach(function (entity) { for (var _i = 0, _a = this._entitiesToAdded; _i < _a.length; _i++) {
var entity = _a[_i];
if (entity instanceof type) if (entity instanceof type)
list.push(entity); list.push(entity);
}); }
return list; return list;
}; };
EntityList.prototype.findComponentOfType = function (type) { EntityList.prototype.findComponentOfType = function (type) {
@@ -5678,6 +5689,23 @@ var TimeUtils = (function () {
return TimeUtils; return TimeUtils;
}()); }());
var es; var es;
(function (es) {
var Graphics = (function () {
function Graphics() {
var _this = this;
var arrayBuffer = new ArrayBuffer(1);
arrayBuffer[0] = 0xffffff;
egret.BitmapData.create("arraybuffer", arrayBuffer, function (bitmapData) {
var tex = new egret.Texture();
tex.bitmapData = bitmapData;
_this.pixelTexture = new es.Sprite(tex);
});
}
return Graphics;
}());
es.Graphics = Graphics;
})(es || (es = {}));
var es;
(function (es) { (function (es) {
var GraphicsCapabilities = (function (_super) { var GraphicsCapabilities = (function (_super) {
__extends(GraphicsCapabilities, _super); __extends(GraphicsCapabilities, _super);
@@ -10687,6 +10715,9 @@ var es;
Vector2Ext.isTriangleCCW = function (a, center, c) { Vector2Ext.isTriangleCCW = function (a, center, c) {
return this.cross(es.Vector2.subtract(center, a), es.Vector2.subtract(c, center)) < 0; return this.cross(es.Vector2.subtract(center, a), es.Vector2.subtract(c, center)) < 0;
}; };
Vector2Ext.halfVector = function () {
return new es.Vector2(0.5, 0.5);
};
Vector2Ext.cross = function (u, v) { Vector2Ext.cross = function (u, v) {
return u.y * v.x - u.x * v.y; return u.y * v.x - u.x * v.y;
}; };
File diff suppressed because one or more lines are too long
+8
View File
@@ -1169,6 +1169,13 @@ declare class TimeUtils {
static secondToTime(time?: number, partition?: string, showHour?: boolean): string; static secondToTime(time?: number, partition?: string, showHour?: boolean): string;
static timeToMillisecond(time: string, partition?: string): string; static timeToMillisecond(time: string, partition?: string): string;
} }
declare module es {
class Graphics {
static Instance: Graphics;
pixelTexture: Sprite;
constructor();
}
}
declare module es { declare module es {
class GraphicsCapabilities extends egret.Capabilities { class GraphicsCapabilities extends egret.Capabilities {
initialize(device: GraphicsDevice): void; initialize(device: GraphicsDevice): void;
@@ -2229,6 +2236,7 @@ declare module es {
declare module es { declare module es {
class Vector2Ext { class Vector2Ext {
static isTriangleCCW(a: Vector2, center: Vector2, c: Vector2): boolean; static isTriangleCCW(a: Vector2, center: Vector2, c: Vector2): boolean;
static halfVector(): Vector2;
static cross(u: Vector2, v: Vector2): number; static cross(u: Vector2, v: Vector2): number;
static perpendicular(first: Vector2, second: Vector2): Vector2; static perpendicular(first: Vector2, second: Vector2): Vector2;
static normalize(vec: Vector2): void; static normalize(vec: Vector2): void;
+50 -19
View File
@@ -1118,6 +1118,7 @@ var es;
Core.emitter.emit(es.CoreEvents.GraphicsDeviceReset); Core.emitter.emit(es.CoreEvents.GraphicsDeviceReset);
}; };
Core.prototype.initialize = function () { Core.prototype.initialize = function () {
es.Graphics.Instance = new es.Graphics();
}; };
Core.prototype.update = function () { Core.prototype.update = function () {
return __awaiter(this, void 0, void 0, function () { return __awaiter(this, void 0, void 0, function () {
@@ -4845,44 +4846,53 @@ var es;
} }
}; };
EntityList.prototype.updateLists = function () { EntityList.prototype.updateLists = function () {
var _this = this;
if (this._entitiesToRemove.length > 0) { if (this._entitiesToRemove.length > 0) {
var temp = this._entitiesToRemove; var temp = this._entitiesToRemove;
this._entitiesToRemove = this._tempEntityList; this._entitiesToRemove = this._tempEntityList;
this._tempEntityList = temp; this._tempEntityList = temp;
this._tempEntityList.forEach(function (entity) { for (var _i = 0, _a = this._tempEntityList; _i < _a.length; _i++) {
_this.removeFromTagList(entity); var entity = _a[_i];
_this._entities.remove(entity); this.removeFromTagList(entity);
this._entities.remove(entity);
entity.onRemovedFromScene(); entity.onRemovedFromScene();
entity.scene = null; entity.scene = null;
_this.scene.entityProcessors.onEntityRemoved(entity); this.scene.entityProcessors.onEntityRemoved(entity);
}); }
this._tempEntityList.length = 0; this._tempEntityList.length = 0;
} }
if (this._entitiesToAdded.length > 0) { if (this._entitiesToAdded.length > 0) {
var temp = this._entitiesToAdded; var temp = this._entitiesToAdded;
this._entitiesToAdded = this._tempEntityList; this._entitiesToAdded = this._tempEntityList;
this._tempEntityList = temp; this._tempEntityList = temp;
this._tempEntityList.forEach(function (entity) { for (var _b = 0, _c = this._tempEntityList; _b < _c.length; _b++) {
if (!_this._entities.contains(entity)) { var entity = _c[_b];
_this._entities.push(entity); if (!this._entities.contains(entity)) {
entity.scene = _this.scene; this._entities.push(entity);
_this.addToTagList(entity); entity.scene = this.scene;
_this.scene.entityProcessors.onEntityAdded(entity); this.addToTagList(entity);
this.scene.entityProcessors.onEntityAdded(entity);
}
}
for (var _d = 0, _e = this._tempEntityList; _d < _e.length; _d++) {
var entity = _e[_d];
entity.onAddedToScene();
} }
});
this._tempEntityList.forEach(function (entity) { return entity.onAddedToScene(); });
this._tempEntityList.length = 0; this._tempEntityList.length = 0;
this._isEntityListUnsorted = true; this._isEntityListUnsorted = true;
} }
if (this._isEntityListUnsorted) { if (this._isEntityListUnsorted) {
this._entities.sort(); this._entities.sort(function (a, b) {
return a.compareTo(b);
});
this._isEntityListUnsorted = false; this._isEntityListUnsorted = false;
} }
if (this._unsortedTags.length > 0) { if (this._unsortedTags.length > 0) {
this._unsortedTags.forEach(function (tag) { for (var _f = 0, _g = this._unsortedTags; _f < _g.length; _f++) {
_this._entityDict.get(tag).sort(); var tag = _g[_f];
this._entityDict.get(tag).sort(function (a, b) {
return a.compareTo(b);
}); });
}
this._unsortedTags.length = 0; this._unsortedTags.length = 0;
} }
}; };
@@ -4906,10 +4916,11 @@ var es;
if (this._entities[i] instanceof type) if (this._entities[i] instanceof type)
list.push(this._entities[i]); list.push(this._entities[i]);
} }
this._entitiesToAdded.forEach(function (entity) { for (var _i = 0, _a = this._entitiesToAdded; _i < _a.length; _i++) {
var entity = _a[_i];
if (entity instanceof type) if (entity instanceof type)
list.push(entity); list.push(entity);
}); }
return list; return list;
}; };
EntityList.prototype.findComponentOfType = function (type) { EntityList.prototype.findComponentOfType = function (type) {
@@ -5678,6 +5689,23 @@ var TimeUtils = (function () {
return TimeUtils; return TimeUtils;
}()); }());
var es; var es;
(function (es) {
var Graphics = (function () {
function Graphics() {
var _this = this;
var arrayBuffer = new ArrayBuffer(1);
arrayBuffer[0] = 0xffffff;
egret.BitmapData.create("arraybuffer", arrayBuffer, function (bitmapData) {
var tex = new egret.Texture();
tex.bitmapData = bitmapData;
_this.pixelTexture = new es.Sprite(tex);
});
}
return Graphics;
}());
es.Graphics = Graphics;
})(es || (es = {}));
var es;
(function (es) { (function (es) {
var GraphicsCapabilities = (function (_super) { var GraphicsCapabilities = (function (_super) {
__extends(GraphicsCapabilities, _super); __extends(GraphicsCapabilities, _super);
@@ -10687,6 +10715,9 @@ var es;
Vector2Ext.isTriangleCCW = function (a, center, c) { Vector2Ext.isTriangleCCW = function (a, center, c) {
return this.cross(es.Vector2.subtract(center, a), es.Vector2.subtract(c, center)) < 0; return this.cross(es.Vector2.subtract(center, a), es.Vector2.subtract(c, center)) < 0;
}; };
Vector2Ext.halfVector = function () {
return new es.Vector2(0.5, 0.5);
};
Vector2Ext.cross = function (u, v) { Vector2Ext.cross = function (u, v) {
return u.y * v.x - u.x * v.y; return u.y * v.x - u.x * v.y;
}; };
+1 -1
View File
File diff suppressed because one or more lines are too long
@@ -1,61 +0,0 @@
module es {
export class PrototypeSpriteRenderer extends SpriteRenderer {
public get width(): number {
return this._width;
}
public get height(): number {
return this._height;
}
public get bounds(): Rectangle {
if (this._areBoundsDirty){
this._bounds.calculateBounds(this.entity.transform.position, this._localOffset, this._origin, this.entity.transform.scale,
this.entity.transform.rotation, this._width, this._height);
this._areBoundsDirty = false;
}
return this._bounds;
}
public skewTopX: number = 0;
public skewBottomX: number = 0;
public skewLeftY: number = 0;
public skewRightY: number = 0;
public _width: number = 0;
public _height: number = 0;
constructor(width: number = 50, height: number = 50){
super(Graphics.Instance.pixelTexture);
this._width = width;
this._height = height;
}
public setWidth(width: number): PrototypeSpriteRenderer {
this._width = width;
return this;
}
public setHeight(height: number): PrototypeSpriteRenderer {
this._height = height;
return this;
}
public setSkew(skewTopX: number, skewBottomX: number, skewLeftY: number, skewRightY: number): PrototypeSpriteRenderer{
this.skewTopX = skewTopX;
this.skewBottomX = skewBottomX;
this.skewLeftY = skewLeftY;
this.skewRightY = skewRightY;
return this;
}
public onAddedToEntity() {
this.originNormalized = Vector2Ext.halfVector();
}
public render(camera: es.Camera) {
}
}
}
+16 -10
View File
@@ -144,7 +144,7 @@ module es {
let temp = this._entitiesToRemove; let temp = this._entitiesToRemove;
this._entitiesToRemove = this._tempEntityList; this._entitiesToRemove = this._tempEntityList;
this._tempEntityList = temp; this._tempEntityList = temp;
this._tempEntityList.forEach(entity => { for (const entity of this._tempEntityList) {
this.removeFromTagList(entity); this.removeFromTagList(entity);
this._entities.remove(entity); this._entities.remove(entity);
@@ -152,7 +152,7 @@ module es {
entity.scene = null; entity.scene = null;
this.scene.entityProcessors.onEntityRemoved(entity); this.scene.entityProcessors.onEntityRemoved(entity);
}); }
this._tempEntityList.length = 0; this._tempEntityList.length = 0;
} }
@@ -161,7 +161,7 @@ module es {
let temp = this._entitiesToAdded; let temp = this._entitiesToAdded;
this._entitiesToAdded = this._tempEntityList; this._entitiesToAdded = this._tempEntityList;
this._tempEntityList = temp; this._tempEntityList = temp;
this._tempEntityList.forEach(entity => { for (const entity of this._tempEntityList) {
if (!this._entities.contains(entity)) { if (!this._entities.contains(entity)) {
this._entities.push(entity); this._entities.push(entity);
entity.scene = this.scene; entity.scene = this.scene;
@@ -170,23 +170,29 @@ module es {
this.scene.entityProcessors.onEntityAdded(entity) this.scene.entityProcessors.onEntityAdded(entity)
} }
}); }
// 现在所有实体都被添加到场景中,我们再次循环并调用onAddedToScene // 现在所有实体都被添加到场景中,我们再次循环并调用onAddedToScene
this._tempEntityList.forEach(entity => entity.onAddedToScene()); for (const entity of this._tempEntityList) {
entity.onAddedToScene();
}
this._tempEntityList.length = 0; this._tempEntityList.length = 0;
this._isEntityListUnsorted = true; this._isEntityListUnsorted = true;
} }
if (this._isEntityListUnsorted) { if (this._isEntityListUnsorted) {
this._entities.sort(); this._entities.sort((a, b)=>{
return a.compareTo(b);
});
this._isEntityListUnsorted = false; this._isEntityListUnsorted = false;
} }
if (this._unsortedTags.length > 0) { if (this._unsortedTags.length > 0) {
this._unsortedTags.forEach(tag => { for (const tag of this._unsortedTags) {
this._entityDict.get(tag).sort(); this._entityDict.get(tag).sort((a, b) => {
return a.compareTo(b);
}); });
}
this._unsortedTags.length = 0; this._unsortedTags.length = 0;
} }
@@ -229,10 +235,10 @@ module es {
if (this._entities[i] instanceof type) if (this._entities[i] instanceof type)
list.push(this._entities[i] as T); list.push(this._entities[i] as T);
} }
this._entitiesToAdded.forEach(entity => { for (const entity of this._entitiesToAdded) {
if (entity instanceof type) if (entity instanceof type)
list.push(entity as T); list.push(entity as T);
}); }
return list; return list;
} }