-
Notifications
You must be signed in to change notification settings - Fork 3
/
in_game_state.js
1938 lines (1717 loc) · 58.6 KB
/
in_game_state.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
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// *** game ***
// NOTE: get_type() returns "game" not "in_game"
// defined up here cuz instructions need them
// and a game might not be init'd before reading instr
g_speed_factor = 1; // multiply all speed constants in the game by this, for easy mode
// Global Variable so cell arrows can draw dots
GLOBAL_is_easy = false;
// flag for whether the tutorial has been played through
// if false, easy mode will include the tutorial
g_tut_done = false;
// game_type is:
// 0 - easy
// 1 - tutorial
// 2 - hard
var in_game_state = function (p, previous_state, game_type) {
// object to return
var obj = game_state(p);
obj.set_previous_state(prev_state);
// whether or not we are testing
// use it wherever
var testing = false;
obj.testing = function() { return testing; };
// --- constants ---
//var num_of_render_levels = 5; Now auto-calculating in init so don't have to change
// --- private variables ---
var GOOD_NOTIFICATION_COLOR = p.color(0, 255, 0);
var BAD_NOTIFICATION_COLOR = p.color(255, 0, 0);
var prev_state = previous_state;
// distance is the x-coordinate of the total distance traveled
// increments with scroll
var distance = 0;
//var score = 0;
var active_cell = null;
var last_active_cell = null;
// multiply each object's scroll amount by this
// factor, which increases throughout the game
var scroll_factor = 1;
if (game_type < 2) {
g_speed_factor = 0.75;
}
scroll_factor *= g_speed_factor;
var game_objects = [];
var paused = false;
var status_bar_height = 40;
var status_height = 20;
var score = num_status_obj(p, {
pos : new p.PVector(p.width - 100, status_height),
text : "Score:",
num : 0,
format : add_commas
});
var mult = num_status_obj(p, {
pos : new p.PVector(p.width - 200, status_height),
text : "Multiplier:",
num : 1,
format : function(num) {
return num+"x";
}
});
var time_elapsed = 0; // Time elapsed in seconds
var set_time = false; //bool that says if we've started counting time
var time_status = num_status_obj(p, {
pos : new p.PVector(85, status_height),
text : "Time:",
num : time_elapsed,
// formats time to mins:secs
format : function(num) {
var secs = num%60;
if (secs < 10) { secs = "0" + secs; }
return p.floor(num/60) + ":" + secs;
}
});
var mutation = mutation_obj(p, { status_height : status_height });
var pause_button = button(p, {
state: function() {
do_pause();
console.log("pause button");
return pause_state(p, obj);
},
rect: {
pos: new p.PVector(25, status_height),
width: 25, height: 25,
}
});
// draw two vertical rects
pause_button.draw = function() {
p.pushMatrix();
// get the rect spec back
var r = pause_button.get_rect();
var w = r.width;
var h = r.height;
p.translate(r.pos.x-w/2, r.pos.y-h/2);
p.strokeWeight(2);
p.stroke(255);
p.noFill();
p.ellipse(w/2, h/2, w, h);
p.noStroke();
p.fill(255);
p.rectMode(p.CORNER);
p.rect(w/4, h/5, w/5, 3*h/5);
p.rect(w-w/4-w/5, h/5, w/5, 3*h/5);
p.popMatrix();
};
// Used to draw all of the statuses (must implement draw)
var all_status_objs = [score, mult, time_status, mutation];
var all_buttons = [pause_button];
var is_tutorial = false;
if (game_type === 1
// if the tutorial hasn't been done, do on easy mode
|| (game_type === 0 && !g_tut_done) ){
is_tutorial = true;
}
if (game_type < 2) {
GLOBAL_is_easy = true;
}
// the popup to draw at a given time
// null if there is none
var tut_window = null;
// flag for unpausing
// when false mouse clicks wont fire
var ok_to_fire = true;
// Call tut_manager.popup(type) when you want to signal a tutorial message
// All the types are in tut_flags
var tut_manager = (function() {
// object to store all the msgs
var tut_msgs = {
spacebar: "Press SPACEBAR or CLICK the mouse to shoot virions out of an infected cell in the direction of the arrow. The dotted lines will help you aim.",
arrows: "Use the LEFT and RIGHT arrow keys to switch between infected cells.",
macrophage: "Watch out for macrophages! They will kill your virion and alert a B cell.",
antibodies: "Oh no! The B cell is producing antibodies! If an antibody attaches to an infected cell, the cell will be marked for destruction by a granulocyte.",
killer: "A granulocyte just destroyed one of your infected cells marked with an antibody and all the virions inside it! Your virus won't be safe from the granulocyte until it mutates, creating a new strain.",
mutation: "Your virus just mutated to a new strain! Each virion can only be attacked by immune cells that know about its strain. Immune cells that know about a certain strain will be filled with the same color as virions of that strain.", strain: "Uh oh! The last virion of the new strain died, so your current highest mutation level went back down. You should try to keep alive the virions of the newest strain - they will earn you the most points!" }; // These flags are set to false when they've already occured
// built based on tut_msgs
var tut_flags = {};
for_each(
keys(tut_msgs),
function(type) {
tut_flags[type] = true;
}
);
var tut_popup = function(txt) {
var obj = {};
var x = p.width/2;
var y = p.height/2;
var w = 400;
var h = 200;
var tw = w-50;
var tut_box = image_manager.get_image("tutorialbox.png");
obj.draw = function() {
/*
p.noStroke();
p.fill(100);
p.rectMode(p.CENTER);
p.rect(x, y, w, h);
*/
p.imageMode(p.CENTER);
p.image(tut_box, x, y);
p.fill(255);
p.textLeading(17);
p.textSize(16);
p.textAlign(p.CENTER, p.CENTER);
p.text(txt, x-tw/2, y-h/2, tw, h-50);
};
return obj;
};
var show_popup = function(text) {
var close_button = button(p, {
state: function() {
tut_window = null;
obj.resume();
all_buttons.pop() // DANGEROUS... Hope we're not adding any other buttons anytime soon
return obj; // the current state
},
rect: {
pos: new p.PVector(p.width/2, p.height/2+60),
//width: 50, height: 50,
//text: "OK"
image: "tut_ok.png"
}
});
all_buttons.push(close_button);
tut_window = tut_popup(text);
sounds.play_sound("TextPopUp");
};
var tut_obj = {
popup : function(type) {
if (is_tutorial && tut_flags[type]) {
tut_pause();
ok_to_fire = false;
text = tut_msgs[type];
show_popup(text);
tut_flags[type] = false;
}
allFinished = true;
for_each(
keys(tut_msgs),
function(type) {
if (tut_flags[type]) {
allFinished = false;
}
}
);
// the tutorial will have been completely finished by now
if (allFinished) {
g_tut_done = true;
}
}
};
return tut_obj;
})()
var all_notifications = [];
var last_notification_time = -1
// takes a string and adds a new notification
var notify = function(note, color) {
var the_pos = null;
if (active_cell) {
console.log("Setting to active cell");
a_pos = active_cell.get_pos();
the_pos = new p.PVector(a_pos.x, a_pos.y - 30);
}
else {
console.log("Setting to default");
the_pos = new p.PVector(p.width * (5/8), p.height * (5/8));
}
if (new Date().getTime() > (last_notification_time + 2000)) { // two seconds
all_notifications.push(notification(p, { "text": note, "color": color,
"pos" : the_pos}));
last_notification_time = new Date().getTime();
}
};
var generator = make_generator(p, {
game : obj,
mutation : mutation
});
// temporary flag TODO
//var game_over = false;
//A mapping from game_object types to their rendering levels
var type_to_level = {
"background_object":0,
"background_edge":0,
"wall":1,
"wall_segment":1,
"particle":2, // general name for level
"cell":3, // general name for level
"wall_cell": 3,
"empty_cell": 3,
"enemy":4, // general name for level
"floater":4,
"tkiller":4,
"antibody":4,
"b_cell": 5,
"multiplier":6,
"info":7
};
// given a type returns the array of objects
// corresponding to that type's level
var level = function(type) {
var lvl = game_objects[type_to_level[type]];
assert (lvl, type+" not a valid type!");
return lvl;
};
/*
// Buttons
// Have a rectangle representing their position and
// a state to go to when pressed
var pause_button = {
state : pause_state(),
rectangle : rectangle(p, {
pos : new p.PVector(p.width - 20, 20),
width : 20,
height : 20
})
};
var help_button = {
state : help_state(),
rectangle : rectangle(p, {
pos : new p.PVector(p.width - 80, 20),
width : 20,
height : 20
})
};
var exit_button = {
state : exit_state(),
rectangle : rectangle(p, {
pos : new p.PVector(20, 20),
width : 20,
height : 20
})
};
*/
//Not ordered
// --- private methods ---
// initialization code goes here
// gets called at the bottom
var init = function() {
// Start playing the game music
if (g.music) {
sounds.play_background_music();
}
//Initialize game_objects to be a list of num_of_render_levels empty lists
var num_of_render_levels = 0;
for (var key in type_to_level){
if (type_to_level.hasOwnProperty(key)) {
var new_level = type_to_level[key];
if (new_level > num_of_render_levels) {
num_of_render_levels = new_level;
}
}
}
for (var i = 0; i < num_of_render_levels; i++) {
game_objects[i] = [];
}
var startx = p.width-150;
if (testing) {
var startx = 150;
}
var initial_cells = [
cell(p, {
pos: new p.PVector(startx, p.height/2),
vel: new p.PVector(0, 0),
state: "alive",
}),
cell(p, {
pos: new p.PVector(startx+170, p.height/2-40),
vel: new p.PVector(0, 0),
state: "alive",
}),
cell(p, {
pos: new p.PVector(startx+170, p.height/2),
vel: new p.PVector(0, 0),
state: "alive",
}),
cell(p, {
pos: new p.PVector(startx+170, p.height/2+40),
vel: new p.PVector(0, 0),
state: "alive",
})
];
//var cell_level = type_to_level["cell"];
//game_objects[cell_level].push(initial_cell);
obj.add_objects(initial_cells);
var initial_par = particle(p, {
pos: new p.PVector(0, p.height/2),
vel: new p.PVector(8, 0),
// start with some gray
mutation_info: mutation.get_info()//p.color(250, 250, 250)
//mutation : mutation
});
obj.add_object(initial_par);
init_walls();
init_back();
//active_cell = initial_cell;
// Add background
//var bg = background(p, { pos : new p.PVector(0, 0)});
//obj.add_object(bg);
//console.log(level("background")[0]);
};
// creates, adds, and returns a new b_cell with no target
var make_b_cell = function() {
var new_b_cell = b_cell(p, {
pos: new p.PVector(p.width+50, p.random(p.height/4, 3*p.height/4)),
});
obj.add_object(new_b_cell);
return new_b_cell;
};
// creates, adds, and returns a new tkiller with no target
var make_tkiller = function() {
var new_tkiller = tkiller(p, {
pos: new p.PVector(p.width+50, p.random(p.height/4, 3*p.height/4)),
});
obj.add_object(new_tkiller);
return new_tkiller;
};
// Returns the current b cell on the screen, if there is one
var get_b_cell = function(level) {
var the_b_cell = null;
do_to_type(
function(b) {
if (b.get_level() === level) {
the_b_cell = b;
}
},
"b_cell", true
);
return the_b_cell;
};
var alert_b_cell = function(flo) {
// gets b_cell of the flo's level
var b = get_b_cell(flo.get_level());
// if there isn't one
if (!b) {
// make one
b = make_b_cell();
b.set_mutation_info(flo.get_mutation_info());
b.set_target(flo);
}
// if there is one
else {
// it should be either active, shooting, or outdated
// so do nothing
}
};
// Alerts the b cell (only one on screen at a time?) to a newly mutated
// obj.
// If o.mutation_level >= b cells current target.mutation_level and o is
// closer than the b cells curr target (or if b cells curr
// target is null), updates b_cells curr target to be o
/*
var alert_b_cell = function(o) {
//var cell_objs = level("b_cell");
//var cell_obj = null;
/*for (var i = 0; i < cell_objs.length; i++) {
cell_obj = cell_objs[i];
if (cell_obj.get_type() === "b_cell") {
the_b_cell = cell_obj;
}
}
/
var the_b_cell = get_b_cell();
if (the_b_cell && the_b_cell.is_alive()) {
var old_target = the_b_cell.get_target();
if (old_target) {
// if new macro has higher or equal level
if (//o.get_level() >= old_target.get_level()
// if new macro has current mutation level
o.get_level() === mutation.get_level()
// and it is closer
&& old_target.get_pos().dist(the_b_cell.get_pos()) >
o.get_pos().dist(the_b_cell.get_pos())) {
the_b_cell.set_target(o);
}
}
else {
the_b_cell.set_target(o);
}
}
};
*/
// sets active_cell to the leftmost infected cell
// if there is one
var next_active_cell = function() {
var sort_fun = function(active_c) { // don't care about active
if (last_active_cell) {
// let's try the nearest cell to the one that died
return function(c1, c2) {
return dist_less_than(last_active_cell, c1, c2) ? -1 : 1;
};
}
// otherwise leftmost
return function(c1, c2) {
return c1.get_pos().x - c2.get_pos().x;
};
}
choose_cell(sort_fun);
};
// Set the next-left or next-right cell to be active, and
// if appropriate sets current active to be not active
// Chooses the closest cell to a_pos
var choose_closest_cell = function(a_pos) {
if (active_cell) { // for the beginning
var old_active = active_cell;
var sort_fun = function(active_cell){
return function(c1, c2){
return c1.get_pos().dist(a_pos) -
c2.get_pos().dist(a_pos);
}
};
choose_cell(sort_fun);
// In case it doesn't change (choose_cell assumes
// that the cell is diff from the curr active)
if (old_active.get_pos().dist(a_pos) <
active_cell.get_pos().dist(a_pos)) {
active_cell.set_state("infected");
active_cell = old_active;
active_cell.set_state("active");
}
}
}
// Chooses the closest cell to the active cell in the direction of comp,
// i.e. such that comp(c1.x, active.x) is true
var choose_cell_helper = function(comp) {
var sort_fun = function(active_c) { //don't rename to active_cell
return function(c1, c2) {
c1x = c1.get_pos().x;
c2x = c2.get_pos().x;
actx = active_c.get_pos().x;
// If one is in the right dir and the other isnt,
// return the one that is
if (comp(c1x, actx) && !(comp(c2x, actx))) {
return -1;
}
else if (!(comp(c1x, actx)) && comp(c2x, actx)) {
return 1;
}
// If they are on the same side, return opposite of comp
else {
if (comp(c1x, c2x)) {
return 1;
}
else {
return -1;
}
}
}
};
choose_cell(sort_fun);
};
// Chooses the next left cell to be active
var choose_left_cell = function() {
choose_cell_helper(function (x, y) {return x < y;});
};
// Same in the right dir
var choose_right_cell = function() {
choose_cell_helper(function (x, y) {return x > y;});
};
// Sets a cell to be active based on sort_fun, and if this is diff
// from curr cell, sets curr cell to not be active
// sort_fun must take the currently active cell, and return a function
// that takes 2 cells, ad returns true if the first is 'better' than
// the second
var choose_cell = function(sort_fun) {
var cells = level("cell");//game_objects[type_to_level["cell"]];
var infecteds = cells.filter(
function(cell) {
// don't want empty_cells
return cell.get_type() === "cell"
&& cell.get_state() === "infected";
});
var sort_f = sort_fun(active_cell);
infecteds.sort(sort_f);
var curr_active = active_cell;
if (infecteds.length > 0) {
// tutorial msg about switching cells
if (infecteds.length > 1) {
tut_manager.popup("arrows");
}
active_cell = infecteds[0];
//If same as current
if (curr_active) { // for the beginning
curr_active.set_state("infected"); //if same, about to change
}
active_cell.set_state("active");
//console.log("got next "+active_cell.to_string());
}
};
// Returns true if source is closer to target1 than target2
// All 3 args are game_objs
var dist_less_than = function(source, target1, target2) {
var source_pos = source.get_pos();
return source_pos.dist(target1.get_pos()) <
source_pos.dist(target2.get_pos());
};
// Updates all tkillers targets
// Tkillers target the closest infected and targeted cell
var update_tkillers_targets = function(){
var all_infected_cells = get_all_infected_cells();
do_to_type(function(tkill){
// remove bad targets
if (tkill.get_target()
// if it's dead
&& (tkill.get_target().is_dead()
// or dying
|| tkill.get_target().get_state() === "dying"
// or offscreen
|| tkill.get_target().is_offscreen()
// or the target mutated
|| !same_mutation_level(tkill, tkill.get_target()))) {
tkill.set_target(null);
}
// set a new target
for_each(
all_infected_cells,
function(infected_cell) {
if (infected_cell.has_antibody()
&& same_mutation_level(infected_cell, tkill)
&& (!tkill.get_target() ||
dist_less_than(tkill,
infected_cell, tkill.get_target()))) {
tkill.set_target(infected_cell);
}
}
);
}, "tkiller", true);
};
// adds new tkillers if need be
var add_tkillers = function() {
var tks = get_all_of_type("tkiller");
do_to_type(
// for each b_cell
function(b) {
// if it needs a tkiller
if (b.is_shooting()) {
// check if one exists
var has_tkiller = false;
for_each(tks, function(tk) {
if (same_mutation_level(b, tk)) {
has_tkiller = true;
}
});
// if one doesn't
if (!has_tkiller) {
console.log("made tkiller for lvl "+b.get_level());
// make one
var tk = make_tkiller();
tk.set_mutation_info(b.get_mutation_info());
}
}
},
"b_cell", true
);
};
// kills the active cell and updates the targets
// of all the tkillers
var kill_active_cell = function() {
active_cell.die();
last_active_cell = active_cell;
active_cell = null;
update_tkillers_targets();
};
//Checks whether any 2 objs are colliding, and if so calls handle_collision on them
var check_collisions = (function() {
// rendering levels to check collisions for:
var to_check = [
//["particle", "particle"],
["particle", "cell"],
["particle", "enemy"],
["particle", "multiplier"],
//["cell", "cell"],
["cell", "enemy"],
//["enemy", "enemy"],
["enemy", "b_cell"],
["b_cell", "wall"],
["particle", "wall"],
["multiplier", "wall"],
["enemy", "wall"]
];
// hey this looks like combinations!
var do_comb = function(arr1, start1, end1,
arr2, start2, end2, fun) {
var i = start1, j = start2;
while (i <= end1) {
while (j <= end2) {
fun(arr1[i], arr2[j]);
j++;
}
i++;
// when arrays are the same
// don't do repeats
if (arr1 === arr2) {
j = i;
}
else {
j = 0;
}
}
};
var collision_fun = function() {
// double combinations!
// for each pair of rendering groups
// do_comb(game_objects, 1, 3,
// game_objects, 1, 3,
for_each(to_check, function(pair) {
var lvl1 = level(pair[0]);
var lvl2 = level(pair[1]);
// for each pair of objects in the groups
do_comb(lvl1, 0, lvl1.length-1,
lvl2, 0, lvl2.length-1, check);
});
//);
};
var check = function(obj1, obj2) {
//console.log("checking "+obj1.to_string()
// +", "+obj2.to_string());
// check the collisions
// don't check collisions with self
if (obj1 !== obj2
&& check_collision(obj1, obj2)) {
handle_collision(obj1, obj2);
//console.log("collision! " +obj1.to_string()
// +", "+obj2.to_string());
}
}
return collision_fun;
}());
// Returns whether 2 objects are colliding
var check_collision = function(obj1, obj2) {
var type1 = obj1.get_type();
var type2 = obj2.get_type();
if (!obj1.is_off_right() &&
!obj2.is_off_right() &&
check_circle_collision(obj1, obj2)) {
var check_again = retrieve(extra_check, type1, type2);
if (check_again !== undefined) {
return check_again(obj1, obj2);
}
else {
check_again = retrieve(extra_check, type2, type1);
if (check_again !== undefined) {
return check_again(obj2, obj1);
}
else {
return true;
}
}
}
else {
return false;
}
};
// Checks for a collision between circle (obj2) and rectangle (obj1)
var check_rectangle_collision = function(rect, circ) {
return overlapping_vertically(circ, rect, rect.get_y_offset()) &&
overlapping_horizontally(circ, rect, rect.get_x_offset());
};
// Rectangle and circle collision helper functions
// Returns whether the circle is overlapping the rectangle, horizontally,
// by offset num of pixels
var overlapping_horizontally = function(circ, rect, offset) {
var circlel = circ.get_left(), circler = circ.get_right();
var rectl = rect.get_left(), rectr = rect.get_right();
return ((circler <= rectr && circler >= (rectl + offset)) ||
(circlel >= rectl && circlel <= (rectr - offset)));
};
// Returns whether the circle is overlapping the rectangle, vertically,
// by offset num of pixels
var overlapping_vertically = function(circ, rect, offset) {
var circlet = circ.get_top(), circleb = circ.get_bottom();
var rectt = rect.get_top(), rectb = rect.get_bottom();
return (circleb <= rectb && circleb >= (rectt + offset) ||
(circlet >= rectt && circlet <= (rectb - offset)));
};
// holds extra collision checking functions for certain
// pairs of types
var extra_check = {
"wall_segment": {
"particle": check_rectangle_collision,
"b_cell": check_rectangle_collision,
"multiplier": //check_rectangle_collision
// had to do manually cuz offset was messed up
function(rect, circ) {
return overlapping_vertically(circ, rect, 0) &&
overlapping_horizontally(circ, rect, 0);
}
},
"floater": {
"b_cell": check_rectangle_collision
},
"wall_cell": {
"particle": check_rectangle_collision
}
};
// checks for collisions between two objects by
// checking if their bounding circles are overlapping
// returns true if they are colliding
var check_circle_collision = function(obj1, obj2) {
// if the distance between their center points
// is <= the sum of their radii then they are colliding
// ONLY WORKS FOR CENTERED SHAPES
return (obj1.get_pos().dist(obj2.get_pos())
<= obj1.get_radius() + obj2.get_radius());
};
// make it public
obj.check_circle_collision = check_circle_collision;
/*
// Reverses 2 objs appropriate velocities
var bounce_collided = (function() {
// A list containing 2 element lists
// Inner lists represent object type pairs to not bounce off each other
var to_exclude = [ ["particle", "multiplier"],
["particle", "particle"],
["wall_segment", "b_cell"],
["particle", "antibody"]];
var is_excluded = function(o1, o2) {
var is_excluded = false;
var type1 = o1.get_type(), type2 = o2.get_type();
var objs_in_list = function(list) {
return ((list[0] === type1 && list[1] === type2) ||
(list[0] === type2 && list[1] === type1));
}
for_each(to_exclude, function(l) {
if (objs_in_list(l)) {
is_excluded = true;
}
});
return is_excluded;
}
return function(obj1, obj2) {
if (!is_excluded(obj1, obj2)) {
bounce(obj1, obj2);
}
}
}());
*/
var bounce = function(obj1, obj2) {
//offset adjusts how closely we check, since we can't check exactly when they collide every time
var offset = 5;
var onel = obj1.get_left(), oner = obj1.get_right();
var onet = obj1.get_top(), oneb = obj1.get_bottom();
var twol = obj2.get_left() + offset, twor = obj2.get_right() - offset;
var twot = obj2.get_top() + offset, twob = obj2.get_bottom() - offset;
//When bouncing, check velocity to make sure they are 'incoming' to each other
//This avoids them getting stuck (makes sure they didn't just collide)
//bounce vertically
var y_vel = obj1.get_vel().y;
var x_vel = obj1.get_vel().x;
if ((onet >= twob && y_vel <= 0) || (oneb <= twot && y_vel >= 0)) {
obj1.reverse_y();
obj2.reverse_y();
}
if ((oner <= twol && x_vel >= 0)
|| (onel > twor && x_vel <= 0)) { //bounce horizontally
obj1.reverse_x();
obj2.reverse_x();
}
};
// handles collisions between different object types
var handle_collision = function(obj1, obj2) {
var ot1 = obj1.get_type();
var ot2 = obj2.get_type();
// try first with one order
var handler = retrieve(collision_handlers, ot1, ot2);
if (handler) {
handler(obj1, obj2);
}
// if it didn't work
else {
// try the other order
handler = retrieve(collision_handlers, ot2, ot1);
if (handler) {
handler(obj2, obj1);
}
else {
//Not an error now?
//throw "Unsupported collision type!";
}
}
// Bounce if appropriate
//bounce_collided(obj1, obj2);
};
// object to store all the handlers
// created once with a closure
// dont manage bouncing/changing direction here
var collision_handlers = (function() {
var nothing = function(obj1, obj2) {};
// infects the cell, kills the particle
var infect = function(par, cell) {
// only if cell is "alive"
// (ie only one particle per cell)
assert(cell, "Not a cell in infect!");
if (cell.get_state() !== "dead"
&& cell.get_state() !== "dying") {
par.die();
}
if (cell.get_state() === "alive") {
tut_manager.popup("spacebar");
//Play sound
sounds.play_sound("cell_infect");
// increase mutation percentage if level is highest on screen
// must be done before setting state of infected cell
// only happens if infected by particle of cur level
if (par.get_level() === mutation.get_level()) {
mutation.infected_cell();
}
cell.set_state("infected");
cell.set_mutation_info(par.get_mutation_info());
// Add 10 to score
score.incr(10 * par.get_level() * mult.get_num());
}
else {
// otherwise deflect
//bounce(par, cell);
}
};
var handlers = {
"particle": {
// particle vs. particle
// do nothing?
"particle": nothing,
// particle vs. cell
// infect the cell, kill the particle
"cell": infect,
// particle vs. wall_cell
// bounce particle off cell
// cell doesn't move
"wall_cell": bounce,
// particle vs. empty_cell
// // infect the cell, kill the particle
// kill the particle
"empty_cell": function(par, cell) {
// TODO diff sound?
sounds.play_sound("cell_infect");
par.die();
},
// particle vs. floater
// kill the particle
// floater takes on color of particle
"floater": function(par, flo) {
par.die();