forked from kesiev/akihabara
-
Notifications
You must be signed in to change notification settings - Fork 1
/
game-tutorial1.html
1293 lines (1156 loc) · 51.6 KB
/
game-tutorial1.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
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
<html>
<head>
<!--
Looking for the tutorial?
Start with resources/tutorial1/bundle-map-begin.js.
Copyright (c) 2011 Greg DeKoenigsberg, http://gregdek.org
Based on the work by Francesco Cottone, http://www.kesiev.com/
-->
<script type="text/javascript" src="akihabara/gbox.js"></script>
<script type="text/javascript" src="akihabara/iphopad.js"></script>
<script type="text/javascript" src="akihabara/trigo.js"></script>
<script type="text/javascript" src="akihabara/toys.js"></script>
<script type="text/javascript" src="akihabara/help.js"></script>
<script type="text/javascript" src="akihabara/tool.js"></script>
<script type="text/javascript" src="akihabara/gamecycle.js"></script>
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
</head>
<body>
</body>
<script>
// Game-specific
var maingame;
var tilemaps={};
var dialogues={};
var credits={};
var noface; // Is a fake "actor" in dialogues. The text is ever in the same place.
var audioserver;
// In games like Zelda, object are alive also outside of the screen.
// So, let's calculate a distance threshold from the camera
function objectIsAlive(th) {
return trigo.getDistance(th,gbox.getCamera())<800;
}
function go() {
gbox.setGroups(["background","player","bonus","foes","walls","playerbullets","foesbullets","sparks","foreground","gamecycle"]);
gbox.setAudioChannels({bgmusic:{volume:0.8},sfx:{volume:1.0}});
// player, walls, bullets and foes are under z-index layer
gbox.setRenderOrder(["background",gbox.ZINDEX_LAYER,"sparks","foreground","gamecycle"]);
maingame=gamecycle.createMaingame("gamecycle","gamecycle");
// Title intro
maingame.gameTitleIntroAnimation=function(reset) {
if (reset) {
gbox.playAudio("default-music");
toys.resetToy(this,"rising");
} else {
gbox.blitFade(gbox.getBufferContext(),{alpha:1,color:"rgb(150,150,150)"});
toys.logos.rising(this,"rising",{image:"logo",x:gbox.getScreenHW()-gbox.getImage("logo").hwidth,y:20,speed:1,gapx:250,reflex:0.1,audioreach:"coin"});
}
},
// No level intro animation
maingame.gameIntroAnimation=function() { return true; }
// No end level animation
maingame.endlevelIntroAnimation=function() { return true; }
// Level animation
maingame.levelIntroAnimation=function(reset) {
if (reset) {
toys.resetToy(this,"default-blinker");
} else {
gbox.blitFade(gbox.getBufferContext(),{alpha:1});
return toys.text.fixed(this,"default-blinker",gbox.getBufferContext(),{font:"big",text:maingame.getNextLevel().label,valign:gbox.ALIGN_MIDDLE,halign:gbox.ALIGN_CENTER,dx:0,dy:0,dw:gbox.getScreenW(),dh:gbox.getScreenH(),time:50});
}
}
// Game is ever over, if the player dies the first time. No life check, since is energy-based.
maingame.gameIsOver=function() { return true; }
// Game ending
maingame.gameEndingIntroAnimation=function(reset){
if (reset) {
toys.resetToy(this,"intro-animation");
} else {
gbox.blitFade(gbox.getBufferContext(),{alpha:1});
return toys.dialogue.render(this,"intro-animation",credits.titles);
}
}
// Display video.
maingame.modalVideo = function() {
//Cancel the link behavior
//e.preventDefault();
//Get the A tag
//var id = $(this).attr('href');
var id="#dialog";
//Get the screen height and width
var maskHeight = $(document).height()-100;
var maskWidth = $(window).width()-100;
//Set height and width to mask to fill up the whole screen
$('#mask').css({'width':maskWidth,'height':maskHeight});
//transition effect
$('#mask').fadeIn(1000);
$('#mask').fadeTo("slow",0.8);
//Get the window height and width
var winH = $(window).height();
var winW = $(window).width();
//Set the popup window to center
$(id).css('top', winH/2-$(id).height()/2);
$(id).css('left', winW/2-$(id).width()/2);
//transition effect
$(id).fadeIn(2000);
//});
}
// Game events are decided by the map.
maingame.gameEvents=function() {
tilemaps.map.mapActions();
}
// Change level
maingame.changeLevel=function(level) {
// Cleanup the level
gbox.trashGroup("playerbullets");
gbox.trashGroup("foesbullets");
gbox.trashGroup("foes");
gbox.trashGroup("bonus");
gbox.trashGroup("walls");
gbox.purgeGarbage(); // Since we're starting, we can purge all now
if (level==null)
level={level:"begin",x:100,y:100,introdialogue:true}; // First stage
// Dialogues are emptied - will be loaded by bundles. Cache is not needed - each bundle
// Contains full dialogues for the floor.
dialogues={};
// Map data is wiped too. Will be loaded by loadBundle. Other data in tilemaps is
// kept (i.e. quest status etc)
delete tilemaps.map;
// Here the map is loaded. During the load time, the game is still.
gbox.addBundle({
file:"resources/tutorial1/bundle-map-"+level.level+".js",
onLoad:function(){ // This "onload" operation is triggered after everything is loaded.
help.finalizeTilemap(tilemaps.map); // Finalize the map into the bundle
gbox.createCanvas("tileslayer",{w:tilemaps.map.w,h:tilemaps.map.h}); // Prepare map's canvas
gbox.blitTilemap(gbox.getCanvasContext("tileslayer"),tilemaps.map); // Render map on the canvas
toys.topview.spawn(gbox.getObject("player","player"),{x:level.x,y:level.y}); // Displace player
tilemaps.map.addObjects(); // Initialize map
if (level.introdialogue) // Eventually starts intro dialogue.
maingame.startDialogue("intro"); // game introduction, if needed
}
});
}
// Game initialization
maingame.initializeGame=function() {
// Prepare hud
// Starting weapons. The following line is "all weapons":
// maingame.hud.setWidget("weapon",{widget:"radio",value:0,tileset:"items",frames:[0,1,2,3,4,5,6,7,8,9],dx:10,dy:10});
// The following line is just the first sword:
maingame.hud.setWidget("weapon",{widget:"radio",value:0,tileset:"items",frames:[0],dx:10,dy:10});
maingame.hud.setWidget("health",{widget:"symbols",tiles:[3,2,1,0],minvalue:0,maxvalue:20,value:12-(maingame.difficulty*4),maxshown:4,tileset:"hud",emptytile:4,dx:40,dy:10,gapx:20,gapy:0});
maingame.hud.setWidget("cash",{widget:"label",font:"small",value:0,minvalue:0,maxvalue:100,dx:gbox.getScreenW()-60,dy:gbox.getScreenH()-24,prepad:3,padwith:" ",clear:true});
maingame.hud.setWidget("SMALLKEY",{widget:"label",font:"small",value:0,minvalue:0,maxvalue:999,dx:gbox.getScreenW()-60,dy:gbox.getScreenH()-43,prepad:3,padwith:" ",clear:true});
maingame.hud.setWidget("BOSSKEY",{widget:"bool",value:0,tileset:"hud",frame:5,dx:gbox.getScreenW()-30,dy:gbox.getScreenH()-66}); // This is shown if value is true or >0
maingame.hud.setWidget("lblkey",{widget:"blit",value:6,tileset:"hud",dx:gbox.getScreenW()-30,dy:gbox.getScreenH()-50});
maingame.hud.setWidget("lblcoin",{widget:"blit",value:7,tileset:"hud",dx:gbox.getScreenW()-30,dy:gbox.getScreenH()-30});
tilemaps={
_defaultblock:100, // The block that is over the borders (a wall)
queststatus:{} // Every step the player does, is marked here (opened doors, sections cleared etc)
};
gbox.addObject({
id:"bg",
group:"background",
blit:function() {
gbox.centerCamera(gbox.getObject("player","player"),{w:tilemaps.map.w,h:tilemaps.map.h});
gbox.blit(gbox.getBufferContext(),gbox.getCanvas("tileslayer"),{dx:0,dy:0,dw:gbox.getScreenW(),dh:gbox.getScreenH(),sourcecamera:true});
}
});
gbox.addObject({
id:"player",
group:"player",
tileset:"player",
zindex:0, // Needed for zindexed objects
stilltimer:0, // is used to block the player while attacking (a la zelda!)
invultimer:0, // Custom attribute. A timer that keep invulnerable.
isPaused:false, // Pauses the player during dialogues, cutscenes etc.
doPause:function(p) {
this.isPaused=p;
},
initialize:function() {
toys.topview.initialize(this,{
haspushing:true,
shadow:{tileset:"shadows",tile:0},
frames:{
standup:{ speed:1, frames:[0] },
standdown:{ speed:1, frames:[3] },
standleft:{ speed:1, frames:[6] },
standright:{ speed:1, frames:[6] },
movingup:{speed:3,frames:[0,1,0,2] },
movingdown:{speed:3,frames:[3,4,3,5] },
movingleft:{speed:3,frames:[6,7] },
movingright:{speed:3,frames:[6,7] },
pushingup:{speed:6,frames:[0,1,0,2] },
pushingdown:{speed:6,frames:[3,4,3,5] },
pushingleft:{speed:6,frames:[6,7] },
pushingright:{speed:6,frames:[6,7] }
}
});
},
collisionEnabled:function() { // Disable collisions when the game is on hold, the player is dead or invulnerable
return !maingame.gameIsHold()&&!this.killed&&!this.invultimer&&!this.isPaused;
},
hitByBullet:function(by) {
if (this.collisionEnabled()) { // If collison are enabled...
maingame.hud.addValue("health","value",-by.power); // Decrease power
if (maingame.hud.getValue("health","value")<=0) // If dead..
this.kill(); // Kill...
else { // Else is just hit
gbox.hitAudio("hurt");
this.accz=-5; // A little jump...
this.invultimer=30; // Stay invulnerable for a while...
this.stilltimer=10; // Stay still for a while...
}
return by.undestructable; // Destroy or not a bullet (decided by the bullet itself)
} else return true; // Bullets are ignored
},
kill:function(by){
gbox.hitAudio("die");
this.frame=8;
this.accz=-8;
maingame.addSmoke(this,"flame-red");
this.killed=true;
maingame.playerDied({wait:50});
},
attack:function() {
// All attack functions, with a switch that differentiates
// behavior by weapon. Weapon ids are pulled from the active
// item in the HUD. Note: this can be extended to items
// that are not necessarily weapons!
gbox.hitAudio("sword");
this.stilltimer=10; // Stay still for a while
this.frame=(this.facing==toys.FACE_UP?9:(this.facing==toys.FACE_DOWN?10:11));
// NOTE: this required a new call in toys.js: getValueByFrame. The previously
// called function, getValue, assumed that the values in .frames would always
// be sequential. Now that weapons can exist in the HUD as [0,3,5] and not
// just [0,1,2], getValue breaks when trying to ascertain the current weapon.
// _currentweapon = maingame.hud.getValue("weapon","value");
_currentweapon = maingame.hud.getValueByFrame("weapon","value");
// FIXME: There is obviously room to optimize the following switch code. :)
switch (_currentweapon) {
case 0: { // Sword, power 1
toys.topview.fireBullet("playerbullets",null,{
power:1, // Custom attribute. Is the damage value of this weapon.
frames:{speed:1,frames:[0,1,2,3]},
fullhit:true, collidegroup:"foes", undestructable:true,
from:this, sidex:this.facing, sidey:this.facing,
tileset:((this.facing==toys.FACE_LEFT)||(this.facing==toys.FACE_RIGHT)?"lefthit":"uphit"),
duration:4, acc:5,
fliph:(this.facing==toys.FACE_RIGHT), flipv:(this.facing==toys.FACE_DOWN),
angle:toys.FACES_ANGLE[this.facing]
});
break;
}
case 2: { // Sword, power 2
toys.topview.fireBullet("playerbullets",null,{
power:2, // Custom attribute. Is the damage value of this weapon.
frames:{speed:1,frames:[0,1,2,3]},
fullhit:true, collidegroup:"foes", undestructable:true,
from:this, sidex:this.facing, sidey:this.facing,
tileset:((this.facing==toys.FACE_LEFT)||(this.facing==toys.FACE_RIGHT)?"lefthit":"uphit"),
duration:4, acc:5,
fliph:(this.facing==toys.FACE_RIGHT), flipv:(this.facing==toys.FACE_DOWN),
angle:toys.FACES_ANGLE[this.facing]
});
break;
}
case 4: { // Sword, power 3
toys.topview.fireBullet("playerbullets",null,{
power:3, // Custom attribute. Is the damage value of this weapon.
frames:{speed:1,frames:[0,1,2,3]},
fullhit:true, collidegroup:"foes", undestructable:true,
from:this, sidex:this.facing, sidey:this.facing,
tileset:((this.facing==toys.FACE_LEFT)||(this.facing==toys.FACE_RIGHT)?"lefthit":"uphit"),
duration:4, acc:5,
fliph:(this.facing==toys.FACE_RIGHT), flipv:(this.facing==toys.FACE_DOWN),
angle:toys.FACES_ANGLE[this.facing]
});
break;
}
case 6: { // Sword, power 5
toys.topview.fireBullet("playerbullets",null,{
power:5, // Custom attribute. Is the damage value of this weapon.
frames:{speed:1,frames:[0,1,2,3]},
fullhit:true, collidegroup:"foes", undestructable:true,
from:this, sidex:this.facing, sidey:this.facing,
tileset:((this.facing==toys.FACE_LEFT)||(this.facing==toys.FACE_RIGHT)?"lefthit":"uphit"),
duration:4, acc:5,
fliph:(this.facing==toys.FACE_RIGHT), flipv:(this.facing==toys.FACE_DOWN),
angle:toys.FACES_ANGLE[this.facing]
});
break;
}
case 8: { // Sword, power 7
toys.topview.fireBullet("playerbullets",null,{
power:7, // Custom attribute. Is the damage value of this weapon.
frames:{speed:1,frames:[0,1,2,3]},
fullhit:true, collidegroup:"foes", undestructable:true,
from:this, sidex:this.facing, sidey:this.facing,
tileset:((this.facing==toys.FACE_LEFT)||(this.facing==toys.FACE_RIGHT)?"lefthit":"uphit"),
duration:4, acc:5,
fliph:(this.facing==toys.FACE_RIGHT), flipv:(this.facing==toys.FACE_DOWN),
angle:toys.FACES_ANGLE[this.facing]
});
break;
}
case 1: { // Arrow, power 1
toys.topview.fireBullet("playerbullets",null,{
power:1, // Custom attribute. Is the damage value of this weapon.
_canhitswitch:true, // Arrows can hit switchs and turn them on
fullhit:true, collidegroup:"foes",
map:tilemaps.map, // Map is specified, since collides with walls
mapindex:"map",
defaulttile:tilemaps._defaultblock,
undestructable:false, // Custom attribute. Is destroyed by the hitted object.
from:this, sidex:this.facing, sidey:this.facing,
tileset:((this.facing==toys.FACE_LEFT)||(this.facing==toys.FACE_RIGHT)?"leftarrow":"uparrow"),
frames:{speed:1,frames:[0,1]},
acc:2, // Arrow speed equals power x 2
fliph:(this.facing==toys.FACE_RIGHT),
flipv:(this.facing==toys.FACE_DOWN),
angle:toys.FACES_ANGLE[this.facing],
spritewalls:"walls",
gapy:8 // Avoid wall collision on start
});
break;
}
case 3: { // Arrow, power 2
toys.topview.fireBullet("playerbullets",null,{
power:2, // Custom attribute. Is the damage value of this weapon.
_canhitswitch:true, // Arrows can hit switchs and turn them on
fullhit:true, collidegroup:"foes",
map:tilemaps.map, // Map is specified, since collides with walls
mapindex:"map",
defaulttile:tilemaps._defaultblock,
undestructable:false, // Custom attribute. Is destroyed by the hitted object.
from:this, sidex:this.facing, sidey:this.facing,
tileset:((this.facing==toys.FACE_LEFT)||(this.facing==toys.FACE_RIGHT)?"leftarrow":"uparrow"),
frames:{speed:1,frames:[0,1]},
acc:4, // Arrow speed equals power x 2
fliph:(this.facing==toys.FACE_RIGHT),
flipv:(this.facing==toys.FACE_DOWN),
angle:toys.FACES_ANGLE[this.facing],
spritewalls:"walls",
gapy:8 // Avoid wall collision on start
});
break;
}
case 5: { // Arrow, power 3
toys.topview.fireBullet("playerbullets",null,{
power:3, // Custom attribute. Is the damage value of this weapon.
_canhitswitch:true, // Arrows can hit switchs and turn them on
fullhit:true, collidegroup:"foes",
map:tilemaps.map, // Map is specified, since collides with walls
mapindex:"map",
defaulttile:tilemaps._defaultblock,
undestructable:false, // Custom attribute. Is destroyed by the hitted object.
from:this, sidex:this.facing, sidey:this.facing,
tileset:((this.facing==toys.FACE_LEFT)||(this.facing==toys.FACE_RIGHT)?"leftarrow":"uparrow"),
frames:{speed:1,frames:[0,1]},
acc:6, // Arrow speed equals power x 2
fliph:(this.facing==toys.FACE_RIGHT),
flipv:(this.facing==toys.FACE_DOWN),
angle:toys.FACES_ANGLE[this.facing],
spritewalls:"walls",
gapy:8 // Avoid wall collision on start
});
break;
}
case 7: { // Arrow, power 5
toys.topview.fireBullet("playerbullets",null,{
power:5, // Custom attribute. Is the damage value of this weapon.
_canhitswitch:true, // Arrows can hit switchs and turn them on
fullhit:true, collidegroup:"foes",
map:tilemaps.map, // Map is specified, since collides with walls
mapindex:"map",
defaulttile:tilemaps._defaultblock,
undestructable:false, // Custom attribute. Is destroyed by the hitted object.
from:this, sidex:this.facing, sidey:this.facing,
tileset:((this.facing==toys.FACE_LEFT)||(this.facing==toys.FACE_RIGHT)?"leftarrow":"uparrow"),
frames:{speed:1,frames:[0,1]},
acc:10, // Arrow speed equals power x 2
fliph:(this.facing==toys.FACE_RIGHT),
flipv:(this.facing==toys.FACE_DOWN),
angle:toys.FACES_ANGLE[this.facing],
spritewalls:"walls",
gapy:8 // Avoid wall collision on start
});
break;
}
case 9: { // Arrow, power 7
toys.topview.fireBullet("playerbullets",null,{
power:7, // Custom attribute. Is the damage value of this weapon.
_canhitswitch:true, // Arrows can hit switchs and turn them on
fullhit:true, collidegroup:"foes",
map:tilemaps.map, // Map is specified, since collides with walls
mapindex:"map",
defaulttile:tilemaps._defaultblock,
undestructable:false, // Custom attribute. Is destroyed by the hitted object.
from:this, sidex:this.facing, sidey:this.facing,
tileset:((this.facing==toys.FACE_LEFT)||(this.facing==toys.FACE_RIGHT)?"leftarrow":"uparrow"),
frames:{speed:1,frames:[0,1]},
acc:14, // Arrow speed equals power x 2
fliph:(this.facing==toys.FACE_RIGHT),
flipv:(this.facing==toys.FACE_DOWN),
angle:toys.FACES_ANGLE[this.facing],
spritewalls:"walls",
gapy:8 // Avoid wall collision on start
});
break;
}
}
},
first:function() {
if (this.stilltimer) this.stilltimer--;
if (this.invultimer) this.invultimer--;
// Counter
this.counter=(this.counter+1)%60;
if (this.stilltimer||maingame.gameIsHold()||this.isPaused||this.killed)
toys.topview.controlKeys(this,{}); // Stays still. No key is moving! :)
else
toys.topview.controlKeys(this,{left:"left",right:"right",up:"up",down:"down"}); // Moves (if not attacking)
toys.topview.handleAccellerations(this);
toys.topview.handleGravity(this); // z-gravity
toys.topview.applyForces(this); // Apply forces
toys.topview.applyGravity(this); // z-gravity
toys.topview.tileCollision(this,tilemaps.map,"map",tilemaps._defaultblock); // tile collisions
toys.topview.floorCollision(this); // Collision with the floor (for z-gravity)
toys.topview.spritewallCollision(this,{group:"walls"}); // Doors and tresaure chests are sprites that acts like a wall.
toys.topview.adjustZindex(this);
if (!this.stilltimer&&!this.killed) toys.topview.setFrame(this); // set the right animation frame (if not attacking)
if (!this.stilltimer&&!this.isPaused&&!maingame.gameIsHold()&&!this.killed)
if (gbox.keyIsHit("a"))
this.attack();
else if (gbox.keyIsHit("b")) {
var ahead=toys.topview.getAheadPixel(this,{distance:5});
ahead.group="walls";
ahead.call="doPlayerAction";
if (!toys.topview.callInColliding(this,ahead)) {// if any action is done
if (maingame.hud.getValue("weapon","frames").length>1)
gbox.hitAudio("default-menu-option");
maingame.hud.addValue("weapon","value",1);
}
}
},
blit:function() {
if ((this.invultimer%2)==0) {
// Shadowed object. First draws the shadow...
gbox.blitTile(gbox.getBufferContext(),{tileset:this.shadow.tileset,tile:this.shadow.tile,dx:this.x,dy:this.y+this.h-gbox.getTiles(this.shadow.tileset).tileh+4,camera:this.camera});
// Then the object. Notes that the y is y+z to have the "over the floor" effect.
gbox.blitTile(gbox.getBufferContext(),{tileset:this.tileset,tile:this.frame,dx:this.x,dy:this.y+this.z,camera:this.camera,fliph:this.fliph,flipv:this.flipv});
}
}
});
};
// Add a video. HIGHLY EXPERIMENTAL. ;)
maingame.addVideo=function(x,y,id) {
gbox.addObject({
isVideo:1, // To tell everyone "I'm a video, not a regular tile".
group:"bonus",
questid:id,
zindex:0,
x:x,
y:y,
initialize:function() {
toys.topview.initialize(this,{
});
},
blit:function() {
//if (gbox.objectIsVisible(this)) {
// var v=this.getVideo(id);
// gbox.blitImageToScreen(gbox._videos["test-video"]);
gbox.blitImageToScreen(gbox._images["test-video"]);
//}
}
});
}
// Add a bonus item. It jumps a while and then disappear.
maingame.addBonus=function(x,y,type,id,expire) {
var frames;
switch (type) {
case "coin": {frames={ standdown:{ speed:2, frames:[0,1,2,3,4,5] } }; break } // Rotating coin
case "arrow": {frames={ standdown:{ speed:3, frames:[6,7] } }; break } // Blinking arrow icon
case "sword": {frames={ standdown:{ speed:2, frames:[10,11] } }; break } // Replacing boss key
case "SMALLKEY": {frames={ standdown:{ speed:3, frames:[8,9] } }; break } // Blinking small key
// case "BOSSKEY": {frames={ standdown:{ speed:3, frames:[10,11] } }; break }
}
gbox.addObject({
group:"bonus",
questid:id,
tileset:"bonus",
zindex:0, // Needed for zindexed objects
x:x,
y:y,
accz:-10, // Bounces
bonustype:type,
expiretime:(expire==null?100:(expire==0?null:expire)),
unpicktime:10,
frames:frames, // You can specify attributes outside from the initialization. Are kept instead the default one.
initialize:function() {
toys.topview.initialize(this,{
shadow:{tileset:"shadows",tile:0}
});
},
first:function() {
if ((this.expiretime!=null)&&this.expiretime) this.expiretime--;
if (this.unpicktime) this.unpicktime--;
if (this.expiretime===0) {
gbox.trashObject(this);
} else if (objectIsAlive(this)) {
// Counter
this.counter=(this.counter+1)%60;
toys.topview.handleAccellerations(this);
toys.topview.handleGravity(this); // z-gravity
toys.topview.applyForces(this); // Apply forces
toys.topview.applyGravity(this); // z-gravity
toys.topview.floorCollision(this,{bounce:2,audiobounce:"beep"}); // Collision with the floor (for z-gravity)
toys.topview.adjustZindex(this); // Set the right zindex
toys.topview.setFrame(this); // set the right animation frame (if not attacking - which has still frame)
if (!this.unpicktime) {
var pl=gbox.getObject("player","player");
if (pl.collisionEnabled()&&(toys.topview.collides(this,pl))) {
gbox.hitAudio("coin");
switch (this.bonustype) {
case "coin": {
maingame.hud.addValue("cash","value",1);
maingame.addQuestClear("1 GOLD");
break;
}
case "sword": {
maingame.addQuestClear(this.questid);
tilemaps.queststatus[this.questid]=true;
_weapons = maingame.hud.getValue("weapon","frames");
switch (this.questid) {
case "sword2x": { _weapons.push(2); break; }
case "sword3x": { _weapons.push(4); break; }
case "sword5x": { _weapons.push(6); break; }
case "sword7x": { _weapons.push(8); break; }
}
_weapons.sort();
maingame.hud.setValue("weapon","frames",_weapons);
break;
}
case "arrow": {
maingame.addQuestClear(this.questid);
tilemaps.queststatus[this.questid]=true;
_weapons = maingame.hud.getValue("weapon","frames");
switch (this.questid) {
case "arrow1x": { _weapons.push(1); break; }
case "arrow2x": { _weapons.push(3); break; }
case "arrow3x": { _weapons.push(5); break; }
case "arrow5x": { _weapons.push(7); break; }
case "arrow7x": { _weapons.push(9); break; }
}
_weapons.sort();
maingame.hud.setValue("weapon","frames",_weapons);
break;
}
case "BOSSKEY":
case "SMALLKEY": {
maingame.addQuestClear(this.bonustype);
if (this.questid) tilemaps.queststatus[this.questid]=true; // Key picked
maingame.hud.addValue(this.bonustype,"value",1); // Add key to inventory
break;
}
}
gbox.trashObject(this);
}
}
}
},
blit:function() {
if (gbox.objectIsVisible(this)) {
// Shadowed object. First draws the shadow...
gbox.blitTile(gbox.getBufferContext(),{tileset:this.shadow.tileset,tile:this.shadow.tile,dx:this.x,dy:this.y+this.h-gbox.getTiles(this.shadow.tileset).tileh+4,camera:this.camera});
if ((this.expiretime>30)||((this.expiretime<30)&&(this.expiretime%2==0)))
// Then the object. Notes that the y is y-z to have the "over the floor" effect.
gbox.blitTile(gbox.getBufferContext(),{tileset:this.tileset,tile:this.frame,dx:this.x,dy:this.y+this.z,camera:this.camera,fliph:this.fliph,flipv:this.flipv});
}
}
});
}
// Changes a tile in the map. It also adds smoke if asked.
maingame.setTileInMap=function(x,y,tile,smoke) {
help.setTileInMap(gbox.getCanvasContext("tileslayer"),tilemaps.map,x,y,tile);
if (smoke) {
var ts=gbox.getTiles(tilemaps.map.tileset);
gbox.hitAudio("explosion"); // Switch sound
maingame.addSmoke({x:x*ts.tilew,y:y*ts.tilew,h:ts.tileh,w:ts.tilew,hh:ts.tilehh,hw:ts.tilehw,camera:true});
}
}
// Add the "QUEST CLEAR" message
maingame.addQuestClear=function(msg) {
if (msg==null) gbox.hitAudio("default-menu-confirm"); // Switch sound
toys.generate.sparks.popupText(gbox.getObject("player","player"),"sparks",null,{font:"big",jump:6,text:(msg==null?"QUEST CLEAR!":msg),keep:20});
}
// Add spreading smoke on an object
maingame.addSmoke=function(ob,color) {
// Since camera is not specified (will be into the initializator), is added on the spark instead from the created object
toys.generate.sparks.simple(ob,"sparks",null,{camera:true,animspeed:2,accy:-3,accx:-3,tileset:(color==null?"flame-white":color)});
toys.generate.sparks.simple(ob,"sparks",null,{camera:true,animspeed:2,accy:-3,accx:3,tileset:(color==null?"flame-white":color)});
toys.generate.sparks.simple(ob,"sparks",null,{camera:true,animspeed:2,accy:3,accx:-3,tileset:(color==null?"flame-white":color)});
toys.generate.sparks.simple(ob,"sparks",null,{camera:true,animspeed:2,accy:3,accx:3,tileset:(color==null?"flame-white":color)});
}
// Add a tresaure chest
maingame.addChest=function(x,y,id,animated,cont,contid,expi) {
var td=gbox.getTiles(tilemaps.map.tileset);
var ob=gbox.addObject({
group:"walls",
tileset:"chest",
zindex:0, // Needed for zindexed objects
x:x*td.tilew,
y:y*td.tileh,
questid:id,
accz:(animated?-10:0), // Bounces
content:cont,
contentid:contid,
expire:expi,
initialize:function() {
toys.topview.initialize(this,{
shadow:{tileset:"shadows",tile:0},
frames:{
standdown:{ speed:1, frames:[0] }
}
});
},
doPlayerAction:function(by) { // When used
if (this.questid!=null) tilemaps.queststatus[this.questid]=true;
gbox.hitAudio("explosion");
maingame.addSmoke(this); // Add a smoke spit
maingame.addBonus(this.x,this.y,this.content,this.contentid,this.expire); // Generate the content bonus
gbox.trashObject(this); // and disappear
},
first:function() {
toys.topview.handleGravity(this); // z-gravity
toys.topview.applyGravity(this); // z-gravity
toys.topview.floorCollision(this,{bounce:2,audiobounce:"beep"}); // Collision with the floor (for z-gravity)
toys.topview.adjustZindex(this); // Set the right zindex
},
blit:function() {
if (gbox.objectIsVisible(this)) {
// Shadowed object. First draws the shadow...
gbox.blitTile(gbox.getBufferContext(),{tileset:this.shadow.tileset,tile:this.shadow.tile,dx:this.x,dy:this.y+this.h-gbox.getTiles(this.shadow.tileset).tileh+4,camera:this.camera});
// Then the object. Notes that the y is y-z to have the "over the floor" effect.
gbox.blitTile(gbox.getBufferContext(),{tileset:this.tileset,tile:this.frame,dx:this.x,dy:this.y+this.z,camera:this.camera,fliph:this.fliph,flipv:this.flipv});
}
}
});
if (animated) maingame.addSmoke(ob);
}
// Add a door
maingame.addDoor=function(id,tileset,x,y,animated,openwith) { // A door constructor. These doors opens shaking and smoking, a la Zelda
var door=toys.topview.makedoor("walls",id,tilemaps.map,{whileMoving:function(){
this.x+=(this.opencounter%2==0?-1:1)
if (this.opencounter%5==0) {
toys.generate.sparks.simple(this,"sparks",null,{alpha:0.7,gapy:this.hh,frames:{speed:4,frames:[3,2,1,2,3]},accy:-help.random(0,4),tileset:"flame-white"});
toys.generate.sparks.simple(this,"sparks",null,{alpha:0.7,gapx:-this.hw/2,gapy:this.hh,frames:{speed:4,frames:[3,2,1,2,3]},accy:-help.random(0,4),accx:-1,tileset:"flame-white"});
toys.generate.sparks.simple(this,"sparks",null,{alpha:0.7,gapx:this.hw/2,gapy:this.hh,frames:{speed:4,frames:[3,2,1,2,3]},accy:-help.random(0,4),accx:1,tileset:"flame-white"});
}
},whenClosed:function() {
this.x++; // Place the door in the right position
},questid:id,openwith:openwith,closing:animated,doorheight:50,fullhit:true,tilex:x,tiley:y,tileset:tileset,audiobefore:"explosion",audioafter:"megaexplosion",frames:{speed:1,frames:[0]}});
if (openwith) {
door.doPlayerAction=function(by) { // When used
if (maingame.hud.getValue(this.openwith,"value")>0) {
if (this.questid!=null) tilemaps.queststatus[this.questid]=true; // Mark this door as opened
maingame.hud.addValue(this.openwith,"value",-1);
this.doOpen();
gbox.hitAudio("default-menu-confirm");
maingame.addQuestClear(openwith+" USED");
} else {
gbox.hitAudio("beepbad");
maingame.addQuestClear("NEEDS "+openwith);
}
}
}
}
// Starts a dialogue
maingame.startDialogue=function(id,pause) {
if ((maingame.difficulty==0)||(!dialogues[id].istutorial)) { // dialogues marked as tutorial are shown only on easy. This flag is in the dialogue itself.
gbox.addObject({
group:"foreground",
id:"dialogue",
dialogueToRead:id,
pause:1+(pause==null?0:1), // Pauses a dialog for a while. Is important to wait a frame very time to cancel the last "b" key press (for interacting, for example)
initialize:function() {
gbox.getObject("player","player").doPause(true); // First pause the player
},
blit:function() {
if (this.pause)
this.pause--;
else if (toys.dialogue.render(this,"dialogue",dialogues[this.dialogueToRead])) { // If the dialogue is ended
if (dialogues[this.dialogueToRead].endgame) // If the dialogue is marked by "endgame"...
maingame.gameIsCompleted(); // The game is completed
else
gbox.getObject("player","player").doPause(false); // Unpause the player
gbox.trashObject(this); // Trash the dialogue itself.
}
}
});
}
}
// Add a still object. Are sprites that supports the z-index (houses, trees.) You can walk around these objects
maingame.addBlock=function(x,y,tileset,frame) {
gbox.addObject({
group:"walls",
tileset:tileset,
zindex:0, // Needed for zindexed objects
x:x,
y:y,
frame:frame,
initialize:function() {
toys.topview.initialize(this); // Any particular initialization. Just the auto z-index
},
blit:function() {
if (gbox.objectIsVisible(this)) {
// Then the object. Notes that the y is y-z to have the "over the floor" effect.
gbox.blitTile(gbox.getBufferContext(),{tileset:this.tileset,tile:this.frame,dx:this.x,dy:this.y+this.z,camera:this.camera,fliph:this.fliph,flipv:this.flipv});
}
}
});
}
// Add a still object. Are sprites that supports the z-index (houses, trees.) You can walk around these objects
maingame.addPuzzleblock=function(x,y,tileset,frame) {
gbox.addObject({
group:"walls",
tileset:tileset,
zindex:0, // Needed for zindexed objects
x:x,
y:y,
frame:frame,
initialize:function() {
toys.topview.initialize(this); // Any particular initialization. Just the auto z-index
},
first:function() {
// GDK: ripped off from move enemy function (and broke the shit out of it!)
// First, set the object to react if force is applied, by using the topview.applyForces function.
if (!this.stilltimer) toys.topview.applyForces(this); // Apply forces
// Without handleAccellerations, the object, once given an accx or accy value, would go
// right off the screen. Adding this function slows it down every turn until it stops.
toys.topview.handleAccellerations(this);
// Not used yet
//toys.topview.handleGravity(this); // z-gravity
//toys.topview.applyGravity(this); // z-gravity
// This works for collisions on the map level.
toys.topview.tileCollision(this,tilemaps.map,"map",100); // tile collisions
// GDK: WHY DOES THE FOLLOWING LINE MAKE THE BOX DISAPPEAR AT STARTUP???
// toys.topview.spritewallCollision(this,{group:"walls"}); // walls collisions
//toys.topview.floorCollision(this); // Collision with the floor (for z-gravity)
//toys.topview.adjustZindex(this); // Set the right zindex
//if (!this.stilltimer) toys.topview.setFrame(this); // set the right animation frame (if not attacking - which has still frame)
// Now, figure out if there's a collision -- and if there is, create forces
// that apply to the object on the next clock cycle.
var pl=gbox.getObject("player","player");
if (pl.collisionEnabled()&&(toys.topview.collides(this,pl))) {
gbox.hitAudio("coin");
this.accx = pl.accx*4;
this.accy = pl.accy*4;
// setting accx and accy works, so long as
// rather than just setting x and y directly, but getting closer!
}
},
blit:function() {
if (gbox.objectIsVisible(this)) {
// Then the object. Notes that the y is y-z to have the "over the floor" effect.
gbox.blitTile(gbox.getBufferContext(),{tileset:this.tileset,tile:this.frame,dx:this.x,dy:this.y+this.z,camera:this.camera,fliph:this.fliph,flipv:this.flipv});
}
}
});
}
// Add a npc (Not Playing Charachter)
maingame.addNpc=function(x,y,still,dialogue,questid,talking,silence) {
// An easy way to create an NPC.
gbox.addObject({
questid:questid,
group:"walls",
tileset:"npc",
zindex:0, // Needed for zindexed objects
x:x,
y:y,
myDialogue:dialogue,
iamTalking:false,
silence:silence,
shadow:{tileset:"shadows",tile:0},
frames:{
still:{ speed:6, frames:still },
talking:{ speed:6, frames:(talking==null?still:talking) }
},
doPlayerAction:function(sw) {
if (this.silence) toys.generate.audio.fadeOut(this,"background",null,{channel:"bgmusic"});
this.iamTalking=true; // go in talking mode
maingame.startDialogue(this.myDialogue); // Starts its dialogue. Is another object because of z-index
},
initialize:function() {
toys.topview.initialize(this); // Any particular initialization. Just the auto z-index
},
first:function(by) {
this.counter=(this.counter+1)%12;
if (this.iamTalking) {
this.frame=help.decideFrame(this.counter,this.frames.talking);
if (!gbox.getObject("foreground","dialogue")) {// Check if the dialogue ended
this.iamTalking=false; // Stop talking
if ((this.questid!=null)&&(!tilemaps.queststatus[this.questid])) {
tilemaps.queststatus[this.questid]=true; // If related to a quest, the quest is marked as done
maingame.addQuestClear();
}
}
} else
this.frame=help.decideFrame(this.counter,this.frames.still);
},
blit:function() {
if (gbox.objectIsVisible(this)) {
// Shadowed object. First draws the shadow...
gbox.blitTile(gbox.getBufferContext(),{tileset:this.shadow.tileset,tile:this.shadow.tile,dx:this.x,dy:this.y+this.h-gbox.getTiles(this.shadow.tileset).tileh+4,camera:this.camera});
// Then the object. Notes that the y is y-z to have the "over the floor" effect.
gbox.blitTile(gbox.getBufferContext(),{tileset:this.tileset,tile:this.frame,dx:this.x,dy:this.y+this.z,camera:this.camera,fliph:this.fliph,flipv:this.flipv});
}
}
});
}
// Add an enemy
maingame.addEnemy=function(id,type,x,y,h,s,cloud) {
var td=gbox.getTiles(tilemaps.map.tileset);
var ob;
switch (type) {
case "eyeswitch": { // The classic eye-shaped switch
ob=gbox.addObject({
questid:id,
group:"foes",
tileset:"npc",
zindex:0, // Needed for zindexed objects
x:x*td.tilew,
y:y*td.tileh,
switchedon:false,
frame:0,
changeSwitch:function(sw) {
this.switchedon=(sw?true:false); // The switch is on
this.frame=(sw?1:0); // Change image
if (this.questid!=null) tilemaps.queststatus[this.questid]=(sw?true:false); // Mark the quest as done
},
initialize:function() {
toys.topview.initialize(this); // Any particular initialization. Just the auto z-index
},
hitByBullet:function(by) {
if (by._canhitswitch&&!this.switchedon) { // if is hit by bullet
gbox.hitAudio("default-menu-option");
this.changeSwitch(true);
maingame.addQuestClear(); // Say "quest clear"
}
},
blit:function() {
if (gbox.objectIsVisible(this)) {
// Then the object. Notes that the y is y-z to have the "over the floor" effect.
gbox.blitTile(gbox.getBufferContext(),{tileset:this.tileset,tile:this.frame,dx:this.x,dy:this.y+this.z,camera:this.camera,fliph:this.fliph,flipv:this.flipv});
}
}
});
break;
}
case "octo": {
ob=gbox.addObject({
id:id,
group:"foes",
tileset:"foe1",
zindex:0, // Needed for zindexed objects
invultimer:0, // Custom attribute. A timer that keep invulnerable.
stilltimer:0, // Custom attribute. A timer that keep the enemy still.
x:x*td.tilew,
y:y*td.tileh,
maxacc:s,
controlmaxacc:s,
initialize:function() {
toys.topview.initialize(this,{
health:h,
speed:s,
shadow:{tileset:"shadows",tile:0},
frames:{
standup:{ speed:1, frames:[1] },
standdown:{ speed:1, frames:[3] },
standleft:{ speed:1, frames:[4] },
standright:{ speed:1, frames:[4] },
movingup:{speed:3,frames:[0,1] },
movingdown:{speed:3,frames:[2,3] },
movingleft:{speed:3,frames:[4,5] },
movingright:{speed:3,frames:[4,5] }
}
});
},
kill:function(by){
gbox.hitAudio("hurt");
toys.generate.sparks.simple(this,"sparks",null,{animspeed:2,accy:-3,tileset:"flame-blue"});
toys.generate.sparks.simple(this,"sparks",null,{animspeed:1,accx:-3,tileset:"flame-blue"});
toys.generate.sparks.simple(this,"sparks",null,{animspeed:1,accx:3,tileset:"flame-blue"});
// if (help.random(0,2)==0) maingame.addBonus(this.x,this.y,"coin"); // reward with a coin, sometime
maingame.addBonus(this.x,this.y,"coin"); // reward with a coin every time, don't be stingy :)
tilemaps.queststatus[this.id]=true; // add this monster to the killed list
gbox.trashObject(this); // Vanish!
},
attack:function() {