-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathui.js
176 lines (152 loc) · 4.29 KB
/
ui.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
// viewport size
WINDOW_WIDTH = 12
WINDOW_HEIGHT = 12
// map dimensions
MAP_WIDTH = 80;
MAP_HEIGHT = 30;
TILE_SIZE = 32;
function create_html () {
$('body')['append']($('<div id="wrapper"><div id="map"><div id="floor"></div><div id="walls"></div></div><div id="hero"></div></div><div id="panel"></div><div id="log"></div>'));
var mw = MAP_WIDTH*TILE_SIZE,
mh = MAP_HEIGHT*TILE_SIZE,
ww = WINDOW_WIDTH*TILE_SIZE,
wh = WINDOW_HEIGHT*TILE_SIZE;
$('#wrapper')['css']({
width: ww*2,
height: wh
});
$('#map')['css']({
width: mw*2,
height: mh,
'margin-left': ww,
'margin-top': Math.floor(WINDOW_HEIGHT/2)*TILE_SIZE
});
$('#floor')['css']({
width: mw,
height: mh
});
}
function get_neighbors (x, y) {
var v = { type: CELL_VOID },
c = map.cells;
return {
self: (c[y] && c[y][x] || v).type,
left: (c[y] && c[y][x-1] || v).type,
right: (c[y] && c[y][x+1] || v).type,
top: (c[y-1] && c[y-1][x] || v).type,
bottom: (c[y+1] && c[y+1][x] || v).type
}
}
function draw_tile (classname, left, top, z) {
return '<b class="' + classname + '" style="left:' + left + 'px;top:' + top + 'px;' + (z? ('z-index:'+z) : '') + '"></b>'
}
function draw_object (x, y, z) {
var cell = map.cells[y][x],
s = '',
left = (x-y)*TILE_SIZE,
top = (x+y)*TILE_SIZE/2,
o = a = '<b style="left:' + left + 'px;' +
'top:' + top + 'px;' +
'z-index' + z + '" class="';
if (cell.objects.length) {
// draw object
s += o + (cell.objects.length > 1 + !!cell.actor ? 'pile' : cell.objects[0].name) + '"></b>'
}
// draw actor (if any)
if (cell.actor && (cell.actor != map.hero)) {
s += a + cell.actor.name + '"></b>'
}
return s;
}
function draw_level() {
var // predictable_rand initial value
Xn = map.level,
m = Math.pow(2, 32),
a = 69069,
c = 5;
// wall decoration classes
DECORATION = ['paint', 'switch', 'plug', 'display', 'calendar', 'ibelieve'],
D_FREQ = 0.05;
var p_rand = function() {
Xn = (a*Xn + c) % m;
return Xn/m;
}
for(var y=0, floor='', walls=''; y<MAP_HEIGHT; y++) {
for(var x=0; x<MAP_WIDTH; x++) {
var obj='<b id="o'+x+'_'+y+'">',
left = (x-y)*TILE_SIZE,
top = (x+y)*TILE_SIZE/2,
z = (x + y)*10,
cells = get_neighbors(x, y),
deco = (p_rand() < D_FREQ ? (' d ' + DECORATION[Math.floor(p_rand()*DECORATION.length)]) : '' );
// draw map elements
switch (cells.self) {
case CELL_VOID:
floor += draw_tile('u', x*TILE_SIZE, y*TILE_SIZE);
break;
case CELL_WALL:
floor += draw_tile('u', x*TILE_SIZE, y*TILE_SIZE);
if ((y<=0) || (cells.top != CELL_WALL)) {
walls += draw_tile('w l a', left, top, z+1);
}
if ((y>=MAP_WIDTH-1) || (cells.bottom != CELL_WALL)) {
walls += draw_tile('w l' + deco, left, top, z+8);
}
if ((x<=0) || (cells.left != CELL_WALL)) {
walls += draw_tile('w r a', left, top, z+2);
}
if ((x>=MAP_WIDTH-1) || (cells.right != CELL_WALL)) {
walls += draw_tile('w r', left, top, z+9);
}
break;
case CELL_ENTRANCE:
case CELL_EXIT:
walls += draw_tile((cells.self == CELL_EXIT ? 'exit' : 'enter'), left, top, z+3);
break;
}
// draw object containers
var s = draw_object(x, y, z+4)
if (s) {
// create container for this field
walls += obj + s + '</b>'
}
}
}
$('#floor')['html'](floor);
$('#walls')['html'](walls);
$('#hero')['css']('z-index', (map.hero.cell.x + map.hero.cell.y)*10+6);
}
function move_camera(x, y) {
var m = document.getElementById('map');
m.style.left = (y-x)*TILE_SIZE + 'px';
m.style.top = -(y+x)*TILE_SIZE/2 + 'px';
}
function repaint (cell) {
var x = cell.x, y = cell.y;
objs = $('#o'+x+'_'+y);
if (cell.actor == map.hero) {
$('#hero')['css']('z-index', (x+y)*10+6);
move_camera(x, y);
}
if (!objs.length) {
objs = $('<b id="o'+x+'_'+y+'"></b>');
$('#walls')['append'](objs);
}
objs['empty']()['html'](draw_object(x, y, (x+y)*10+4))
}
function repaint_all() {
draw_level();
move_camera(map.hero.cell.x, map.hero.cell.y);
}
function S() {
create_html();
map = new dungeon_map(MAP_WIDTH, MAP_HEIGHT);
map.repaint = repaint;
map.repaint_all = repaint_all;
map.generate();
panel();
intro();
}
window['S'] = S;
if ((navigator.userAgent.indexOf('Windows') == -1) && (navigator.userAgent.indexOf('Macintosh') == -1))
document.documentElement.id = "linux";