-
Notifications
You must be signed in to change notification settings - Fork 21
/
GameAction.js
160 lines (144 loc) · 3.84 KB
/
GameAction.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* The GameAction handles DOM events for use in games.
* @name GameAction
* @constructor GameAction
*/
/**
* A map of static constants for internal use
* @type {Object}
* @memberOf GameAction#
* @property {Number} NORMAL Normal behavior. The isPressed() method returns true as long as the key is held down.
* @property {Number} DETECT_INITAL_PRESS_ONLY Initial press behavior. The isPressed() method returns true only after the key is first pressed, and not again until the key is released and pressed again.
* @property {Number} STATE_RELEASED Value for released state
* @property {Number} STATE_PRESSED Value for pressed state
* @property {Number} STATE_WAITING_FOR_RELEASE Value for waiting for release state
* @property {Number} STATE_MOVED Value for moved state
*/
const statics = {
NORMAL: 0,
DETECT_INITAL_PRESS_ONLY: 1,
STATE_RELEASED: 0,
STATE_PRESSED: 1,
STATE_WAITING_FOR_RELEASE: 2,
STATE_MOVED: 3
};
class GameAction {
constructor(options = {}){
/**
* A name to reference the GameAction with
* @type {String}
* @memberOf GameAction#
* @default
*/
this.name = null;
/**
* Whether or not to detect only the intial press of the game action
* @type {Number}
* @memberOf GameAction#
* @default
*/
this.behavior = 0;
/**
* How many times the GameAction has been pressed
* @type {Number}
* @memberOf GameAction#
* @default
*/
this.amount = 0;
/**
* The current state of the GameAction
* @type {Number}
* @memberOf GameAction#
* @default
*/
this.state = 0;
this.statics = statics;
Object.assign(this, options);
this.reset();
}
/**
* Resets this GameAction so that it appears like it hasn't been pressed.
* @function
* @memberOf GameAction#
*/
reset() {
this.state = statics.STATE_RELEASED;
this.amount = 0;
}
/**
* Taps this GameAction. Same as calling press() followed by release().
* @function
* @memberOf GameAction#
*/
tap() {
this.press();
this.release();
}
/**
* Signals that the key was pressed.
* @function
* @memberOf GameAction#
*/
press() {
this.state = statics.STATE_PRESSED;
if(this.behavior === statics.DETECT_INITAL_PRESS_ONLY){
this.pressAmt(1);
}
}
/**
* Signals that the key was pressed a specified number of times, or that the mouse move a specified distance.
* @function
* @memberOf GameAction#
* @param {Number} amount the number of times the key is pressed
*/
pressAmt(amount) {
if (this.state !== statics.STATE_WAITING_FOR_RELEASE) {
this.amount += amount;
this.state = statics.STATE_WAITING_FOR_RELEASE;
}
}
/**
* Signals that the key was released
* @function
* @memberOf GameAction#
*/
release() {
this.state = statics.STATE_RELEASED;
}
/**
* Returns whether the key was pressed or not since last checked.
* @function
* @memberOf GameAction#
* @return {Boolean} True if the key is pressed, else false
*/
isPressed() {
if(this.state === statics.STATE_PRESSED){
return true;
} else {
return false;
}
}
/**
* For keys, this is the number of times the key was pressed since it was last checked.
* For mouse movement, this is the distance moved.
*
* This Resets the amount to zero after being checked!
*
* @function
* @memberOf GameAction#
* @return {Number} Number of times the key was pressed or distance mouse was moved
*/
getAmount() {
var retVal = this.amount;
if (retVal !== 0) {
if (this.state === statics.STATE_RELEASED) {
this.amount = 0;
} else if (this.behavior === statics.DETECT_INITAL_PRESS_ONLY) {
this.state = statics.STATE_WAITING_FOR_RELEASE;
this.amount = 0;
}
}
return retVal;
}
}
module.exports = GameAction;