优化updateList,性能提高百倍!

This commit is contained in:
yhh
2021-02-01 11:11:53 +08:00
parent 25fca1ed56
commit d7ec4d087c
6 changed files with 50 additions and 60 deletions

View File

@@ -1683,7 +1683,7 @@ declare module es {
/**
* 通过标签跟踪实体,便于检索
*/
_entityDict: Map<number, Entity[]>;
_entityDict: Map<number, Set<Entity>>;
_unsortedTags: Set<number>;
constructor(scene: Scene);
readonly count: number;
@@ -1709,7 +1709,7 @@ declare module es {
* @param entity
*/
contains(entity: Entity): boolean;
getTagList(tag: number): Entity[];
getTagList(tag: number): Set<Entity>;
addToTagList(entity: Entity): void;
removeFromTagList(entity: Entity): void;
update(): void;

View File

@@ -4078,23 +4078,18 @@ var es;
EntityList.prototype.getTagList = function (tag) {
var list = this._entityDict.get(tag);
if (!list) {
list = [];
list = new Set();
this._entityDict.set(tag, list);
}
return list;
};
EntityList.prototype.addToTagList = function (entity) {
var list = this.getTagList(entity.tag);
if (list.findIndex(function (e) { return e.id == entity.id; }) == -1) {
list.push(entity);
this._unsortedTags.add(entity.tag);
}
this.getTagList(entity.tag).add(entity);
this._unsortedTags.add(entity.tag);
};
EntityList.prototype.removeFromTagList = function (entity) {
var list = this._entityDict.get(entity.tag);
if (list) {
new linq.List(list).remove(entity);
}
list.delete(entity);
};
EntityList.prototype.update = function () {
var e_11, _a;
@@ -4144,11 +4139,6 @@ var es;
this._entities.sort(es.Entity.entityComparer.compare);
this._isEntityListUnsorted = false;
}
// 根据需要对标签列表进行排序
if (this._unsortedTags.size > 0) {
this._unsortedTags.forEach(function (value) { return _this._entityDict.get(value).sort(function (a, b) { return a.compareTo(b); }); });
this._unsortedTags.clear();
}
};
/**
* 返回第一个找到的名字为name的实体。如果没有找到则返回null
@@ -4172,11 +4162,23 @@ var es;
* @param tag
*/
EntityList.prototype.entitiesWithTag = function (tag) {
var e_12, _a;
var list = this.getTagList(tag);
var returnList = es.ListPool.obtain();
returnList.length = this._entities.length;
for (var i = 0; i < list.length; i++)
returnList.push(list[i]);
try {
for (var list_1 = __values(list), list_1_1 = list_1.next(); !list_1_1.done; list_1_1 = list_1.next()) {
var entity = list_1_1.value;
returnList.push(entity);
}
}
catch (e_12_1) { e_12 = { error: e_12_1 }; }
finally {
try {
if (list_1_1 && !list_1_1.done && (_a = list_1.return)) _a.call(list_1);
}
finally { if (e_12) throw e_12.error; }
}
return returnList;
};
/**
@@ -7151,7 +7153,7 @@ var es;
* @param layerMask
*/
SpatialHash.prototype.overlapRectangle = function (rect, results, layerMask) {
var e_12, _a;
var e_13, _a;
this._overlapTestBox.updateBox(rect.width, rect.height);
this._overlapTestBox.position = rect.location;
var resultCounter = 0;
@@ -7182,12 +7184,12 @@ var es;
return resultCounter;
}
}
catch (e_12_1) { e_12 = { error: e_12_1 }; }
catch (e_13_1) { e_13 = { error: e_13_1 }; }
finally {
try {
if (potentials_1_1 && !potentials_1_1.done && (_a = potentials_1.return)) _a.call(potentials_1);
}
finally { if (e_12) throw e_12.error; }
finally { if (e_13) throw e_13.error; }
}
return resultCounter;
};
@@ -7199,7 +7201,7 @@ var es;
* @param layerMask
*/
SpatialHash.prototype.overlapCircle = function (circleCenter, radius, results, layerMask) {
var e_13, _a;
var e_14, _a;
var bounds = new es.Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2);
this._overlapTestCircle.radius = radius;
this._overlapTestCircle.position = circleCenter;
@@ -7232,12 +7234,12 @@ var es;
return resultCounter;
}
}
catch (e_13_1) { e_13 = { error: e_13_1 }; }
catch (e_14_1) { e_14 = { error: e_14_1 }; }
finally {
try {
if (potentials_2_1 && !potentials_2_1.done && (_a = potentials_2.return)) _a.call(potentials_2);
}
finally { if (e_13) throw e_13.error; }
finally { if (e_14) throw e_14.error; }
}
return resultCounter;
};
@@ -9072,7 +9074,7 @@ var es;
* 重置标记记录
*/
TimeRuler.prototype.resetLog = function () {
var e_14, _a;
var e_15, _a;
if (!es.Core.Instance.debug)
return;
try {
@@ -9090,12 +9092,12 @@ var es;
}
}
}
catch (e_14_1) { e_14 = { error: e_14_1 }; }
catch (e_15_1) { e_15 = { error: e_15_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_14) throw e_14.error; }
finally { if (e_15) throw e_15.error; }
}
};
/**
@@ -11685,7 +11687,7 @@ var linq;
* 创建一个Set从一个Enumerable.List< T>。
*/
List.prototype.toSet = function () {
var e_15, _a;
var e_16, _a;
var result = new Set();
try {
for (var _b = __values(this._elements), _c = _b.next(); !_c.done; _c = _b.next()) {
@@ -11693,12 +11695,12 @@ var linq;
result.add(x);
}
}
catch (e_15_1) { e_15 = { error: e_15_1 }; }
catch (e_16_1) { e_16 = { error: e_16_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_15) throw e_15.error; }
finally { if (e_16) throw e_16.error; }
}
return result;
};
@@ -11947,7 +11949,7 @@ var es;
* 计算可见性多边形并返回三角形扇形的顶点减去中心顶点。返回的数组来自ListPool
*/
VisibilityComputer.prototype.end = function () {
var e_16, _a;
var e_17, _a;
var output = es.ListPool.obtain();
this.updateSegments();
this._endPoints.sort(this._radialComparer.compare);
@@ -11986,12 +11988,12 @@ var es;
}
}
}
catch (e_16_1) { e_16 = { error: e_16_1 }; }
catch (e_17_1) { e_17 = { error: e_17_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_16) throw e_16.error; }
finally { if (e_17) throw e_17.error; }
}
}
VisibilityComputer._openSegments.clear();
@@ -12107,7 +12109,7 @@ var es;
* 处理片段,以便我们稍后对它们进行分类
*/
VisibilityComputer.prototype.updateSegments = function () {
var e_17, _a;
var e_18, _a;
try {
for (var _b = __values(this._segments), _c = _b.next(); !_c.done; _c = _b.next()) {
var segment = _c.value;
@@ -12125,12 +12127,12 @@ var es;
segment.p2.begin = !segment.p1.begin;
}
}
catch (e_17_1) { e_17 = { error: e_17_1 }; }
catch (e_18_1) { e_18 = { error: e_18_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_17) throw e_17.error; }
finally { if (e_18) throw e_18.error; }
}
// 如果我们有一个聚光灯,我们需要存储前两个段的角度。
// 这些是光斑的边界,我们将用它们来过滤它们之外的任何顶点。

File diff suppressed because one or more lines are too long

View File

@@ -39,6 +39,5 @@ gulp.task('build', ['copy'], () => {
.pipe(gulp.dest('../extensions/behaviourTree-ai/source/lib/'))
.pipe(gulp.dest('../extensions/ecs-star/lib/'))
.pipe(gulp.dest('../extensions/ecs-tween/lib/'))
.pipe(gulp.dest('../engine_support/egret/lib/'))
])
});

View File

@@ -20,7 +20,7 @@ module es {
/**
* 通过标签跟踪实体,便于检索
*/
public _entityDict: Map<number, Entity[]> = new Map<number, Entity[]>();
public _entityDict: Map<number, Set<Entity>> = new Map<number, Set<Entity>>();
public _unsortedTags: Set<number> = new Set<number>();
constructor(scene: Scene) {
@@ -101,7 +101,7 @@ module es {
public getTagList(tag: number) {
let list = this._entityDict.get(tag);
if (!list) {
list = [];
list = new Set();
this._entityDict.set(tag, list);
}
@@ -109,18 +109,13 @@ module es {
}
public addToTagList(entity: Entity) {
let list = this.getTagList(entity.tag);
if (list.findIndex(e => e.id == entity.id) == -1) {
list.push(entity);
this._unsortedTags.add(entity.tag);
}
this.getTagList(entity.tag).add(entity);
this._unsortedTags.add(entity.tag);
}
public removeFromTagList(entity: Entity) {
let list = this._entityDict.get(entity.tag);
if (list) {
new linq.List(list).remove(entity);
}
list.delete(entity);
}
public update() {
@@ -168,13 +163,6 @@ module es {
this._entities.sort(Entity.entityComparer.compare);
this._isEntityListUnsorted = false;
}
// 根据需要对标签列表进行排序
if (this._unsortedTags.size > 0) {
this._unsortedTags.forEach(value => this._entityDict.get(value).sort((a, b) => a.compareTo(b)));
this._unsortedTags.clear();
}
}
/**
@@ -206,8 +194,9 @@ module es {
let returnList = ListPool.obtain<Entity>();
returnList.length = this._entities.length;
for (let i = 0; i < list.length; i++)
returnList.push(list[i]);
for (let entity of list) {
returnList.push(entity);
}
return returnList;
}

View File

@@ -10,7 +10,7 @@ module es {
* @param type
* @param cacheCount
*/
public static warmCache(type: any, cacheCount: number){
public static warmCache(type: any, cacheCount: number) {
cacheCount -= this._objectQueue.length;
if (cacheCount > 0) {
for (let i = 0; i < cacheCount; i++) {
@@ -23,7 +23,7 @@ module es {
* 将缓存修剪为cacheCount项目
* @param cacheCount
*/
public static trimCache(cacheCount: number){
public static trimCache(cacheCount: number) {
while (cacheCount > this._objectQueue.length)
this._objectQueue.shift();
}
@@ -52,7 +52,7 @@ module es {
public static free<T>(obj: T) {
this._objectQueue.unshift(obj);
if (isIPoolable(obj)){
if (isIPoolable(obj)) {
obj["reset"]();
}
}