修复bitset调整数组大小时错误/componentTypeManager获取修复

This commit is contained in:
yhh
2020-11-24 18:21:26 +08:00
parent 73b484d894
commit 8bc367ab48
8 changed files with 50 additions and 32 deletions

View File

@@ -9,11 +9,12 @@ module es {
private _bits: number[];
constructor(nbits: number = 64) {
let length = nbits >> 6;
let length = nbits >> 6 >>> 0;
if ((nbits & BitSet.LONG_MASK) != 0)
length++;
this._bits = new Array(length);
this._bits.fill(0);
}
public and(bs: BitSet) {
@@ -33,7 +34,7 @@ module es {
}
public cardinality(): number {
let card = 0;
let card = 0 >>> 0;
for (let i = this._bits.length - 1; i >= 0; i--) {
let a = this._bits[i];
@@ -47,7 +48,7 @@ module es {
a = ((a >> 1) & 0x5555555555555555) + (a & 0x5555555555555555);
a = ((a >> 2) & 0x3333333333333333) + (a & 0x3333333333333333);
let b = ((a >> 32) + a);
let b = ((a >> 32) + a) >>> 0;
b = ((b >> 4) & 0x0f0f0f0f) + (b & 0x0f0f0f0f);
b = ((b >> 8) & 0x00ff00ff) + (b & 0x00ff00ff);
card += ((b >> 16) & 0x0000ffff) + (b & 0x0000ffff);
@@ -126,9 +127,9 @@ module es {
private ensure(lastElt: number) {
if (lastElt >= this._bits.length) {
let nd = new Number[lastElt + 1];
nd = this._bits.copyWithin(0, 0, this._bits.length);
this._bits = nd;
let startIndex = this._bits.length;
this._bits.length = lastElt + 1;
this._bits.fill(0, startIndex, lastElt + 1);
}
}
}

View File

@@ -86,7 +86,7 @@ module es {
if (isIUpdatable(component))
this._updatableComponents.remove(component);
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component), false);
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component["__proto__"]["constructor"]), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
}
}
@@ -98,7 +98,7 @@ module es {
if (isIUpdatable(component))
this._updatableComponents.add(component);
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component));
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component["__proto__"]["constructor"]));
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
}
}
@@ -123,7 +123,7 @@ module es {
if (isIUpdatable(component))
this._updatableComponents.add(component);
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component));
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component["__proto__"]["constructor"]));
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
this._components.add(component);
@@ -160,7 +160,7 @@ module es {
if (isIUpdatable(component))
this._updatableComponents.remove(component);
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component), false);
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component["__proto__"]["constructor"]), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
component.onRemovedFromEntity();

View File

@@ -12,6 +12,8 @@ module es {
if (!this._componentTypesMask.has(type)) {
this.add(type);
v = this._componentTypesMask.get(type);
} else {
v = this._componentTypesMask.get(type);
}
return v;

View File

@@ -20,18 +20,25 @@ module es {
return this.oneSet;
}
public IsIntersted(e: Entity) {
public isInterestedEntity(e: Entity) {
return this.isInterested(e.componentBits);
}
public isInterested(componentBits: BitSet) {
// 检查实体是否拥有该方面中定义的所有组件
if (!this.allSet.isEmpty()) {
for (let i = this.allSet.nextSetBit(0); i >= 0; i = this.allSet.nextSetBit(i + 1)) {
if (!e.componentBits.get(i))
if (!componentBits.get(i))
return false;
}
}
if (!this.exclusionSet.isEmpty() && this.exclusionSet.intersects(e.componentBits))
// 如果我们仍然感兴趣,检查该实体是否拥有任何一个排除组件,如果有,那么系统就不感兴趣
if (!this.exclusionSet.isEmpty() && this.exclusionSet.intersects(componentBits))
return false;
if (!this.oneSet.isEmpty() && !this.oneSet.intersects(e.componentBits))
// 如果我们仍然感兴趣检查该实体是否拥有oneSet中的任何一个组件。如果是系统就会感兴趣
if (!this.oneSet.isEmpty() && !this.oneSet.intersects(componentBits))
return false;
return true;