import { Jump, Forces, ComponentNames, Velocity, Mass, Control, } from "../components"; import { Game } from "../Game"; import { KeyConstants, PhysicsConstants } from "../config"; import { Action } from "../interfaces"; import { System, SystemNames } from "."; export class Input extends System { private keys: Set; private actionTimeStamps: Map; constructor() { super(SystemNames.Input); this.keys = new Set(); this.actionTimeStamps = new Map(); } public keyPressed(key: string) { this.keys.add(key); } public keyReleased(key: string) { this.keys.delete(key); } private hasSomeKey(keys?: string[]): boolean { if (keys) { return keys.some((key) => this.keys.has(key)); } return false; } public update(_dt: number, game: Game) { game.forEachEntityWithComponent(ComponentNames.Control, (entity) => { const controlComponent = entity.getComponent( ComponentNames.Control, ); if (this.hasSomeKey(KeyConstants.ActionKeys.get(Action.MOVE_RIGHT))) { controlComponent.controlVelocityComponent.velocity.dCartesian.dx += PhysicsConstants.PLAYER_MOVE_VEL; } if (this.hasSomeKey(KeyConstants.ActionKeys.get(Action.MOVE_LEFT))) { controlComponent.controlVelocityComponent.velocity.dCartesian.dx += -PhysicsConstants.PLAYER_MOVE_VEL; } if (entity.hasComponent(ComponentNames.Jump)) { const velocity = entity.getComponent( ComponentNames.Velocity, ).velocity; const jump = entity.getComponent(ComponentNames.Jump); if (this.hasSomeKey(KeyConstants.ActionKeys.get(Action.JUMP))) { if (jump.canJump) { this.actionTimeStamps.set(Action.JUMP, performance.now()); velocity.dCartesian.dy += PhysicsConstants.PLAYER_JUMP_INITIAL_VEL; jump.canJump = false; } if ( performance.now() - (this.actionTimeStamps.get(Action.JUMP) || 0) < PhysicsConstants.MAX_JUMP_TIME_MS ) { const mass = entity.getComponent(ComponentNames.Mass).mass; entity.getComponent(ComponentNames.Forces)?.forces.push({ fCartesian: { fy: mass * PhysicsConstants.PLAYER_JUMP_ACC, fx: 0, }, torque: 0, }); } } } }); } }