93 lines
2.1 KiB
TypeScript

import { ControllerState } from "../../models/controller-state";
import { useAppStore } from "../../stores/app-store";
export abstract class KeyContext {
public abstract readonly name: string;
protected store = useAppStore();
public handleKey(event: KeyboardEvent): void {
switch (event.key) {
case 'ArrowRight':
this.onKeyRight();
break;
case 'ArrowLeft':
this.onKeyLeft();
break;
case 'ArrowUp':
this.onKeyUp();
break;
case 'ArrowDown':
this.onKeyDown();
break;
case 'Escape':
this.onEscape();
break;
case 'Enter':
this.onEnter();
break;
case ' ':
this.onSpace();
break;
default:
break;
}
}
public handleState(state: ControllerState) {
if (state.joystick.x === 0) {
this.onKeyLeft()
}
else if (state.joystick.x === 255) {
this.onKeyRight()
}
if (state.joystick.y === 0) {
this.onKeyUp()
}
else if (state.joystick.y === 255) {
this.onKeyDown()
}
if ((state.buttons & 0x04) !== 0) {
this.onEnter()
}
if ((state.buttons & 0x08) !== 0) {
this.onSpace()
}
// TODO should be 0x01 when the power button will work
if ((state.buttons & 0x02) !== 0) {
this.onEscape()
}
}
protected onKeyRight(): void {
console.log('onKeyRight');
}
protected onKeyLeft(): void {
console.log('onKeyLeft');
}
protected onKeyUp(): void {
console.log('onKeyUp');
}
protected onKeyDown(): void {
console.log('onKeyDown');
}
protected onEscape(): void {
console.log('onEscape');
}
protected onEnter(): void {
console.log('onEnter');
}
protected onSpace(): void {
console.log('onSpace');
}
}