新增identifierPool用于实体id复用

This commit is contained in:
yhh
2021-05-11 17:23:04 +08:00
parent e94b7b23e4
commit 63d307b95a
6 changed files with 65 additions and 11 deletions

View File

@@ -234,7 +234,6 @@ declare module es {
compare(self: Entity, other: Entity): number; compare(self: Entity, other: Entity): number;
} }
class Entity implements IEqualityComparable { class Entity implements IEqualityComparable {
static _idGenerator: number;
static entityComparer: IComparer<Entity>; static entityComparer: IComparer<Entity>;
/** /**
* 当前实体所属的场景 * 当前实体所属的场景
@@ -261,7 +260,7 @@ declare module es {
*/ */
updateInterval: number; updateInterval: number;
componentBits: Bits; componentBits: Bits;
constructor(name: string); constructor(name: string, id: number);
_isDestroyed: boolean; _isDestroyed: boolean;
/** /**
* 如果调用了destroy那么在下一次处理实体之前这将一直为true * 如果调用了destroy那么在下一次处理实体之前这将一直为true
@@ -651,6 +650,7 @@ declare module es {
*/ */
readonly entityProcessors: EntityProcessorList; readonly entityProcessors: EntityProcessorList;
readonly _sceneComponents: SceneComponent[]; readonly _sceneComponents: SceneComponent[];
readonly identifierPool: IdentifierPool;
private _didSceneBegin; private _didSceneBegin;
constructor(); constructor();
/** /**
@@ -1863,6 +1863,15 @@ declare module es {
static getHashCode(str: any): number; static getHashCode(str: any): number;
} }
} }
declare module es {
class IdentifierPool {
private ids;
private nextAvailableId_;
constructor();
checkOut(): number;
checkIn(id: number): void;
}
}
declare module es { declare module es {
class Matcher { class Matcher {
protected allSet: (new (...args: any[]) => Component)[]; protected allSet: (new (...args: any[]) => Component)[];

View File

@@ -600,7 +600,7 @@ var es;
}()); }());
es.EntityComparer = EntityComparer; es.EntityComparer = EntityComparer;
var Entity = /** @class */ (function () { var Entity = /** @class */ (function () {
function Entity(name) { function Entity(name, id) {
/** /**
* 指定应该调用这个entity update方法的频率。1表示每一帧2表示每一帧以此类推 * 指定应该调用这个entity update方法的频率。1表示每一帧2表示每一帧以此类推
*/ */
@@ -612,7 +612,7 @@ var es;
this.transform = new es.Transform(this); this.transform = new es.Transform(this);
this.componentBits = new es.Bits(); this.componentBits = new es.Bits();
this.name = name; this.name = name;
this.id = Entity._idGenerator++; this.id = id;
} }
Object.defineProperty(Entity.prototype, "isDestroyed", { Object.defineProperty(Entity.prototype, "isDestroyed", {
/** /**
@@ -896,6 +896,7 @@ var es;
*/ */
Entity.prototype.destroy = function () { Entity.prototype.destroy = function () {
this._isDestroyed = true; this._isDestroyed = true;
this.scene.identifierPool.checkIn(this.id);
this.scene.entities.remove(this); this.scene.entities.remove(this);
this.transform.parent = null; this.transform.parent = null;
// 销毁所有子项 // 销毁所有子项
@@ -1056,7 +1057,6 @@ var es;
Entity.prototype.toString = function () { Entity.prototype.toString = function () {
return "[Entity: name: " + this.name + ", tag: " + this.tag + ", enabled: " + this.enabled + ", depth: " + this.updateOrder + "]"; return "[Entity: name: " + this.name + ", tag: " + this.tag + ", enabled: " + this.enabled + ", depth: " + this.updateOrder + "]";
}; };
Entity._idGenerator = 0;
Entity.entityComparer = new EntityComparer(); Entity.entityComparer = new EntityComparer();
return Entity; return Entity;
}()); }());
@@ -1431,6 +1431,7 @@ var es;
this._sceneComponents = []; this._sceneComponents = [];
this.entities = new es.EntityList(this); this.entities = new es.EntityList(this);
this.entityProcessors = new es.EntityProcessorList(); this.entityProcessors = new es.EntityProcessorList();
this.identifierPool = new es.IdentifierPool();
this.initialize(); this.initialize();
} }
/** /**
@@ -1532,7 +1533,7 @@ var es;
* @param name * @param name
*/ */
Scene.prototype.createEntity = function (name) { Scene.prototype.createEntity = function (name) {
var entity = new es.Entity(name); var entity = new es.Entity(name, this.identifierPool.checkOut());
return this.addEntity(entity); return this.addEntity(entity);
}; };
/** /**
@@ -4448,6 +4449,26 @@ var es;
es.HashHelpers = HashHelpers; es.HashHelpers = HashHelpers;
})(es || (es = {})); })(es || (es = {}));
var es; var es;
(function (es) {
var IdentifierPool = /** @class */ (function () {
function IdentifierPool() {
this.nextAvailableId_ = 0;
this.ids = new es.Bag();
}
IdentifierPool.prototype.checkOut = function () {
if (this.ids.size() > 0) {
return this.ids.removeLast();
}
return this.nextAvailableId_++;
};
IdentifierPool.prototype.checkIn = function (id) {
this.ids.add(id);
};
return IdentifierPool;
}());
es.IdentifierPool = IdentifierPool;
})(es || (es = {}));
var es;
(function (es) { (function (es) {
var Matcher = /** @class */ (function () { var Matcher = /** @class */ (function () {
function Matcher() { function Matcher() {

File diff suppressed because one or more lines are too long

View File

@@ -9,7 +9,6 @@ module es {
} }
export class Entity implements IEqualityComparable { export class Entity implements IEqualityComparable {
public static _idGenerator: number = 0;
public static entityComparer: IComparer<Entity> = new EntityComparer(); public static entityComparer: IComparer<Entity> = new EntityComparer();
/** /**
* 当前实体所属的场景 * 当前实体所属的场景
@@ -37,12 +36,12 @@ module es {
public updateInterval: number = 1; public updateInterval: number = 1;
public componentBits: Bits; public componentBits: Bits;
constructor(name: string) { constructor(name: string, id: number) {
this.components = new ComponentList(this); this.components = new ComponentList(this);
this.transform = new Transform(this); this.transform = new Transform(this);
this.componentBits = new Bits(); this.componentBits = new Bits();
this.name = name; this.name = name;
this.id = Entity._idGenerator++; this.id = id;
} }
public _isDestroyed: boolean; public _isDestroyed: boolean;
@@ -319,6 +318,7 @@ module es {
*/ */
public destroy() { public destroy() {
this._isDestroyed = true; this._isDestroyed = true;
this.scene.identifierPool.checkIn(this.id);
this.scene.entities.remove(this); this.scene.entities.remove(this);
this.transform.parent = null; this.transform.parent = null;

View File

@@ -13,11 +13,13 @@ module es {
public readonly entityProcessors: EntityProcessorList; public readonly entityProcessors: EntityProcessorList;
public readonly _sceneComponents: SceneComponent[] = []; public readonly _sceneComponents: SceneComponent[] = [];
public readonly identifierPool: IdentifierPool;
private _didSceneBegin: boolean; private _didSceneBegin: boolean;
constructor() { constructor() {
this.entities = new EntityList(this); this.entities = new EntityList(this);
this.entityProcessors = new EntityProcessorList(); this.entityProcessors = new EntityProcessorList();
this.identifierPool = new IdentifierPool();
this.initialize(); this.initialize();
} }
@@ -145,7 +147,7 @@ module es {
* @param name * @param name
*/ */
public createEntity(name: string) { public createEntity(name: string) {
let entity = new Entity(name); let entity = new Entity(name, this.identifierPool.checkOut());
return this.addEntity(entity); return this.addEntity(entity);
} }

View File

@@ -0,0 +1,22 @@
module es {
export class IdentifierPool {
private ids: Bag<number>;
private nextAvailableId_ = 0;
constructor() {
this.ids = new Bag<number>();
}
public checkOut(): number {
if (this.ids.size() > 0) {
return this.ids.removeLast();
}
return this.nextAvailableId_++;
}
public checkIn(id: number): void {
this.ids.add(id);
}
}
}