From 72c6c7de12e9833f52bf2d0718d70f044f8ab57e Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Thu, 20 Jul 2023 20:47:32 -0700 Subject: a bit of refactoring; importing engine into bun for server --- engine/systems/Input.ts | 83 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 engine/systems/Input.ts (limited to 'engine/systems/Input.ts') diff --git a/engine/systems/Input.ts b/engine/systems/Input.ts new file mode 100644 index 0000000..9a92163 --- /dev/null +++ b/engine/systems/Input.ts @@ -0,0 +1,83 @@ +import { + Jump, + Forces, + Acceleration, + ComponentNames, + Velocity, + Mass, +} from "../components"; +import { Game } from "../Game"; +import { KeyConstants, PhysicsConstants } from "../config"; +import type { Entity } from "../entities"; +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 { + return keys.some((key) => this.keys.has(key)); + } + + public update(dt: number, game: Game) { + game.componentEntities.get(ComponentNames.Control)?.forEach((entityId) => { + const entity = game.entities.get(entityId); + if (!entity.hasComponent(ComponentNames.Velocity)) { + return; + } + + const velocity = entity.getComponent(ComponentNames.Velocity); + + if (this.hasSomeKey(KeyConstants.ActionKeys.get(Action.MOVE_RIGHT))) { + velocity.dCartesian.dx = PhysicsConstants.PLAYER_MOVE_VEL; + } else if ( + this.hasSomeKey(KeyConstants.ActionKeys.get(Action.MOVE_LEFT)) + ) { + velocity.dCartesian.dx = -PhysicsConstants.PLAYER_MOVE_VEL; + } else { + velocity.dCartesian.dx = 0; + } + }); + + game.componentEntities.get(ComponentNames.Jump)?.forEach((entityId) => { + const entity = game.entities.get(entityId); + const jump = entity.getComponent(ComponentNames.Jump); + const velocity = entity.getComponent(ComponentNames.Velocity); + + 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) < + 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 }, + }); + } + } + }); + } +} -- cgit v1.2.3-70-g09d2