This repository has been archived on 2025-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
rpgedit/src/view/input/directionhandler.ts

37 lines
811 B
TypeScript

import { InputHandler } from './inputhandler';
import { Listenable } from '../../util';
import { vec2 } from 'gl-matrix';
export const enum Keycode {
Left = 37,
Up = 38,
Right = 39,
Down = 40,
}
export class DirectionHandler extends Listenable<[vec2]> {
private readonly input: InputHandler;
constructor() {
super();
this.input = new InputHandler(new Set([Keycode.Left, Keycode.Up, Keycode.Right, Keycode.Down]));
this.input.addListener(() => {
const dir = vec2.create();
if (this.input.has(Keycode.Left))
vec2.add(dir, dir, [-1, 0]);
if (this.input.has(Keycode.Up))
vec2.add(dir, dir, [0, -1]);
if (this.input.has(Keycode.Right))
vec2.add(dir, dir, [1, 0]);
if (this.input.has(Keycode.Down))
vec2.add(dir, dir, [0, 1]);
this.runListeners(dir);
});
}
}