import { System } from '../core/System.ts'; import { SkillRegistry } from '../skills/SkillRegistry.ts'; import { SystemName, ComponentType } from '../core/Constants.ts'; import { Events } from '../core/EventBus.ts'; import type { Entity } from '../core/Entity.ts'; import type { Skills } from '../components/Skills.ts'; import type { Intent } from '../components/Intent.ts'; /** * System responsible for managing skill cooldowns and activating skills based on entity intent. */ export class SkillSystem extends System { constructor() { super(SystemName.SKILL); this.requiredComponents = [ComponentType.SKILLS]; this.priority = 30; } /** * Process all entities with skills, updating cooldowns and activating skills if intended. * @param deltaTime - Time elapsed since last frame in seconds * @param entities - Entities matching system requirements */ process(deltaTime: number, entities: Entity[]): void { entities.forEach((entity) => { const skills = entity.getComponent(ComponentType.SKILLS); const intent = entity.getComponent(ComponentType.INTENT); if (!skills) return; skills.updateCooldowns(deltaTime); if (intent && intent.action === 'skill_use') { const skillId = intent.data.skillId; if (skillId && !skills.isOnCooldown(skillId)) { this.activateSkill(entity, skillId); } intent.clear(); } }); } /** * Activate a specific skill for an entity. * @param entity - The entity performing the skill * @param skillId - The ID of the skill to activate */ activateSkill(entity: Entity, skillId: string): void { const skill = SkillRegistry.get(skillId); if (!skill) { console.warn(`Skill not found: ${skillId}`); return; } if (skill.activate(entity, this.engine)) { const skills = entity.getComponent(ComponentType.SKILLS); if (skills) { skills.setCooldown(skillId, skill.cooldown); } this.engine.emit(Events.SKILL_COOLDOWN_STARTED, { skillId }); } } }