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
64
src/core/System.ts
Normal file
64
src/core/System.ts
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
import { Entity } from './Entity.ts';
|
||||
import type { Engine } from './Engine.ts';
|
||||
|
||||
/**
|
||||
* Base System class for ECS architecture.
|
||||
* Systems contain logic that operates on entities with specific components.
|
||||
*/
|
||||
export class System {
|
||||
/** Unique identifier for the system */
|
||||
readonly name: string;
|
||||
|
||||
/** List of component types required by this system */
|
||||
requiredComponents: string[];
|
||||
|
||||
/** Execution priority (lower runs first) */
|
||||
priority: number;
|
||||
|
||||
/** Reference to the game engine */
|
||||
protected engine!: Engine;
|
||||
|
||||
/**
|
||||
* @param name - The unique name of the system
|
||||
*/
|
||||
constructor(name: string) {
|
||||
this.name = name;
|
||||
this.requiredComponents = [];
|
||||
this.priority = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an entity matches this system's requirements.
|
||||
* @param entity - The entity to check
|
||||
* @returns True if the entity is active and has all required components
|
||||
*/
|
||||
matches(entity: Entity): boolean {
|
||||
if (!entity.active) return false;
|
||||
return this.requiredComponents.every((componentType) => entity.hasComponent(componentType));
|
||||
}
|
||||
|
||||
/**
|
||||
* Main update entry point called every frame.
|
||||
* @param deltaTime - Time elapsed since last frame in seconds
|
||||
* @param entities - All entities in the engine
|
||||
*/
|
||||
update(deltaTime: number, entities: Entity[]): void {
|
||||
const matchingEntities = entities.filter((entity) => this.matches(entity));
|
||||
this.process(deltaTime, matchingEntities);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process matching entities. To be implemented by subclasses.
|
||||
* @param _deltaTime - Time elapsed since last frame in seconds
|
||||
* @param _entities - Filtered entities that match this system
|
||||
*/
|
||||
process(_deltaTime: number, _entities: Entity[]): void {}
|
||||
|
||||
/**
|
||||
* Called when system is added to engine.
|
||||
* @param engine - The game engine instance
|
||||
*/
|
||||
init(engine: Engine): void {
|
||||
this.engine = engine;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue