-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ball.as
235 lines (172 loc) · 4.11 KB
/
Ball.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package
{
import flash.display.*;
import flash.events.*;
import flash.ui.*;
public class Ball extends Shape
{
public var vx: Number;
public var vy: Number;
private var x1: Number;
private var y1: Number;
private var x2: Number;
private var y2: Number;
private var game: Game;
private var contact: LineIntersection;
private var respawn: Boolean = false;
public var trail: Trail;
public var radius:Number = 4;
public var stuckTimer:int = 0;
public function Ball (_game: Game)
{
game = _game;
graphics.beginFill(0xFFFFFF);
graphics.drawCircle(0, 0, radius);
graphics.endFill();
spawn();
trail = new Trail(x,y);
}
private function spawn (): void
{
respawn = false;
x = 320;
y = 240;
if (game.attractMode) {
y = 160;
}
vx = Math.random() * 2 + 3;
vx *= (Math.random() < 0.5) ? -1 : 1;
vy = Math.random() * 6 - 3;
vx *= 0.5;
vy *= 0.5;
}
private function test (ax: Number, ay: Number, bx: Number, by: Number, r: Number = 6): Boolean
{
contact = new LineIntersection();
var movement: Line = new Line(x1, y1, x2, y2);
var barrier: Line = new Line(ax, ay, bx, by);
var hit: Boolean = false;
if (r > 0)
{
hit = Line.intersectsR(movement, barrier, r, contact);
}
else
{
hit = Line.intersects(movement, barrier, contact);
}
if (hit)
{
x2 = contact.x;
y2 = contact.y;
var nx: Number = by - ay;
var ny: Number = ax - bx;
var nz: Number = Math.sqrt(nx*nx + ny*ny);
nx /= nz;
ny /= nz;
var speed: Number = vx*nx + vy*ny;
//trace("Old speed: " + vx + ", " + vy);
vx -= 2 * nx * speed;
vy -= 2 * ny * speed;
//trace("New speed: " + vx + ", " + vy);
x2 += (1 - contact.t) * vx;
y2 += (1 - contact.t) * vy;
return true;
}
return false;
}
public function update (loop:int): void
{
if (loop == 0 && respawn)
{
spawn();
}
var vz:Number = Math.sqrt(vx*vx + vy*vy);
var maxSpeed:Number = 4;
if (vz > maxSpeed) {
vx *= maxSpeed / vz;
vy *= maxSpeed / vz;
}
var minXSpeed:Number = 1;
if (vx > -minXSpeed && vx < minXSpeed) {
vx = (vx < 0) ? -minXSpeed : minXSpeed;
}
x1 = x;
y1 = y;
x2 = x + vx;
y2 = y + vy;
var p1: Player = game.player1;
var p2: Player = game.player2;
/*if (test(p1.x, p1.top, p1.x, p1.bottom, radius + 2))
{
var diff: Number = contact.y - p1.y;
vy += diff * 0.1;
vy -= p1.vy * 0.1;
vx += 0.25;
}
if(test(p2.x, p2.top, p2.x, p2.bottom, radius + 2))
{
diff = contact.y - p2.y;
vy += diff * 0.1;
vy -= p2.vy * 0.1;
vx -= 0.25;
}*/
if (test(0, 0, 0, 480, radius)) // left
{
game.score2.value += 1;
if (game.score2.value < Settings.targetScore) {
game.player1.newContinent();
}
if (vx > 3)
{
vx -= 0.25;
}
respawn = true;
}
if(test(640, 480, 640, 0, radius)) // right
{
game.score1.value += 1;
if (game.score1.value < Settings.targetScore) {
game.player2.newContinent();
}
if (vx < -3)
{
vx += 0.25;
}
respawn = true;
}
test(0, 480, 640, 480, radius); // bottom
test(640, 0, 0, 0, radius); // top
for each (var lineBall:LineBall in game.lines) {
var xArr: Array = lineBall.xArray;
var yArr: Array = lineBall.yArray;
for (var i: uint = 0; i < xArr.length - 1; i++)
{
test(xArr[i], yArr[i], xArr[i+1], yArr[i+1]);
}
}
x = x2;// - 0.01 * vx;
y = y2;// - 0.01 * vy;
if (trail.circles.length > 10) {
var stuck:Boolean = true;
for each (var t:DisplayObject in trail.circles) {
var dx:Number = t.x - x;
var dy:Number = t.y - y;
if (dx*dx + dy*dy > 200) {
stuck = false;
}
}
if (stuck) {
if (stuckTimer > 90) {
respawn = true;
}
stuckTimer++;
} else {
stuckTimer = 0;
}
}
if (loop == 0) {
trail.update(x,y);
}
}
}
}