新增identifierPool用于实体id复用
This commit is contained in:
13
source/bin/framework.d.ts
vendored
13
source/bin/framework.d.ts
vendored
@@ -234,7 +234,6 @@ declare module es {
|
||||
compare(self: Entity, other: Entity): number;
|
||||
}
|
||||
class Entity implements IEqualityComparable {
|
||||
static _idGenerator: number;
|
||||
static entityComparer: IComparer<Entity>;
|
||||
/**
|
||||
* 当前实体所属的场景
|
||||
@@ -261,7 +260,7 @@ declare module es {
|
||||
*/
|
||||
updateInterval: number;
|
||||
componentBits: Bits;
|
||||
constructor(name: string);
|
||||
constructor(name: string, id: number);
|
||||
_isDestroyed: boolean;
|
||||
/**
|
||||
* 如果调用了destroy,那么在下一次处理实体之前这将一直为true
|
||||
@@ -651,6 +650,7 @@ declare module es {
|
||||
*/
|
||||
readonly entityProcessors: EntityProcessorList;
|
||||
readonly _sceneComponents: SceneComponent[];
|
||||
readonly identifierPool: IdentifierPool;
|
||||
private _didSceneBegin;
|
||||
constructor();
|
||||
/**
|
||||
@@ -1863,6 +1863,15 @@ declare module es {
|
||||
static getHashCode(str: any): number;
|
||||
}
|
||||
}
|
||||
declare module es {
|
||||
class IdentifierPool {
|
||||
private ids;
|
||||
private nextAvailableId_;
|
||||
constructor();
|
||||
checkOut(): number;
|
||||
checkIn(id: number): void;
|
||||
}
|
||||
}
|
||||
declare module es {
|
||||
class Matcher {
|
||||
protected allSet: (new (...args: any[]) => Component)[];
|
||||
|
||||
@@ -600,7 +600,7 @@ var es;
|
||||
}());
|
||||
es.EntityComparer = EntityComparer;
|
||||
var Entity = /** @class */ (function () {
|
||||
function Entity(name) {
|
||||
function Entity(name, id) {
|
||||
/**
|
||||
* 指定应该调用这个entity update方法的频率。1表示每一帧,2表示每一帧,以此类推
|
||||
*/
|
||||
@@ -612,7 +612,7 @@ var es;
|
||||
this.transform = new es.Transform(this);
|
||||
this.componentBits = new es.Bits();
|
||||
this.name = name;
|
||||
this.id = Entity._idGenerator++;
|
||||
this.id = id;
|
||||
}
|
||||
Object.defineProperty(Entity.prototype, "isDestroyed", {
|
||||
/**
|
||||
@@ -896,6 +896,7 @@ var es;
|
||||
*/
|
||||
Entity.prototype.destroy = function () {
|
||||
this._isDestroyed = true;
|
||||
this.scene.identifierPool.checkIn(this.id);
|
||||
this.scene.entities.remove(this);
|
||||
this.transform.parent = null;
|
||||
// 销毁所有子项
|
||||
@@ -1056,7 +1057,6 @@ var es;
|
||||
Entity.prototype.toString = function () {
|
||||
return "[Entity: name: " + this.name + ", tag: " + this.tag + ", enabled: " + this.enabled + ", depth: " + this.updateOrder + "]";
|
||||
};
|
||||
Entity._idGenerator = 0;
|
||||
Entity.entityComparer = new EntityComparer();
|
||||
return Entity;
|
||||
}());
|
||||
@@ -1431,6 +1431,7 @@ var es;
|
||||
this._sceneComponents = [];
|
||||
this.entities = new es.EntityList(this);
|
||||
this.entityProcessors = new es.EntityProcessorList();
|
||||
this.identifierPool = new es.IdentifierPool();
|
||||
this.initialize();
|
||||
}
|
||||
/**
|
||||
@@ -1532,7 +1533,7 @@ var es;
|
||||
* @param 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);
|
||||
};
|
||||
/**
|
||||
@@ -4448,6 +4449,26 @@ var es;
|
||||
es.HashHelpers = HashHelpers;
|
||||
})(es || (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) {
|
||||
var Matcher = /** @class */ (function () {
|
||||
function Matcher() {
|
||||
|
||||
2
source/bin/framework.min.js
vendored
2
source/bin/framework.min.js
vendored
File diff suppressed because one or more lines are too long
@@ -9,7 +9,6 @@ module es {
|
||||
}
|
||||
|
||||
export class Entity implements IEqualityComparable {
|
||||
public static _idGenerator: number = 0;
|
||||
public static entityComparer: IComparer<Entity> = new EntityComparer();
|
||||
/**
|
||||
* 当前实体所属的场景
|
||||
@@ -37,12 +36,12 @@ module es {
|
||||
public updateInterval: number = 1;
|
||||
public componentBits: Bits;
|
||||
|
||||
constructor(name: string) {
|
||||
constructor(name: string, id: number) {
|
||||
this.components = new ComponentList(this);
|
||||
this.transform = new Transform(this);
|
||||
this.componentBits = new Bits();
|
||||
this.name = name;
|
||||
this.id = Entity._idGenerator++;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public _isDestroyed: boolean;
|
||||
@@ -319,6 +318,7 @@ module es {
|
||||
*/
|
||||
public destroy() {
|
||||
this._isDestroyed = true;
|
||||
this.scene.identifierPool.checkIn(this.id);
|
||||
this.scene.entities.remove(this);
|
||||
this.transform.parent = null;
|
||||
|
||||
|
||||
@@ -13,11 +13,13 @@ module es {
|
||||
public readonly entityProcessors: EntityProcessorList;
|
||||
|
||||
public readonly _sceneComponents: SceneComponent[] = [];
|
||||
public readonly identifierPool: IdentifierPool;
|
||||
private _didSceneBegin: boolean;
|
||||
|
||||
constructor() {
|
||||
this.entities = new EntityList(this);
|
||||
this.entityProcessors = new EntityProcessorList();
|
||||
this.identifierPool = new IdentifierPool();
|
||||
|
||||
this.initialize();
|
||||
}
|
||||
@@ -145,7 +147,7 @@ module es {
|
||||
* @param name
|
||||
*/
|
||||
public createEntity(name: string) {
|
||||
let entity = new Entity(name);
|
||||
let entity = new Entity(name, this.identifierPool.checkOut());
|
||||
return this.addEntity(entity);
|
||||
}
|
||||
|
||||
|
||||
22
source/src/ECS/Utils/IdentifierPool.ts
Normal file
22
source/src/ECS/Utils/IdentifierPool.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user