使用BitMask64Data.segments扩展ComponentRegistry
This commit is contained in:
@@ -20,7 +20,6 @@ export class ComponentRegistry {
|
||||
private static componentNameToId = new Map<string, number>();
|
||||
private static maskCache = new Map<string, BitMask64Data>();
|
||||
private static nextBitIndex = 0;
|
||||
private static maxComponents = 64; // 支持最多64种组件类型
|
||||
|
||||
/**
|
||||
* 注册组件类型并分配位掩码
|
||||
@@ -29,16 +28,12 @@ export class ComponentRegistry {
|
||||
*/
|
||||
public static register<T extends Component>(componentType: ComponentType<T>): number {
|
||||
const typeName = getComponentTypeName(componentType);
|
||||
|
||||
|
||||
if (this.componentTypes.has(componentType)) {
|
||||
const existingIndex = this.componentTypes.get(componentType)!;
|
||||
return existingIndex;
|
||||
}
|
||||
|
||||
if (this.nextBitIndex >= this.maxComponents) {
|
||||
throw new Error(`Maximum number of component types (${this.maxComponents}) exceeded`);
|
||||
}
|
||||
|
||||
const bitIndex = this.nextBitIndex++;
|
||||
this.componentTypes.set(componentType, bitIndex);
|
||||
this.bitIndexToType.set(bitIndex, componentType);
|
||||
@@ -59,7 +54,10 @@ export class ComponentRegistry {
|
||||
const typeName = getComponentTypeName(componentType);
|
||||
throw new Error(`Component type ${typeName} is not registered`);
|
||||
}
|
||||
return BitMask64Utils.create(bitIndex);
|
||||
|
||||
const mask: BitMask64Data = { lo: 0, hi: 0 };
|
||||
BitMask64Utils.setBitExtended(mask, bitIndex);
|
||||
return mask;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -94,6 +92,14 @@ export class ComponentRegistry {
|
||||
return (this.bitIndexToType.get(bitIndex) as ComponentType) || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前已注册的组件类型数量
|
||||
* @returns 已注册数量
|
||||
*/
|
||||
public static getRegisteredCount(): number {
|
||||
return this.nextBitIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过名称获取组件类型
|
||||
* @param componentName 组件名称
|
||||
@@ -138,10 +144,6 @@ export class ComponentRegistry {
|
||||
return this.componentNameToId.get(componentName)!;
|
||||
}
|
||||
|
||||
if (this.nextBitIndex >= this.maxComponents) {
|
||||
throw new Error(`Maximum number of component types (${this.maxComponents}) exceeded`);
|
||||
}
|
||||
|
||||
const bitIndex = this.nextBitIndex++;
|
||||
this.componentNameToId.set(componentName, bitIndex);
|
||||
return bitIndex;
|
||||
|
||||
@@ -188,10 +188,10 @@ export class Entity {
|
||||
const components: Component[] = [];
|
||||
const mask = this._componentMask;
|
||||
|
||||
// 遍历位掩码中设置的位
|
||||
for (let bitIndex = 0; bitIndex < 64; bitIndex++) {
|
||||
const bitMask = BitMask64Utils.create(bitIndex);
|
||||
if (BitMask64Utils.hasAny(mask, bitMask)) {
|
||||
const maxBitIndex = ComponentRegistry.getRegisteredCount();
|
||||
|
||||
for (let bitIndex = 0; bitIndex < maxBitIndex; bitIndex++) {
|
||||
if (BitMask64Utils.getBitExtended(mask, bitIndex)) {
|
||||
const componentType = ComponentRegistry.getTypeByBitIndex(bitIndex);
|
||||
if (componentType) {
|
||||
let component: Component | null = null;
|
||||
@@ -504,7 +504,7 @@ export class Entity {
|
||||
this._localComponents.delete(componentType);
|
||||
|
||||
// 更新位掩码
|
||||
BitMask64Utils.clearBit(this._componentMask, bitIndex);
|
||||
BitMask64Utils.clearBitExtended(this._componentMask, bitIndex);
|
||||
|
||||
// 使缓存失效
|
||||
this._componentCache = null;
|
||||
|
||||
@@ -61,7 +61,32 @@ export class BitMask64Utils {
|
||||
* @returns 如果掩码包含bits中的任意位则返回true
|
||||
*/
|
||||
public static hasAny(mask: BitMask64Data, bits: BitMask64Data): boolean {
|
||||
return (mask.lo & bits.lo) !== 0 || (mask.hi & bits.hi) !== 0;
|
||||
// 检查第一个 64 位段
|
||||
if ((mask.lo & bits.lo) !== 0 || (mask.hi & bits.hi) !== 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 如果 bits 没有扩展段,检查完成
|
||||
if (!bits.segments || bits.segments.length === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 如果 bits 有扩展段但 mask 没有,返回 false
|
||||
if (!mask.segments || mask.segments.length === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查每个扩展段
|
||||
const minSegments = Math.min(mask.segments.length, bits.segments.length);
|
||||
for (let i = 0; i < minSegments; i++) {
|
||||
const maskSeg = mask.segments[i];
|
||||
const bitsSeg = bits.segments[i];
|
||||
if ((maskSeg.lo & bitsSeg.lo) !== 0 || (maskSeg.hi & bitsSeg.hi) !== 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -181,6 +206,24 @@ export class BitMask64Utils {
|
||||
public static orInPlace(target: BitMask64Data, other: BitMask64Data): void {
|
||||
target.lo |= other.lo;
|
||||
target.hi |= other.hi;
|
||||
|
||||
// 处理扩展段
|
||||
if (other.segments && other.segments.length > 0) {
|
||||
if (!target.segments) {
|
||||
target.segments = [];
|
||||
}
|
||||
|
||||
// 确保 target 有足够的段
|
||||
while (target.segments.length < other.segments.length) {
|
||||
target.segments.push({ lo: 0, hi: 0 });
|
||||
}
|
||||
|
||||
// 对每个段执行或操作
|
||||
for (let i = 0; i < other.segments.length; i++) {
|
||||
target.segments[i].lo |= other.segments[i].lo;
|
||||
target.segments[i].hi |= other.segments[i].hi;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user