forked from SpringRoll/SpringRoll
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKey.js
More file actions
65 lines (62 loc) · 1.65 KB
/
Key.js
File metadata and controls
65 lines (62 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* Represents a single key on the keyboard and the functions related to it.
* @class Key
* @property {0 | 1 | 2} state The current state of the key. 0 = inactive, 1 = active, 2 = to be set to inactive.
* @property {string} key The name of the key we are targeting.
* @property {object} actions
* @property {function} actions.down Function to be called while the key is held down.
* @property {function} actions.up Function to be called when the key is lifted up.
* @param {string} key What this object represents.
* @param {Function} [down=() => {}] Function to be called while the key is held down.
* @param {Function} [up=() => {}] Function to be called when the key is lifted up.
*/
export class Key {
/**
* Creates an instance of Key.
*/
constructor(key, down, up) {
this.key = key;
this._state = 0;
this.actions = {
up,
down
};
}
/**
*
* Updates the internal state of the key. Accepts a range between 0-2. Will set key state to 0 if nothing is passed.
* @param {0 | 1 | 2} [state=0]
* @memberof Key
*/
updateState(state = 0) {
if (state < 3 && state > -1) {
this._state = state;
}
}
/**
* Calls the relevant action for the current state of the key.
* @memberof Key
*/
action() {
if (1 === this.state) {
if (this.actions.down) {
this.actions.down();
}
} else if (2 === this.state) {
if (this.actions.up) {
this.actions.up();
}
this.updateState(0);
}
}
/**
*
* Returns the current state of the key.
* @readonly
* @returns { number }
* @memberof Key
*/
get state() {
return this._state;
}
}