-
Notifications
You must be signed in to change notification settings - Fork 3
/
Game.hx
158 lines (134 loc) · 4.39 KB
/
Game.hx
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
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.text.TextField;
import flash.display.Graphics;
import flash.Lib;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.display.Sprite;
import flash.display.Bitmap;
import World;
import World.World;
import World.Tile;
import DebugTextField;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import Simulate;
enum GameState {
initialization;
titleScreen;
gamePlay;
}
class Game {
public static var WORLD_HEIGHT = 600;
public static var WORLD_WIDTH = 600;
public static var rootmc : MovieClip;
public static var mainmc : MovieClip;
public static var backgroundmc : MovieClip;
public static var debugtf : DebugTextField;
public static var lastClick;
static function handleclick(event : MouseEvent) {
lastClick = event;
debugtf.trace("you mousedowned at " + event.localX + " " + event.localY + "\n");
// teleport the badget there
var tile_coord:Coor = {x : cast (event.stageX / World.tilesize) ,
y : cast (event.stageY / World.tilesize)};
trace ("teleporting to :" + Jump.stringofcoor(tile_coord));
World.moveBadger(World.worldState, tile_coord.x, tile_coord.y);
}
public static var lastKey;
static function handlekeydown(event : KeyboardEvent) {
lastKey = event;
debugtf.trace("you pushed this button: " + event.charCode + " " +
event.keyCode + "\n");
if(event.charCode == 100){ // 'd'
debugtf.visible = !debugtf.visible;
}
if(event.keyCode == 39) { // ->
// XXX make badger move right
trace("moveright");
}
if(event.keyCode == 37) { // <-
// XXX make badger move left
trace("moveleft");
}
}
// XXX this will wrap stupidly and everything will be ruined forever
static var frame : Int = 0;
static function mainLoop(e : Event) {
// race condition lol
//trace('mainloop:');
if (gamestate == initialization) {
return;
} else if (gamestate == titleScreen) {
if (lastClick == null && lastKey == null) {
return;
} else {
gamestate = GameState.gamePlay;
startGame();
return;
}
}
if (!LoadStuff.loadsDone() ||
!World.tilesLoaded) {
return;
}
Achievements.tick();
// trace('clearthetiles:');
World.clearTheTiles();
// trace('drawthetiles:');
World.drawTheTiles(frame++);
var state0 = World.worldState;
var state1 = World.worldState.copy();
var badger_coord = World.findAndRemoveBadgers(state1)[0]; //XXX
var badg_x = badger_coord.x;
var badg_y = badger_coord.y;
//trace ("badger x = " + badg_x + " and y = " + badg_y);
var jump_dests = Utils.map(function(j:Jump.Jmp) { return j.dest; },
Jump.validJumps(state0, state1, badg_x, badg_y));
Simulate.drawMovesRel(badg_x, badg_y, jump_dests);
// This is retardo -- you need to do this as part of PROPOSAL X.
if ((frame % 5) == 0) {
World.worldState = Simulate.step(World.worldState);
}
}
private static function myTrace( v : Dynamic, ?inf : haxe.PosInfos ) {
debugtf.trace(v+"\n");
}
public static var gamestate = initialization;
static function main() {
try {
haxe.Log.trace = myTrace;
rootmc = flash.Lib.current;
mainmc = new MovieClip();
backgroundmc = new MovieClip();
mainmc.addEventListener(Event.ENTER_FRAME, mainLoop);
debugtf = new DebugTextField();
rootmc.addChild(backgroundmc);
rootmc.addChild(mainmc);
rootmc.addChild(debugtf);
mainmc.stage.addEventListener(MouseEvent.MOUSE_DOWN, handleclick );
mainmc.stage.addEventListener(KeyboardEvent.KEY_DOWN, handlekeydown );
LoadStuff.loadImageAndCall("title.png", function(l) {
Game.setBackground(l);
});
gamestate = titleScreen;
//flash.Lib.setErrorHandler(Utils.myHandler);
} catch ( s : String ) {
trace("Exception (String): " + s);
} catch ( unknown : Dynamic ) {
trace("Exception (Dynamic): " + Std.string(unknown));
}
}
public static function setBackground(l:flash.display.DisplayObject) {
while (backgroundmc.numChildren > 0) {
backgroundmc.removeChildAt(0);
}
backgroundmc.addChild(l);
}
static function startGame() {
Achievements.init();
World.loadStuff();
}
}