75 lines
2.5 KiB
TypeScript
75 lines
2.5 KiB
TypeScript
import { System } from '../core/System.ts';
|
|
import { SystemName, ComponentType } from '../core/Constants.ts';
|
|
import type { Entity } from '../core/Entity.ts';
|
|
import type { Health } from '../components/Health.ts';
|
|
import type { Evolution } from '../components/Evolution.ts';
|
|
import type { Absorbable } from '../components/Absorbable.ts';
|
|
import type { MenuSystem } from './MenuSystem.ts';
|
|
|
|
/**
|
|
* System responsible for managing entity death, game over states, and cleanup of dead entities.
|
|
*/
|
|
export class DeathSystem extends System {
|
|
constructor() {
|
|
super(SystemName.DEATH);
|
|
this.requiredComponents = [ComponentType.HEALTH];
|
|
this.priority = 50;
|
|
}
|
|
|
|
/**
|
|
* Override update to process all entities (including inactive/dead ones).
|
|
* @param deltaTime - Time elapsed since last frame
|
|
* @param _entities - Filtered active entities (not used, uses raw engine entities)
|
|
*/
|
|
update(deltaTime: number, _entities: Entity[]): void {
|
|
const allEntities = this.engine.entities;
|
|
this.process(deltaTime, allEntities);
|
|
}
|
|
|
|
/**
|
|
* Process death logic for all entities.
|
|
* @param _deltaTime - Time elapsed since last frame
|
|
* @param allEntities - All entities in the engine
|
|
*/
|
|
process(_deltaTime: number, allEntities: Entity[]): void {
|
|
allEntities.forEach((entity) => {
|
|
const health = entity.getComponent<Health>(ComponentType.HEALTH);
|
|
if (!health) return;
|
|
|
|
if (health.isDead()) {
|
|
const evolution = entity.getComponent<Evolution>(ComponentType.EVOLUTION);
|
|
if (evolution) {
|
|
const menuSystem = this.engine.systems.find((s) => s.name === SystemName.MENU) as
|
|
| MenuSystem
|
|
| undefined;
|
|
if (menuSystem) {
|
|
menuSystem.showGameOver();
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (entity.active || !entity.deathTime) {
|
|
if (entity.active) {
|
|
entity.active = false;
|
|
}
|
|
if (!entity.deathTime) {
|
|
entity.deathTime = Date.now();
|
|
}
|
|
}
|
|
|
|
const absorbable = entity.getComponent<Absorbable>(ComponentType.ABSORBABLE);
|
|
if (absorbable && !absorbable.absorbed) {
|
|
const timeSinceDeath = (Date.now() - (entity.deathTime || 0)) / 1000;
|
|
if (timeSinceDeath > 3.0) {
|
|
this.engine.removeEntity(entity);
|
|
}
|
|
} else {
|
|
const timeSinceDeath = (Date.now() - (entity.deathTime || 0)) / 1000;
|
|
if (timeSinceDeath > 0.5) {
|
|
this.engine.removeEntity(entity);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|