2025-05-02 23:44:21 +00:00
|
|
|
import { globalConfig } from "../core/config";
|
|
|
|
|
import { createLogger } from "../core/logging";
|
|
|
|
|
import { newEmptyMap } from "../core/utils";
|
|
|
|
|
import { BasicSerializableObject, types } from "../savegame/serialization";
|
2024-06-20 09:59:07 +00:00
|
|
|
import { Component } from "./component";
|
|
|
|
|
import { Entity } from "./entity";
|
2025-05-02 23:44:21 +00:00
|
|
|
import { GameRoot } from "./root";
|
2024-06-20 09:59:07 +00:00
|
|
|
|
|
|
|
|
const logger = createLogger("entity_manager");
|
|
|
|
|
|
|
|
|
|
// Manages all entities
|
|
|
|
|
|
|
|
|
|
// NOTICE: We use arrayDeleteValue instead of fastArrayDeleteValue since that does not preserve the order
|
|
|
|
|
// This is slower but we need it for the street path generation
|
|
|
|
|
|
|
|
|
|
export class EntityManager extends BasicSerializableObject {
|
2025-05-02 23:44:21 +00:00
|
|
|
readonly root: GameRoot;
|
|
|
|
|
readonly entities = new Map<number, Entity>();
|
2024-06-20 09:59:07 +00:00
|
|
|
|
2025-05-02 23:44:21 +00:00
|
|
|
// We store a separate list with entities to destroy, since we don't destroy
|
|
|
|
|
// them instantly
|
|
|
|
|
private destroyList: Entity[] = [];
|
2024-06-20 09:59:07 +00:00
|
|
|
|
2025-05-02 23:44:21 +00:00
|
|
|
// Store a map from componentid to entities - This is used by the game system
|
|
|
|
|
// for faster processing
|
|
|
|
|
private readonly componentToEntity: Record<string, Set<Entity>> = newEmptyMap();
|
2024-06-20 09:59:07 +00:00
|
|
|
|
2025-05-02 23:44:21 +00:00
|
|
|
// Store the next uid to use
|
|
|
|
|
private nextUid = 10000;
|
2024-06-20 09:59:07 +00:00
|
|
|
|
2025-05-02 23:44:21 +00:00
|
|
|
constructor(root: GameRoot) {
|
|
|
|
|
super();
|
|
|
|
|
this.root = root;
|
2024-06-20 09:59:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static getId() {
|
|
|
|
|
return "EntityManager";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static getSchema() {
|
|
|
|
|
return {
|
|
|
|
|
nextUid: types.uint,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getStatsText() {
|
2025-05-02 23:44:21 +00:00
|
|
|
return this.entities.size + " entities [" + this.destroyList.length + " to kill]";
|
2024-06-20 09:59:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Main update
|
|
|
|
|
update() {
|
|
|
|
|
this.processDestroyList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Registers a new entity
|
2025-05-02 23:44:21 +00:00
|
|
|
* @param uid Optional predefined uid
|
2024-06-20 09:59:07 +00:00
|
|
|
*/
|
2025-05-03 00:04:39 +00:00
|
|
|
registerEntity(entity: Entity, uid = this.generateUid()) {
|
2024-06-20 09:59:07 +00:00
|
|
|
if (G_IS_DEV && !globalConfig.debug.disableSlowAsserts) {
|
2025-05-02 23:44:21 +00:00
|
|
|
assert(
|
|
|
|
|
this.entities.get(entity.uid) !== entity,
|
|
|
|
|
`RegisterEntity() called twice for entity ${entity}`
|
|
|
|
|
);
|
2024-06-20 09:59:07 +00:00
|
|
|
}
|
|
|
|
|
assert(!entity.destroyed, `Attempting to register destroyed entity ${entity}`);
|
|
|
|
|
|
2025-05-03 00:04:39 +00:00
|
|
|
if (G_IS_DEV && !globalConfig.debug.disableSlowAsserts) {
|
2024-06-20 09:59:07 +00:00
|
|
|
assert(!this.findByUid(uid, false), "Entity uid already taken: " + uid);
|
|
|
|
|
assert(uid >= 0 && uid < Number.MAX_SAFE_INTEGER, "Invalid uid passed: " + uid);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-02 23:44:21 +00:00
|
|
|
// Give each entity a unique id
|
2025-05-03 00:04:39 +00:00
|
|
|
entity.uid = uid;
|
2025-05-02 23:44:21 +00:00
|
|
|
entity.registered = true;
|
|
|
|
|
|
|
|
|
|
this.entities.set(entity.uid, entity);
|
2024-06-20 09:59:07 +00:00
|
|
|
|
|
|
|
|
// Register into the componentToEntity map
|
|
|
|
|
for (const componentId in entity.components) {
|
|
|
|
|
if (entity.components[componentId]) {
|
2025-05-02 23:44:21 +00:00
|
|
|
const set = (this.componentToEntity[componentId] ??= new Set());
|
|
|
|
|
set.add(entity);
|
2024-06-20 09:59:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.root.signals.entityAdded.dispatch(entity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generates a new uid
|
|
|
|
|
*/
|
2025-05-02 23:44:21 +00:00
|
|
|
generateUid(): number {
|
2024-06-20 09:59:07 +00:00
|
|
|
return this.nextUid++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Call to attach a new component after the creation of the entity
|
|
|
|
|
*/
|
2025-05-02 23:44:21 +00:00
|
|
|
attachDynamicComponent(entity: Entity, component: Component) {
|
2024-06-20 09:59:07 +00:00
|
|
|
entity.addComponent(component, true);
|
2025-05-02 23:44:21 +00:00
|
|
|
const componentId = /** @type {typeof Component} */ component.constructor.getId();
|
|
|
|
|
const set = (this.componentToEntity[componentId] ??= new Set());
|
|
|
|
|
set.add(entity);
|
2024-06-20 09:59:07 +00:00
|
|
|
this.root.signals.entityGotNewComponent.dispatch(entity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Call to remove a component after the creation of the entity
|
|
|
|
|
*/
|
2025-05-02 23:44:21 +00:00
|
|
|
removeDynamicComponent(entity: Entity, component: typeof Component) {
|
2024-06-20 09:59:07 +00:00
|
|
|
entity.removeComponent(component, true);
|
2025-05-02 23:44:21 +00:00
|
|
|
const componentId = /** @type {typeof Component} */ component.constructor.getId();
|
2024-06-20 09:59:07 +00:00
|
|
|
|
2025-05-02 23:44:21 +00:00
|
|
|
this.componentToEntity[componentId].delete(entity);
|
2024-06-20 09:59:07 +00:00
|
|
|
this.root.signals.entityComponentRemoved.dispatch(entity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-05-02 23:44:21 +00:00
|
|
|
* Finds an entity by its uid
|
2024-06-20 09:59:07 +00:00
|
|
|
*/
|
2025-05-02 23:44:21 +00:00
|
|
|
findByUid(uid: number, errorWhenNotFound = true): Entity {
|
|
|
|
|
const entity = this.entities.get(uid);
|
|
|
|
|
|
|
|
|
|
if (entity === undefined || entity.queuedForDestroy || entity.destroyed) {
|
|
|
|
|
if (errorWhenNotFound) {
|
|
|
|
|
logger.warn("Entity with UID", uid, "not found (destroyed)");
|
2024-06-20 09:59:07 +00:00
|
|
|
}
|
2025-05-02 23:44:21 +00:00
|
|
|
|
|
|
|
|
return null;
|
2024-06-20 09:59:07 +00:00
|
|
|
}
|
2025-05-02 23:44:21 +00:00
|
|
|
|
|
|
|
|
return entity;
|
2024-06-20 09:59:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a map which gives a mapping from UID to Entity.
|
|
|
|
|
* This map is not updated.
|
|
|
|
|
*/
|
2025-05-02 23:44:21 +00:00
|
|
|
getFrozenUidSearchMap(): Map<number, Entity> {
|
2024-06-20 09:59:07 +00:00
|
|
|
const result = new Map();
|
2025-05-02 23:44:21 +00:00
|
|
|
for (const [uid, entity] of this.entities) {
|
2024-06-20 09:59:07 +00:00
|
|
|
if (!entity.queuedForDestroy && !entity.destroyed) {
|
2025-05-02 23:44:21 +00:00
|
|
|
result.set(uid, entity);
|
2024-06-20 09:59:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns all entities having the given component
|
2025-05-02 23:59:57 +00:00
|
|
|
* @deprecated use {@link getEntitiesWithComponent} instead
|
2024-06-20 09:59:07 +00:00
|
|
|
*/
|
2025-05-02 23:44:21 +00:00
|
|
|
getAllWithComponent(componentHandle: typeof Component): Entity[] {
|
|
|
|
|
return [...(this.componentToEntity[componentHandle.getId()] ?? new Set())];
|
2024-06-20 09:59:07 +00:00
|
|
|
}
|
|
|
|
|
|
2025-05-02 23:59:57 +00:00
|
|
|
/**
|
|
|
|
|
* A version of {@link getAllWithComponent} that returns a Set
|
|
|
|
|
*/
|
|
|
|
|
getEntitiesWithComponent(componentHandle: typeof Component): Set<Entity> {
|
|
|
|
|
return new Set(this.componentToEntity[componentHandle.getId()] ?? []);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-20 09:59:07 +00:00
|
|
|
/**
|
|
|
|
|
* Unregisters all components of an entity from the component to entity mapping
|
|
|
|
|
*/
|
2025-05-02 23:44:21 +00:00
|
|
|
unregisterEntityComponents(entity: Entity) {
|
2024-06-20 09:59:07 +00:00
|
|
|
for (const componentId in entity.components) {
|
|
|
|
|
if (entity.components[componentId]) {
|
2025-05-02 23:44:21 +00:00
|
|
|
this.componentToEntity[componentId].delete(entity);
|
2024-06-20 09:59:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Processes the entities to destroy and actually destroys them
|
|
|
|
|
processDestroyList() {
|
|
|
|
|
for (let i = 0; i < this.destroyList.length; ++i) {
|
|
|
|
|
const entity = this.destroyList[i];
|
|
|
|
|
|
|
|
|
|
// Remove from entities list
|
2025-05-02 23:44:21 +00:00
|
|
|
this.entities.delete(entity.uid);
|
2024-06-20 09:59:07 +00:00
|
|
|
|
|
|
|
|
// Remove from componentToEntity list
|
|
|
|
|
this.unregisterEntityComponents(entity);
|
|
|
|
|
|
|
|
|
|
entity.registered = false;
|
|
|
|
|
entity.destroyed = true;
|
|
|
|
|
|
|
|
|
|
this.root.signals.entityDestroyed.dispatch(entity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.destroyList = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Queues an entity for destruction
|
|
|
|
|
*/
|
2025-05-02 23:44:21 +00:00
|
|
|
destroyEntity(entity: Entity) {
|
2024-06-20 09:59:07 +00:00
|
|
|
if (entity.destroyed) {
|
|
|
|
|
logger.error("Tried to destroy already destroyed entity:", entity.uid);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (entity.queuedForDestroy) {
|
|
|
|
|
logger.error("Trying to destroy entity which is already queued for destroy!", entity.uid);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.destroyList.indexOf(entity) < 0) {
|
|
|
|
|
this.destroyList.push(entity);
|
|
|
|
|
entity.queuedForDestroy = true;
|
|
|
|
|
this.root.signals.entityQueuedForDestroy.dispatch(entity);
|
|
|
|
|
} else {
|
|
|
|
|
assert(false, "Trying to destroy entity twice");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|