feat: migrate JavaScript files to TypeScript, enhancing type safety and maintainability across the codebase
This commit is contained in:
parent
3db2bb9160
commit
c582f2004e
107 changed files with 5876 additions and 3588 deletions
75
src/systems/DeathSystem.ts
Normal file
75
src/systems/DeathSystem.ts
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue