-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Can now switch between gamepad and keyboard
- Loading branch information
Showing
5 changed files
with
171 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,73 @@ | ||
import { Key, KeyCode, KeyListener } from "../../browser/key"; | ||
import { Logging } from "../../logging"; | ||
import { Undefined } from "../../types/undefined"; | ||
|
||
export type KeyboardControls<TAction extends string> = { | ||
[index in TAction]: KeyCode; | ||
export enum InputModalityType { | ||
Keyboard = 'Keyboard', | ||
Gamepad = 'Gamepad', | ||
} | ||
|
||
export interface MappedInputModality<TAction extends string> { | ||
readonly type: InputModalityType; | ||
readonly lastEventTimestamp: number; | ||
isDown(action: TAction): boolean; | ||
isUp(action: TAction): boolean; | ||
justWentDown(action: TAction): boolean; | ||
justWentUp(action: TAction): boolean; | ||
start(): void; | ||
tick(): void; | ||
} | ||
|
||
export class AsshatInput<TAction extends string> { | ||
constructor(readonly keyboardControls: KeyboardControls<TAction>) { | ||
private _currentModality?: MappedInputModality<TAction>; | ||
|
||
constructor(private readonly _modalities: MappedInputModality<TAction>[]) { | ||
console.log(...Logging.componentArgs(this)); | ||
} | ||
|
||
isDown(action: TAction) { | ||
return Key.isDown(this.keyboardControls[action]); | ||
return this._currentModality?.isDown(action) as boolean; | ||
} | ||
|
||
isUp(action: TAction) { | ||
return Key.isUp(this.keyboardControls[action]); | ||
return this._currentModality?.isUp(action) as boolean; | ||
} | ||
|
||
justWentDown(action: TAction) { | ||
return Key.justWentDown(this.keyboardControls[action]); | ||
return this._currentModality?.justWentDown(action) as boolean; | ||
} | ||
|
||
justWentUp(action: TAction) { | ||
return Key.justWentUp(this.keyboardControls[action]); | ||
return this._currentModality?.justWentUp(action) as boolean; | ||
} | ||
|
||
start() { | ||
KeyListener.start(); | ||
for (const modality of this._modalities) | ||
modality.start(); | ||
} | ||
|
||
tick() { | ||
KeyListener.advance(); | ||
let latestEventTimestamp = -1; | ||
let modalityWithLatestEventTimestamp = Undefined<MappedInputModality<TAction>>(); | ||
|
||
for (let i = 0; i < this._modalities.length; i++) { | ||
const modality = this._modalities[i]; | ||
modality.tick(); | ||
if (modality.lastEventTimestamp > latestEventTimestamp) { | ||
latestEventTimestamp = modality.lastEventTimestamp; | ||
modalityWithLatestEventTimestamp = modality; | ||
} | ||
} | ||
|
||
if (modalityWithLatestEventTimestamp !== this._currentModality) { | ||
const fromType = this._currentModality?.type; | ||
this._currentModality = modalityWithLatestEventTimestamp; | ||
const toType = this._currentModality?.type; | ||
if (fromType && toType) | ||
this.onModalityChanged(fromType, toType); | ||
} | ||
} | ||
|
||
protected onModalityChanged(from: InputModalityType, to: InputModalityType) { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { KeyCode, KeyListener } from "../../browser/key-listener"; | ||
import { InputModalityType, MappedInputModality } from "./asshat-input"; | ||
|
||
export type KeyboardControls<TAction extends string> = { | ||
[index in TAction]: KeyCode; | ||
} | ||
|
||
export class MappedKeyboard<TAction extends string> implements MappedInputModality<TAction> { | ||
private readonly _keyListener: LimitedKeyListener; | ||
|
||
readonly type = InputModalityType.Keyboard; | ||
get lastEventTimestamp() { | ||
return this._keyListener.lastEventTimestamp; | ||
}; | ||
|
||
constructor(private readonly _controls: KeyboardControls<TAction>) { | ||
const keyCodes = Object.values(_controls as Record<string, KeyCode>); | ||
this._keyListener = new LimitedKeyListener(new Set(keyCodes)); | ||
} | ||
|
||
isDown(action: TAction): boolean { | ||
return this._keyListener.isDown(this._controls[action]); | ||
} | ||
|
||
isUp(action: TAction): boolean { | ||
return this._keyListener.isUp(this._controls[action]); | ||
} | ||
|
||
justWentDown(action: TAction): boolean { | ||
return this._keyListener.justWentDown(this._controls[action]); | ||
} | ||
|
||
justWentUp(action: TAction): boolean { | ||
return this._keyListener.justWentUp(this._controls[action]); | ||
} | ||
|
||
start(): void { | ||
this._keyListener.start(); | ||
} | ||
|
||
tick(): void { | ||
this._keyListener.tick(); | ||
} | ||
} | ||
|
||
class LimitedKeyListener extends KeyListener { | ||
lastEventTimestamp = -1; | ||
|
||
constructor(private readonly _keyCodes: Set<KeyCode>) { | ||
super(); | ||
} | ||
|
||
protected onKeyDown(event: KeyboardEvent): void { | ||
if (!this._keyCodes.has(event.code as KeyCode)) | ||
return; | ||
this.lastEventTimestamp = performance.now(); | ||
super.onKeyDown(event); | ||
} | ||
|
||
protected onKeyUp(event: KeyboardEvent): void { | ||
if (!this._keyCodes.has(event.code as KeyCode)) | ||
return; | ||
super.onKeyUp(event); | ||
} | ||
} |