-
Notifications
You must be signed in to change notification settings - Fork 1
/
LineBall.as
207 lines (150 loc) · 3.68 KB
/
LineBall.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
package
{
import flash.display.*;
import flash.events.*;
import flash.ui.*;
public class LineBall
{
public var canvas: Shape;
public var xArray: Array;
public var yArray: Array;
private var maxLength: Number = 1800;
private var vx: Number;
private 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;
public var colour: uint = 0xFF0000;
public function LineBall (_game: Game)
{
game = _game;
canvas = new Shape();
spawn();
}
private function spawn (): void
{
x2 = 320;
y2 = 160;
vx = Math.random() * 3 + 2;
vx *= (Math.random() < 0.5) ? -1 : 1;
vy = Math.random() * 6 - 3;
xArray = new Array();
yArray = new Array();
xArray[0] = x2;
yArray[0] = y2;
xArray[1] = x2;
yArray[1] = y2;
}
private function test (ax: Number, ay: Number, bx: Number, by: Number, r: Number = 2): 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;
xArray.push(contact.x);
yArray.push(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;
vx -= 2 * nx * speed;
vy -= 2 * ny * speed;
x2 += (1 - contact.t) * vx;
y2 += (1 - contact.t) * vy;
return true;
}
return false;
}
public function update (): void
{
var i: uint
xArray.pop();
yArray.pop();
x1 = x2;
y1 = y2;
x2 = x1 + vx;
y2 = y1 + vy;
/*for each (var other:LineBall in game.lines) {
for (i = 0; i < other.xArray.length - 1; i++) {
test(other.xArray[i], other.yArray[i], other.xArray[i+1], other.yArray[i+1]);
}
}*/
var p1: Player = game.player1;
var p2: Player = game.player2;
if (test(p1.x, p1.top, p1.x, p1.bottom))
{
var diff: Number = contact.y - p1.y;
vy += diff * 0.1;
vy -= p1.vy * 0.1;
}
if(test(p2.x, p2.top, p2.x, p2.bottom))
{
diff = contact.y - p2.y;
vy += diff * 0.1;
vy -= p2.vy * 0.1;
}
test(0, 0, 0, 480); // left
test(640, 480, 640, 0); // right
test(0, 480, 640, 480); // bottom
test(640, 0, 0, 0); // top
//x2 = x2 - 0.01 * vx;
//y2 = y2 - 0.01 * vy;
xArray.push(x2);
yArray.push(y2);
var length: Number = 0;
for (i = 0; i < xArray.length - 1; i++)
{
var dx: Number = xArray[i] - xArray[i+1];
var dy: Number = yArray[i] - yArray[i+1];
length += Math.sqrt(dx*dx + dy*dy);
}
var excess: Number = length - maxLength;
// shorten tail
while (excess > 0)
{
dx = xArray[1] - xArray[0];
dy = yArray[1] - yArray[0];
var dz: Number = Math.sqrt(dx*dx + dy*dy);
if (dz > excess)
{
dx /= dz;
dy /= dz;
xArray[0] += dx * excess;
yArray[0] += dy * excess;
break;
}
else
{
excess -= dz;
xArray.shift();
yArray.shift();
}
}
// draw
canvas.graphics.clear();
canvas.graphics.lineStyle(1, colour);
canvas.graphics.moveTo(xArray[0], yArray[0]);
for (i = 1; i < xArray.length; i++)
{
canvas.graphics.lineTo(xArray[i], yArray[i]);
}
}
}
}