This commit is contained in:
yhh
2021-04-23 21:27:34 +08:00
parent 99eca546d2
commit d576a95548
6 changed files with 82 additions and 9 deletions

View File

@@ -1771,6 +1771,16 @@ declare module es {
onEntityDisabled(): void; onEntityDisabled(): void;
} }
} }
declare module es {
class ComponentTypeFactory {
private componentTypes_;
private componentTypeCount_;
types: Bag<ComponentType>;
constructor();
getTypeFor(c: any): ComponentType;
getIndexFor(c: any): number;
}
}
declare module es { declare module es {
class ComponentTypeManager { class ComponentTypeManager {
private static _componentTypesMask; private static _componentTypesMask;

View File

@@ -3134,7 +3134,7 @@ var es;
EntitySystem.prototype.initialize = function () { EntitySystem.prototype.initialize = function () {
}; };
EntitySystem.prototype.onChanged = function (entity) { EntitySystem.prototype.onChanged = function (entity) {
var contains = entity.getSystemBits().get(this.systemIndex_); var contains = new es.List(this._entities).contains(entity);
var interest = this._matcher.isInterestedEntity(entity); var interest = this._matcher.isInterestedEntity(entity);
if (interest && !contains) if (interest && !contains)
this.add(entity); this.add(entity);
@@ -3143,13 +3143,11 @@ var es;
}; };
EntitySystem.prototype.add = function (entity) { EntitySystem.prototype.add = function (entity) {
this._entities.push(entity); this._entities.push(entity);
entity.getSystemBits().set(this.systemIndex_);
this.onAdded(entity); this.onAdded(entity);
}; };
EntitySystem.prototype.onAdded = function (entity) { }; EntitySystem.prototype.onAdded = function (entity) { };
EntitySystem.prototype.remove = function (entity) { EntitySystem.prototype.remove = function (entity) {
new es.List(this._entities).remove(entity); new es.List(this._entities).remove(entity);
entity.getSystemBits().clear(this.systemIndex_);
this.onRemoved(entity); this.onRemoved(entity);
}; };
EntitySystem.prototype.onRemoved = function (entity) { }; EntitySystem.prototype.onRemoved = function (entity) { };
@@ -4255,6 +4253,34 @@ var es;
es.ComponentList = ComponentList; es.ComponentList = ComponentList;
})(es || (es = {})); })(es || (es = {}));
var es; var es;
(function (es) {
var ComponentTypeFactory = /** @class */ (function () {
function ComponentTypeFactory() {
this.componentTypeCount_ = 0;
this.componentTypes_ = {};
this.types = new es.Bag();
}
ComponentTypeFactory.prototype.getTypeFor = function (c) {
if ("number" === typeof c) {
return this.types.get(c);
}
var type = this.componentTypes_[es.getClassName(c)];
if (type == null) {
var index = this.componentTypeCount_++;
type = new es.ComponentType(c, index);
this.componentTypes_[es.getClassName(c)] = type;
this.types.set(index, type);
}
return type;
};
ComponentTypeFactory.prototype.getIndexFor = function (c) {
return this.getTypeFor(c).getIndex();
};
return ComponentTypeFactory;
}());
es.ComponentTypeFactory = ComponentTypeFactory;
})(es || (es = {}));
var es;
(function (es) { (function (es) {
var ComponentTypeManager = /** @class */ (function () { var ComponentTypeManager = /** @class */ (function () {
function ComponentTypeManager() { function ComponentTypeManager() {
@@ -11602,7 +11628,7 @@ var es;
function TypeUtils() { function TypeUtils() {
} }
TypeUtils.getType = function (obj) { TypeUtils.getType = function (obj) {
return obj["__proto__"]["constructor"]; return obj.constructor;
}; };
return TypeUtils; return TypeUtils;
}()); }());

File diff suppressed because one or more lines are too long

View File

@@ -60,7 +60,7 @@ module es {
} }
public onChanged(entity: Entity) { public onChanged(entity: Entity) {
let contains = entity.getSystemBits().get(this.systemIndex_); let contains = new es.List(this._entities).contains(entity);
let interest = this._matcher.isInterestedEntity(entity); let interest = this._matcher.isInterestedEntity(entity);
if (interest && !contains) if (interest && !contains)
@@ -71,7 +71,6 @@ module es {
public add(entity: Entity) { public add(entity: Entity) {
this._entities.push(entity); this._entities.push(entity);
entity.getSystemBits().set(this.systemIndex_);
this.onAdded(entity); this.onAdded(entity);
} }
@@ -79,7 +78,6 @@ module es {
public remove(entity: Entity) { public remove(entity: Entity) {
new es.List(this._entities).remove(entity); new es.List(this._entities).remove(entity);
entity.getSystemBits().clear(this.systemIndex_);
this.onRemoved(entity); this.onRemoved(entity);
} }

View File

@@ -0,0 +1,39 @@
module es {
interface IdentityHashMap {
[key: string]: ComponentType;
}
export class ComponentTypeFactory {
private componentTypes_: IdentityHashMap;
private componentTypeCount_ = 0;
public types: Bag<ComponentType>;
constructor() {
this.componentTypes_ = {};
this.types = new Bag<ComponentType>();
}
public getTypeFor(c): ComponentType {
if ("number" === typeof c) {
return this.types.get(c);
}
let type: ComponentType = this.componentTypes_[getClassName(c)];
if (type == null) {
const index: number = this.componentTypeCount_++;
type = new ComponentType(c, index);
this.componentTypes_[getClassName(c)] = type;
this.types.set(index, type);
}
return type;
}
public getIndexFor(c): number {
return this.getTypeFor(c).getIndex();
}
}
}

View File

@@ -1,7 +1,7 @@
module es { module es {
export class TypeUtils { export class TypeUtils {
public static getType(obj: any){ public static getType(obj: any){
return obj["__proto__"]["constructor"]; return obj.constructor;
} }
} }
} }