-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameInput.as
113 lines (101 loc) · 2.62 KB
/
GameInput.as
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
package
{
import net.flashpunk.utils.Input;
import net.flashpunk.Entity;
import mx.utils.ObjectUtil;
/**
* Collecting all the input detection (for the game not the editor)
* in one place to make it easier to implement recordings and change
* input keys
*/
public class GameInput extends Entity
{
public static const GAME_PLAY:int=0;
public static const RECORD:int=1;
public static const PLAYBACK: int=2;
public static const FREEZE:int=3;
public var mode:int;
public var frame:int;
private var lastControls:Object = {};
private var controls:Object = {};
private var data:Object = {};
public function GameInput(mode:int=0):void {
this.mode=mode;
frame=0;
updateControl();
// trace(mode);
}
public function loadPlaybackData(data:Object):void {
this.data = data;
}
public function getPlaybackData():Object {
if (mode == RECORD) {
return data;
} else {
return null;
}
}
public override function update():void {
frame++;
updateControl();
}
public function updateControl():void {
if (mode == FREEZE) {
// If we're frozen we don't want any input to happen
return;
} else if (mode == PLAYBACK) {
// Here we are in playback mode
// Update the lastControls
lastControls = ObjectUtil.copy(controls);
for each (var i:String in controls) {
lastControls[i] = controls[i];
if (i == "down0") {
// trace (controls[i]);
}
}
if (data[frame]) {
var tempVec:Array = data[frame];
for each (var s:String in tempVec) {
controls[s] = !controls[s];
}
}
return;
} else {
tempVec = [];
// We are in RECORD or GAME_PLAY mode so
for (i in GC.inputKeys) {
// The funny syntax here is to correct probelms with controls[i] being undefined
lastControls[i] = controls[i] ? true : false;
controls[i] = false;
var v:Array = GC.inputKeys[i];
for each (var k:int in v) {
if (Input.check(k)) {
controls[i] = true;
break;
}
}
if (lastControls[i] != controls[i] && mode == RECORD) {
tempVec.push(i);
}
}
if (mode == RECORD && tempVec.length > 0) {
// Store the controls
data[frame] = tempVec;
}
}
}
public function check(str:String):Boolean {
return controls[str];
}
public function pressed(str:String):Boolean {
if (controls[str] && !lastControls[str]) {
// trace(str, controls[str], !lastControls[str]);
}
return controls[str] && !lastControls[str];
}
public function released(str:String):Boolean {
return !controls[str] && lastControls[str];
}
}
}
// vim: foldmethod=indent:cindent