/
Paddle.hx
215 lines (171 loc) · 4.49 KB
/
Paddle.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
import com.haxepunk.Entity;
import com.haxepunk.HXP;
import com.haxepunk.utils.Draw;
import com.haxepunk.utils.Input;
import com.haxepunk.utils.Key;
import nme.geom.Point;
enum InputType {
PITCH_CONTROLS_DIRECTION;
PITCH_CONTROLS_POSITION;
KEYBOARD;
}
class Paddle extends Entity {
public static var maxIgnoredPitch:Int = 20;
public static var minPitch:Int = 56;
public static var maxPitch:Int = 113;
public static var medPitch:Int = 80;
public var vel:Float;
public var pitch:Float;
public var controls:InputType;
public var recentering:Bool;
public var needsCalibration:Bool;
public var lineWidth:Float;
public function new () {
super();
x = HXP.width / 2;
y = HXP.height - 50;
width = 150;
height = 10;
centerOrigin();
type = "paddle";
vel = 0;
pitch = 0;
controls = PITCH_CONTROLS_POSITION;
recentering = false;
needsCalibration = true;
lineWidth = 2;
}
override public function update () : Void {
if (Main.debugMode && Input.pressed(Key.I)) {
y = HXP.height - 50;
if (controls == PITCH_CONTROLS_DIRECTION)
controls = PITCH_CONTROLS_POSITION;
else if (controls == PITCH_CONTROLS_POSITION)
controls = KEYBOARD;
else
controls = PITCH_CONTROLS_DIRECTION;
}
doMotion();
var left = 0.0;
var right = cast(HXP.width, Float);
if (Std.is(world, BreakoutWorld)) {
var bw = cast(world, BreakoutWorld);
left = bw.left;
right = bw.right;
}
if (x - halfWidth < left) {
x = left + halfWidth;
vel = 0.9 * Math.abs(vel);
}
else if (x + halfWidth > right) {
x = right - halfWidth;
vel = -0.9 * Math.abs(vel);
}
}
public function doMotion () : Void {
if (recentering)
doRecentering();
else if (Std.is(world, BreakoutWorld)
&& cast(world, BreakoutWorld).ballsLeft == 0)
doMotionLost();
else if (controls == KEYBOARD)
doMotionKeyboard();
else
doMotionMic();
}
public function doRecentering () : Void {
var p = new Point(HXP.width/2 - x, HXP.height - 50 - y);
if (p.length > 5)
p.normalize(5);
x += p.x;
y += p.y;
if (Math.abs(x - HXP.width/2) < 0.01
&& Math.abs(y - HXP.height + 50) < 0.01)
{
recentering = false;
waitForCalibration();
}
}
public function doMotionLost () : Void {
x += vel;
vel *= 0.8;
drop();
}
public function doMotionKeyboard () : Void {
var dx = (if (Input.check(Key.RIGHT)) 1 else 0)
- (if (Input.check(Key.LEFT)) 1 else 0);
vel += 3 * dx;
vel *= 0.8;
x += vel;
var ball = cast(world.typeFirst("ball"), Ball);
if (ball != null && Input.pressed(Key.SPACE))
ball.launch();
}
public function doMotionMic () : Void {
// In keyboard mode, we don't care if we're calibrated, so check
// here.
if (needsCalibration)
return;
pitch = Pitch.getPitch();
if (pitch <= maxIgnoredPitch) {
if (controls == PITCH_CONTROLS_POSITION)
drop();
return;
}
if (pitch < minPitch) pitch = minPitch;
if (pitch > maxPitch) pitch = maxPitch;
// rescale pitch to [-1, 1].
if (pitch < medPitch)
pitch = (1 - medPitch/pitch) / (Math.sqrt(2) - 1);
else
pitch = (pitch/medPitch - 1) / (Math.sqrt(2) - 1);
if (controls == PITCH_CONTROLS_POSITION) {
var left = 0.0;
var right = cast(HXP.width, Float);
if (Std.is(world, BreakoutWorld)) {
var bw = cast(world, BreakoutWorld);
left = bw.left;
right = bw.right;
}
var target = (pitch * 0.5 + 0.5) * (right - left - width) + left + halfWidth;
vel = (target - x)*0.5;
x += (target - x);
y += (HXP.height - 50 - y) * 0.5;
} else {
vel += pitch * 40;
var max = 20;
if (vel < -max) vel = -max;
if (vel > max) vel = max;
vel *= 0.8;
x += vel;
}
var ball = cast(world.typeFirst("ball"), Ball);
if (ball != null)
ball.launch();
}
public function drop () : Void {
y += (HXP.height + 50 - y) * 0.2;
}
public function waitForCalibration () : Void {
needsCalibration = true;
world.add(new Activator());
}
override public function render () : Void {
super.render();
Draw.rect(Std.int(x - halfWidth), Std.int(y - halfHeight),
width, height, 0x0000CC);
var lineleft = Std.int(x + pitch * (halfWidth-1) - lineWidth/2);
Draw.rect(lineleft, Std.int(y - halfHeight + 2),
Std.int(Math.min(lineWidth, right-lineleft-2)),
height-4, 0xFFFFFF);
}
public function calibrate (pitch:Int) {
medPitch = pitch;
minPitch = Std.int(pitch / Math.sqrt(2));
maxPitch = Std.int(pitch * Math.sqrt(2));
needsCalibration = false;
}
public function recenter () : Void {
recentering = true;
}
}