-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUI.hx
288 lines (236 loc) · 7.6 KB
/
UI.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
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
// javascript ui class
import js.Lib;
class UI
{
var game: Game;
var alertWindow: Dynamic; // alert window element
var alertText: Dynamic; // alert text element
public var cursorX: Int; // cursor x,y in map coordinates
public var cursorY: Int;
public var prevX: Int; // previous mouse coordinates
public var prevY: Int;
public var justClicked: Bool; // hack: skip first mouse move after click
public var msgLocked: Bool; // is msg panel locked until mouse click?
public function new(g: Game)
{
game = g;
e("version").innerHTML = Game.version;
e("map").onclick = onMapClick;
e("map").onmousemove = onMapMove;
e("restart").onclick = onRestart;
msgLocked = false;
// check if canvas is available
var map = e("map");
if (!(untyped map).getContext)
Lib.window.alert("No canvas available. Please use Mozilla Firefox 3.5+ or Google Chrome.");
// alert window
alertWindow = Lib.document.createElement("alertWindow");
alertWindow.style.visibility = 'hidden';
alertWindow.style.position = 'absolute';
alertWindow.style.zIndex = 20;
alertWindow.style.width = 600;
alertWindow.style.height = 450;
alertWindow.style.left = 200;
alertWindow.style.top = 50;
alertWindow.style.background = '#222';
alertWindow.style.border = '4px double #ffffff';
Lib.document.body.appendChild(alertWindow);
// alert text
alertText = Lib.document.createElement("alertText");
alertText.style.overflow = 'auto';
alertText.style.position = 'absolute';
alertText.style.left = 10;
alertText.style.top = 10;
alertText.style.width = 580;
alertText.style.height = 400;
alertText.style.background = '#111';
alertText.style.border = '1px solid #777';
alertWindow.appendChild(alertText);
// alert close button
var alertClose = createCloseButton(alertWindow, 260, 415, 'alertClose');
alertClose.onclick = onAlertCloseClick;
}
// create close button
function createCloseButton(container: Dynamic, x: Int, y: Int, name: String)
{
var b: Dynamic = Lib.document.createElement(name);
b.innerHTML = '<b>Close</b>';
b.style.fontSize = 20;
b.style.position = 'absolute';
b.style.width = 80;
b.style.height = 25;
b.style.left = x;
b.style.top = y;
b.style.background = '#111';
b.style.border = '1px outset #777';
b.style.cursor = 'pointer';
b.style.textAlign = 'center';
container.appendChild(b);
return b;
}
// hide alert
function onAlertCloseClick(event)
{
alertWindow.style.visibility = 'hidden';
}
// on clicking restart button
public function onRestart(event)
{
msgLocked = false;
msg("", false);
game.restart();
}
// on map mouse click
public function onMapClick(event)
{
if (game.isFinished)
return;
// clean msg lock
if (msgLocked)
{
msgLocked = false;
msg("", false);
}
var map = e("map");
var x = event.clientX - map.offsetLeft;
var y = event.clientY - map.offsetTop;
var cellX = Std.int((x - 5) / cellSize);
var cellY = Std.int((y - 7) / cellSize);
// trace(x + "," + y + " -> " + cellX + "," + cellY);
var cell = game.map.get(cellX, cellY);
if (cell != null && cell.hasAdjacentWalkable())
{
cell.activate();
}
justClicked = true;
paintStatus();
}
// on map mouse move
public function onMapMove(event)
{
if (justClicked)
{
justClicked = false;
return;
}
if (game.isFinished)
return;
var map = e("map");
var x = event.clientX - map.offsetLeft;
var y = event.clientY - map.offsetTop;
cursorX = Std.int((x - 5) / cellSize);
cursorY = Std.int((y - 7) / cellSize);
// paint changed rectangle
game.map.paint(getRect(cursorX, cursorY, repaintRadius));
// check, if mouse moved more then the rect, repaint all
var dx = prevX - x;
var dy = prevY - y;
if (dx * dx + dy * dy > 10000)
game.map.paint();
var cell = game.map.get(cursorX, cursorY);
if (cell != null && cell.isVisible)
msg(cell.getNote(), false);
else msg("", false);
prevX = x;
prevY = y;
}
// get a rect around a cell
public static function getRect(x: Int, y: Int, radius: Int)
{
var rect = {
x: 3 + (x - radius) * cellSize,
y: 2 + (y - radius) * cellSize,
w: cellSize * radius * 2, h: cellSize * radius * 2 };
if (radius == 0)
{
rect.w = cellSize;
rect.h = cellSize;
}
return rect;
}
// show a message
public function msg(s: String, ?isLocked: Bool)
{
if (isLocked == null)
isLocked = true;
// trace(isLocked + " " + msgLocked);
if (msgLocked && !isLocked)
return;
if (isLocked)
msgLocked = true;
e("msg").innerHTML = s;
}
// repaint status panel (fsta)
public function paintStatus()
{
var s = "<table width=100%><tr><td halign=left>Obelisks found: " +
game.map.obelisksFound() + " / " + game.map.obelisks.length +
"<td halign=left>Obelisks shattered: " +
game.map.obelisksShattered() + " / " + game.map.obelisks.length +
"<td halign=left>Creatures destroyed: " +
game.zombiesDestroyed + " / " + game.zombies +
"<td halign=right><p style='text-align:right; margin-right:5'>" +
"Turns: " + game.turns + "</table>";
e("status").innerHTML = s;
}
// get element shortcut
public static inline function e(s)
{
return Lib.document.getElementById(s);
}
// finish the game (ui)
public function finish(isVictory: Bool)
{
var el = untyped UI.e("map");
var map = el.getContext("2d");
map.fillStyle = "rgba(0, 0, 0, 0.5)";
map.fillRect(0, 0, el.width, el.height);
map.fillStyle = "white";
var text = "";
if (isVictory)
text = "You have found all obelisks in " + game.turns + " turns!";
else text = "You have never returned from the swamp...";
var metrics = map.measureText(text);
map.fillText(text, (el.width - metrics.width) / 2,
(el.height - cellSize) / 2);
if (isVictory)
{
text = "You have managed to destroy " + game.zombiesDestroyed +
" creatures.";
var metrics = map.measureText(text);
map.fillText(text, (el.width - metrics.width) / 2,
(el.height - cellSize) / 2 + 40);
}
}
// track stuff through google analytics
public inline function track(action: String, ?label: String, ?value: Int)
{
action = "obelisk " + action + " " + Game.version;
if (label == null)
label = '';
if (value == null)
value = 0;
untyped pageTracker._trackEvent('Black Obelisk', action, label, value);
}
// message with confirmation
public function alert(s)
{
alertText.innerHTML = '<center>' + s + '</center>';
alertWindow.style.visibility = 'visible';
}
// get a stored variable (cookie)
public inline function getVar(name: String)
{
return untyped getCookie(name);
}
// get a stored variable (cookie)
public inline function setVar(name: String, val: String)
{
return untyped setCookie(name, val,
untyped __js__("new Date(2015, 0, 0, 0, 0, 0, 0)"));
}
public static var cellSize: Int = 40;
public static var mapWidth: Int = 25;
public static var mapHeight: Int = 16;
public static var repaintRadius: Int = 3;
}