forked from Noxeangel/DEMOG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrabbits_coop_core.js
868 lines (761 loc) · 24.8 KB
/
rabbits_coop_core.js
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
require('./player.js');
var UUID = require('node-uuid');
//require('./public/js/game/Balloons.js');
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Varibles declaration
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//var balloon_speed = 6;
//var balloon_width = 16;
//var balloon_height = 16;
var SKIP_GAME = false;
var lines = 4; //must be changed in main_rabbits_client.js also
var number = 10; //must be changed in main_rabbits_client.js also
var shot_speed = 6;
var shot_width = 8;
var shot_height = 8;
var timerMove = 30;
var canMoveLeft = true;
var canMoveRight = true;
var recordInputEach = 250;
var goal_speed = 2;
var goalballoonY = 20;
var goal_width = 39;
var goal_height = 56;
var waitEnd = -1;
var writeResults1 = true;
var writeResults2 = true;
var balloon_speed = 4;
var balloon_width = 39;
var balloon_height = 56;
var balloonsX_spacing = 60;
var balloonsY_spacing = 60;
var balloonsY = 150;
var balloonsX = 100;
var points_per_enemy = 25;
var launcherSpeed = 10;
var state_game = 'STATE_GAME';
var state_endAnim = 'STATE_ENDANIM';
var state_share = 'STATE_SHARE';
var state_reload = 'STATE_RELOAD';
var state_fall = 'STATE_FALL';
var canMoveAI2 = true;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Space Invaders Game Core Constructor
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var rabbits_game_core = function(maxIter)
{
this.id =undefined;
this.viewport;
this.state = state_game;
this.maxIter = maxIter;
this.isEnded = false;
this.canMoveAI = false;
this.world =
{
width : 800,
height : 600
};
this.p1 = undefined;
this.p2 = undefined;
this.launcherNumber = 1;
this.loopNumber = new Date().getTime();
this.loopThreshold = 1500;
this.reloaded = false;
this.inputsP1 = [];
this.inputsP2 = [];
this.balloons = new Balloons(balloonsX,lines,number);
this.goalShip = undefined;
this.score = 0;
this.given = -1;
this.kept = -1;
this.p1MissedSeesaw = 0;
this.p2MissedSeesaw = 0;
this.p1DistanceSeesaw = 0; // average distance from the center of the landed rabbit to the center of the seesaw
this.p2DistanceSeesaw = 0;
this.p1DistanceTimes = 0; // to calculate average distance
this.p2DistanceTimes = 0; // to calculate average distance
this.p1BalloonsPopped = 0;
this.p2BalloonsPopped = 0;
this.shareSteps = '';
this.goalballoonX = 100;
this.goalballoonY = 100;
this.goalShipAlive = true;
this.balloonsLeft = false;
this.goalballoonLeft = false;
this.flyer = new Rect(270,350,40,46);
this.launcher = new Rect(350,549,96,53);
this.flyer.x = 270; //initial value erased by init_abs below
this.flyer.y = 500;
this.startMilliseconds = -1;
this.gameLength = -1;
this.init_speed = 0.1;
this.init_angle = 0;
this.init_abs = 400;
this.inAirTime = 1200.0;
this.timeScale = 10;
this.angleDirection = -1;
this.p1Ended = false;
this.p2Ended = false;
};
//This line is used to tell node.js that he can access the constructor
module.exports = global.rabbits_game_core = rabbits_game_core;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Game Objects constructors
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var Balloons = function(x,lines,number)
{
this.x = x;
this.y = balloonsY;
this.array = [];
this.lines = lines;
this.number = number;
this.tmpX;
this.tmpY;
this.alive = true;
this.numBalloons = this.lines * this.number;
};
Balloons.prototype.Init = function()
{
for(var j = 0; j < this.lines; j++)
{
for(var i = 0; i < this.number; i ++)
{
this.tmpX = i * balloonsX_spacing + balloonsX;
this.tmpY = j * balloonsY_spacing + balloonsY;
this.array.push(new Balloon(this.tmpX,this.tmpY));
}
}
};
Balloons.prototype.Move = function(x)
{
this.x += x;
if(this.x > 800)
{
this.x -= (800 + balloonsX_spacing * number);
}
for(var i = 0 ; i < this.array.length; i ++)
{
this.array[i].rect.x += x;
if(this.array[i].rect.x > 800)
{
this.array[i].rect.x -= 800 + balloonsX_spacing * number;
}
}
};
Balloons.prototype.KillBalloon = function(i)
{
this.array[i].alive = false;
this.numBalloons --;
};
var Balloon = function(x,y)
{
this.rect = new Rect(x,y,balloon_width,balloon_height);
this.alive = true;
};
var Rect = function(x,y,w,h)
{
this.x = x;
this.y = y;
this.w = w;
this.h = h;
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Game States Init functions
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
rabbits_game_core.prototype.beginInit = function()
{
//console.log('inside begin init1');
this.p1.emit("message",'COLOR,1'); //says what sprites client should use (so that the two clients control a rabbit of the same color)
if (!this.p1.player.result.timedOut)
{
this.p2.emit("message",'COLOR,2');
}
this.startMilliseconds = new Date().getTime();
this.p1.emit('updateTime',false);
if (!this.p1.player.result.timedOut)
{
this.p2.emit('updateTime',false);
}
this.balloons.Init();
this.beginGame();
//console.log('inside begin init2');
};
rabbits_game_core.prototype.beginGame = function()
{
this.p1.emit('message', 'GAME_START');
if (!this.p1.player.result.timedOut)
{
this.p2.emit('message', 'GAME_START');
}
//console.log('inside begin game');
};
/*
rabbits_game_core.prototype.beginShare = function(client)
{
if(client.userid == this.p1.userid)
{
this.p1.emit('message', 'SHARE_STATE');
this.p2.emit('message', 'SHARE_WAIT');
}
else
{
this.p1.emit('message', 'SHARE_WAIT');
this.p2.emit('message', 'SHARE_STATE');
}
};
*/
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Update functions
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Update Message Anatomy
// Element 0 = 'UPDATE'
// Element 1 = launcherRabbit x
// Element 2 = Flyrabbit x & y
// Element 3 = launcherNumber
// Element 4 = baloons pack x
// Element 5 = goalString
// Element 6 = scoreString
// Element 7 = ownNumber
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Main Update function (will call all the other functions)
//This function is called by sioserver.js
rabbits_game_core.prototype.physic_update = function(deltaT)
{
switch (this.state)
{
case state_game:
if (SKIP_GAME)
{
this.state = state_share;
this.p1.emit('message','SHARE_STATE');
}
this.setDirections();
this.moveBalloons();
this.moveMother();
this.moveFlyer(deltaT);
this.checkCollisions();
if (this.p1.player.result.timedOut && this.launcherNumber == 2 && (new Date().getTime() - this.loopNumber) > this.loopThreshold)
{
this.loopNumber = new Date().getTime();
this.canMoveAI = !this.canMoveAI;
if (this.canMoveAI)
{
this.loopThreshold = Math.floor((Math.random() * 500) + 50);
} else
{
this.loopThreshold = Math.floor((Math.random() * 800) + 400);
}
}
if (this.p1.player.result.timedOut && this.launcherNumber == 2 && this.canMoveAI && canMoveAI2)
{
this.moveAI();
}
this.sendUpdate();
break;
case state_endAnim:
this.animMotherFall();
this.sendUpdate();
break;
case state_share:
this.sendUpdate();
break;
default:
this.setDirections();
this.moveBalloons();
this.moveMother();
this.moveFlyer(deltaT);
this.checkCollisions();
this.sendUpdate();
break;
}
};
rabbits_game_core.prototype.update = function(deltaT) {
}; //game_core.update
//This function send the updates messages to the players
rabbits_game_core.prototype.sendUpdate = function()
{
var p1string = this.launcher.x+',';
var flyerString = this.generateFlyerString();
var launcherString = this.launcherNumber+',';
var balloonsString = this.generateBalloonString()+',';
var goalString = this.goalballoonX+'#'+this.goalballoonY+',';
var scoreString = this.score;
if (this.state == state_game || this.state == state_endAnim || this.state == state_reload)
{
this.p1.emit("message",'UPDATE,'+p1string+flyerString+launcherString+balloonsString+goalString+scoreString+',1');
if (!this.p1.player.result.timedOut)
{
this.p2.emit("message",'UPDATE,'+p1string+flyerString+launcherString+balloonsString+goalString+scoreString+',2');
}
}
};
rabbits_game_core.prototype.generateBalloonString = function()
{
var tmpString ='';
tmpString += (this.balloons.x+'#');
for(var i = 0; i < this.balloons.array.length; i ++)
{
if(this.balloons.array[i].alive)
{
tmpString += '1#';
}
else
{
tmpString += '0#';
}
}
//console.log(tmpString);
return tmpString;
};
rabbits_game_core.prototype.generateFlyerString = function()
{
var tmpString = '';
tmpString += (this.flyer.x+'#'+this.flyer.y+',');
//console.log(tmpString);
return tmpString;
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// World Computing functions
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Set the directions of the balloon lines, to avoid colliding with the wall
rabbits_game_core.prototype.setDirections = function()
{
/*
if(this.balloons.x > (this.world.width - balloonsX_spacing * number) && !this.balloonsLeft)
{
this.balloonsLeft = true;
}
if(this.balloons.x <0 && this.balloonsLeft)
{
this.balloonsLeft = false;
}
*/
/*
if( Math.random() > 0.95)
{
this.goalballoonLeft = !this.goalballoonLeft;
}
*/
if(this.goalballoonX > (this.world.width - goal_width) && !this.goalballoonLeft)
{
this.goalballoonLeft = true;
}
if(this.goalballoonX < 0 && this.goalballoonLeft)
{
this.goalballoonLeft = false;
}
};
//Move the balloon lines
rabbits_game_core.prototype.moveBalloons = function()
{
if(this.balloonsLeft)
{
//this.balloons.x -= balloon_speed;
this.balloons.Move(-balloon_speed);
}
else
{
//this.balloons.x += balloon_speed;
this.balloons.Move(balloon_speed);
}
};
//Move the goalballoon
rabbits_game_core.prototype.moveMother = function()
{
if(this.goalballoonLeft)
{
this.goalballoonX -= goal_speed;
}
else
{
this.goalballoonX += goal_speed;
}
};
//Move AI if player is in solo mode
rabbits_game_core.prototype.moveAI = function()
{
var timeToReachFloor = (3.8+Math.random())* 2 * (this.init_speed * this.timeScale + Math.sqrt(2500 * Math.pow(this.timeScale,2) + Math.pow(this.init_speed,2) * Math.pow(this.timeScale,2))); // calculates time necessary to reach y = 550 (solution of equation with this.flyer.y below for this.flyer.y == 550)
var xToReach = Math.round(this.init_angle * this.angleDirection * timeToReachFloor / this.timeScale + this.init_abs);
if (xToReach > (this.launcher.x))
{
this.onInput(this.p2, ['INPUT','0','1','0']);
} else if (xToReach < (this.launcher.x) && this.reloaded == true)
{
this.onInput(this.p2, ['INPUT','1','0','0']);
} else
{
this.canMoveAI = false;
}
};
rabbits_game_core.prototype.animMotherFall = function()
{
if (this.goalballoonY > 500)
{
waitEnd++;
if (waitEnd > 150) //wait a bit when the rabbit reaches floor
{
this.state = state_share;
this.gameLength = (new Date().getTime()) - this.startMilliseconds;
if(this.launcherNumber == 2)
{
this.p1.emit('message','SHARE_STATE');
if (!this.p1.player.result.timedOut)
{
this.p2.emit('message','SHARE_WAIT');
}
}
else
{
this.p1.emit('message','SHARE_WAIT');
if (!this.p1.player.result.timedOut)
{
this.p2.emit('message','SHARE_STATE');
} else
{
var currentTime = new Date().getTime();
while (currentTime + 9223 >= new Date().getTime()) {}
this.Share(this.p2,['SHARE','500']);
}
}
}
}
else
{
this.goalballoonY += goal_speed;
}
};
rabbits_game_core.prototype.moveFlyer = function(deltaT)
{
var deltaX;
if(this.state == state_reload)
{
this.flyer.x = 400 - this.flyer.w;
this.flyer.y = 400;
this.init_abs = 400 - this.flyer.w;
this.init_angle = 0;
this.init_speed = 0.2;
this.inAirTime = 1000;
this.state = state_game;
//this.inAirTime = 0;
//this.sendUpdate();
}
else if(this.state == state_game)
{
if(this.flyer.x < 0 )
{
this.angleDirection = 1;
this.init_angle = 0;
this.init_abs = 0;
this.flyer.x = 0;
}
if ( this.flyer.x > 800 - 40)
{
this.angleDirection = -1;
this.init_angle = 0
this.init_abs = 800 -40;
this.flyer.x = 800 - 40;
}
if(this.flyer.y > 550) //flyer touches the ground
{
this.reloaded = false;
this.launcherNumber = (this.launcherNumber == 1)? 2 : 1;
if (this.launcherNumber == 2)
{
this.loopNumber = new Date().getTime();
this.loopThreshold = Math.floor((Math.random() * 1000) + 700);
this.canMoveAI = false;
}
//console.log(this.launcherNumber);
this.inAirTime = 0;
deltaX = (this.launcher.x + this.launcher.w/2)-(this.flyer.x+this.flyer.w/2); //distance between the middle of the seesaw and the middle of the flyer
//console.log(deltaX);
if((deltaX <= 0 && Math.abs(deltaX) < (this.launcher.w/2 + this.flyer.w/2) && this.launcherNumber == 2) || (deltaX >= 0 && Math.abs(deltaX) < (this.launcher.w/2 + this.flyer.w/2) && this.launcherNumber == 1)) // flyer is on the seesaw, and on the good side of the seesaw
{
deltaX = Math.abs(deltaX);
if (this.launcherNumber == 2)
{
this.p1DistanceSeesaw += deltaX;
this.p1DistanceTimes += 1;
} else
{
this.p2DistanceSeesaw += deltaX;
this.p2DistanceTimes += 1;
}
//console.log("inside");
this.calculateTrajectory(deltaX);
}
else
{
if(this.state == state_game)
{
this.reloaded = true;
if (this.launcherNumber == 2) {this.p1MissedSeesaw += 1} else {this.p2MissedSeesaw += 1}
if (this.launcherNumber == 2)
{
this.loopNumber = new Date().getTime();
this.loopThreshold = 10000; // we allow the AI to move in one step
this.canMoveAI = true;
canMoveAI2 = false;
setTimeout(function(){canMoveAI2 = true},1000); //necessary to use a variable outside of this. for the timeout to work
}
this.state = state_reload;
//this.p1.emit('message',state_reload);
if (!this.p1.player.result.timedOut)
{
this.p2.emit('message',state_reload);
}
}
}
this.flyer.y = 549;
}
else
{
this.inAirTime += deltaT;
this.flyer.x = Math.round(this.init_angle * this.angleDirection * this.inAirTime / this.timeScale + this.init_abs);
this.flyer.y = 450 - Math.round( this.init_speed*( 4* this.inAirTime / this.timeScale - (Math.pow(this.inAirTime / this.timeScale,2)) / 100));
}
}
};
rabbits_game_core.prototype.calculateTrajectory = function(deltaX)
{
if ((this.launcher.x < 100) || (this.launcher.x + this.launcher.w > 700)) {var ordonnees = 0.4;} // to get away from the edges, we boost init_speed and init_angle when the launcher is close from the edges
else {var ordonnees = 0;}
this.init_speed = 0.01 * deltaX + 0.3 + ordonnees;
this.init_angle = 0.01 * deltaX + 0.1 + ordonnees;
this.angleDirection = (this.launcherNumber == 1)? -1 : 1;
if (this.angleDirection == 1)
{
this.init_abs = this.launcher.x; // give the x position of the not-flying player
} else
{
this.init_abs = this.launcher.x + this.launcher.w - this.flyer.w;
}
};
rabbits_game_core.prototype.checkCollisions = function()
{
for(var j = 0; j < this.balloons.array.length; j++)
{
if(this.balloons.array[j].alive)
{
if(this.doCollide(this.flyer,this.balloons.array[j].rect))
{
if (this.launcherNumber == 2) {this.p1BalloonsPopped += 1} else {this.p2BalloonsPopped += 1}
this.balloons.KillBalloon(j);
this.score += points_per_enemy;
}
}
}
if(this.doCollide(this.flyer,new Rect(this.goalballoonX,this.goalballoonY,goal_width,goal_height)))
{
if(this.balloons.numBalloons == 0)
{
//this.score+= 100;
this.state = state_endAnim;
this.p1.emit('message',"ANIM_STATE");
if (!this.p1.player.result.timedOut)
{
this.p2.emit('message',"ANIM_STATE");
}
}
}
};
rabbits_game_core.prototype.doCollide = function(rect1,rect2)
{
return(!((rect1.x > rect2.x + rect2.w) || (rect1.x + rect1.w < rect2.x) || (rect1.y > rect2.y + rect2.h) || (rect1.y + rect1.h < rect2.y)));
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Input functions
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
rabbits_game_core.prototype.onInput = function(client, data){
if(client.userid == this.p1.userid)
{
if(writeResults1)
{
this.inputsP1.push(data+','+Math.round(this.balloons.x*100)/100+','+this.goalballoonX);
writeResults1 = false;
setTimeout(function(){writeResults1 = true},recordInputEach);
}
}
else if(client.userid == this.p2.userid)
{
if(writeResults2)
{
this.inputsP2.push(data+','+Math.round(this.balloons.x*100)/100+','+this.goalballoonX);
writeResults2 = false;
setTimeout(function(){writeResults2 = true},recordInputEach);
}
}
if(this.state == state_game || this.state == state_reload)
{
if(data[1] == '1')
{
if(canMoveLeft)
{
if(this.launcher.x > 0)
{
this.launcher.x -= launcherSpeed;
}
canMoveLeft = false;
setTimeout(function(){canMoveLeft = true},timerMove);
}
}
if(data[2] == '1')
{
if(canMoveRight)
{
if(this.launcher.x < this.world.width -96)
{
this.launcher.x += launcherSpeed;
}
canMoveRight = false;
setTimeout(function(){canMoveRight = true},timerMove);
}
}
}
else if(this.state == state_share)
{
if(data[1] == '1')
{
if(this.launcher.x > 100)
{
this.launcher.x -= launcherSpeed;
}
}
if(data[2] == '1')
{
if(this.launcher.x < this.world.width - 96 - 100)
{
this.launcher.x += launcherSpeed;
}
}
}
};
rabbits_game_core.prototype.shareInput = function(client,data)
{
if(client.userid == this.p1.userid)
{
this.launcher.x = parseInt(data[1]);
}
else
{
this.launcher.x = parseInt(data[1]);
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Debug functions (only used to test game states)
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
rabbits_game_core.prototype.PlayerEnded = function(client , data)
{
if(client.userid == this.p1.userid)
{
this.p1Ended = true;
this.p1.player.result.currentGame++;
this.p1.emit('message','LOBBY');
}
else //this function will never be called by AI
{
this.p2Ended = true;
this.p2.player.result.currentGame++;
this.p2.emit('message','LOBBY');
}
if((this.p1Ended && this.p2Ended) || this.p1.player.result.timedOut)
{
console.log('ended game');
this.EndGame();
}
};
rabbits_game_core.prototype.EndGame = function()
{
this.p1.player.currentRepetition ++;
this.p2.player.currentRepetition ++;
//this.p1.emit('sendEmail'); // this goes to main_..._client.js, which has to send it to app.js through socket.emit then
this.isEnded = true;
};
rabbits_game_core.prototype.Share = function(client, data)
{
this.given = parseInt(data[1]);
this.kept = 1000 - parseInt(data[1]);
this.p1DistanceSeesaw /= this.p1DistanceTimes;
this.p2DistanceSeesaw /= this.p2DistanceTimes;
this.shareSteps = data[2];
//console.log(client.userid + data);
if(client.userid == this.p2.userid)
{
this.sharer = this.p2;
this.p1.player.score += this.given;
this.p2.player.score += this.kept;
this.p1.player.SetGameResultRabbits(this.id,false,this.score,this.given,this.kept,this.p1MissedSeesaw,this.p1DistanceSeesaw,this.p1BalloonsPopped,this.gameLength,true);
this.p2.player.SetGameResultRabbits(this.id,true,this.score,this.given,this.kept,this.p2MissedSeesaw,this.p2DistanceSeesaw,this.p2BalloonsPopped,this.gameLength,false);
this.p1.emit('message','GIVEN_AMMOUNT,'+this.given+',RECIEVER');
if (!this.p1.player.result.timedOut)
{
this.p2.emit('message','GIVEN_AMMOUNT,'+this.given+',SHARER');
}
}
else
{
this.sharer = this.p1;
this.p2.player.score += this.given;
this.p1.player.score += this.kept;
this.p1.player.SetGameResultRabbits(this.id,true,this.score,this.given,this.kept,this.p1MissedSeesaw,this.p1DistanceSeesaw,this.p1BalloonsPopped,this.gameLength,true);
this.p2.player.SetGameResultRabbits(this.id,false,this.score,this.given,this.kept,this.p2MissedSeesaw,this.p2DistanceSeesaw,this.p2BalloonsPopped,this.gameLength,false);
this.p1.emit('message','GIVEN_AMMOUNT,'+this.given+',SHARER');
if (!this.p1.player.result.timedOut)
{
this.p2.emit('message','GIVEN_AMMOUNT,'+this.given+',RECIEVER');
}
}
//setTimeout(this.EndGame(),2000);
};
rabbits_game_core.prototype.GetResult = function()
{
return('Game ID : '+ this.id+'\nTotal Score : '+ this.score+'\n'+this.p1.userid+'\n'+this.p2.userid);
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Client Messages handler
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
rabbits_game_core.prototype.onMessage = function(client, data){
var splittedData = data.split(',');
switch (splittedData[0])
{
case 'INPUT':
splittedData.push((new Date().getTime()) - this.startMilliseconds);
this.onInput(client, splittedData);
break;
case 'MOUSE_INPUT':
this.shareInput(client, splittedData);
break;
case 'ANIM_END':
break;
case 'STATE_GAME':
this.state = state_game;
break;
case 'SHARE':
this.Share(client,splittedData);
break;
case 'ENDED':
this.PlayerEnded(client, splittedData);
break;
}
};