forked from khoaha/player_ttl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tilt_to_live.html
299 lines (266 loc) · 8.37 KB
/
tilt_to_live.html
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<!DOCTYPE html>
<html>
<head>
<title>Tilt to Live - Animatron HTML5 Player Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="../player/vendor/matrix.js" type="text/javascript"></script>
<script src="../player/anm.player.js" type="text/javascript"></script>
<script src="../player/anm.collisions.js" type="text/javascript"></script>
<script src="../player/anm.builder.js" type="text/javascript"></script>
<script src="../player/animatron_import.js" type="text/javascript"></script>
<script type="text/javascript">
function start() {
var b = Builder._$, B = Builder, C = anm.C;
var width = 640;
var height = 480;
var points = 0;
var time = 0;
var baseTime = 0;
var numEnemies = 80;
var spawnRate = .25;
var spawn = [4, 14];
var mouseX = 0;
var mouseY = 0;
var targetX = 0;
var targetY = 0;
var dot = b().circle([ 0, 0 ], 4).fill('#f00');
var swarm = new Array(numEnemies);
var instruct = b().paint(function(ctx) {
ctx.font = '30pt sans-serif';
ctx.fillText('Tilt & Survive', 200, 40);
ctx.font = '18pt sans-serif';
ctx.fillText('Instructions:', 8, 80);
ctx.fillText('Maneuver to avoid the swarm and collect powerups!', 16, 110);
ctx.fillText('Controls:', 8, 140);
ctx.fillText('Move mouse to steer dot', 16, 170);
ctx.fillText('Powerups:', 8, 200);
ctx.fillText('Green - Shields one hit', 16, 230);
ctx.fillText('Red - Fires projectile in front', 16, 260);
ctx.fillText('Yellow - Creates explosion at center', 16, 290);
});
var next = b().paint(function(ctx) {
ctx.font = '18pt sans-serif';
ctx.fillText('Press any key to continue', 180, 400);
});
var msg = b().paint(function(ctx) {
ctx.font = '30pt sans-serif';
ctx.fillText('Game Over', 210, 260);
});
var scoreDisp = b().paint(function(ctx) {
ctx.font = '18pt sans-serif';
ctx.fillText(''+Math.floor(time+points), 8, height-8);
});
var started = false;
var over = false;
var spanTime = new Array(numEnemies);
var spawnTime = new Array(numEnemies);
var maxPowerups = 3;
var powerRate = .007;
var powerSet = [b().circle([ 0, 0 ], 6).fill('#ff0'), b().circle([ 0, 0 ], 6).fill('#0f0'), b().circle([ 0, 0 ], 6).fill('#f03')];
var powerups = new Array();
var powerType = new Array();
// nullify all powerup slots
for (var i=0; i<maxPowerups; i++)
powerups.push(null);
// powerup variables
var shield = false;
var shieldEnd = false;
// canvas 1
var scene1 = b();
var reset = function() {
console.log("reset");
points = 0;
scene1.deach(function(elm) {
if (elm != scoreDisp.v && elm != dot.v && elm != area.v && elm != msg.v) {
scene1.remove(elm);
}
});
msg.disable();
for (var i=0; i<maxPowerups; i++) {
powerups[i] = null;
powerType[i] = null;
}
for (var i=0; i<swarm.length; i++)
swarm[i] = null;
over = false;
}
var baseModifier = function(t) {
// update time
if (!over)
time = t - baseTime;
else
baseTime = t;
if (time > 3) {
if (Math.random() <= powerRate) {
var type = Math.floor(Math.random()*powerSet.length);
for (var i=0; i<maxPowerups; i++) {
if (powerups[i] == null) {
var p = b(powerSet[type]).modify(powerModifier, i);
powerups[i] = p;
powerType[i] = type;
scene1.add(p);
console.log("created powerup type "+type);
break;
}
}
}
if (Math.random() <= spawnRate) {
for (var i=0; i<numEnemies; i++) {
if (swarm[i] == null) {
swarm[i] = b().circle([ 0, 0 ], 2).fill('#000')
.modify(enemyModifier, i);
// initialize position
swarm[i].v.state.x = Math.random()*width;
swarm[i].v.state.y = Math.random()*height;
// set spawn variables
spanTime[i] = Math.random()*(spawn[1] - spawn[0]) + spawn[0];
spawnTime[i] = t;
scene1.add(swarm[i]);
break;
}
}
}
}
}
var beamModifier = function(t, ang) {
// check enemies
for (var i=0; i<numEnemies; i++) {
if (swarm[i] != null) {
if (this.$.intersects(swarm[i].v)) {
if (!over)
points += 7;
scene1.remove(swarm[i]);
swarm[i] = null;
}
}
}
// rotate and move
if (this.x <= -24 || this.x >= width+24 || this.y <= -24 || this.y >= height+24)
scene1.remove(this.$);
else {
this.angle = (this.angle + .15) % 6.28;
this.x += 3*Math.cos(ang);
this.y += 3*Math.sin(ang);
}
}
var explodeModifier = function(t) {
// check enemies
for (var i=0; i<numEnemies; i++) {
if (swarm[i] != null) {
if (this.$.intersects(swarm[i].v)) {
if (!over)
points += 5;
scene1.remove(swarm[i]);
swarm[i] = null;
}
}
}
// grow
if (this.sx > 16)
scene1.remove(this.$);
else {
this.sx *= 1.06;
this.sy *= 1.06;
this.alpha = (17-this.sx)/16;
}
}
var powerModifier = function(t, i) {
// set position
if (this.x == 0 && this.y == 0) {
this.x = Math.random()*width;
this.y = Math.random()*height;
}
// collision check
if (this.$.intersects(dot.v)) {
console.log("triggered powerup type "+powerType[i]);
scene1.remove(powerups[i]);
powerups[i] = null;
// handle each powerup
switch (powerType[i]) {
case 0: scene1.add(b().circle([ this.x, this.y ], 6).fill('#ff0').nostroke()
.modify(explodeModifier)); break;
case 1: shield = true; break;
case 2: var p = b().path([ [-56, 32], [0, -64], [56, 32], [-56, 32] ]).fill('#f03').nostroke()
.modify(beamModifier, Math.atan2(this.y - targetY, this.x - targetX));
scene1.add(p);
p.v.state.x = this.x;
p.v.state.y = this.y;
}
if (!over)
points += 30;
}
}
var enemyModifier = function(t, i) {
if (t - spawnTime[i] >= spanTime[i]) {
scene1.remove(this.$);
swarm[i] = null;
}
var ang = Math.atan2(targetY - this.y, targetX - this.x);
this.x += Math.cos(ang) * Math.max(.1, Math.min(Math.atan(t - spawnTime[i]), Math.abs(targetX - this.x)/20));
this.y += Math.sin(ang) * Math.max(.1, Math.min(Math.atan(t - spawnTime[i]), Math.abs(targetY - this.y)/20));
// collision check
if (this.$.intersects(dot.v) && t-spawnTime[i] >= .5) {
if (shield) {
shieldEnd = true;
} else {
over = true;
msg.enable();
next.enable();
}
scene1.remove(this.$);
swarm[i] = null;
}
// end shield for frame
if (i == numEnemies-1 && shieldEnd) {
shield = false;
shieldEnd = false;
}
};
var playerModifier = function(t) {
// update position
this.x += (mouseX - this.x)/10;
this.y += (mouseY - this.y)/10;
targetX = this.x;
targetY = this.y;
if (shield)
dot.stroke('#0f0', 2);
else
dot.stroke('#000', 1);
}
var area = b().rect([width/2, height/2], [width, height]).fill('#fff').modify(baseModifier);
dot.on(C.X_MMOVE, function(evt) {
mouseX = evt.pos[0];
mouseY = evt.pos[1];
})
.modify(playerModifier);
scene1.add(area);
scene1.add(instruct);
scene1.add(next);
var init = function() {
started = true;
for (var i=0; i<swarm.length; i++)
swarm[i] = null;
scene1.add(dot);
scene1.add(msg);
scene1.add(scoreDisp);
instruct.disable();
next.disable();
}
scene1.on(C.X_KPRESS, function(evt) {
console.log(evt.key, evt.char);
if (!started)
init();
else if (over)
reset();
})
msg.disable();
createPlayer('canvas1', {'mode': C.M_DYNAMIC}).load(scene1).play();
}
</script>
</head>
<body onload="start();">
<h1>Tilt to Live - Animatron HTML5 Player Demo</h1>
<!-- canvas1 -->
<canvas id="canvas1" width="640" height="480"></canvas>
</body>
</html>