-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
1746 lines (1685 loc) · 82.3 KB
/
main.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
/***********************************************************************
*
* TheLastCrusade -- developed for HarkTheSound.org by Robert Overman
*
* From the original Win version:
* www.cs.unc.edu/Research/assist/et/projects/RPG/TheLastCrusade.htm
*
***********************************************************************/
/* @TODO: Sell back items -> need for primary and secondary weapons??*/
dojo.provide('main');
dojo.require('dojo.parser');
dojo.require('dojo.hash');
dojo.require('dojox.timing');
dojo.require('widgets.map');
dojo.require('widgets.player');
/////////////////////////////////
dojo.require("dijit.form.Slider");
//////////////////////////////////
dojo.declare('main', null, {
//@TODO: weird fadechannel bug
constructor: function() {
dojo.global.WEAPON = 0;
dojo.global.ARMOR = 1;
dojo.global.POTION = 2;
dojo.global.GOLD = 3;
dojo.global.SPECIAL = 4;
//state
//@TODO: All the Y/N states should probably be rolled into
//one Y/N state with a callback
this.sOff = 0;
this.sMenu = 1;
this.sMove = 2;
this.sFight = 3;
this.sRun = 4;
this.sListen = 5;
this.sAddItem = 6;
this.sPotionCycle = 7;
this.sPotionChoice = 8;
this.sVendor = 9;
this.sVendorScroll = 10;
this.sBuy = 11;
this.sFriend = 12;
this.sLepEncounter = 13;
this.sLepGame = 14;
this.sLepAgain = 15;
this.sPaused = 16;
//---//
this.gameOver = false;
this.state = this.sOff;
this.tempState = -1;
this.potentialItems = new Array(); //items to ask player if he/she wants
this.start = true;
this.firstFriend = true;
this.tempItem = null;
this.skipVendors = false;
this.skipFriends = false;
this.skipLep = false;
this._mapIndex = 0;
this.map = null;
this.enemy = null;
this.vendor = null;
this.friend = null;
this.enemyData = null;
this.vendorData = null;
this.friendData = null;
this.lepArray = new Array(3);
this.lepData = null;
this.keyDelay = 0;
this.directions = dojo.byId("directions");
this.offerSaying ="";
//handles//
this.pauseHandle = null;
this.prefsHandle = null;
//-------//
this._readingInstructions = false;
this.duringMove = false;
this.potionIndex = 0;
this.itemIndex = 0;
//messages to display with corresponding state
var stateHandle = dojo.subscribe("stateStatus", dojo.hitch(this, function(message){
switch (message){
case this.sOff:
this.directions.innerHTML = "";
break;
case this.sMenu:
this.directions.innerHTML = "1: New Game <br> 2: Resume Saved Game <br> 3: Instructions For Playing";
break;
case this.sMove:
this.directions.innerHTML = "Up Arrow: Move North <br> Down Arrow: Move South <br> Right Arrow: Move East <br> Left Arrow: Move West <br> S: Search Location for Items <br> D: Query Directions <br> P: Use Potion";
break;
case this.sFight:
this.directions.innerHTML = "A: Attack <br> R: Attempt to Run Away <br> P: Use Potion <br> Q: Query Enemy ";
break;
case this.sRun:
this.directions.innerHTML = "Up Arrow: Yes, run away <br> Down Arrow: No, stay and fight";
break;
case this.sListen:
this.directions.innerHTML = "F: Skip";
break;
case this.sAddItem:
this.directions.innerHTML = "Up Arrow: Yes, add item <br> Down Arrow: No, discard item";
break;
case this.sPotionCycle:
this.directions.innerHTML = "Use the left and right arrow keys to cycle through the potions <br> Press Escape to cancel";
break;
case this.sPotionChoice:
this.directions.innerHTML = "Up Arrow: Accept, use potion <br> Left/Right Arrow: Choose Another <br> Press Escape to cancel";
break;
case this.sVendor:
this.directions.innerHTML = "Up Arrow: Yes check out inventory <br> Down Arrow: No, I don't want to buy anything";
break;
case this.sVendorScroll:
this.directions.innerHTML = "Up Arrow: Buy item <br> Left/Right Arrow: Choose Another <br> Escape: Return to game";
break;
case this.sBuy:
this.directions.innerHTML = "Up Arrow:: Yes, buy item <br> Down Arrow: No, continue looking";
break;
case this.sFriend:
this.directions.innerHTML = "Up Arrow: Talk to friend <br> Down Arrow: Do not talk to friend";
break;
case this.sLepEncounter:
this.directions.innerHTML = "Up Arrow: Play the leprechaun's game <br> Down Arrow: Refuse to play";
break;
case this.sLepGame:
this.directions.innerHTML = "Press 1,2 or 3 to play <br> Press 'A' to attempt to kill the leprechaun.";
break;
case this.sLepAgain:
this.directions.innerHTML = "Up Arrow: Play again <br> Down Arrow: Don't play again";
break;
}
}));
var def = uow.getAudio({defaultCaching: true}); //get JSonic
def.then(dojo.hitch(this, function(audio) {
this._audio = audio;
///////////////////////REMOVE AFTER MOVING TO HARK SITE/////////
var slider = new dijit.form.HorizontalSlider({
name: "master",
value: 1,
minimum: 0,
maximum: 1,
intermediateChanges: true,
style: "width:300px;",
onChange: dojo.hitch(this, function(val) {
this._audio.setProperty({name : 'volume', value: val, channel : 'music', immediate : true});
this._audio.setProperty({name : 'volume', value: val, channel : 'sound', immediate : true});
this._audio.setProperty({name : 'volume', value: val, channel : 'speech', immediate : true});
if(this.map){
this.map.changeVolume(val);
}
if(this.player){
this.player.changeVolume(val);
}
})
},
"master");
var slider = new dijit.form.HorizontalSlider({
name: "speechRate",
value: 150,
minimum: 1,
maximum: 300,
intermediateChanges: true,
style: "width:300px;",
onChange: dojo.hitch(this,function(val) {
this._audio.setProperty({name : 'rate', value: Math.floor(val), channel : 'speech', immediate : true});
if(this.player){
this.player.changeRate(val);
}
})
},
"speechRate");
var slider = new dijit.form.HorizontalSlider({
name: "speechVol",
value: 1,
minimum: 0,
maximum: 1,
intermediateChanges: true,
style: "width:300px;",
onChange: dojo.hitch(this,function(val) {
this._audio.setProperty({name : 'volume', value: val, channel : 'speech', immediate : true});
if(this.player){
this.player.changeVolume(val);
}
})
},
"speechVol");
var slider = new dijit.form.HorizontalSlider({
name: "soundVol",
value: 1,
minimum: 0,
maximum: 1,
intermediateChanges: true,
style: "width:300px;",
onChange: dojo.hitch(this,function(val) {
this._audio.setProperty({name : 'volume', value: val, channel : 'sound', immediate : true});
})
},
"soundVol");
var slider = new dijit.form.HorizontalSlider({
name: "music",
value: 1,
minimum: 0,
maximum: 1,
intermediateChanges: true,
style: "width:300px;",
onChange: dojo.hitch(this,function(val) {
if(this.map){
this.map.changeVolume(val);
}
this._audio.setProperty({name : 'volume', value: val, channel : 'music', immediate : true});
})
},
"music");
////////////////////////////////////////////////////////////////
this._initSounds();
dojo.connect(dojo.global, 'onkeyup', dojo.hitch(this, '_removeKeyDownFlag'));
dojo.connect(dojo.global, 'onkeydown', dojo.hitch(this, '_analyzeKey'));
this.pauseHandle = dojo.subscribe('/org/hark/pause', this.pauseCallback);
this.prefsHandle = dojo.subscribe('/org/hark/prefs/response', this.prefsCallback);
this._keyHasGoneUp = true;
this._start();
}));
},
////////////////--------Hark Integration----------------////////////
pauseCallback: function(paused){
/*if(paused){
this.tempState = this.state;
this.setState(this.sOff);
//turn off sound
this._audio.setProperty({name : 'volume', value: 0, channel : 'music', immediate : true});
this._audio.setProperty({name : 'volume', value: 0, channel : 'sound', immediate : true});
this._audio.setProperty({name : 'volume', value: 0, channel : 'speech', immediate : true});
if(this.map){
this.map.changeVolume(0);
}
if(this.player){
this.player.changeVolume(0);
}
}
else{
if(this.tempState == -1){}
else{
this.setState(this.tempState);
this.tempState = -1;
//turn on sound
dojo.publish('/org/hark/prefs/request');
}
}*/
},
prefsCallback: function(prefs, which){
/*console.log("Prefs: ", prefs, " Which: ", which);
if(prefs == null){}
else if(which == null){ //do everything
this._audio.setProperty({name : 'rate', value: Math.floor(prefs.speechRate), channel : 'speech', immediate : true});
if(this.player){
this.player.changeRate(prefs.speechRate);
}
this._audio.setProperty({name : 'volume', value: prefs.volume, channel : 'music', immediate : true});
this._audio.setProperty({name : 'volume', value: prefs.volume, channel : 'sound', immediate : true});
this._audio.setProperty({name : 'volume', value: prefs.volume, channel : 'speech', immediate : true});
if(this.map){
this.map.changeVolume(prefs.volume);
}
if(this.player){
this.player.changeVolume(prefs.volume);
}
this._audio.setProperty({name : 'volume', value: prefs.speechVolume, channel : 'speech', immediate : true});
if(this.player){
this.player.changeVolume(prefs.speechVolume);
}
this._audio.setProperty({name : 'volume', value: prefs.soundVolume, channel : 'sound', immediate : true});
if(this.map){
this.map.changeVolume(prefs.musicVolume);
}
this._audio.setProperty({name : 'volume', value: prefs.musicVolume, channel : 'music', immediate : true});
}
else if(which == "mouseEnabled"){
}
else if(which == "speechRate"){
this._audio.setProperty({name : 'rate', value: Math.floor(prefs.speechRate), channel : 'speech', immediate : true});
if(this.player){
this.player.changeRate(prefs.speechRate);
}
}
else if(which == "volume"){
this._audio.setProperty({name : 'volume', value: prefs.volume, channel : 'music', immediate : true});
this._audio.setProperty({name : 'volume', value: prefs.volume, channel : 'sound', immediate : true});
this._audio.setProperty({name : 'volume', value: prefs.volume, channel : 'speech', immediate : true});
if(this.map){
this.map.changeVolume(prefs.volume);
}
if(this.player){
this.player.changeVolume(prefs.volume);
}
}
else if(which == "speechVolume"){
this._audio.setProperty({name : 'volume', value: prefs.speechVolume, channel : 'speech', immediate : true});
if(this.player){
this.player.changeVolume(prefs.speechVolume);
}
}
else if(which == "soundVolume"){
this._audio.setProperty({name : 'volume', value: prefs.soundVolume, channel : 'sound', immediate : true});
}
else if(which == "musicVolume"){
if(this.map){
this.map.changeVolume(prefs.musicVolume);
}
this._audio.setProperty({name : 'volume', value: prefs.musicVolume, channel : 'music', immediate : true});
}
* */
},
////////////////////////////////////////////////////////////////////
/*******************************************************************
*
* Resets all boolean values/other game data for restarting the game
* for when player killed for example
*
******************************************************************/
_gameReset: function(){
this.state = this.sOff;
this.potentialItems = new Array(); //items to ask player if he/she wants
this.start = true;
this.firstFriend = true;
this.tempItem = null;
this.gameOver = false;
this.skipVendors = false;
this.skipFriends = false;
this.skipLep = false;
this._mapIndex = 0;
this.map = null;
this.enemy = null;
this.vendor = null;
this.friend = null;
this.enemyData = null;
this.vendorData = null;
this.friendData = null;
this.player = null;
this.lepArray = new Array(3);
this.lepData = null;
this.keyDelay = 0;
this.offerSaying ="";
this._readingInstructions = false;
this.duringMove = false;
this.potionIndex = 0;
this.itemIndex = 0;
},
/*******************************************************************
*
* begin gameplay
*
******************************************************************/
_start: function(){
var sRate = 1;
this.mapList = ["forest.json", "graveyard.json", "castle.json"];
this._audio.getProperty({name:'rate', channel: 'speech'})
.anyAfter(dojo.hitch(this,function(rate){
sRate = rate;
this._audio.getProperty({name:'volume', channel: 'speech'})
.anyAfter(dojo.hitch(this,function(volume){
var ad = {rate:sRate, volume: volume};
this.player = new widgets.player({audioData: ad}, null);
this._audio.play({url: 'sounds/general/' + this.title, channel:'sound'});
this._audio.setProperty({name: 'loop', channel: 'music', value: true});
this._audio.play({url: "sounds/general/"+ this.theme, channel: 'music'});
this._audio.play({url: "sounds/general/" + this.menu, channel: 'sound'});
this.setState(this.sMenu);
}));
}));
},
/*******************************************************************
*
* Load the map and visit the first node. Also sets up how to
* handle game widget destruction
*
******************************************************************/
_loadMap: function(fileName) {
var mapHandle = dojo.subscribe("mapStatus", dojo.hitch(this, function(message){
if (message == "mapDestroy") {
dojo.unsubscribe(mapHandle);
this._mapIndex++;
if(this._mapIndex==this.mapList.length)
{
//done with game, do something
console.log("Finished all games");
}
else
{
this._loadMap(this.mapList[this._mapIndex]);
}
}
}));
var file = fileName;
var mapRequest = {
url : "games/" + file,
handleAs : 'json',
preventCache: true,
error: function(error) {console.log(error);}
};
var dataDef = dojo.xhrGet(mapRequest);
dataDef.addCallback(dojo.hitch(this, function(data) {
this._audio.getProperty({name:'volume', channel:'music'})
.anyAfter(dojo.hitch(this,function(volume){
this.map = new widgets.map({mapData: data, audioData: {volume: volume}}, null);
if(this.start){
this.start = false;
}
else{
this.player.equipWeakItems(this.map);
}
this._audio.say({text: "You are now entering the " + this.map.Name, channel: 'speech'})
.anyAfter(dojo.hitch(this,function(){
this.setState(this.sMove);
this.exploreNode();
}));
}));
}));
},
/*******************************************************************
*
* Remove key down flag. Checking key down flag prevents streaming
* requests to server from holding down a key
*
******************************************************************/
_removeKeyDownFlag: function() {
if (this.keyDelayTimer && this.keyDelayTimer.isRunning){} //do nothing
else{
this.keyDelayTimer = new dojox.timing.Timer(this.keyDelay*1000);
this.keyDelayTimer.onTick = dojo.hitch(this, function() {
this.keyDelayTimer.stop(); //prevent from running again.
this._keyHasGoneUp = true;
});
this.keyDelayTimer.start();
}
},
/*******************************************************************
*
* setup all sound names from original game, most will eventually
* be deleted
*
******************************************************************/
_initSounds: function(){
this.theme = "main_theme";
this.title = "title";
// Story
this.story = "introduction";
this.ending = "end_king";
// Menu
this.menu = "menu";
// Directions
this.dirChar = "dir_characters";
this.dirEnem = "dir_enemy";
this.dirItem = "dir_items";
this.dirLoca = "dir_location";
this.dirSave = "dir_save";
this.dirSkip = "dir_skip";
this.dirSpac = "dir_spacebar";
this.dirQuit = "dir_quit";
// Battle
this.fightsong = "fight";
// Leprechaun
this.lepplay = "play_lep";
this.lepmore = "more_lep";
this.lepgone = "lepgone";
this.leprules = "rules_lep";
this.lep123 = "123_lep";
this.lepwin = "pwin_lep";
this.leplose = "plose_lep";
this.lepagain = "again_lep";
this.lepbye = "goodbye_lep";
this.lepgood = "noplaygood_lep";
this.lepmad = "noplaymad_lep";
this.leplive = "survive_lep";
this.lepdie = "death_lep";
// Misc
this.instruct = "g_instruc";
this.equip = "equip";
},
/*******************************************************************
*
* Analyze user input given the state of the game
*
******************************************************************/
_analyzeKey: function(evt){
if (this._keyHasGoneUp) {
this._keyHasGoneUp = false;
switch(this.state){
case this.sOff:
break;
case this.sMove:
var result;
switch(evt.keyCode){
case dojo.keys.DOWN_ARROW:
this.setState(this.sOff);
this.player.stopAudio();
evt.preventDefault();
result = this.map.move(this.map.SOUTH, this.player);
this.moveResult(result);
break;
case dojo.keys.LEFT_ARROW:
this.setState(this.sOff);
this.player.stopAudio();
evt.preventDefault();
result = this.map.move(this.map.WEST, this.player);
this.moveResult(result);
break;
case dojo.keys.RIGHT_ARROW:
this.setState(this.sOff);
this.player.stopAudio();
evt.preventDefault();
result = this.map.move(this.map.EAST, this.player);
this.moveResult(result);
break;
case dojo.keys.UP_ARROW:
this.setState(this.sOff);
this.player.stopAudio();
evt.preventDefault();
result = this.map.move(this.map.NORTH, this.player);
this.moveResult(result);
break;
case dojo.keys.SPACE:
this.setState(this.sOff);
this.fadeChannel('music');
this._stopAudio();
this.player.readStats();
//don't need to wait through all of stats
this.setState(this.sMove);
break;
case 83: //search for items
this.setState(this.sOff);
this._stopAudio();
var def = this.examineItems(this.map.nodes[this.map.currentNodeIndex].Items, "You found ");
def.then(dojo.hitch(this,function(result){
if(result.found){
//empty item array
this.map.nodes[this.map.currentNodeIndex].Items = new Array();
this.setState(this.sMove);
}
else{
this._audio.say({text:"You did not find any items.", channel:'speech'});
this.setState(this.sMove);
}
}));
break;
case 68: //query directions
this.setState(this.sOff);
this._stopAudio();
var directions = this.map.queryDirections();
var stringArr = new Array();
if(directions[0]!= -1){
stringArr.push(" north,");
}
if(directions[1]!= -1){
stringArr.push(" south, ");
}
if(directions[2]!= -1){
stringArr.push(" east, ");
}
if(directions[3]!= -1){
stringArr.push(" west, ");
}
if(stringArr.length == 1){
this._audio.say({text: "The " + stringArr[0] + " passage is open.", channel: 'speech'});
}
else{
this._audio.say({text: " The ", channel: 'speech'});
dojo.forEach(stringArr, dojo.hitch(this,function(dir, idx){
if(idx == (stringArr.length -1)){
this._audio.say({text: " and ", channel: 'speech'});
}
this._audio.say({text: dir, channel: 'speech'});
}));
this._audio.say({text:" passages are open.", channel: 'speech'});
}
this.setState(this.sMove);
break;
case 80: //P: use potion if available
this.setState(this.sOff);
this.player.stopAudio();
if(this.player.potions.length != 0){
this._audio.say({text:"Use the left and right arrow keys to cycle through the potions. Press the up arrow to select a potion and escape to cancel.", channel: "speech"});
this.duringMove = true;
this.setState(this.sPotionCycle);
}
else{
this._audio.say({text:"You do not have any potions.", channel: "speech"})
.anyAfter(dojo.hitch(this,function(){
this.setState(this.sMove);
}));
}
break;
}
break;
case this.sMenu:
switch(evt.keyCode){
// 3 to read the menu
case dojo.keys.NUMPAD_3:
case 51:
this.setState(this.sOff);
this.fadeChannel('sound');
this.readMenu();
break;
// 1 to start new game
case dojo.keys.NUMPAD_1:
case 49:
this.setState(this.sOff);
var d1 = this.fadeChannel('music');
d1.then(dojo.hitch(this, function(){
var def3 = this.fadeChannel('sound')
return def3;
})).then(dojo.hitch(this, function(){
this.setState(this.sListen);
this._audio.play({url: 'sounds/general/' + this.story, channel: 'speech'})
.anyAfter(dojo.hitch(this, function(){
//state is changed again in load map to move
this._loadMap(this.mapList[this._mapIndex]);
}));
}));
break;
}
break;
case this.sFight:
switch(evt.keyCode){
case 65: // A attack
this.setState(this.sOff);
this.player.stopAudio();
//player attack
var def = this.playerAttack();
def.then(dojo.hitch(this, function(result){
if(result.vanquished){
var def2 = this.examineItems(this.enemy.Items, "You recovered ");
def2.then(dojo.hitch(this,function(){
this.map.removeNPC(this.enemyData[1]);
this.enemy = null;
if(this.gameOver){
this._endGame();
}
else{
this.setState(this.sMove);
this.map.visitCurrentNode();
}
}));
}
else{
var def = this.enemyAttack();
def.then(dojo.hitch(this,function(){
this.setState(this.sFight);
}));
}
}));
break;
case 82: //R run away
this.setState(this.sOff);
this.player.stopAudio();
var randZeroTo99=Math.floor(Math.random()*100);
if(randZeroTo99 > this.enemy.RunPerc){ //fail
this._audio.say({text: "Your attempt to run away has failed. You must continue to fight the " + this.enemy.Name, channel:'speech'})
.anyAfter(dojo.hitch(this, function(){
var def = this.enemyAttack();
def.then(dojo.hitch(this,function(){
this.setState(this.sFight);
}));
}));
}
else{//success
this.fadeChannel("music");
this._audio.say({text: "You have abandoned the fight and returned to your previous location.", channel:'speech'});
//restore enemy health if you run away
this.enemy.HP = this.enemy.MaxHP;
this.enemy = null;
var def = this.map.returnPrevious();
def.then(dojo.hitch(this,function(){
this.setState(this.sMove);
}));
}
break;
case 80: //P: use potion if available
this.setState(this.sOff);
this.player.stopAudio();
if(this.player.potions.length != 0){
this._audio.say({text:"Use the left and right arrow keys to cycle through the potions. Press the up arrow to select a potion and escape to cancel.", channel: "speech"});
this.setState(this.sPotionCycle);
}
else{
this._audio.say({text:"You do not have any potion.", channel: "speech"})
.anyAfter(dojo.hitch(this,function(){
this.setState(this.sFight);
}));
}
break;
case 81: //Q
this.setState(this.sOff);
this.player.stopAudio();
this._queryEnemy();
this.setState(this.sFight);
break;
}
break;
case this.sRun:
switch(evt.keyCode){
case dojo.keys.UP_ARROW: //Y
this.setState(this.sOff);
this._audio.stop({channel: "speech"});
var randZeroTo99=Math.floor(Math.random()*100);
if(randZeroTo99 > this.enemy.RunPerc){ //fail
this._audio.say({text: "Your attempt to run away has failed. You must now fight the " + this.enemy.Name, channel:'speech'})
.anyAfter(dojo.hitch(this, function(){
var def = this.enemyAttack();
def.then(dojo.hitch(this,function(){
this.setState(this.sFight);
}));
}));
}
else{//success
this.fadeChannel("music");
this._audio.say({text: "You have avoided the " + this.enemy.Name + " and returned to your previous location.", channel:'speech'});
this.enemy = null;
var def = this.map.returnPrevious();
def.then(dojo.hitch(this,function(){
this.setState(this.sMove);
}));
}
break;
case dojo.keys.DOWN_ARROW: //N
this.setState(this.sOff);
this._audio.stop({channel: "speech"});
//enemy should also attack
this._audio.say({text: "You have chosen to stand your ground.", channel:'speech'})
.anyAfter(dojo.hitch(this, function(){
var def = this.enemyAttack();
def.then(dojo.hitch(this,function(){
this.setState(this.sFight);
}));
}));
break;
}
break;
case this.sListen:
switch(evt.keyCode){
// 'f' to stop sounds on main
case 70:
this.setState(this.sOff);
this._stopAudio();
if(this._readingInstructions){
this._readingInstructions = false;
this._audio.play({url: "sounds/general/"+ this.theme, channel:'music'});
this.setState(this.sMenu);
}
break;
}
break;
case this.sAddItem:
switch(evt.keyCode){
case dojo.keys.UP_ARROW: //Y
this.setState(this.sOff);
this._audio.play({url: "sounds/general/"+ this.equip, channel: 'sound'});
this._audio.say({text: this.tempItem.iName + " equipped.", channel: 'speech'});
this.player.addItem(this.tempItem);
this.tempItem = null;
this.offerItems();
break;
case dojo.keys.DOWN_ARROW: //N
this.setState(this.sOff);
this.tempItem = null;
this.offerItems();
break;
}
break;
case this.sPotionCycle:
var move = false;
switch(evt.keyCode){
case dojo.keys.LEFT_ARROW:
this.setState(this.sOff);
this._audio.stop({channel:'speech'});
move = true;
if(--this.potionIndex < 0){
this.potionIndex = (this.player.potions.length - 1);
}
break;
case dojo.keys.RIGHT_ARROW:
this.setState(this.sOff);
this._audio.stop({channel:'speecj'});
move = true;
if(++this.potionIndex >= this.player.potions.length){
this.potionIndex = 0;
}
break;
case dojo.keys.ESCAPE:
this.setState(this.sOff);
this._audio.stop({channel:'speech'});
this.player.stopAudio();
if(this.duringMove){
this.duringMove = false;
this.setState(this.sMove);
}
else{
this.setState(this.sFight);
}
break;
}
if(move){
this._audio.say({text: this.player.potions[this.potionIndex].iName + "level " + this.player.potions[this.potionIndex].iValue, channel: "speech"});
this.setState(this.sPotionChoice);
}
break;
case this.sPotionChoice:
switch(evt.keyCode){
case dojo.keys.LEFT_ARROW:
this.setState(this.sOff);
this._audio.stop({channel:'speech'});
if(--this.potionIndex < 0){
this.potionIndex = (this.player.potions.length - 1);
}
this._audio.say({text: this.player.potions[this.potionIndex].iName + "level " + this.player.potions[this.potionIndex].iValue, channel: "speech"});
this.setState(this.sPotionChoice);
break;
case dojo.keys.RIGHT_ARROW:
this.setState(this.sOff);
this._audio.stop({channel:'speech'});
if(++this.potionIndex >= this.player.potions.length){
this.potionIndex = 0;
}
this._audio.say({text: this.player.potions[this.potionIndex].iName + "level " + this.player.potions[this.potionIndex].iValue, channel: "speech"});
this.setState(this.sPotionChoice);
break;
case dojo.keys.UP_ARROW: //accept
this.setState(this.sOff);
this._audio.stop({channel:'speech'});
this._usePotion();
break;
case dojo.keys.ESCAPE:
this.setState(this.sOff);
this._audio.stop({channel:'speech'});
this.player.stopAudio();
if(this.duringMove){
this.duringMove = false;
this.setState(this.sMove);
}
else{
this.setState(this.sFight);
}
break;
}
break;
case this.sVendor:
switch(evt.keyCode){
case dojo.keys.UP_ARROW: //Y
this.setState(this.sOff);
this._audio.stop({channel:'speech'});
this.player.stopAudio();
this._audio.say({text: "You currently have " + this.player.gold + " gold.", channel:'speech'});
this._audio.say({text: "Use the arrow keys to cycle through the options and press the up arrow to select a purchase. Press escape to return to the game.", channel:'speech'});
this.setState(this.sVendorScroll);
break;
case dojo.keys.DOWN_ARROW: //N
this.setState(this.sOff);
this._audio.stop({channel:'speech'});
this.player.stopAudio();
this.skipVendors = true;
this.vendor = null;
this.exploreNode();
break;
}
break;
case this.sVendorScroll:
switch(evt.keyCode){
case dojo.keys.LEFT_ARROW:
this.setState(this.sOff);
this._audio.stop({channel:'speech'});
if(--this.itemIndex < 0){
this.itemIndex = (this.vendor.Items.length - 1);
}
this._audio.say({text: this.vendor.Items[this.itemIndex].iName + "price " + this.vendor.Items[this.itemIndex].iValue + " gold.", channel: "speech"});
this.setState(this.sVendorScroll);
break;
case dojo.keys.RIGHT_ARROW:
this.setState(this.sOff);
this._audio.stop({channel:'speech'});
if(++this.itemIndex >= this.vendor.Items.length){
this.itemIndex = 0;
}
this._audio.say({text: this.vendor.Items[this.itemIndex].iName + "price " + this.vendor.Items[this.itemIndex].iValue + " gold.", channel: "speech"});
this.setState(this.sVendorScroll);
break;
case dojo.keys.UP_ARROW: //Buy
this.setState(this.sOff);
this._audio.stop({channel:'speech'});
this._buyItem();
break;
case dojo.keys.ESCAPE:
this.setState(this.sOff);
this._audio.stop({channel:'speech'});
this.player.stopAudio();
this.skipVendors = true;
this.vendor = null;
this.exploreNode();
break;
}
break;
case this.sBuy:
switch(evt.keyCode){
case dojo.keys.UP_ARROW: //Y
this.setState(this.sOff);
this._audio.stop({channel:'speech'});
if(this.player.gold < this.vendor.Items[this.itemIndex].iValue){
this._audio.say({text:"You cannot afford this item.", channel:'speech'});
this.setState(this.sVendorScroll);
}
else{
this._audio.play({url: "sounds/general/"+ this.equip, channel: 'sound'});
this.player.addItem(dojo.clone(this.vendor.Items[this.itemIndex]));
//take away gold
this.player.gold-= this.vendor.Items[this.itemIndex].iValue;
if(this.vendor.Items[this.itemIndex].iType != dojo.global.POTION){
this.vendor.Items.splice(this.itemIndex, 1);
}
this.itemIndex = 0;
if(this.vendor.Items.length == 0){
this._audio.say({text: "That was the last item in our inventory. Thank you for your business.", channel: 'speech'})
.anyAfter(dojo.hitch(this,function(){
//remove vendor
this.map.removeNPC(this.vendorData[1]);
this.vendor = null;
this.exploreNode();
}));
}
else{
this._audio.say({text: "You currently have " + this.player.gold + " gold.", channel:'speech'});
this._audio.say({text: "Use the arrow keys to cycle through the options and press up to select a purchase. Press escape to return to the game.", channel:'speech'});
this.setState(this.sVendorScroll);
}
}
break;
case dojo.keys.DOWN_ARROW: //N
this.setState(this.sOff);
this._audio.stop({channel:'speech'});
this._audio.say({text: "You currently have " + this.player.gold + " gold.", channel:'speech'});
this._audio.say({text: "Use the arrow keys to cycle through the options and press bee to select a purchase. Press escape to return to the game.", channel:'speech'});
this.setState(this.sVendorScroll);
break;
}
break;
//have to fill up an array, go through it one by one and callback when all done
case this.sFriend:
switch(evt.keyCode){
case dojo.keys.UP_ARROW: //Y
this.setState(this.sOff);
this._audio.stop({channel:'speech'});
this.player.stopAudio();
//play sound and give all items to player
this._audio.play({url: "sounds/" + this.map.Name + ".sounds/" + this.map.sounds[this.friend.ActionSound], channel: 'speech'})
.anyAfter(dojo.hitch(this,function(){
var def = this.examineItems(this.friend.Items, this.friend.Name+ "has given you ");
def.then(dojo.hitch(this,function(){
//remove friend or what??
this.map.removeNPC(this.friendData[1]);
this.friend = null;
this.exploreNode();
}));
}));
this.setState(this.sListen);
break;
case dojo.keys.DOWN_ARROW: //N
this.setState(this.sOff);
this._audio.stop({channel:'speech'});