-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathController.hx
220 lines (173 loc) · 4.83 KB
/
Controller.hx
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package ;
import flixel.FlxBasic;
import flixel.FlxG;
import flixel.util.FlxPoint;
enum KeyStyle
{
TOGGLE;
HOLD;
PRESS;
}
class IntPoint
{
public var X : Int = 0;
public var Y : Int = 0;
public function new(newX : Int, newY : Int)
{
X = newX;
Y = newY;
}
}
class ActionButton
{
public var ButtonName : String;
public var Keys : Array<String> = new Array<String>();
var _style : KeyStyle = PRESS;
var _active : Bool = false;
public function new (name : String, keyList : Array<String> = null, inputStyle : KeyStyle)
{
ButtonName = name;
Keys = keyList;
_style = inputStyle;
_active = false;
}
/**
* Set a key, adding to its input list and changing its style.
* @param keyList Keys to add to this key's list.
* @param ?inputStyle The new input style. Defaults to PRESS.
*/
public function SetKey(keyList : Array<String>, inputStyle : KeyStyle) : Void
{
// Add the newly assigned keys to this one, and change styles.
if (keyList != null)
{
Keys = Keys.concat(keyList);
}
_style = inputStyle;
_active = false;
}
/**
* Get the value of an action key, based on its key style.
* If it's a hold, it returns 1 for held and 0 for not.
* If it's a press, it returns whether it was just pressed.
* If it's a toggle, it returns the on/off state of that key toggle.
* @return Boolean, for if that action is active/
* toggled.
*/
public function GetKeyInput() : Bool
{
var _returnValue : Bool = false;
switch (_style)
{
case PRESS:
_returnValue = FlxG.keys.anyJustPressed(Keys);
case TOGGLE:
if (FlxG.keys.anyJustPressed(Keys))
{
_active = !_active;
}
_returnValue = _active;
case HOLD:
_returnValue = FlxG.keys.anyPressed(Keys);
//_active = _returnValue;
}
return _returnValue;
}
}
/**
* The controller is meant to provide a control interface.
* @author Colin McMillan
*/
class Controller
{
var _actionList : Array<ActionButton>;
var _buttonsInUse : Array<String>;
/**
* Create a new controller.
*/
public function new()
{
_actionList = new Array<ActionButton>();
_buttonsInUse = new Array<String>();
}
/**
* Gets the direction keys currently pressed. Validates to ensure
* that opposite directions being pressed results in 0.
* @return A FlxPoint, with x and y being either 1 or -1,
* corresponding to the direction keys pressed.
* 1 for down/right, -1 for up/left.
* 0 for neither/both in that axis.
*/
public function GetDirection() : IntPoint
{
var _directions : IntPoint = new IntPoint(0, 0);
// Update all direction keys pressed.
var _up = FlxG.keys.anyPressed(["UP", "W"]);
var _down = FlxG.keys.anyPressed(["DOWN", "S"]);
var _right = FlxG.keys.anyPressed(["RIGHT", "D"]);
var _left = FlxG.keys.anyPressed(["LEFT", "A"]);
if ((_up) && (!_down))
{
_directions.Y = -1;
}
else if ((_down) && (!_up))
{
_directions.Y = 1;
}
if ((_left) && (!_right))
{
_directions.X = -1;
}
else if ((!_left) && (_right))
{
_directions.X = 1;
}
return _directions;
}
public function SetButton(actionName : String, ?keyList : Array<String> = null, inputStyle : KeyStyle) : ActionButton
{
var buttonChanged : ActionButton = null;
// If keys are given...
if (keyList != null)
{
// For each key provided...
for (enteredKey in keyList)
{
// ... remove that key from the existing "in use" list. If the key is removed thusly...
if (_buttonsInUse.remove(enteredKey))
{
// Go through the action list and remove that key from other actions.
for (action in _actionList)
{
action.Keys.remove(enteredKey);
}
// Now each key is free for the new assignment.
}
// And add the entered key into the buttonsInUse array.
_buttonsInUse.push(enteredKey);
}
// Now every key in the new key list is freed and entered into the list of in-use keys for the controller.
}
// This SHOULD prevent keys being used for multiple actions. Yanks out already-in-use keys if applicable,
// and adds the new keys into the list of all keys in use either way.
// For each action button that already exists...
for (existingActionButton in _actionList)
{
// Check if its name matches the new one.
if (existingActionButton.ButtonName == actionName)
{
// If so, add to its keylist and set inputStyle.
existingActionButton.SetKey(keyList, inputStyle);
buttonChanged = existingActionButton;
}
} // The combination of the for scan and the if SHOULD prevent duplicate action names.
// If the above sequence didn't change a key...
if (buttonChanged == null)
{
// Create a new ActionButton, and add it to the actionList.
buttonChanged = new ActionButton(actionName, keyList, inputStyle);
_actionList.push(buttonChanged);
}
return buttonChanged;
}
}