Files
esengine/src/ECS/Entity.ts

1328 lines
36 KiB
TypeScript
Raw Normal View History

import { Component } from './Component';
import { ComponentRegistry, ComponentType } from './Core/ComponentStorage';
import { EventBus } from './Core/EventBus';
/**
*
*
* ID比较
*/
export class EntityComparer {
/**
*
*
* @param self -
* @param other -
* @returns self优先级更高other优先级更高0
*/
public compare(self: Entity, other: Entity): number {
let compare = self.updateOrder - other.updateOrder;
if (compare == 0)
compare = self.id - other.id;
return compare;
}
}
/**
*
*/
interface ComponentCacheEntry<T extends Component = Component> {
component: T;
lastAccessed: number;
accessCount: number;
}
/**
*
*/
interface ComponentCacheConfig {
maxSize: number;
ttl: number; // 生存时间(毫秒)
enableLRU: boolean; // 是否启用LRU淘汰策略
}
/**
*
*/
class ComponentCache {
private cache = new Map<ComponentType, ComponentCacheEntry>();
private accessOrder: ComponentType[] = [];
private config: ComponentCacheConfig;
constructor(config: ComponentCacheConfig = {
maxSize: 16,
ttl: 5000,
enableLRU: true
}) {
this.config = config;
}
public get<T extends Component>(type: ComponentType<T>): T | null {
const entry = this.cache.get(type);
if (!entry) {
return null;
}
// 检查TTL
if (Date.now() - entry.lastAccessed > this.config.ttl) {
this.cache.delete(type);
this.removeFromAccessOrder(type);
return null;
}
// 更新访问信息
entry.lastAccessed = Date.now();
entry.accessCount++;
// 更新LRU顺序
if (this.config.enableLRU) {
this.updateAccessOrder(type);
}
return entry.component as T;
}
public set<T extends Component>(type: ComponentType<T>, component: T): void {
// 检查缓存大小限制
if (this.cache.size >= this.config.maxSize && !this.cache.has(type)) {
this.evictLeastRecentlyUsed();
}
const entry: ComponentCacheEntry<T> = {
component,
lastAccessed: Date.now(),
accessCount: 1
};
this.cache.set(type, entry);
if (this.config.enableLRU) {
this.updateAccessOrder(type);
}
}
public delete(type: ComponentType): boolean {
const deleted = this.cache.delete(type);
if (deleted) {
this.removeFromAccessOrder(type);
}
return deleted;
}
public clear(): void {
this.cache.clear();
this.accessOrder.length = 0;
}
public has(type: ComponentType): boolean {
return this.cache.has(type);
}
private evictLeastRecentlyUsed(): void {
if (this.accessOrder.length > 0) {
const lruType = this.accessOrder[0];
this.cache.delete(lruType);
this.accessOrder.shift();
}
}
private updateAccessOrder(type: ComponentType): void {
this.removeFromAccessOrder(type);
this.accessOrder.push(type);
}
private removeFromAccessOrder(type: ComponentType): void {
const index = this.accessOrder.indexOf(type);
if (index !== -1) {
this.accessOrder.splice(index, 1);
}
}
public getStats(): {
size: number;
maxSize: number;
hitRate: number;
averageAccessCount: number;
} {
let totalAccess = 0;
let totalHits = 0;
for (const entry of this.cache.values()) {
totalAccess += entry.accessCount;
totalHits++;
}
return {
size: this.cache.size,
maxSize: this.config.maxSize,
hitRate: totalAccess > 0 ? totalHits / totalAccess : 0,
averageAccessCount: this.cache.size > 0 ? totalAccess / this.cache.size : 0
};
}
}
/**
*
*
* ECS架构中的实体Entity
*
*
*
* @example
* ```typescript
* // 创建实体
* const entity = new Entity("Player", 1);
*
* // 添加组件
* const healthComponent = entity.addComponent(new HealthComponent(100));
*
* // 获取组件
* const health = entity.getComponent(HealthComponent);
*
2025-06-10 13:12:14 +08:00
* // 添加位置组件
* entity.addComponent(new PositionComponent(100, 200));
*
* // 添加子实体
* const weapon = new Entity("Weapon", 2);
* entity.addChild(weapon);
* ```
*/
export class Entity {
/**
*
*/
public static entityComparer: EntityComparer = new EntityComparer();
/**
* 线
*
*/
public static eventBus: EventBus | null = null;
/**
*
*
*
*/
public name: string;
/**
*
*
*
*/
public readonly id: number;
/**
*
*
*
*/
public readonly components: Component[] = [];
/**
*
*
*
*/
public scene: any; // 使用any避免循环依赖
/**
*
*
*
*/
public updateInterval: number = 1;
/**
*
*
*
*/
public _isDestroyed: boolean = false;
/**
*
*
*
*/
private _parent: Entity | null = null;
/**
*
*
*
*/
private _children: Entity[] = [];
/**
*
*
*
*/
private _active: boolean = true;
/**
*
*
*
*/
private _tag: number = 0;
/**
*
*
*
*/
private _enabled: boolean = true;
/**
*
*
*
*/
private _updateOrder: number = 0;
/**
*
*
*
*/
private _componentMask: bigint = BigInt(0);
/**
*
*
*
*/
private _componentTypeToIndex = new Map<ComponentType, number>();
/**
*
*
* 访
*/
private _componentCache: ComponentCache;
/**
* 访
*
* 访
*/
private _componentAccessStats = new Map<ComponentType, {
accessCount: number;
lastAccessed: number;
cacheHits: number;
cacheMisses: number;
}>();
/**
*
*
* @param name -
* @param id -
*/
constructor(name: string, id: number) {
this.name = name;
this.id = id;
// 初始化组件缓存
this._componentCache = new ComponentCache();
}
/**
*
*
* @returns true
*/
public get isDestroyed(): boolean {
return this._isDestroyed;
}
/**
*
*
* @returns null
*/
public get parent(): Entity | null {
return this._parent;
}
/**
*
*
* @returns
*/
public get children(): readonly Entity[] {
return [...this._children];
}
/**
*
*
* @returns
*/
public get childCount(): number {
return this._children.length;
}
/**
*
*
* @returns true
*/
public get active(): boolean {
return this._active;
}
/**
*
*
*
*
* @param value -
*/
public set active(value: boolean) {
if (this._active !== value) {
this._active = value;
this.onActiveChanged();
}
}
/**
*
*
* true
*
* @returns
*/
public get activeInHierarchy(): boolean {
if (!this._active) return false;
if (this._parent) return this._parent.activeInHierarchy;
return true;
}
/**
*
*
* @returns
*/
public get tag(): number {
return this._tag;
}
/**
*
*
* @param value -
*/
public set tag(value: number) {
this._tag = value;
}
/**
*
*
* @returns true
*/
public get enabled(): boolean {
return this._enabled;
}
/**
*
*
* @param value -
*/
public set enabled(value: boolean) {
this._enabled = value;
}
/**
*
*
* @returns
*/
public get updateOrder(): number {
return this._updateOrder;
}
/**
*
*
* @param value -
*/
public set updateOrder(value: number) {
this._updateOrder = value;
}
/**
*
*
* @returns
*/
public get componentMask(): bigint {
return this._componentMask;
}
/**
*
*
* @param componentType -
* @param args -
* @returns
*/
public createComponent<T extends Component>(
componentType: ComponentType<T>,
...args: any[]
): T {
const component = new componentType(...args);
return this.addComponent(component);
}
/**
*
*
* @param component -
* @returns
*/
private addComponentInternal<T extends Component>(component: T): T {
const componentType = component.constructor as ComponentType<T>;
// 注册组件类型(如果尚未注册)
if (!ComponentRegistry.isRegistered(componentType)) {
ComponentRegistry.register(componentType);
}
// 设置组件的实体引用
component.entity = this;
// 添加到组件列表并建立索引映射
const index = this.components.length;
this.components.push(component);
this._componentTypeToIndex.set(componentType, index);
// 更新位掩码
this._componentMask |= ComponentRegistry.getBitMask(componentType);
// 添加到缓存
this._componentCache.set(componentType, component);
// 初始化访问统计
this._componentAccessStats.set(componentType, {
accessCount: 0,
lastAccessed: Date.now(),
cacheHits: 0,
cacheMisses: 0
});
return component;
}
/**
*
*
* @param component -
* @returns
* @throws {Error}
*/
public addComponent<T extends Component>(component: T): T {
const componentType = component.constructor as ComponentType<T>;
// 检查是否已有此类型的组件
if (this.hasComponent(componentType)) {
throw new Error(`Entity ${this.name} already has component ${componentType.name}`);
}
// 使用内部方法添加组件
this.addComponentInternal(component);
// 如果场景存在且有组件存储管理器,添加到存储器
if (this.scene && this.scene.componentStorageManager) {
this.scene.componentStorageManager.addComponent(this.id, component);
}
// 调用组件的生命周期方法
component.onAddedToEntity();
// 发射组件添加事件
if (Entity.eventBus) {
Entity.eventBus.emitComponentAdded({
timestamp: Date.now(),
source: 'Entity',
entityId: this.id,
entityName: this.name,
entityTag: this.tag?.toString(),
componentType: componentType.name,
component: component
});
}
// 通知场景实体已改变
if (this.scene && this.scene.entityProcessors) {
for (const processor of this.scene.entityProcessors.processors) {
processor.onChanged(this);
}
}
return component;
}
/**
*
*
* @param type -
* @returns null
*/
public getComponent<T extends Component>(type: ComponentType<T>): T | null {
// 更新访问统计
this.updateComponentAccessStats(type);
// 首先检查位掩码,快速排除
if (!ComponentRegistry.isRegistered(type)) {
this.recordCacheMiss(type);
return null;
}
const mask = ComponentRegistry.getBitMask(type);
if ((this._componentMask & mask) === BigInt(0)) {
this.recordCacheMiss(type);
return null;
}
// 尝试从缓存获取O(1)
const cachedComponent = this._componentCache.get(type);
if (cachedComponent) {
this.recordCacheHit(type);
return cachedComponent;
}
// 尝试从索引映射获取O(1)
const index = this._componentTypeToIndex.get(type);
if (index !== undefined && index < this.components.length) {
const component = this.components[index];
if (component && component.constructor === type) {
// 添加到缓存
this._componentCache.set(type, component);
this.recordCacheHit(type);
return component as T;
}
}
// 如果场景有组件存储管理器,从存储器获取
if (this.scene && this.scene.componentStorageManager) {
const component = this.scene.componentStorageManager.getComponent(this.id, type);
if (component) {
// 更新本地缓存和索引
this._componentCache.set(type, component);
this.rebuildComponentIndex();
this.recordCacheHit(type);
return component;
}
}
// 最后回退到线性搜索并重建索引
for (let i = 0; i < this.components.length; i++) {
const component = this.components[i];
if (component instanceof type) {
// 重建索引映射
this._componentTypeToIndex.set(type, i);
this._componentCache.set(type, component);
this.recordCacheHit(type);
return component as T;
}
}
this.recordCacheMiss(type);
return null;
}
/**
* 访
*
* @param type -
*/
private updateComponentAccessStats(type: ComponentType): void {
let stats = this._componentAccessStats.get(type);
if (!stats) {
stats = {
accessCount: 0,
lastAccessed: Date.now(),
cacheHits: 0,
cacheMisses: 0
};
this._componentAccessStats.set(type, stats);
}
stats.accessCount++;
stats.lastAccessed = Date.now();
}
/**
*
*
* @param type -
*/
private recordCacheHit(type: ComponentType): void {
const stats = this._componentAccessStats.get(type);
if (stats) {
stats.cacheHits++;
}
}
/**
*
*
* @param type -
*/
private recordCacheMiss(type: ComponentType): void {
const stats = this._componentAccessStats.get(type);
if (stats) {
stats.cacheMisses++;
}
}
/**
*
*/
private rebuildComponentIndex(): void {
this._componentTypeToIndex.clear();
for (let i = 0; i < this.components.length; i++) {
const component = this.components[i];
const componentType = component.constructor as ComponentType;
this._componentTypeToIndex.set(componentType, i);
}
}
/**
*
*
* @param type -
* @returns true
*/
public hasComponent<T extends Component>(type: ComponentType<T>): boolean {
if (!ComponentRegistry.isRegistered(type)) {
return false;
}
const mask = ComponentRegistry.getBitMask(type);
return (this._componentMask & mask) !== BigInt(0);
}
/**
*
*
* @param type -
* @param args - 使
* @returns
*/
public getOrCreateComponent<T extends Component>(
type: ComponentType<T>,
...args: any[]
): T {
let component = this.getComponent(type);
if (!component) {
component = this.createComponent(type, ...args);
}
return component;
}
/**
*
*
* @param component -
*/
public removeComponent(component: Component): void {
const componentType = component.constructor as ComponentType;
// 从组件列表中移除
const index = this.components.indexOf(component);
if (index !== -1) {
this.components.splice(index, 1);
// 重建索引映射(因为数组索引发生了变化)
this.rebuildComponentIndex();
}
// 从缓存中移除
this._componentCache.delete(componentType);
// 清除访问统计
this._componentAccessStats.delete(componentType);
// 更新位掩码
if (ComponentRegistry.isRegistered(componentType)) {
this._componentMask &= ~ComponentRegistry.getBitMask(componentType);
}
// 从组件存储管理器中移除
if (this.scene && this.scene.componentStorageManager) {
this.scene.componentStorageManager.removeComponent(this.id, componentType);
}
// 调用组件的生命周期方法
component.onRemovedFromEntity();
// 发射组件移除事件
if (Entity.eventBus) {
Entity.eventBus.emitComponentRemoved({
timestamp: Date.now(),
source: 'Entity',
entityId: this.id,
entityName: this.name,
entityTag: this.tag?.toString(),
componentType: componentType.name,
component: component
});
}
// 清除组件的实体引用
component.entity = null as any;
// 通知场景实体已改变
if (this.scene && this.scene.entityProcessors) {
for (const processor of this.scene.entityProcessors.processors) {
processor.onChanged(this);
}
}
}
/**
*
*
* @param type -
* @returns null
*/
public removeComponentByType<T extends Component>(type: ComponentType<T>): T | null {
const component = this.getComponent(type);
if (component) {
this.removeComponent(component);
return component;
}
return null;
}
/**
*
*/
public removeAllComponents(): void {
// 复制组件列表,避免在迭代时修改
const componentsToRemove = [...this.components];
// 清空所有缓存和索引
this._componentCache.clear();
this._componentTypeToIndex.clear();
this._componentAccessStats.clear();
this._componentMask = BigInt(0);
// 移除组件
for (const component of componentsToRemove) {
const componentType = component.constructor as ComponentType;
// 从组件存储管理器中移除
if (this.scene && this.scene.componentStorageManager) {
this.scene.componentStorageManager.removeComponent(this.id, componentType);
}
// 调用组件的生命周期方法
component.onRemovedFromEntity();
// 清除组件的实体引用
component.entity = null as any;
}
// 清空组件列表
this.components.length = 0;
// 通知场景实体已改变
if (this.scene && this.scene.entityProcessors) {
for (const processor of this.scene.entityProcessors.processors) {
processor.onChanged(this);
}
}
}
/**
*
*
* @param components -
* @returns
*/
public addComponents<T extends Component>(components: T[]): T[] {
const addedComponents: T[] = [];
for (const component of components) {
try {
addedComponents.push(this.addComponent(component));
} catch (error) {
// 如果某个组件添加失败,继续添加其他组件
console.warn(`添加组件失败 ${component.constructor.name}:`, error);
}
}
return addedComponents;
}
/**
*
*
* @param componentTypes -
* @returns
*/
public removeComponentsByTypes<T extends Component>(componentTypes: ComponentType<T>[]): (T | null)[] {
const removedComponents: (T | null)[] = [];
for (const componentType of componentTypes) {
removedComponents.push(this.removeComponentByType(componentType));
}
return removedComponents;
}
/**
*
*
* @returns
*/
public getComponentCacheStats(): {
cacheStats: ReturnType<ComponentCache['getStats']>;
accessStats: Map<string, {
accessCount: number;
lastAccessed: number;
cacheHits: number;
cacheMisses: number;
hitRate: number;
}>;
indexMappingSize: number;
totalComponents: number;
} {
const accessStats = new Map<string, {
accessCount: number;
lastAccessed: number;
cacheHits: number;
cacheMisses: number;
hitRate: number;
}>();
for (const [componentType, stats] of this._componentAccessStats) {
const total = stats.cacheHits + stats.cacheMisses;
accessStats.set(componentType.name, {
...stats,
hitRate: total > 0 ? stats.cacheHits / total : 0
});
}
return {
cacheStats: this._componentCache.getStats(),
accessStats,
indexMappingSize: this._componentTypeToIndex.size,
totalComponents: this.components.length
};
}
/**
*
*
* 访
*/
public warmUpComponentCache(): void {
for (let i = 0; i < this.components.length; i++) {
const component = this.components[i];
const componentType = component.constructor as ComponentType;
// 更新索引映射
this._componentTypeToIndex.set(componentType, i);
// 添加到缓存
this._componentCache.set(componentType, component);
}
}
/**
*
*
*
*/
public cleanupComponentCache(): void {
// ComponentCache内部会自动处理TTL过期
// 这里我们可以强制清理一些不常用的缓存项
const now = Date.now();
const cleanupThreshold = 30000; // 30秒未访问的组件从缓存中移除
for (const [componentType, stats] of this._componentAccessStats) {
if (now - stats.lastAccessed > cleanupThreshold && stats.accessCount < 5) {
this._componentCache.delete(componentType);
}
}
}
/**
*
*
* @param type -
* @returns
*/
public getComponents<T extends Component>(type: ComponentType<T>): T[] {
const result: T[] = [];
for (const component of this.components) {
if (component instanceof type) {
result.push(component as T);
}
}
return result;
}
/**
*
*
* @param child -
* @returns
*/
public addChild(child: Entity): Entity {
if (child === this) {
throw new Error("Entity cannot be its own child");
}
if (child._parent === this) {
return child; // 已经是子实体
}
// 如果子实体已有父实体,先从原父实体移除
if (child._parent) {
child._parent.removeChild(child);
}
// 设置父子关系
child._parent = this;
this._children.push(child);
// 如果子实体还没有场景,设置为父实体的场景
if (!child.scene && this.scene) {
child.scene = this.scene;
this.scene.addEntity(child);
}
return child;
}
/**
*
*
* @param child -
* @returns
*/
public removeChild(child: Entity): boolean {
const index = this._children.indexOf(child);
if (index === -1) {
return false;
}
// 移除父子关系
this._children.splice(index, 1);
child._parent = null;
return true;
}
/**
*
*/
public removeAllChildren(): void {
// 复制子实体列表,避免在迭代时修改
const childrenToRemove = [...this._children];
for (const child of childrenToRemove) {
this.removeChild(child);
}
}
/**
*
*
* @param name -
* @param recursive -
* @returns null
*/
public findChild(name: string, recursive: boolean = false): Entity | null {
// 在直接子实体中查找
for (const child of this._children) {
if (child.name === name) {
return child;
}
}
// 递归查找
if (recursive) {
for (const child of this._children) {
const found = child.findChild(name, true);
if (found) {
return found;
}
}
}
return null;
}
/**
*
*
* @param tag -
* @param recursive -
* @returns
*/
public findChildrenByTag(tag: number, recursive: boolean = false): Entity[] {
const result: Entity[] = [];
// 在直接子实体中查找
for (const child of this._children) {
if (child.tag === tag) {
result.push(child);
}
}
// 递归查找
if (recursive) {
for (const child of this._children) {
result.push(...child.findChildrenByTag(tag, true));
}
}
return result;
}
/**
*
*
* @returns
*/
public getRoot(): Entity {
let root: Entity = this;
while (root._parent) {
root = root._parent;
}
return root;
}
/**
*
*
* @param entity -
* @returns true
*/
public isAncestorOf(entity: Entity): boolean {
let current = entity._parent;
while (current) {
if (current === this) {
return true;
}
current = current._parent;
}
return false;
}
/**
*
*
* @param entity -
* @returns true
*/
public isDescendantOf(entity: Entity): boolean {
return entity.isAncestorOf(this);
}
/**
*
*
* @returns 0
*/
public getDepth(): number {
let depth = 0;
let current = this._parent;
while (current) {
depth++;
current = current._parent;
}
return depth;
}
/**
*
*
* @param callback -
* @param recursive -
*/
public forEachChild(callback: (child: Entity, index: number) => void, recursive: boolean = false): void {
this._children.forEach((child, index) => {
callback(child, index);
if (recursive) {
child.forEachChild(callback, true);
}
});
}
/**
*
*/
private onActiveChanged(): void {
// 通知所有组件活跃状态改变
for (const component of this.components) {
if ('onActiveChanged' in component && typeof component.onActiveChanged === 'function') {
(component as any).onActiveChanged();
}
}
// 通知场景实体状态改变
if (this.scene && this.scene.eventSystem) {
this.scene.eventSystem.emitSync('entity:activeChanged', {
entity: this,
active: this._active,
activeInHierarchy: this.activeInHierarchy
});
}
}
/**
*
*
*
*/
public update(): void {
if (!this.activeInHierarchy || this._isDestroyed) {
return;
}
// 更新所有组件
for (const component of this.components) {
if (component.enabled) {
component.update();
}
}
// 更新所有子实体
for (const child of this._children) {
child.update();
}
}
/**
*
*
*
*/
public destroy(): void {
if (this._isDestroyed) {
return;
}
this._isDestroyed = true;
// 销毁所有子实体
const childrenToDestroy = [...this._children];
for (const child of childrenToDestroy) {
child.destroy();
}
// 从父实体中移除
if (this._parent) {
this._parent.removeChild(this);
}
// 移除所有组件
this.removeAllComponents();
// 从场景中移除
if (this.scene && this.scene.entities) {
this.scene.entities.remove(this);
}
}
/**
*
*
* @param other -
* @returns
*/
public compareTo(other: Entity): number {
return EntityComparer.prototype.compare(this, other);
}
/**
*
*
* @returns
*/
public toString(): string {
return `Entity[${this.name}:${this.id}]`;
}
/**
*
*
* @returns
*/
public getDebugInfo(): {
name: string;
id: number;
enabled: boolean;
active: boolean;
activeInHierarchy: boolean;
destroyed: boolean;
componentCount: number;
componentTypes: string[];
componentMask: string;
parentId: number | null;
childCount: number;
childIds: number[];
depth: number;
componentCache: {
size: number;
maxSize: number;
hitRate: number;
averageAccessCount: number;
};
componentAccessStats: Array<{
componentType: string;
accessCount: number;
cacheHits: number;
cacheMisses: number;
hitRate: number;
lastAccessed: string;
}>;
indexMappingSize: number;
} {
const cacheStats = this.getComponentCacheStats();
const accessStatsArray = Array.from(cacheStats.accessStats.entries()).map(([type, stats]) => ({
componentType: type,
accessCount: stats.accessCount,
cacheHits: stats.cacheHits,
cacheMisses: stats.cacheMisses,
hitRate: stats.hitRate,
lastAccessed: new Date(stats.lastAccessed).toISOString()
}));
return {
name: this.name,
id: this.id,
enabled: this._enabled,
active: this._active,
activeInHierarchy: this.activeInHierarchy,
destroyed: this._isDestroyed,
componentCount: this.components.length,
componentTypes: this.components.map(c => c.constructor.name),
componentMask: this._componentMask.toString(2), // 二进制表示
parentId: this._parent?.id || null,
childCount: this._children.length,
childIds: this._children.map(c => c.id),
depth: this.getDepth(),
componentCache: cacheStats.cacheStats,
componentAccessStats: accessStatsArray,
indexMappingSize: cacheStats.indexMappingSize
};
}
}