-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsketch.js
More file actions
1980 lines (1811 loc) · 73.7 KB
/
Copy pathsketch.js
File metadata and controls
1980 lines (1811 loc) · 73.7 KB
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
var _assets; // dictionary
var _timerManager; // dictionary
var _stageManager;
var _spriteManager;
var _sceneSpace; // canvas
var _playSpace; // canvas
var _scoreBoardSpace; // canvas
var _tutorialBoardSpace; // canvas
var _dynamicObjManager;
var _protagonist;
var _beatManager;
var stage_name = ['menu', 'School', 'Party', 'Workspace']; // stage names
// representative colors of the topics
// 0-4: magenta(#ff00aa), blue(#00aaf0), purple(#aa00ff), orange(#ffaa00), green(#c9e3ac)
var topic_colors = ['255,0,170', '0,170,240', '170,0,255', '255,170,0', '201,227,172'];
// mouse buffer used to avoid repeat mouse click
var _isMousePressed = false; // if the mouse if pressed
var _mouseBuffer = false;
var _mouseBufferTimer = 0;
// in-game parameters
var _Width = 600; // width of the canvas
var _Height = 400; // height of the canvas
var _sceneheight = 130; // height of the top scene graphic
var round_time = 120; // total time per round(sec)
var _fragScore = 10; // score of each single fragment
var _probOfTorpedo = 10; // generative probability of torpedo
var _nonTopicFrag = 60; // generative probability of non-topic fragment
var tutorial_text = [
`1) You are going to participate in a group discussion.`,
`2) Unfortunately, you're not talkative. So you will
have to prepare yourself before you're going to talk.`,
`3) Catch the ideas in your mind, accumalate them and
speak out, which is what you're going to do.`,
`4) The red heart represents your mind. Use W, A, S, D
to control it to collect fragments of thoughts.`,
`5) The groove at the bottom is your courage power.
If the power is full, you will be able to speak out.`,
`6) The color of the bubble represents the current
topic people are talking on.`,
`7) The colorful balls represent the fragments of
thoughts in your mind.`,
`8) In order to fill up the courage, you will need to
collect the fragments in the same color as the topic.`,
`9) Dodge from the distractions as they will decrease
your courage power.`
]
var pixel_font; // font used in the interface
var digit_font; // font used in the tutorial
var text_font; // font used in texts
var fram_rate = 60;
var global_timer = 0;
function preload(){
// soundFormats('mp3', 'ogg');
// initialize assets manager
_assets = new Map();
// load assets (only the assets used in menu stage are preloaded)
// stage images
_assets.set('stage1_back', new asset('static_image', 'assets/graphics/Stage1_back.png'));
_assets.set('stage1_front', new asset('static_image', 'assets/graphics/Stage1_front.png'));
_assets.set('stage2_back', new asset('static_image', 'assets/graphics/Stage2_back.png'));
_assets.set('stage2_front', new asset('static_image', 'assets/graphics/Stage2_front.png'));
_assets.set('stage3_back', new asset('static_image', 'assets/graphics/Stage3_back.png'));
_assets.set('stage3_front', new asset('static_image', 'assets/graphics/Stage3_front.png'));
// load characters
_assets.set('p1a', new asset('dynamic_image', 'assets/graphics/p1a', 4, '.png'));
_assets.set('p1b', new asset('dynamic_image', 'assets/graphics/p1b', 4, '.png'));
_assets.set('p1c', new asset('dynamic_image', 'assets/graphics/p1c', 4, '.png'));
_assets.set('p1d', new asset('dynamic_image', 'assets/graphics/p1d', 4, '.png'));
_assets.set('p1e', new asset('dynamic_image', 'assets/graphics/p1e', 4, '.png'));
_assets.set('p1f', new asset('dynamic_image', 'assets/graphics/p1f', 4, '.png'));
_assets.set('p2a', new asset('dynamic_image', 'assets/graphics/p2a', 4, '.png'));
_assets.set('p2b', new asset('dynamic_image', 'assets/graphics/p2b', 4, '.png'));
_assets.set('p2c', new asset('dynamic_image', 'assets/graphics/p2c', 4, '.png'));
_assets.set('p2d', new asset('dynamic_image', 'assets/graphics/p2d', 4, '.png'));
_assets.set('p2e', new asset('dynamic_image', 'assets/graphics/p2e', 4, '.png'));
_assets.set('p3a', new asset('dynamic_image', 'assets/graphics/p3a', 4, '.png'));
_assets.set('p3b', new asset('dynamic_image', 'assets/graphics/p3b', 4, '.png'));
_assets.set('p3c', new asset('dynamic_image', 'assets/graphics/p3c', 4, '.png'));
_assets.set('p3d', new asset('dynamic_image', 'assets/graphics/p3d', 4, '.png'));
// load menu UIs
_assets.set('button_huge', new asset('dynamic_image', 'assets/graphics/button_huge_', 2, '.png'))
_assets.set('button_big', new asset('dynamic_image', 'assets/graphics/button_big_', 2, '.png'));
_assets.set('button_back', new asset('dynamic_image', 'assets/graphics/button_back_', 2, '.png'));
// load stage data
_assets.set('data_1', loadJSON('assets/stage_parameter_1.json'));
_assets.set('data_2', loadJSON('assets/stage_parameter_2.json'));
_assets.set('data_3', loadJSON('assets/stage_parameter_3.json'));
}
function setup(){
var myCanvas = createCanvas(_Width, _Height);
myCanvas.parent("main");
angleMode(DEGREES);
noStroke();
// load all the remaining assets
// load graphic files
// load thumbnails
_assets.set('thumbnail_Stage1', new asset('static_image', 'assets/graphics/thumbnail_Stage1.png'));
_assets.set('thumbnail_Stage2', new asset('static_image', 'assets/graphics/thumbnail_Stage2.png'));
_assets.set('thumbnail_Stage3', new asset('static_image', 'assets/graphics/thumbnail_Stage3.png'));
// load tutorial images
_assets.set('tutorial_01', new asset('static_image', 'assets/graphics/tutorial_1.png'));
_assets.set('tutorial_02', new asset('static_image', 'assets/graphics/tutorial_2.png'));
_assets.set('tutorial_03', new asset('static_image', 'assets/graphics/tutorial_3.png'));
_assets.set('tutorial_04', new asset('static_image', 'assets/graphics/tutorial_4.png'));
_assets.set('tutorial_05', new asset('static_image', 'assets/graphics/tutorial_5.png'));
_assets.set('tutorial_06', new asset('static_image', 'assets/graphics/tutorial_6.png'));
_assets.set('tutorial_07', new asset('static_image', 'assets/graphics/tutorial_7.png'));
_assets.set('tutorial_08', new asset('static_image', 'assets/graphics/tutorial_8.png'));
_assets.set('tutorial_09', new asset('static_image', 'assets/graphics/tutorial_9.png'));
// load UI elements
_assets.set('board_A', new asset('static_image', 'assets/graphics/board_A.png'));
_assets.set('board_B', new asset('static_image', 'assets/graphics/board_B.png'));
_assets.set('board_X', new asset('static_image', 'assets/graphics/board_X.png'));
_assets.set('board_Y', new asset('static_image', 'assets/graphics/board_Y.png'));
_assets.set('board_O', new asset('static_image', 'assets/graphics/board_O.png'));
_assets.set('topic_A', new asset('static_image', 'assets/graphics/topic_A.png'));
_assets.set('topic_B', new asset('static_image', 'assets/graphics/topic_B.png'));
_assets.set('topic_X', new asset('static_image', 'assets/graphics/topic_X.png'));
_assets.set('topic_Y', new asset('static_image', 'assets/graphics/topic_Y.png'));
_assets.set('topic_O', new asset('static_image', 'assets/graphics/topic_O.png'));
_assets.set('courage_gauge', new asset('static_image', 'assets/graphics/courage_gauge.png'));
_assets.set('courage_gauge_full', new asset('static_image', 'assets/graphics/courage_gauge_full.png'));
_assets.set('courage', new asset('static_image', 'assets/graphics/courage.png'));
_assets.set('courage_full', new asset('static_image', 'assets/graphics/courage_full.png'));
_assets.set('tutorial_board', new asset('static_image', 'assets/graphics/tutorial_board.png'));
_assets.set('score_board', new asset('static_image', 'assets/graphics/score_board.png'));
_assets.set('button_small', new asset('dynamic_image', 'assets/graphics/button_small_', 2, '.png'));
_assets.set('star_light', new asset('static_image', 'assets/graphics/star_light.png'));
_assets.set('star_dark', new asset('static_image', 'assets/graphics/star_dark.png'));
_assets.set('smallstar_light', new asset('static_image', 'assets/graphics/star_light(small).png'));
_assets.set('smallstar_dark', new asset('static_image', 'assets/graphics/star_dark(small).png'));
// load dialogue bubbles
_assets.set('d_01', new asset('static_image', 'assets/graphics/Dialogue_A1.png'));
_assets.set('d_02', new asset('static_image', 'assets/graphics/Dialogue_A2.png'));
_assets.set('d_11', new asset('static_image', 'assets/graphics/Dialogue_B1.png'));
_assets.set('d_12', new asset('static_image', 'assets/graphics/Dialogue_B2.png'));
_assets.set('d_21', new asset('static_image', 'assets/graphics/Dialogue_X1.png'));
_assets.set('d_22', new asset('static_image', 'assets/graphics/Dialogue_X2.png'));
_assets.set('d_31', new asset('static_image', 'assets/graphics/Dialogue_Y1.png'));
_assets.set('d_32', new asset('static_image', 'assets/graphics/Dialogue_Y2.png'));
_assets.set('d_41', new asset('static_image', 'assets/graphics/Dialogue_O1.png'));
_assets.set('d_42', new asset('static_image', 'assets/graphics/Dialogue_O2.png'));
_assets.set('d_51', new asset('static_image', 'assets/graphics/Dialogue_thumbup1.png'));
_assets.set('d_52', new asset('static_image', 'assets/graphics/Dialogue_thumbup2.png'));
// bubbles array
_assets.set('bubbles_right', [_assets.get('d_01').content, _assets.get('d_11').content,
_assets.get('d_21').content, _assets.get('d_31').content, _assets.get('d_41').content, _assets.get('d_51').content]);
_assets.set('bubbles_left', [_assets.get('d_02').content, _assets.get('d_12').content,
_assets.get('d_22').content, _assets.get('d_32').content, _assets.get('d_42').content, _assets.get('d_52').content]);
// load dynamic objects
_assets.set('protagonist', new asset('static_image', 'assets/graphics/Protagonist(Revised).png'));
_assets.set('torpedo', new asset('static_image', 'assets/graphics/torpedo.png'));
_assets.set('frag_A', new asset('static_image', 'assets/graphics/fragA.png'));
_assets.set('frag_B', new asset('static_image', 'assets/graphics/fragB.png'));
_assets.set('frag_X', new asset('static_image', 'assets/graphics/fragX.png'));
_assets.set('frag_Y', new asset('static_image', 'assets/graphics/fragY.png'));
_assets.set('frag_O', new asset('static_image', 'assets/graphics/fragO.png'));
_assets.set('shape_A', new asset('static_image', 'assets/graphics/shape_A.png'));
_assets.set('shape_B', new asset('static_image', 'assets/graphics/shape_B.png'));
_assets.set('shape_X', new asset('static_image', 'assets/graphics/shape_X.png'));
_assets.set('shape_Y', new asset('static_image', 'assets/graphics/shape_Y.png'));
_assets.set('shape_O', new asset('static_image', 'assets/graphics/shape_O.png'));
// fragments array
_assets.set('frags', [_assets.get('frag_A'), _assets.get('frag_B'), _assets.get('frag_X'), _assets.get('frag_Y'), _assets.get('frag_O'), ]);
_assets.set('frags_shape', [_assets.get('shape_A'), _assets.get('shape_B'), _assets.get('shape_X'), _assets.get('shape_Y'), _assets.get('shape_O')]);
// load sound files
_assets.set('clickbutton', new asset('sound', 'assets/soundeffects/sfx_sounds_button6.wav'));
_assets.set('collide', new asset('sound', 'assets/soundeffects/sfx_exp_shortest_hard7.wav'));
_assets.set('absorbed', new asset('sound', 'assets/soundeffects/sfx_lowhealth_alarmloop6.wav'));
_assets.set('explode', new asset('sound', 'assets/soundeffects/sfx_exp_short_hard2.wav'));
_assets.set('shuttle', new asset('sound', 'assets/soundeffects/sfx_exp_short_hard14.wav'));
_assets.set('success', new asset('sound', 'assets/soundeffects/sfx_exp_odd5.wav'));
_assets.set('cheer', new asset('sound', 'assets/soundeffects/ES_Pyro Explosion.mp3'))
_assets.set('checkout', new asset('sound', 'assets/soundeffects/bell_03.ogg'));
// load background music
_assets.set('music_0', new asset('sound', 'assets/soundeffects/Cluster Block v0_8.mp3'));
_assets.set('music_1', new asset('sound', 'assets/soundeffects/The Adventure Begins 8-bit remix.ogg'));
_assets.set('music_2', new asset('sound', 'assets/soundeffects/Shake and Bake.mp3'));
_assets.set('music_3', new asset('sound', 'assets/soundeffects/S31-Through the Portal.ogg'));
// load the font
pixel_font = loadFont('assets/Minecraft.ttf');
digit_font = loadFont('assets/pixelmix.ttf');
text_font = pixel_font;
textFont(pixel_font);
// Initialize the stage_manager
_stageManager = stageManager.get();
_stageManager.stages.push(new menu('menu', 0, _assets.get('button_huge').content));
// Intialize all the stages(stage name, stage number, back image, front image, stage data)
_stageManager.stages.push(new stage('school', 1, _assets.get('stage1_back').content, _assets.get('stage1_front').content, _assets.get('data_1')));
_stageManager.stages.push(new stage('party', 2, _assets.get('stage2_back').content, _assets.get('stage2_front').content, _assets.get('data_2')));
_stageManager.stages.push(new stage('meeting', 3, _assets.get('stage3_back').content, _assets.get('stage3_front').content, _assets.get('data_3')));
// initialize characters(x position, images, bubble position(true-left, false-right), isSlient(default: false), y position(default))
_stageManager.stages[1].characters.push(new character(138, _assets.get('p1a').content, true));
_stageManager.stages[1].characters.push(new character(363, _assets.get('p1b').content, false));
_stageManager.stages[1].characters.push(new character(192, _assets.get('p1c').content, false));
_stageManager.stages[1].characters.push(new character(273, _assets.get('p1d').content, true));
_stageManager.stages[1].characters.push(new character(456, _assets.get('p1e').content, false, false, _sceneheight - 92));
_stageManager.stages[1].characters.push(new character(75, _assets.get('p1f').content, false, true, _sceneheight - 96));
_stageManager.stages[2].characters.push(new character(273, _assets.get('p2a').content, false));
_stageManager.stages[2].characters.push(new character(156, _assets.get('p2b').content, true));
_stageManager.stages[2].characters.push(new character(393, _assets.get('p2c').content, true));
_stageManager.stages[2].characters.push(new character(462, _assets.get('p2d').content, false));
_stageManager.stages[2].characters.push(new character(24, _assets.get('p2e').content, false, true));
_stageManager.stages[3].characters.push(new character(225, _assets.get('p3a').content, false));
_stageManager.stages[3].characters.push(new character(150, _assets.get('p3b').content, true));
_stageManager.stages[3].characters.push(new character(315, _assets.get('p3c').content, false));
_stageManager.stages[3].characters.push(new character(435, _assets.get('p3d').content, false));
// Initialize the scene space area
_sceneSpace = createGraphics(width, _sceneheight);
// Initialize the play space area
_playSpace = createGraphics(width, height - _sceneheight);
_playSpace.background(0);
_playSpace.noStroke();
_playSpace.angleMode(DEGREES);
_playSpace.imageMode(CENTER);
// Initialize the sprite manager
_spriteManager = spriteManager.get();
// Initialize the score board area
_scoreBoardSpace = createGraphics(_spriteManager.score_board.width, _spriteManager.score_board.height);
_scoreBoardSpace.noStroke();
// Initialize the tutorial space area
_tutorialBoardSpace = createGraphics(_spriteManager.tutorial_board.width, _spriteManager.tutorial_board.height);
_tutorialBoardSpace.noStroke();
_tutorialBoardSpace.textFont(digit_font);
_tutorialBoardSpace.textLeading(16);
// Initialize the dynamic_obj_manager
_dynamicObjManager = dynamicObjManager.get();
// Initialize the protagonist(postion X, position Y, moving speed, size)
_protagonist = protagonist.get(width/2, _playSpace.height/2, 4, 23);
// Initialize the beat_manager(diameter of beats, duration of the beat)
_beatManager = beatManager.get(50, 30);
// Initialize the timer_manager
_timerManager = new Map();
// global timer will always record the run time of the whole program
_timerManager.set('global_timer', setInterval(function(){global_timer++;}), 1000);
// count down timer records records when a countdown timer is created
_timerManager.set('count_down', 0);
_timerManager.set('loading_interval', 0);
_timerManager.set('loading_timeout', 0);
_timerManager.set('talking', 0);
// load the menu
_stageManager.switchStage(0);
}
function draw() {
background(0);
_stageManager.render(); // render the current stage
_stageManager.checkStatus(); // check if the stage status needs updated
}
function mousePressed(){
// let x = mouseX;
// let y = mouseY - _sceneheight;
_isMousePressed = true;
}
function mouseReleased(){
_isMousePressed = false;
}
// check if any key is down, return all the pressed keys as an array
function keyEvents(){
let pressedKeys = [];
// ASCII keycode: left/a, right/d, up/w, down/s, enter-13, esc-27
if(keyIsDown(65) || keyIsDown(37)) pressedKeys.push(true);
else pressedKeys.push(false);
if(keyIsDown(68) || keyIsDown(39)) pressedKeys.push(true);
else pressedKeys.push(false);
if(keyIsDown(87) || keyIsDown(38)) pressedKeys.push(true);
else pressedKeys.push(false);
if(keyIsDown(83) || keyIsDown(40)) pressedKeys.push(true);
else pressedKeys.push(false);
return pressedKeys; // key order: [LEFT, RIGHT, UP, DOWN]
}
// <---------- Assets ---------->
class asset{
constructor(type, path, num = 1, suffix = ''){
switch(type){
case 'dynamic_image':
this.content = [];
for(let i = 1; i <= num; i++) this.content.push(loadImage(path + i + suffix));
break;
case 'static_image':
this.content = loadImage(path);
break;
case 'sound':
this.content = loadSound(path);
break;
default:
console.log('Warning: File type is invalid:', path);
}
}
}
// <---------- Stage ---------->
class stageManager{
static get(){
if(!this.instance) this.instance = new stageManager();
return this.instance;
}
constructor(){
this.curStage = 0; // current stage number
this.nextStage = 0; // next stage number
this.statusNum = 0;
this.tutorialOn = true; // if the tutorial is on
this.stages = []; // all the stages
this.gameData = {}; // current stage data
this.playTime = round_time; // total play time in this round
this.topicTimer = -1; // current topic timer
this.barrageTimers = []; // barrage timers in current stage
// scene monitors
this.talkingTimer = -1;
this.isTalking = false; // if someone is talking
this.whoIsTalking = -1; // who is talking
this.protagonistStartTalking = false; // the protagonist starts talking
this.protagonistTalking = false; // if the protagonist is talking
// transition mask
this.fadeAlp = 1;
this.fadeSpd = 0.5 / fram_rate; // stage fade speed (alp difference per sec)
this.appearSpd = 0.6 / fram_rate; // protagonist appearing speed (alp difference per sec)
}
// switch to and load a new stage
switchStage(num){
this.curStage = num;
// load game data
this.gameData = this.stages[num].data;
// reset the stage
this.resetStage(num);
// if the tutorial is on, play the tutorial first(status is stopped temporarily)
if(this.tutorialOn && num !== 0){
_spriteManager.tutorial_board.pageTurn(true);
this.statusNum --;
}
textAlign(CENTER); // centralize texts
this.fadeAlp = 1; // reset the fade alpha
_protagonist.alp = 0; // reset the fade alpha of the protagonist
}
// reset current stage
resetStage(num){
if(num !== 0){
this.statusNum = 0; // reset status number
this.playTime = round_time; // reset play time
clearTimeout(this.topicTimer); // clear topic timer
this.topicTimer = -1; // reset topic timer
this.barrageTimers = []; // reset barrage timers
this.talkingTimer = -1; // reset talking timers
this.whoIsTalking = -1; // reset who is talking
this.isTalking = false;
this.protagonistStartTalking = false;
this.protagonistTalking = false;
_playSpace.textFont(pixel_font);
_spriteManager.reset(); // reset all the sprites
_dynamicObjManager.clearFragment(); // clear all the dynamic objects in the canvas
_spriteManager.courage_gauge.clear(); // clear the courage gauge
}else{
this.statusNum = 0; // reset status number
this.stages[0].reset();
}
}
// update the talking state
updateTalk(){
if(!this.protagonistStartTalking && !this.protagonistTalking){
if(!this.isTalking){ // if no one is talking, try to talk
let ifTalk = random(0, 100) > 80 ? true : false;
if(ifTalk){
let who = floor(random(1, this.stages[this.curStage].characters.length)); // randomly select one character(except for the protagonist)
this.whoIsTalking = who; // who is talking
this.startTalk(who); // someone random starts talking
}
}
}else if(this.protagonistStartTalking){
if(this.isTalking = true && this.whoIsTalking !== -1){ // if the protagonist is going to talk, stop others talking
this.stopTalk();
}
this.whoIsTalking = 0;
this.startTalk(0);
}
}
// someone starts talking
startTalk(who){
this.stages[this.curStage].characters[who].isTalking = true;
if(this.protagonistStartTalking){
this.protagonistStartTalking = false;
this.protagonistTalking = true;
// // everybody thumbs up
// for(let i = 1; i < this.stages[this.curStage].characters.length; i++){
// this.stages[this.curStage].characters[i].thumbup(true);
// }
this.talkingTimer = setTimeout(function(){
_stageManager.stages[_stageManager.curStage].characters[0].isTalking = false;
// // everybody thumbs down
// for(let i = 1; i < this.stages[this.curStage].characters.length; i++){
// this.stages[this.curStage].characters[i].thumbup(false);
// }
_stageManager.protagonistTalking = false;
_stageManager.isTalking = false;
_stageManager.whoIsTalking = -1;
clearTimeout(_stageManager.talkingTimer);
}, 2000);
}else{
this.isTalking = true;
this.talkingTimer = setTimeout(function(){
_stageManager.stages[_stageManager.curStage].characters[who].isTalking = false;
_stageManager.isTalking = false;
_stageManager.whoIsTalking = -1;
clearTimeout(_stageManager.talkingTimer);
}, random(1500, 3000));
}
}
// make everyone shut up
stopTalk(){
clearTimeout(this.talkingTimer);
this.stages[this.curStage].characters[this.whoIsTalking].isTalking = false;
this.isTalking = false;
}
// update barrage
updateBarrage(mode, dir, max_spd, min_spd, tensity){
let ifShoot = random(0, 100) < tensity ? true : false;
if(ifShoot){
let direct = 0;
let x = 0, y = 0; // start position of the fragment
// decide the type of the fragment to be created
let whatType = random(0, 100);
let type = -2; // type of the fragment to be created
switch(mode){
case "random": // "random" mode means type of the fragment is random
if(whatType < _probOfTorpedo) type = -1; // shoot a terpedo
else if(whatType > _nonTopicFrag) type = _spriteManager.topic_board.curTopic; // shoot a fragment matching the current topic
else type = floor(random(0, 5)); // shoot a random fragment
break;
case "torpedo": // "specific" mode means type of the fragment will only be terpedo
type = -1;
break;
case "noTorpedo": // "noTorpedo" mode means type of the fragment will not be terpedo
if(whatType > _nonTopicFrag) type = _spriteManager.topic_board.curTopic; // shoot a fragment matching the current topic
else type = floor(random(0, 5)); // shoot a random fragment
break;
default:
console.log('The barrage mode cannot be read.');
}
// decide the moving direction of the fragment to be created
switch(dir[0]){
case "disc": // the data is discrete
direct = dir[floor(random(1, dir.length))];
break;
case "cont":
let n = floor(random(0, floor(dir.length / 2)));
direct = random(dir[n * 2 + 1], dir[n * 2 + 2]);
break;
default:
console.log('The direction type cannot be read');
}
// where to create the fragment
if((direct >= -45 && direct <= 45) || (direct >= 315 && direct <= 405)){ // from left edge
x = -20;
y = random(20, _playSpace.height - 20);
}else if(direct >= 45 && direct <= 135){ // from top edge
x = random(20, _playSpace.width - 20);
y = -20;
}else if(direct >= 135 && direct <= 225){ // from right edge
x = _playSpace.width + 20;
y = random(20, _playSpace.height - 20);
}else if(direct >= 225 && direct <= 315){ // from bottom edge
x = random(20, _playSpace.width - 20);
y = _playSpace.height + 20;
}
// create the fragment
_dynamicObjManager.createFragment(x, y, type, direct, random(min_spd, max_spd));
}
}
// update topics
updateStage(){
let game_data = this.gameData;
let t = round_time - this.playTime;
// check if the topic needs to change
for(let i in game_data.topic_timing){
if(t === game_data.topic_timing[i] && _spriteManager.topic_board.curTopic !== game_data.topic_order[i]){
// play the switching sound & flashing text
let topic_color = topic_colors[game_data.topic_order[i]];
_spriteManager.createFloatingText(-100, 70, "TOPIC CHANGE ", "horizontal_flash", 33, topic_color); // create floating text
_assets.get("shuttle").content.play(); // play the sound
_beatManager.createBeat(width/2, _sceneheight + 7, topic_color, 120, 400);
// change to a new topic
_spriteManager.topic_board.change(game_data.topic_order[i], game_data.topic_duration[i]);
// reset talking status, anyone talking currently is to be shut up
if(this.isTalking) this.stopTalk();
// restart characters talking status
if(this.topicTimer !== -1){
clearInterval(this.topicTimer);
this.topicTimer = setInterval(function(){_stageManager.updateTalk();}, 100);
}
break;
}
}
// check the barrage status
for(let i in game_data.barrage){
let bar = game_data.barrage[i]; // barrage object
if(t === bar.start && !this.barrageTimers[i]){ // start a new round of barrages
this.barrageTimers.push(setInterval(function(){_stageManager.updateBarrage(
bar.mode, bar.dir, bar.max_spd, bar.min_spd, bar.tensity
);}, 100));
}
if(t === bar.end){ // end a previous round of barrages
clearInterval(this.barrageTimers[i]);
}
}
}
// *check the current status
checkStatus(){
// check if render the menu
if(this.curStage !== 0){
switch(this.statusNum){
case -1: // *status -1 -> 0: tutorial
push();
fill(0);
rect(0, 0, width, height);
pop();
_spriteManager.tutorial_board.render();
if(_spriteManager.tutorial_board.isOver) this.statusNum ++;
break;
case 0: // status 0 -> 1: scene fade in
if(this.fadeAlp <= 0){
this.statusNum++;
imageMode(CENTER);
rectMode(CENTER);
// initialize the topic board
_spriteManager.topic_board.change(this.gameData.topic_order[0], this.gameData.topic_duration[0]);
}
break;
case 1: // status 1 -> 2: interface loaded
if(_spriteManager.topic_board.isReady && _spriteManager.courage_gauge.isReady) this.statusNum++;
break;
case 2: // status 2 -> 3: protagonist appear
if(_protagonist.alp >= 1){
_timerManager.set('count_down', setInterval(function(){_spriteManager.count_down--;}, 800)); // start countdown
this.statusNum++;
}else _protagonist.alp += this.appearSpd;
break;
case 3: // status 3 -> 4: game start count down
if(_spriteManager.count_down < 0){
clearInterval(_timerManager.get('count_down'));
// start play countdown
_timerManager.set('count_down', setInterval(function(){
_stageManager.playTime --;
_spriteManager.topic_board.topicRemainTime --; // always substract remaining time of the topic
}, 1000));
this.topicTimer = setInterval(function(){_stageManager.updateTalk();}, 100);
_protagonist.control = true; // realse the protagonist
this.stages[this.curStage].music.loop(); // start to play the background music
this.statusNum++;
}
break;
case 4: // status 4 -> 5: game play
if(this.playTime === 0){ // when the play time equals 0, the game is over
clearInterval(_timerManager.get('count_down')); // stop play countdown
_protagonist.control = false; // halt the protagonist
// stop talking
clearInterval(this.topicTimer); // stop attempting to talk
if(this.isTalking) this.stopTalk(); // everyone stop talking
// clear the courage gauge
_spriteManager.courage_gauge.clear();
// set up the score board
_scoreBoardSpace.textLeading(22);
_spriteManager.score_board.enableButton(true); // enable the buttons on score board
this.stages[this.curStage].music.stop(); // stop playing the background music
_assets.get('checkout').content.play(); // play the checkout sound
// record the current score of the stage
let score = _spriteManager.score_board.evaluate();
this.stages[this.curStage].scores.push(score);
this.stages[this.curStage].bestScore = maxInArray(this.stages[this.curStage].scores);
this.statusNum++;
}
break;
case 5: // status 5 -> 6: score board appear
if(this.curStage !== this.nextStage){
this.statusNum++;
_spriteManager.score_board.enableButton(false); // disable the buttons on score board
rectMode(CORNER);
}
break;
case 6: // status 6 -> : scene fade out
this.fadeOut();
if(this.fadeAlp >= 1) this.switchStage(this.nextStage);
break;
}
}else{
switch(this.statusNum){
case 0: // status 0 -> 1: menu fade in
this.fadeIn();
if(this.fadeAlp <= 0){
this.statusNum++;
_stageManager.stages[0].play_button.enable(true); // enable the play button on the menu
this.stages[0].music.loop(); // start to play the background music
}
break;
case 1: // status 1 -> 2: enter the stage selection page
break;
case 2: // status 2 -> 3: waiting to change stage
if(this.curStage !== this.nextStage){
this.statusNum++;
_stageManager.stages[0].back_button.enable(false); // disable the back button on the bottom
_stageManager.stages[0].enableButton(false); // disable the buttons on menu
this.stages[0].music.stop(); // stop playing the background music
rectMode(CORNER);
}
break;
case 3: // status 3 -> : menu fade out
this.fadeOut();
if(this.fadeAlp >= 1) this.switchStage(this.nextStage);
break;
}
}
}
// *render the current stage
render(){
let status = this.statusNum;
// check if render the menu
if(this.curStage !== 0 && status >= 0){
/* stage render order:
scene & dynamic objects
sprites
beats
mask
score board
mask
*/
imageMode(CORNER);
// render the stage
image(_sceneSpace, 0, 0);
this.stages[this.curStage].render();
image(_playSpace, 0, _sceneheight); // render the play space
_playSpace.background(0); // refresh the play space background
// check which elements need to render
// status 2 -> 3: protagonist appear
if(status >= 2){
_protagonist.render();
}
// status 3 -> 4: game start count down
let count_down = _spriteManager.count_down;
if(status >= 3 && count_down >= 0){ // render the start countdown texts
fill(255);
textSize(24);
text(_spriteManager.count_down_texts[count_down], width/2, height/2);
}
// status 4 -> 5: game play
if(status >= 4){
this.updateStage(); // update stage data
_dynamicObjManager.render();
}
// status 1 -> 2: interface loaded
if(status >= 1){
_spriteManager.render();
_beatManager.render();
}
// status 5 -> 6: score board appear
if(status >= 5){
push(); // display the black mask
rectMode(CORNER);
fill(0, 140);
rect(0, 0, width, height);
pop();
_spriteManager.score_board.render();
}
// status 0 -> 1: black mask fade in
if(status === 0){
this.fadeIn();
}
}else if(status >= 0){ // if the current stage is menu
this.stages[this.curStage].render();
if(status > 1){
// render "SELECT STAGE" title
push();
textSize(40);
stroke(255);
strokeWeight(5);
fill(0);
text("SELECT A STAGE", _Width/2, 100);
pop();
// render the stage select buttons
this.stages[this.curStage].renderButton();
// render the back button
this.stages[this.curStage].back_button.render();
}else{
// render the game title
push();
textSize(52);
stroke(255);
strokeWeight(5);
fill(0);
text("SPEAK OUT", _Width/2, 100);
pop();
// render the play button
this.stages[this.curStage].play_button.render();
}
}
}
// stage fade in
fadeIn(){
this.fadeAlp -= this.fadeSpd;
push();
fill(0, max(255 * this.fadeAlp, 0));
rect(0, 0, width, height);
pop();
}
// stage fade out
fadeOut(){
this.fadeAlp += this.fadeSpd;
push();
fill(0, max(255 * this.fadeAlp, 0));
rect(0, 0, width, height);
pop();
}
}
class stage{
constructor(name, num, backimg, frontimg, data = {}){
this.name = name;
this.num = num;
// initialize the scene picture area at the top
this.img_back = backimg;
this.img_front = frontimg;
// characters in the scene
this.characters = [];
// the topic & barrage data of the stage
this.data = data;
// back ground music
this.music = _assets.get('music_' + this.num).content;
this.music.setVolume(0.7);
// max courage required to speak out
this.max_courage = this.data.max_courage || 100;
// history score of the stage
this.scores = [];
this.bestScore = 0;
}
// render the stage
render(canvas = _sceneSpace){
canvas.image(this.img_back, 0, -20);
// render characters
for(let i in this.characters){
this.characters[i].render(canvas);
}
canvas.image(this.img_front, 0, -20);
}
}
class menu{
constructor(name, num, buttonImg, title = 0){
this.name = name;
this.num = num;
this.title = title;
this.scroll = []; // scroll showing all the stages
this.scrollNum = [3, 1, 2, 3]; // order of the scene to be rendered on the scroll
for(let i = 0; i < 4; i++){ // adding canvas to the scroll
this.scroll.push(createGraphics(_Width, _sceneheight));
}
this.pos_y = 0 - _sceneheight; // initial scroll y origin
this.play_button = new cusButton(_Width/2 - 139, _Height/2, 278, 68, function(){
_stageManager.statusNum ++;
_stageManager.stages[0].enableButton(true); // enable the stage select buttons
_stageManager.stages[0].back_button.enable(true); // enable the back button
_stageManager.stages[0].play_button.enable(false); // disable the play button
}, buttonImg, "PLAY", 0, 26);
this.back_button = new cusButton(15, _Height - 61, 70, 48, function(){
_stageManager.statusNum --;
_stageManager.stages[0].enableButton(false); // disable the stage select buttons
_stageManager.stages[0].play_button.enable(true); // enable the play button
_stageManager.stages[0].back_button.enable(false); // disable the back button
}, _assets.get('button_back').content);
this.buttons = []; // stage select buttons
for(let i = 1; i <= 3; i++){
this.buttons.push(new cusButtonImg(_Width/2 - 262 + (i-1) * 184, 220, 164, 93,
function(){_stageManager.nextStage = i;},
function(canvas, x, y){
let evl = _stageManager.stages[i].bestScore;
for(let j = 0; j < 3; j++){
if(j <= evl - 1) canvas.image(_assets.get('smallstar_light').content, x + 25 + j * 43, y + 52);
else canvas.image(_assets.get('smallstar_dark').content, x + 25 + j * 43, y + 52);
}
},
_assets.get('thumbnail_Stage' + i).content, "STAGE_" + i + '\n' + stage_name[i], 255, 17));
}
this.music = _assets.get('music_0').content; // background music of menu stage
this.music.setVolume(0.6);
}
// reset the menu stage
reset(){
this.pos_y = 0 - _sceneheight
}
// enable/disable the buttons
enableButton(ifEnable){
for(let i in this.buttons){
this.buttons[i].enable(ifEnable);
}
}
// render the stage select buttons
renderButton(){
for(let i in this.buttons){
this.buttons[i].render();
}
}
// render the menu background
render(canvas = window){
// roll down the scroll
this.pos_y += 0.5;
// reset the position of the scroll
if(this.pos_y >= 0){
this.pos_y = 0 - _sceneheight;
this.scrollNum = shiftArr(this.scrollNum, 1, 3);
}
// render stage scenes to the scroll
canvas.imageMode(CORNER);
for(let i = 0; i < this.scroll.length; i++){
_stageManager.stages[this.scrollNum[i]].render(this.scroll[i]);
canvas.image(this.scroll[i], 0, this.pos_y + i * _sceneheight);
}
}
}
// <---------- Sprite ---------->
class spriteManager{
static get(){
if(!this.instance) this.instance = new spriteManager();
return this.instance;
}
constructor(){
// Initialize all the sprites
this.tutorial_board = new tutorial( // the tutorial board
width/2, 0 - height/2, width/2, height/2,
[_assets.get('tutorial_board').content, _assets.get('tutorial_01').content, _assets.get('tutorial_02').content,
_assets.get('tutorial_03').content, _assets.get('tutorial_04').content, _assets.get('tutorial_05').content,
_assets.get('tutorial_06').content, _assets.get('tutorial_07').content, _assets.get('tutorial_08').content,
_assets.get('tutorial_09').content], _assets.get('button_small').content
);
this.topic_board = new topicBoard( // the topic board
width/2, -20, width/2, 17,
[_assets.get('board_A').content, _assets.get('board_B').content, _assets.get('board_X').content, _assets.get('board_Y').content,
_assets.get('board_O').content, _assets.get('topic_A').content, _assets.get('topic_B').content, _assets.get('topic_X').content,
_assets.get('topic_Y').content, _assets.get('topic_O').content
]);
this.courage_gauge = new courageGauge( // the courage gauge
width/2, _playSpace.height + 50, width/2, _playSpace.height - 20,
[_assets.get('courage_gauge').content, _assets.get('courage_gauge_full').content,
_assets.get('courage').content, _assets.get('courage_full').content
]);
this.count_down = 3; // game start countdown
this.count_down_texts = ['GO', '1', '2', '3']; // game start countdown texts
this.play_countdown = new timer(width/2, -20, width/2, 21); // the play time countdown
this.score_board = new scoreboard( // the score board turns up when the game ends
width/2, -100, width/2, height/2, _assets.get('score_board').content, _assets.get('button_small').content);
this.floating_texts = []; // floating texts
this.gravity_objects = []; // objects influenced by the gravity
}
// reset all the sprites
reset(){
this.topic_board.reset();
this.topic_board.clear();
this.courage_gauge.reset();
this.count_down = 3;
this.play_countdown.reset();
this.score_board.reset();
this.score_board.clear();
}
// render the sprite to the canvas
render(){
this.topic_board.render();
this.play_countdown.render();
this.courage_gauge.render();
// render all the floating texts
if(this.floating_texts.length > 0){
for(let i in this.floating_texts){
if(this.floating_texts[i].render(_playSpace)) this.floating_texts.splice(i, 1);
}
}
let gobj = this.gravity_objects;
// render all the gravity objects
if(gobj.length > 0){
for(let i in gobj){
gobj[i].render();
if(gobj[i].update()) gobj.splice(i, 1);
}
}
}
// create a new floating text
createFloatingText(x, y, txt, mode = "vertical_float", size = 26, color = '255, 255, 255', ifOutline){
this.floating_texts.push(new floatingText(x, y, txt, mode, size, color, ifOutline));
}
// create a new colored bar
createColorBar(x, y, vx, vy){
this.gravity_objects.push(new gravityObjects(x, y, vx, vy));
}
boost(canvas = window){
for(let i = 0; i < 50; i++){
this.createColorBar(canvas.width/2, canvas.height/2, random(-8, 8), random(-1, -10));
}
}
}
// sprites refer to all the individual changable elements on the screen
class sprite{
constructor(x, y, tx, ty, imgs = [], rate = 40){
this.opos_x = x; // initial x position
this.opos_y = y; // initial y position
this.pos_x = x; // current x position
this.pos_y = y; // current x position
this.targt_x = tx; // target x position
this.targt_y = ty; // target y position
this.imgs = imgs; // image sequence
this.rate = rate; // bigger to slower the moving spd
this.isReady = false; // inform if the sprite has reach the right position
}
// reset the position of the sprite
reset(){