listpool根据type划分池
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
module es {
|
||||
export abstract class Collider extends Component {
|
||||
export class Collider extends Component {
|
||||
public static readonly lateSortOrder = 999;
|
||||
public castSortOrder: number = 0;
|
||||
/**
|
||||
|
||||
@@ -73,7 +73,7 @@ module es {
|
||||
}
|
||||
}
|
||||
|
||||
ListPool.free(colliders);
|
||||
ListPool.free(Collider, colliders);
|
||||
|
||||
return collisionResult.collider != null;
|
||||
}
|
||||
|
||||
@@ -223,7 +223,7 @@ module es {
|
||||
public entitiesWithTag(tag: number) {
|
||||
let list = this.getTagList(tag);
|
||||
|
||||
let returnList = ListPool.obtain<Entity>();
|
||||
let returnList = ListPool.obtain<Entity>(Entity);
|
||||
if (list.size > 0) {
|
||||
for (let entity of list) {
|
||||
returnList.push(entity);
|
||||
@@ -286,7 +286,7 @@ module es {
|
||||
* @param type
|
||||
*/
|
||||
public findComponentsOfType<T extends Component>(type): T[] {
|
||||
let comps = ListPool.obtain<T>();
|
||||
let comps = ListPool.obtain<T>(type);
|
||||
if (this._entities.length > 0) {
|
||||
for (let i = 0, s = this._entities.length; i < s; i++) {
|
||||
let entity = this._entities[i];
|
||||
|
||||
@@ -76,7 +76,7 @@ module es {
|
||||
*/
|
||||
public static getOptimizedDrawingPoints(start: Vector2, firstCtrlPoint: Vector2, secondCtrlPoint: Vector2,
|
||||
end: Vector2, distanceTolerance: number = 1) {
|
||||
let points = ListPool.obtain<Vector2>();
|
||||
let points = ListPool.obtain<Vector2>(Vector2);
|
||||
points.push(start);
|
||||
this.recursiveGetOptimizedDrawingPoints(start, firstCtrlPoint, secondCtrlPoint, end, points, distanceTolerance);
|
||||
points.push(end);
|
||||
|
||||
@@ -3,17 +3,18 @@ module es {
|
||||
* 可以用于列表池的简单类
|
||||
*/
|
||||
export class ListPool {
|
||||
private static readonly _objectQueue = [];
|
||||
private static readonly _objectQueue: Map<any, any[]> = new Map();
|
||||
|
||||
/**
|
||||
* 预热缓存,使用最大的cacheCount对象填充缓存
|
||||
* @param cacheCount
|
||||
*/
|
||||
public static warmCache(cacheCount: number) {
|
||||
cacheCount -= this._objectQueue.length;
|
||||
public static warmCache<T>(type: new (...args) => T, cacheCount: number) {
|
||||
this.checkCreate(type);
|
||||
cacheCount -= this._objectQueue.get(type).length;
|
||||
if (cacheCount > 0) {
|
||||
for (let i = 0; i < cacheCount; i++) {
|
||||
this._objectQueue.unshift([]);
|
||||
this._objectQueue.get(type).unshift([]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,24 +23,27 @@ module es {
|
||||
* 将缓存修剪为cacheCount项目
|
||||
* @param cacheCount
|
||||
*/
|
||||
public static trimCache(cacheCount) {
|
||||
while (cacheCount > this._objectQueue.length)
|
||||
this._objectQueue.shift();
|
||||
public static trimCache<T>(type: new (...args) => T, cacheCount: number) {
|
||||
this.checkCreate(type);
|
||||
while (cacheCount > this._objectQueue.get(type).length)
|
||||
this._objectQueue.get(type).shift();
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除缓存
|
||||
*/
|
||||
public static clearCache() {
|
||||
this._objectQueue.length = 0;
|
||||
public static clearCache<T>(type: new (...args) => T) {
|
||||
this.checkCreate(type);
|
||||
this._objectQueue.get(type).length = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果可以的话,从堆栈中弹出一个项
|
||||
*/
|
||||
public static obtain<T>(): T[] {
|
||||
if (this._objectQueue.length > 0)
|
||||
return this._objectQueue.shift();
|
||||
public static obtain<T>(type: new (...args) => T): T[] {
|
||||
this.checkCreate(type);
|
||||
if (this._objectQueue.get(type).length > 0)
|
||||
return this._objectQueue.get(type).shift();
|
||||
|
||||
return [];
|
||||
}
|
||||
@@ -48,9 +52,15 @@ module es {
|
||||
* 将项推回堆栈
|
||||
* @param obj
|
||||
*/
|
||||
public static free<T>(obj: Array<T>) {
|
||||
this._objectQueue.unshift(obj);
|
||||
public static free<T>(type: new (...args) => T, obj: Array<T>) {
|
||||
this.checkCreate(type);
|
||||
this._objectQueue.get(type).unshift(obj);
|
||||
obj.length = 0;
|
||||
}
|
||||
|
||||
private static checkCreate<T>(type: new (...args) => T) {
|
||||
if (!this._objectQueue.get(type))
|
||||
this._objectQueue.set(type, []);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -283,7 +283,7 @@ module es {
|
||||
* @param list
|
||||
* @param itemCount 从列表中返回的随机项目的数量
|
||||
*/
|
||||
public static randomItems<T>(list: T[], itemCount: number){
|
||||
public static randomItems<T>(type: any, list: T[], itemCount: number){
|
||||
let set = new Set<T>();
|
||||
while (set.size != itemCount) {
|
||||
let item = this.randomItem(list);
|
||||
@@ -291,7 +291,7 @@ module es {
|
||||
set.add(item);
|
||||
}
|
||||
|
||||
let items = es.ListPool.obtain<T>();
|
||||
let items = es.ListPool.obtain<T>(type);
|
||||
set.forEach(value => items.push(value));
|
||||
return items;
|
||||
}
|
||||
|
||||
@@ -148,7 +148,7 @@ module es {
|
||||
* 计算可见性多边形,并返回三角形扇形的顶点(减去中心顶点)。返回的数组来自ListPool
|
||||
*/
|
||||
public end(): Vector2[] {
|
||||
let output = ListPool.obtain<Vector2>();
|
||||
let output = ListPool.obtain<Vector2>(Vector2);
|
||||
this.updateSegments();
|
||||
this._endPoints.sort(this._radialComparer.compare);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user