-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathobjects-update.metajs
404 lines (392 loc) · 14.1 KB
/
objects-update.metajs
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
if (ticCounter>0 && pause) return;
ticCounter++;
var p, dist;
if ( ship ) {
life.style.width = Math.round(ship.life) + '%';
if ( shipAlive ) {
if (shipAlive) ship.life += .01;
if (ownerOfPlanet(ship.planet)==0) ship.life += .02; // repair bonus
if (ship.life > 100) ship.life = 100;
var absShipV = abs(ship.vx) + abs(ship.vx);
if ( absShipV > 1 ) zoom = 2/absShipV;
else zoom = 2;
if ( zoom < 0.5 ) zoom = 0.5;
}
}
//focus = sun; zoom = (h/biggerPlanetD)/2.1; // debug
// Update planets positions:
for ( var id in planets ) {
p = planets[id];
if (!p.d) {
p.x = p.y = 0;
} else {
p.a += p.v;
var oldx = p.x, oldy = p.y;
p.x = cos(p.a)*p.d + planets[p.p||'sun'].x;
p.y = sin(p.a)*p.d + planets[p.p||'sun'].y;
p.vx = p.x - oldx;
p.vy = p.y - oldy;
}
}
p = planets;
/*** Initializations ********************************************************/
if ( boids.length == 0 ) {
log('Initialize Boids'); //debug
for (var i=0; i<13; i++) newBoid(0, 1+rand(), 0,0, 0,0, null, p.p2a);
newBoid(1, 3, 0,0, 0,0, null, p.p3a);
newBoid(1, 3, 0,0, 0,0, null, p.p3b);
newBoid(1, 3, 0,0, 0,0, null, p.p3c);
}
if ( ticCounter%(10*framesPerSec) == 0 ) {
// Invasion from outer space each 10 secs
// the ship will never see it born
newBoid(1, 2, (ship.x>0?-1.2:1.2)*biggerPlanetD, rand(4000), 0, rand(10));
}
if ( !ship ) {
log('Initialize Ship'); //debug
shipAlive = true;
ship = {
crew: [newBoid(0, 2, 0,0, 0,0, null, null, false)],
weapon: true,
planet: p.p2a,
rotate: -pi/2,
r: 10,
life: 100
};
focus = ship;
}
// Update ship position:
if (shipAlive) {
if ( lastMsgToUsr < now()-secs(10) ) { // Much time without a message...
if (ship.planet) {
if ( ownerOfPlanet(ship.planet) == 0 ) { // on one of the player's planet
if ( ship.crew.length < 3 )
msgToUsr( "",
"C'mon folks! Enter on the ship.",
"Full this ship and let's get another planet!"
);
else
msgToUsr(
"You can takeoff, boss.",
"Let's get another planet!",
"The ship is full! Let's get another planet!!!"
);
} else if ( ownerOfPlanet(ship.planet) > 0 ) { // Enemy's planet
if ( ship.crew.length == 0 )
msgToUsr(
'This reddish can attack the our landed ship.',
'They can attack the ship, boss.',
'What are you waiting for? Takeoff!'
);
} else { // Non owned planet
if ( ship.crew.length == 0 )
msgToUsr(
'Takeoff and come back with some crew.',
'If you had taken some crew, we could owned this planet...'
);
}
} else if ( distP2P(ship,sun).d < orbDist1*.9 ) { // ship is fling near to the sun
msgToUsr(
"DANGER!!! DANGER!!!",
"I hate the sun lower orbit!!!",
"Boss! The sun can destroy us!",
"oOooww daddy... Let's go back home... pleezzz?"
);
} else if ( distP2P(ship,sun).d > biggerPlanetD*1.1) { // ship is fling far away
msgToUsr(
"Where are you going?",
"We are too far away, boss!",
"oOooww daddy... Let's go back home... pleezzz?"
);
} else { // ship is fling on interplanetary space
if ( ship.crew.length == 0 )
msgToUsr("",
"Let's take some crew, so we can own another planet.", "",
"Hey! Let's take a crew, so you can own some planet.", "",
"Bud... You need to take some crew.", "",
"Boss, where is your green ones?", ""
);
else // fly with some crew
msgToUsr("",
"Let's take another planet?", "",
"Uninhabited planets are easier to take.", "",
"Hey pally, let's rule some reddish planet?", "",
"Boss, you can let the autopilot to land us.", "",
"You only need to gently approach the planet. The autopilot land us.",
"Hey bud, let's fire our missiles against a reddish planet?"
);
}
}
updatePosition(ship,
function(p) {
ship.rotateV = 0;
if (boidsOnPlanet(p).length < 7) releaseCrew();
else if (ownerOfPlanet(p)>0.3) msgToUsr('Are you crazy? We will die!');
},
{
15: function(p, proximity) { // Near to the planet
// Auto pilot try to help to align the ship for landing
var angleToPlanet = getAngleToPlanet(ship, p);
// Remove near to zero jump (useless complete turn the ship)
if ( angleToPlanet < pi/2 && ship.rotate > pi*1.5 ) ship.rotate -= completeRad;
if ( angleToPlanet > pi*1.5 && ship.rotate < pi/2 ) ship.rotate += completeRad;
// Adjust ship rotation
if ( angleToPlanet-ship.rotate > 0.2 ) ship.rotate += 0.1;
else if ( angleToPlanet-ship.rotate > 0.1 ) ship.rotate += 0.03;
if ( angleToPlanet-ship.rotate < -0.2 ) ship.rotate -= 0.1;
else if ( angleToPlanet-ship.rotate < -0.1 ) ship.rotate -= 0.03;
},
0: function(p, proximity) { // Touch the planet
if (p == sun) { // Ship die if dropped into sun
shipDamage(1e5, 'You crash in the sun before colonize all start system.');
} else { // Ship landed a planet
if (proximity < -1.5) { // Too hard! Explode.
var impactMsg = Math.round(proximity*-100) +
'uF.</strong> Maximum is 150uF.';
shipDamage(90,
'You crash in a planet before colonize all start system.' +
'<p/><strong>Impact: ' + impactMsg,
function() {
newExplosion([ship, p], true, 10);
msgToUsr(
'Slow down! The <strong>impact was ' + impactMsg +
'You was just about to explode.'
);
}
);
} else if ( distP2P(ship, landingPos(ship, p)).d > 20 ) {
shipDamage(50,
'You crash in a planet before colonize all start system.' +
'<p/><strong>Bad angle.</strong>',
function() {
newExplosion([ship, p], true, 10);
msgToUsr(
'Trust on your landing autopilot.',
'The landing autopilot needs your collaboration.'
);
}
);
}
ship.planet = p;
ship.rotate = getAngleToPlanet(ship, p);
playAudio('landing');
}
}
}
);
if (usrInput.btU) {
if (ship.planet) {
ship.vx = cos(ship.rotate) * 0.2 + ship.planet.vx;
ship.vy = sin(ship.rotate) * 0.2 + ship.planet.vy;
ship.planet = false;
} else {
ship.vx += cos(ship.rotate) * 0.1;
ship.vy += sin(ship.rotate) * 0.1;
}
}
if ( usrInput.btL ) ship.rotateJet = -0.01;
else if ( usrInput.btR ) ship.rotateJet = 0.01;
else ship.rotateJet = 0;
ship.rotateV += ship.rotateJet; // Side jets increase angular velocity
ship.rotate += ship.rotateV; // Rotation state increased by angular velocity
ship.rotateV *= 0.99; // Rotation friction (Yeah... this is a fake space)
}
// Update Cannons
cannons.forEach(function(cannon){
updatePosition(cannon);
if (cannon.build<10) cannon.build += 0.02;
// Rotate to find the target
if ( cannon.build >= 10 // is operational
&& distP2P(cannon,ship).d < 800 // not so distant
&& shipAlive // obvious...
&& ship.planet != cannon.planet // ship is not here
) {
var shipAngle = getAngleToPlanet(ship, cannon.planet);
// Remove near to zero jump (useless complete turn the ship)
if ( shipAngle < pi/2 && cannon.rotate > pi*1.5 ) cannon.rotate -= completeRad;
if ( shipAngle > pi*1.5 && cannon.rotate < pi/2 ) cannon.rotate += completeRad;
// Adjust ship rotation
if ( shipAngle-cannon.rotate > 0.1 ) cannon.rotate += 0.03;
else if ( shipAngle-cannon.rotate > 0.02 ) cannon.rotate += 0.01;
else if ( shipAngle-cannon.rotate > 0.01 ) cannon.rotate += 0.003;
else if ( shipAngle-cannon.rotate < -0.1 ) cannon.rotate -= 0.03;
else if ( shipAngle-cannon.rotate < -0.02 ) cannon.rotate -= 0.01;
else if ( shipAngle-cannon.rotate < -0.01 ) cannon.rotate -= 0.003;
// Shot!
if ( abs(shipAngle-cannon.rotate) < 0.2 // target locked
&& (now()-cannon.lastShot) > secs(3) // cannon loaded
) {
cannon.lastShot = now();
newMissile(1, cannon.x, cannon.y,
cannon.planet.vx + cos(cannon.rotate) * 4,
cannon.planet.vy + sin(cannon.rotate) * 4
);
playAudio('gun');
}
}
});
// Update missiles
missiles.forEach(function(m) {
updatePosition(m, function(){},
{
2: function(p, proximity) { // Touch the planet
m.planet = p
newExplosion(m, true, 25);
missiles = missiles.remove(m);
// Kill near boids and cannons
var deadCounter = 0, cannon = getCannonFromPlanet(p);
if ( cannon && distP2P(m,cannon).d < 40 ) {
destroyCannon(cannon);
deadCounter++;
}
boids.forEach(function(b){
if ( distP2P(m,b).d < 40 ) {
deadCounter += b.o==0 ? -99 : 1;
newExplosion(b, false, 2, .2);
boidDie(b);
}
if ( deadCounter < 0 ) { // friend killed
if (m.o==0) msgToUsr(
"OMG! You killed Kenny. You bastard!",
"Ops... You shot a green one.",
"Do you know who is your team?"
);
else msgToUsr('',
"OMG! You killed Kenny. You bastard!", '',
"FUUUUU!!! Reddish are shoting us!", '',
"Tell my wife... I... love... guacamole.", ''
);
} else if ( deadCounter == 0 ) { // no one killed
if (m.o==0) msgToUsr( "",
"How can you miss that!?",
"Thank you, for the lost missile.",
"Did you know how much that missile costs to our economy?"
);
} else { // enemy killed
msgToUsr( "",
"Suck it!", "Suck it reddish!",
"A reddish cleaner missile.",
"This is MY planet!"
);
}
});
}
}
);
m.t = (m.t||0)+1;
if ( m.t > 1000 ) { // to much time. Die now.
missiles = missiles.remove(m);
newExplosion(m, false, 4);
}
if ( m.o!=0 && distP2P(ship,m).d < ship.r + m.r ) {
missiles = missiles.remove(m);
newExplosion([m, ship], true, 20);
shipDamage(25, 'Your shields was not enough for this missile.');
}
});
// Update explosions
explosions.forEach(function(e) {
updatePosition(e);
e.t = e.t ? e.t+1 : .5;
if (e.t>19) explosions = explosions.remove(e); // explosion end
if (!e.inc) e.inc = 1;
});
// Update boids positions:
boids.forEach(function(b){
updatePosition(b,
function(p) { // in a planet callback
if ( ship.planet == p ) {
// The player ship is in this boid's planet!
if (
( boidsOnPlanet(p).length < 4 // Too few boids here...
|| (now()-b.shipIO) < secs(5) ) // Exit the ship right now
&& abs(ship.rotate-b.rotate) < pi/2 // Near to the ship?
) { // Run away!
b.rotateV += b.rotate<ship.rotate ? -2e-4 : 2e-4;
} else if (boidsOnPlanet(p).length > 3) { // Get the ship!
b.rotateV += b.rotate<ship.rotate ? 1e-4 : -1e-4;
} else {
b.rotateV += b.rotate<ship.rotate ? -1e-5 : 1e-5;
}
if (
abs(ship.rotate-b.rotate) < .01 && // boid is above the ship
ship.crew.length < 3 && // there is room for it
(now()-b.shipIO) > secs(3) // Was not in the ship
) {
if ( shipAlive ) {
if ( b.o == 0 ) addCrew(b); // is one of the player's boids
else { // enemy boid attacks the ship
newExplosion([b, ship], false, 2, .2);
shipDamage(1,
'Enemy boids has destroyed your ship.',
function() {
msgToUsr('Enemy boids are attacking the ship!');
}
);
}
}
}
} else {
// No ship here... boring life...
// Never stops to walk the surface
if ( (b.rotateV||0) == 0 ) b.rotateV = .01;
if ( abs(b.rotateV) < .002 ) {
// Prevent boids to walk all together, and never touch:
if( rand()<-0.49 ) b.rotateV *= -1; // 1% chance to change direction
b.rotateV*=1.5;
}
}
// grow boid
if ( b.r <= 2 ) { // not so big for any planet
b.r += b.o==0 ? 0.004 : 0.002;
}
else if ( boidsOnPlanet(p).length < p.r/6 ) { // not too many
// only still growing an reproduct if there is space
if ( b.r <= 3 ) b.r += 0.003;
else if ( rand() < -.49 ) { // 1% chance for reproduction
b.r = 1.5;
newBoid(b.o, 1+rand(.5), 0,0, 0,0, b.rotate+rand(.001), p);
}
}
},
{
0: function(p, proximity) { // Touch the planet
if (p == sun) { // Boid die if dropped into sun
boidDie(b);
newExplosion([b, sun], false, 2, .3);
} else { // Boid landed a planet
b.planet = p;
b.rotate = getAngleToPlanet(b, p);
b.o==0 ?
onUpdateBoidsOnPlanet('playerLanding', b.planet):
onUpdateBoidsOnPlanet('enemyLanding', b.planet);
}
}
}
);
// test boid to boid collision
boids.forEach(function(b2){
if ( b2 != b ) {
dist = distP2P(b2,b);
if ( (dist.d - b.r - b2.r) < 0 ) {
if (b.o != b2.o) { // Boids conflict. Both die.
boidDie(b); boidDie(b2);
if (b.planet) onUpdateBoidsOnPlanet('conflict', b.planet);
newExplosion(b, false, 2, .2);
newExplosion(b2, false, 2, .2);
} else { // Friend touch
if (b.planet) { // in a planet
var inc = b.r/5000 * ( b.rotate<b2.rotate ? 1 : -1 );
b.rotateV -= inc;
b2.rotateV += inc;
} else { // in the space
b.vx += (1-dist.x)/100;
b.vy += (1-dist.y)/100;
b2.vx -= (1-dist.x)/100;
b2.vy -= (1-dist.y)/100;
}
}
}
}
});
});