@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
68 lines (58 loc) • 1.42 kB
JavaScript
import Signal from "../../../core/events/signal/Signal.js";
/**
* Representation of an input device key/button
*
* @author Alex Goldring
* @copyright Company Named Limited (c) 2025
*/
export class InputDeviceSwitch {
/**
* Switch press
* @readonly
* @type {Signal<>}
*/
down = new Signal()
/**
* Switch release
* @readonly
* @type {Signal<>}
*/
up = new Signal()
/**
* is button currently being pressed?
* Do not modify directly
* @type {boolean}
*/
is_down = false
/**
* Convenience accessor, dual of {@link is_down}
* @returns {boolean}
*/
get is_up() {
return !this.is_down;
}
/**
* Execute press, will trigger {@link down} signal if previous state was up
* Idempotent
* NOTE: make sure you understand the implications of using this
*/
press() {
if (this.is_down) {
return;
}
this.is_down = true;
this.down.send0();
}
/**
* Execute release, will trigger {@link up} signal if previous state was down
* Idempotent
* NOTE: make sure you understand the implications of using this
*/
release() {
if (!this.is_down) {
return;
}
this.is_down = false;
this.up.send0();
}
}