-
Notifications
You must be signed in to change notification settings - Fork 15
/
Game.as
1438 lines (1239 loc) · 46.2 KB
/
Game.as
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
package {
import com.adobe.serialization.json.JSON;
import com.robotacid.ai.Brain;
import com.robotacid.ai.HorrorBrain;
import com.robotacid.ai.Node;
import com.robotacid.ai.PlayerBrain;
import com.robotacid.level.Content;
import com.robotacid.level.MapBitmap;
import com.robotacid.level.Map;
import com.robotacid.engine.*;
import com.robotacid.geom.Pixel;
import com.robotacid.geom.Trig;
import com.robotacid.gfx.*;
import com.robotacid.phys.Collider;
import com.robotacid.phys.CollisionWorld;
import com.robotacid.sound.SoundManager;
import com.robotacid.sound.SoundQueue;
import com.robotacid.ui.Console;
import com.robotacid.ui.Dialog;
import com.robotacid.ui.Editor;
import com.robotacid.ui.menu.DeathMenu;
import com.robotacid.ui.menu.EditorMenuList;
import com.robotacid.ui.menu.GameMenu;
import com.robotacid.ui.menu.Menu;
import com.robotacid.ui.menu.MenuCarousel;
import com.robotacid.ui.menu.PlayerConsumedMenu;
import com.robotacid.ui.menu.QuestMenuList;
import com.robotacid.ui.menu.QuestMenuOption;
import com.robotacid.ui.menu.TitleMenu;
import com.robotacid.ui.ProgressBar;
import com.robotacid.ui.TextBox;
import com.robotacid.ui.MiniMap;
import com.robotacid.ui.Key;
import com.robotacid.ui.Transition;
import com.robotacid.util.clips.stopClips;
import com.robotacid.util.FPS;
import com.robotacid.util.HiddenInt;
import com.robotacid.util.misc.onScreen;
import com.robotacid.util.LZW;
import com.robotacid.util.RLE;
import com.robotacid.util.XorRandom;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.display.MovieClip;
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.StageQuality;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.external.ExternalInterface;
import flash.geom.ColorTransform;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.media.Sound;
import flash.net.SharedObject;
import flash.text.TextField;
import flash.ui.Keyboard;
import flash.utils.ByteArray;
import flash.utils.getTimer;
/**
* Red Rogue
*
* A roguelike platform game
*
* This is the top level class that serves as a Controller to the rest of the code
*
* @author Aaron Steed, robotacid.com
*/
[SWF(width = "640", height = "480", frameRate="30", backgroundColor = "#000000")]
public class Game extends Sprite {
public static const BUILD_NUM:int = 460;
public static const TEST_BED_INIT:Boolean = false;
public static const ONLINE:Boolean = true;
public static var game:Game;
public static var renderer:Renderer;
public static var debug:Graphics;
public static var debugStay:Graphics;
public static var dialog:Dialog;
// core engine objects
public var player:Player;
public var minion:Minion;
public var balrog:Balrog;
public var library:Library;
public var map:Map;
public var content:Content;
public var entrance:Portal;
public var world:CollisionWorld;
public var random:XorRandom;
public var soundQueue:SoundQueue;
public var sleep:Sleep;
public var transition:Transition;
public var editor:Editor;
public var epilogue:Epilogue;
// graphics
public var mapTileManager:MapTileManager;
public var lightMap:LightMap;
public var lightning:Lightning;
// ui
public var gameMenu:GameMenu;
public var deathMenu:DeathMenu;
public var playerConsumedMenu:PlayerConsumedMenu;
public var titleMenu:TitleMenu;
public var focusPrompt:Sprite;
public var titleGfx:Sprite;
public var miniMapHolder:Sprite;
public var console:Console;
public var confusionOverlayHolder:Sprite;
public var menuCarousel:MenuCarousel;
public var miniMap:MiniMap;
public var playerActionBar:ProgressBar;
public var playerHealthBar:ProgressBar;
public var playerXpBar:ProgressBar;
public var levelNumGfx:MovieClip;
public var minionHealthBar:ProgressBar;
public var enemyHealthBar:ProgressBar;
public var livesPanel:LivesPanel;
public var keyItemStatus:Sprite;
public var fpsText:TextBox;
public var titlePressMenuText:TextBox
public var instructions:MovieClip;
public var instructionsHolder:Sprite;
// debug
public var info:TextField;
// lists
public var entities:Vector.<Entity>;
public var items:Array;
public var torches:Vector.<Torch>;
public var effects:Vector.<Effect>;
public var portals:Vector.<Portal>;
public var chaosWalls:Vector.<ChaosWall>;
public var explosions:Vector.<Explosion>;
// states
public var state:int;
public var focusPreviousState:int;
public var instructionsPreviousState:int;
public var frameCount:int;
public var mousePressedCount:int;
public var mousePressed:Boolean;
public var paused:Boolean;
public var shakeDirX:int;
public var shakeDirY:int;
public var deepestLevelReached:int;
public var forceFocus:Boolean = true;
public var portalHash:Object;
public var dogmaticMode:Boolean;
public var lives:HiddenInt;
public var livesAvailable:HiddenInt;
public var multiplayer:Boolean;
public var firstInstructions:Boolean;
public var endGameEvent:Boolean;
// temp variables
private var i:int;
public static var point:Point = new Point();
// CONSTANTS
public static const SCALE:Number = 16;
public static const INV_SCALE:Number = 1.0 / 16;
// states
public static const GAME:int = 0;
public static const MENU:int = 1;
public static const DIALOG:int = 2;
public static const TITLE:int = 3;
public static const INSTRUCTIONS:int = 4;
public static const EPILOGUE:int = 5;
public static const UNFOCUSED:int = 6;
public static const WIDTH:Number = 320;
public static const HEIGHT:Number = 240;
// game key properties
public static const UP_KEY:int = 0;
public static const DOWN_KEY:int = 1;
public static const LEFT_KEY:int = 2;
public static const RIGHT_KEY:int = 3;
public static const MENU_KEY:int = 4;
public static const MAX_LEVEL:int = 20;
public static const CONSOLE_Y:Number = HEIGHT - Console.HEIGHT;
public static const HEALTH_GLOW_RATIO:Number = 0.3;
public static const DEFAULT_BAR_COL:uint = 0xFFCCCCCC;
public static const DISABLED_BAR_COL:uint = 0xFFAA0000;
public static const GLOW_BAR_COL:uint = 0xAA0000;
public static const RED_COL:ColorTransform = new ColorTransform(1, 0, 0, 1, -85);
public static const SOUND_DIST_MAX:int = 12;
public static const INV_SOUND_DIST_MAX:Number = 1.0 / SOUND_DIST_MAX;
public static const SOUND_HORIZ_DIST_MULTIPLIER:Number = 1.5;
public static var fullscreenOn:Boolean;
public static var allowScriptAccess:Boolean;
public function Game():void {
game = this;
UserData.game = this;
Entity.game = this;
LightMap.game = this;
Effect.game = this;
FX.game = this;
Map.game = this;
Content.game = this;
Brain.game = this;
MapBitmap.game = this;
Lightning.game = this;
ItemMovieClip.game = this;
SceneManager.game = this;
QuestMenuList.game = this;
QuestMenuOption.game = this;
Dialog.game = this;
EditorMenuList.game = this;
Menu.game = this;
// detect allowScriptAccess for tracking
allowScriptAccess = ExternalInterface.available;
if(allowScriptAccess){
try{
ExternalInterface.call("");
} catch(e:Error){
allowScriptAccess = false;
}
}
random = new XorRandom();
var byteArray:ByteArray;
byteArray = new Character.statsData();
Character.stats = JSON.decode(byteArray.readUTFBytes(byteArray.length));
byteArray = new Item.statsData();
Item.stats = JSON.decode(byteArray.readUTFBytes(byteArray.length));
// init UserData
UserData.initSettings();
UserData.initGameState();
UserData.pull();
// check the game is alive
if(UserData.gameState.dead) UserData.initGameState();
// misc static settings
Map.seed = UserData.settings.randomSeed;
Menu.moveDelay = UserData.settings.menuMoveSpeed;
dogmaticMode = UserData.settings.dogmaticMode;
multiplayer = UserData.settings.multiplayer;
endGameEvent = false;
firstInstructions = ONLINE;
state = (!ONLINE || UserData.settings.playerConsumed || TEST_BED_INIT) ? GAME : TITLE;
library = new Library;
renderer = new Renderer(this);
renderer.init();
DebrisFX.IGNORE_PROPERTIES = (
Collider.CHARACTER | Collider.LEDGE | Collider.LADDER | Collider.HEAD | Collider.CORPSE
);
DripFX.IGNORE_PROPERTIES = (
Collider.CHARACTER | Collider.LADDER | Collider.HEAD | Collider.CORPSE
);
Effect.BANNED_RANDOM_ENCHANTMENTS[Effect.PORTAL] = true;
Effect.BANNED_RANDOM_ENCHANTMENTS[Effect.NULL] = true;
Effect.BANNED_RANDOM_ENCHANTMENTS[Effect.CHAOS] = true;
Effect.BANNED_RANDOM_ENCHANTMENTS[Effect.IDENTIFY] = true;
Effect.BANNED_RANDOM_ENCHANTMENTS[Effect.HOLY] = true;
TextBox.init();
MapTileConverter.init();
ProgressBar.initGlowTable();
sleep = new Sleep(this, renderer);
transition = new Transition();
lightning = new Lightning();
editor = new Editor(this, renderer);
FPS.start();
// SOUND INIT
SoundManager.init();
soundQueue = new SoundQueue();
lives = new HiddenInt();
livesAvailable = new HiddenInt(UserData.settings.livesAvailable);
trackEvent("load complete");
if (stage) addedToStage();
else addEventListener(Event.ADDED_TO_STAGE, addedToStage);
}
private function addedToStage(e:Event = null):void{
removeEventListener(Event.ADDED_TO_STAGE, init);
// KEYS INIT
if(!Key.initialized){
Key.init(stage);
Key.custom = UserData.settings.customKeys.slice();
Key.hotKeyTotal = 10;
}
// GRAPHICS INIT
scaleX = scaleY = 2;
stage.quality = StageQuality.LOW;
// GAME INIT
init();
}
/* The initialisation is quite long, so I'm breaking it up with some comment lines */
private function init():void {
// settings seed has priority over gameState
var randomSeed:uint = Map.seed ? Map.seed : uint(UserData.gameState.randomSeed);
Map.random = new XorRandom(randomSeed);
// GAME GFX AND UI INIT
if(state == GAME || state == MENU){
renderer.createRenderLayers(this);
addChild(editor.bitmap);
addChild(sleep);
if(!console){
console = new Console();
console.y = CONSOLE_Y;
}
addChild(console);
miniMapHolder = new Sprite();
addChild(miniMapHolder);
if(miniMap) miniMapHolder.addChild(miniMap);
keyItemStatus = new KeyUI();
keyItemStatus.x = 5 + MiniMap.WIDTH + 2;
keyItemStatus.y = 5 + (MiniMap.HEIGHT * 0.5 - keyItemStatus.height * 0.5) >> 0;
addChild(keyItemStatus);
keyItemStatus.visible = false;
playerHealthBar = new ProgressBar(5, CONSOLE_Y - 22, 27, 17, HEALTH_GLOW_RATIO, 0xAA0000);
playerHealthBar.barCol = 0xFFCCCCCC;
addChild(playerHealthBar);
var hpBitmap:Bitmap = new library.HPB;
hpBitmap.x = 3;
hpBitmap.y = 5;
playerHealthBar.addChild(hpBitmap);
playerHealthBar.update();
livesPanel = new LivesPanel();
livesPanel.y = -(livesPanel.height + 2);
livesPanel.visible = false;
playerHealthBar.addChild(livesPanel);
minionHealthBar = new ProgressBar(playerHealthBar.x + playerHealthBar.bitmap.width + 1, playerHealthBar.y, 27, 5, HEALTH_GLOW_RATIO, 0xAA0000);
minionHealthBar.barCol = 0xFFCCCCCC;
addChild(minionHealthBar);
var mhpBitmap:Bitmap = new library.MHPB;
mhpBitmap.x = minionHealthBar.width + 1;
minionHealthBar.addChild(mhpBitmap);
minionHealthBar.visible = false;
minionHealthBar.update();
playerXpBar = new ProgressBar(playerHealthBar.x + playerHealthBar.bitmap.width + 1, minionHealthBar.y + minionHealthBar.height, 27, 5);
playerXpBar.barCol = 0xFFCCCCCC;
addChild(playerXpBar);
levelNumGfx = new LevelNumMC();
levelNumGfx.stop();
levelNumGfx.x = playerXpBar.width + 1;
playerXpBar.addChild(levelNumGfx);
playerXpBar.update();
playerActionBar = new ProgressBar(playerHealthBar.x + playerHealthBar.bitmap.width + 1, playerXpBar.y + playerXpBar.height, 27, 5);
playerActionBar.barCol = 0xFFCCCCCC;
var actBitmap:Bitmap = new library.ACT;
actBitmap.x = playerActionBar.width + 1;
playerActionBar.addChild(actBitmap);
addChild(playerActionBar);
playerActionBar.update();
enemyHealthBar = new ProgressBar(WIDTH - 32, playerHealthBar.y, 27, 17, HEALTH_GLOW_RATIO, 0xAA0000);
enemyHealthBar.barCol = 0xFFCCCCCC;
addChild(enemyHealthBar);
enemyHealthBar.active = false;
enemyHealthBar.alpha = 0;
confusionOverlayHolder = new Sprite();
addChild(confusionOverlayHolder);
instructionsHolder = new Sprite();
addChild(instructionsHolder);
} else if(state == TITLE){
addChild(getTitleGfx());
titlePressMenuText = new TextBox(Menu.LIST_WIDTH * 2, 12, Dialog.ROLL_OUT_COL);
titlePressMenuText.align = "center";
titlePressMenuText.text = "press menu key (" + Key.keyString(Key.custom[MENU_KEY]) + ") to begin";
titlePressMenuText.bitmapData.colorTransform(titlePressMenuText.bitmapData.rect, RED_COL);
titlePressMenuText.x = (WIDTH * 0.5 - titlePressMenuText.width * 0.5) >> 0;
titlePressMenuText.y = (HEIGHT * 0.5) + 10;
addChild(titlePressMenuText);
}
addChild(transition);
// menu init
if(!menuCarousel){
menuCarousel = new MenuCarousel();
gameMenu = new GameMenu(WIDTH, CONSOLE_Y, this);
deathMenu = new DeathMenu(WIDTH, CONSOLE_Y, this);
playerConsumedMenu = new PlayerConsumedMenu(WIDTH, CONSOLE_Y, this);
titleMenu = new TitleMenu(gameMenu);
menuCarousel.addMenu(gameMenu);
menuCarousel.addMenu(deathMenu);
menuCarousel.addMenu(playerConsumedMenu);
menuCarousel.addMenu(titleMenu);
} else {
// update the rng seed
if(Map.seed == 0) gameMenu.seedInputList.option.name = "" + Map.random.seed;
titleMenu.continueOption.active = Boolean(UserData.gameState.player.xml);
titleMenu.update();
}
addChild(menuCarousel);
if(!focusPrompt){
createFocusPrompt();
stage.addEventListener(Event.DEACTIVATE, onFocusLost);
stage.addEventListener(Event.ACTIVATE, onFocus);
}
// CREATE FIRST LEVEL =================================================================
if(state == GAME || state == MENU){
menuCarousel.setCurrentMenu(gameMenu);
/**/
// debugging textfield
info = new TextField();
addChild(info);
info.textColor = 0xFFFFFF;
info.selectable = false;
info.text = "";
info.visible = true;
// fps text box
fpsText = new TextBox(35, 12);
fpsText.x = WIDTH - (fpsText.width + 2);
fpsText.y = HEIGHT - (fpsText.height + 2);
fpsText.visible = gameMenu.debugOption.active;
addChild(fpsText);
// STATES
frameCount = 1;
deepestLevelReached = 1;
lives.value = 0;
// LISTS
entities = new Vector.<Entity>();
items = [];
effects = new Vector.<Effect>();
torches = new Vector.<Torch>();
portals = new Vector.<Portal>();
chaosWalls = new Vector.<ChaosWall>();
explosions = new Vector.<Explosion>();
portalHash = {};
// load identified rune names from areas
Item.runeNames = UserData.gameState.runeNames;
UserData.loadRuneNames();
Brain.initCharacterLists();
Brain.voiceCount = Brain.VOICE_DELAY + random.range(Brain.VOICE_DELAY);
// ALL CONTENT FOR THE RANDOM SEED GENERATED FROM THIS POINT FORWARD
content = new Content();
Writing.createStoryCharCodes(Map.random);
Sleep.initDreams();
// LEVEL SPECIFIC INIT
// This stuff that follows requires the bones of a level to initialise
Player.previousLevel = UserData.gameState.player.previousLevel;
Player.previousPortalType = UserData.gameState.player.previousPortalType;
Player.previousMapType = UserData.gameState.player.previousMapType;
setLevel(UserData.gameState.player.currentLevel, UserData.gameState.player.currentMapType);
// init area visit notices
var levelName:String = Map.getName(map.level, map.type);
if(!UserData.gameState.visitedHash){
UserData.gameState.visitedHash = {};
UserData.gameState.visitedHash[levelName] = true;
}
} else if(state == TITLE){
menuCarousel.setCurrentMenu(titleMenu);
titlePressMenuText.visible = !menuCarousel.active;
}
// fire up listeners
addListeners();
if(TEST_BED_INIT) initTestBed();
else if(ONLINE && !UserData.settings.playerConsumed){
if(state == GAME || state == MENU){
if(firstInstructions){
transition.init(initInstructions, null, "", true);
} else {
transition.init(function():void{}, null, levelName, true);
}
}
}
// this is a hack to force clicking on the game when the browser first pulls in the swf
if(forceFocus){
onFocusLost();
forceFocus = false;
} else {
changeMusic();
}
}
/* Pedantically clear all memory and re-init the project */
public function reset(newGame:Boolean = true):void{
removeEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
removeEventListener(MouseEvent.MOUSE_UP, mouseUp);
removeEventListener(Event.ENTER_FRAME, main);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.removeEventListener(Event.DEACTIVATE, onFocusLost);
stage.removeEventListener(Event.ACTIVATE, onFocus);
while(numChildren > 0){
removeChildAt(0);
}
player = null;
minion = null;
balrog = null;
mapTileManager = null;
map = null;
world = null;
lightMap = null;
mapTileManager = null;
if(newGame){
UserData.initGameState();
UserData.push();
}
SoundManager.musicTimes = {};
if(console){
console.log = "";
console.logLines = 0;
}
if(editor) editor.deactivate();
init();
}
/* Enters the testing area */
public function launchTestBed():void{
UserData.disabled = true;
setLevel( -1, Map.MAIN_DUNGEON);
gameMenu.editorList.setLight(gameMenu.editorList.lightList.selection);
}
/* Enters the the testing area from game init */
public function initTestBed():void{
gameMenu.editorList.renderAIPathsList.selection = EditorMenuList.ON;
gameMenu.editorList.renderAIGraphList.selection = EditorMenuList.ON;
gameMenu.editorList.renderCollisionList.selection = EditorMenuList.ON;
launchTestBed();
}
/* Used to change to a new level in the dungeon
*
* This method tries to wipe all layers whilst leaving the gaming architecture in place
*/
public function setLevel(level:int, type:int):void{
var enchantment:XML, effect:Effect;
editor.deactivate();
// saving settings in an area would delete the content there
gameMenu.saveSettingsOption.active = type != Map.AREA;
// maintain debug state if present
if(map && map.level == -1){
level = -1;
Player.previousLevel = -1;
Player.previousMapType = Map.MAIN_DUNGEON;
Player.previousPortalType = Portal.PORTAL;
}
if(map){
// left over content needs to be pulled back into the content manager to be found
// if the level is visited again
content.recycleLevel(map.level, map.type);
// capture gameState
UserData.saveSettings();
UserData.saveGameState(level, type);
UserData.push();
}
var mapNameStr:String = Map.getName(level, type);
if(type == Map.MAIN_DUNGEON) mapNameStr += ":" + level;
// elements to update:
// game objects list needs to be emptied
// items list needs to be emptied
// colliders list needs to be emptied
// new map
// clear rendering layers
if(level > deepestLevelReached && deepestLevelReached < MAX_LEVEL) deepestLevelReached = level;
// dismiss entity effects - leave player and minion alone
var i:int;
for(i = 0; i < entities.length; i++){
if(entities[i] is Character && entities[i] != minion && (entities[i] as Character).effects){
(entities[i] as Character).removeEffects();
}
}
// clear lists
entities.length = 0;
items.length = 0;
torches.length = 0;
renderer.fx.length = 0;
portals.length = 0;
chaosWalls.length = 0;
explosions.length = 0;
portalHash = {};
Brain.monsterCharacters.length = 0;
Brain.voiceCount = Brain.VOICE_DELAY + random.range(Brain.VOICE_DELAY);
map = new Map(level, type);
Brain.initMapGraph(map.bitmap, map.stairsDown);
if(!mapTileManager){
mapTileManager = new MapTileManager(this, renderer.canvas, SCALE, map.width, map.height, WIDTH, HEIGHT);
mapTileManager.setLayers(map.layers);
} else {
mapTileManager.newMap(map.width, map.height, map.layers);
}
renderer.backBitmapData = mapTileManager.layerToBitmapData(MapTileManager.BACKGROUND_LAYER);
if(type == Map.MAIN_DUNGEON && level <= Writing.story.length){
Writing.renderWritings();
}
renderer.blockBitmapData = renderer.backBitmapData.clone();
if(type != Map.AREA){
renderer.blockBitmapData = mapTileManager.layerToBitmapData(MapTileManager.BLOCK_LAYER, renderer.blockBitmapData);
} else {
mapTileManager.setLayerUpdate(MapTileManager.BLOCK_LAYER, false);
}
// modify the mapRect to conceal secrets
mapTileManager.mapRect = map.bitmap.adjustedMapRect;
renderer.camera.mapRect = map.bitmap.adjustedMapRect;
world = new CollisionWorld(map.width, map.height, SCALE);
world.map = createPropertyMap(mapTileManager.mapLayers[MapTileManager.BLOCK_LAYER]);
Explosion.initMap(map.width, map.height);
// collider debug
//world.debug = debug;
if(!lightMap) lightMap = new LightMap(world.map);
else {
lightMap.newMap(world.map);
lightMap.setLight(player, player.light);
}
mapTileManager.init(map.start.x, map.start.y);
if(!miniMap){
miniMap = new MiniMap(world.map, this, renderer);
miniMapHolder.addChild(miniMap);;
miniMap.y = miniMap.x = 5;
} else {
miniMap.newMap(world.map);
}
if(map.type != Map.AREA && map.cleared) miniMap.reveal();
if(!player){
var playerMc:MovieClip = new RogueMC();
var startX:Number = (map.start.x + 0.5) * SCALE;
var startY:Number = (map.start.y + 1) * SCALE;
player = new Player(playerMc, startX, startY);
var minionMc:MovieClip = new SkeletonMC();
minion = new Minion(minionMc, startX, startY, Character.SKELETON);
if(!UserData.settings.playerConsumed){
// load the state of the player, if there is one
if(UserData.gameState.player.xml){
var playerXML:XML = UserData.gameState.player.xml
player.level = int(playerXML.@level);
player.xp = Number(playerXML.@xp);
// the character may have been reskinned, so we just force a reskin anyway to set stats
player.changeName(int(playerXML.@name));
if(UserData.gameState.player.health) player.health = UserData.gameState.player.health;
player.applyHealth(0);
player.addXP(0);
levelNumGfx.gotoAndStop(player.level);
player.keyItem = UserData.gameState.player.keyItem;
for each(enchantment in playerXML.effect){
effect = new Effect(int(enchantment.@name), int(enchantment.@level), int(enchantment.@source), player, int(enchantment.@count), false, false);
}
gameMenu.inventoryList.loadFromObject(UserData.gameState.inventory);
if(!UserData.gameState.minion || UserData.settings.minionConsumed) minion = null;
// load the state of the minion, if there is one
else if(UserData.gameState.minion.xml){
var minionXML:XML = UserData.gameState.minion.xml
minion.level = int(minionXML.@level);
// the character may have been reskinned, so we just force a reskin anyway to set stats
minion.changeName(int(minionXML.@name));
if(UserData.gameState.minion.health) minion.health = UserData.gameState.minion.health;
minion.applyHealth(0);
for each(enchantment in minionXML.effect){
effect = new Effect(int(enchantment.@name), int(enchantment.@level), int(enchantment.@source), minion, int(enchantment.@count), false, false);
}
}
game.console.print("welcome back " + player.nameToString() + " (" + mapNameStr + ")");
} else {
game.console.print("welcome " + player.nameToString());
}
player.snapCamera();
} else {
player.active = false;
minion = null;
game.console.print("");
}
} else {
player.collider.x = -player.collider.width * 0.5 + (map.start.x + 0.5) * SCALE;
player.collider.y = -player.collider.height + (map.start.y + 1) * SCALE;
player.mapX = (player.collider.x + player.collider.width * 0.5) * INV_SCALE;
player.mapY = (player.collider.y + player.collider.height * 0.5) * INV_SCALE;
player.snapCamera();
}
keyItemStatus.visible = player.keyItem;
if(minion){
minion.collider.x = -minion.collider.width * 0.5 + (map.start.x + 0.5) * SCALE;
minion.collider.y = -minion.collider.height + (map.start.y + 1) * SCALE;
minion.mapX = (minion.collider.x + minion.collider.width * 0.5) * INV_SCALE;
minion.mapY = (minion.collider.y + minion.collider.height * 0.5) * INV_SCALE;
entities.push(minion);
if(minion.light) lightMap.setLight(minion, minion.light, 150);
minion.prepareToEnter(entrance);
minion.brain.clear();
minion.addMinimapFeature();
} else {
minionHealthBar.visible = false;
gameMenu.summonOption.active = false;
gameMenu.update();
}
if(balrog){
balrog.collider.x = -balrog.collider.width * 0.5 + (map.start.x + 0.5) * SCALE;
balrog.collider.y = -balrog.collider.height + (map.start.y + 1) * SCALE;
balrog.mapX = (balrog.collider.x + balrog.collider.width * 0.5) * INV_SCALE;
balrog.mapY = (balrog.collider.y + balrog.collider.height * 0.5) * INV_SCALE;
balrog.addMinimapFeature();
balrog.consumedPlayer = UserData.settings.playerConsumed;
if(balrog.consumedPlayer){
balrog.snapCamera();
}
}
// outside areas are set pieces, meant to give contrast to the dungeon and give the player
// and minion a back-story
if(type == Map.AREA){
renderer.lightBitmap.visible = false;
miniMap.visible = false;
SoundManager.fadeMusic("music1", -SoundManager.DEFAULT_FADE_STEP);
// the overworld changes the rogue to a colour version and reverts all polymorph effects
if(level == Map.OVERWORLD){
var skinMc:MovieClip;
// unequip face armour if worn
if(player.armour && player.armour.name == Item.FACE) player.unequip(player.armour);
if(minion && minion.armour && minion.armour.name == Item.FACE) minion.unequip(minion.armour);
// change the rogue to a colour version and revert the minion if changed
skinMc = new RogueColMC();
if(player.name != Character.ROGUE)
console.print(player.nameToString() + " reverts to human form");
player.changeName(Character.ROGUE, new RogueColMC);
if(minion){
if(minion.name == Character.HUSBAND || UserData.gameState.husband){
if(minion.name != Character.HUSBAND)
console.print(minion.nameToString() + " reverts to human form");
minion.changeName(Character.HUSBAND, new AtColMC);
} else if(minion.name != Character.SKELETON){
minion.changeName(Character.SKELETON);
console.print(minion.nameToString() + " reverts to undead form");
}
}
} else if(level == Map.UNDERWORLD){
if(player.undead) player.applyHealth(player.totalHealth);
if(minion && minion.undead) minion.applyHealth(minion.totalHealth);
}
} else if(level == -1){
renderer.lightBitmap.visible = false;
} else if(Player.previousMapType == Map.AREA){
renderer.lightBitmap.visible = true;
miniMap.visible = true;
// revert to black and white rogue
if(Player.previousLevel == Map.OVERWORLD){
player.changeName(Character.ROGUE, new RogueMC);
// technically we wouldn't get here because of the ending triggering
if(minion){
if(minion.name == Character.HUSBAND){
minion.changeName(Character.HUSBAND, new AtMC);
}
}
}
}
// some levels/areas require constant graphical effects
renderer.sceneManager = SceneManager.getSceneManager(level, type);
// any targeted enemy is no longer on this level
if(enemyHealthBar.active) enemyHealthBar.deactivate();
// when chasing the balrog the player will enter the level after the balrog
// emphasising the nature of the chase
if(
balrog &&
balrog.levelState == Balrog.ENTER_STAIRS_UP &&
entrance.type == Portal.STAIRS &&
Player.previousLevel < level
){
player.prepareToEnter(entrance);
if(minion) minion.enterCount *= 2;
balrog.enterLevel(entrance);
} else {
player.enterLevel(entrance, Player.previousLevel < level ? Collider.RIGHT : Collider.LEFT);
}
endGameEvent = false;
if(!player.active) consumedPlayerInit();
/**/else {
// end game checks
if(map.type == Map.AREA){
if(map.level == Map.UNDERWORLD){
// check for yendor (and debugging), activate death
if(
gameMenu.inventoryList.getItem(Item.YENDOR, Item.ARMOUR) &&
minion &&
minion.name != Character.HUSBAND
){
endGameEvent = true;
for(i = 0; i < entities.length; i++){
if(entities[i] is Stone && (entities[i] as Stone).name == Stone.DEATH){
(entities[i] as Stone).setEndGameEvent();
break;
}
}
}
} else if(map.level == Map.OVERWORLD){
// check for yendor or husband
if(
gameMenu.inventoryList.getItem(Item.YENDOR, Item.ARMOUR) ||
(minion && minion.name == Character.HUSBAND)
){
endGameEvent = true;
}
}
}
}
changeMusic();
trackEvent("set level", mapNameStr);
}
private function addListeners():void{
stage.addEventListener(Event.DEACTIVATE, onFocusLost);
stage.addEventListener(Event.ACTIVATE, onFocus);
addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
addEventListener(MouseEvent.MOUSE_UP, mouseUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
addEventListener(Event.ENTER_FRAME, main);
}
// =================================================================================================
// MAIN LOOP
// =================================================================================================
private function main(e:Event):void {
if(fpsText) fpsText.text = "fps:" + FPS.value;
// copy out these debug tools when needed
//var t:int = getTimer();
//info.text = game.player.mapX + " " + game.player.mapY;
//info.appendText("pixels" + (getTimer() - t) + "\n"); t = getTimer();
if(state == GAME) {
var advance:Boolean = true;
if(dogmaticMode){
if(!player.asleep && player.searchRadius == -1 && player.state == Character.WALKING && Key.keysPressed == 0) advance = false;
}
if(transition.active) transition.main();
else if(advance){
var collider:Collider;
var entity:Entity;
var character:Character;
var effect:Effect;
debug.clear();
debug.lineStyle(1, 0x00FF00);
// unfortunately lightning has to be drawn on the fly - so we clear it here
renderer.lightningShape.graphics.clear();
world.main();
// update chaos walls
for(i = chaosWalls.length - 1; i > -1; i--){
entity = chaosWalls[i];
if(entity.active) entity.main();
else chaosWalls.splice(i, 1);
}
// reset damping and update mapX/mapY before attacks
for(i = 0; i < world.colliders.length; i++){
collider = world.colliders[i];
if((collider.properties & Collider.CHARACTER) && !(collider.properties & Collider.GATE)){
collider.pushDamping = 0;
character = collider.userData as Character;
character.mapX = (collider.x + collider.width * 0.5) * INV_SCALE;
character.mapY = (collider.y + collider.height * 0.5) * INV_SCALE;
character.mapProperties = world.map[character.mapY][character.mapX];
}
}
if(player.active){
player.main();
if(player.asleep){
sleep.main();
if(player.quickenQueued) player.setAsleep(false);
}
if(transition.active) return;
}
if(balrog && balrog.active){
balrog.main();
}
// update items
for(i = items.length - 1; i > -1; i--){
entity = items[i];
if(entity.active){
if(entity.callMain) entity.main();
} else {
items.splice(i, 1);
}
}
// update the rest of the game objects
for(i = entities.length - 1; i > -1; i--){
entity = entities[i];
if(entity.active){
if(entity.callMain) entity.main();