-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
300 lines (238 loc) · 5.83 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8" />
<title>mapgen 0.1</title>
<script type="text/javascript" src="scripts/spryMap-min2.js"></script>
<script type="text/javascript">
var cv, cv2;
var c, c2;
function init() {
cv = document.getElementById('cv');
c = cv.getContext('2d');
cv2 = document.getElementById('cv2');
c2 = cv2.getContext('2d');
}
var map;
function el (id) {
return document.getElementById(id);
}
function rand (range) {
return Math.floor(Math.random()*(range+1))
}
//create map
function createMap() {
var w = el('map_width').value;
var h = el('map_height').value;
map = new Array(w)
for (x=0;x<=w;x++) {
map[x] = new Array(h);
for (y=0;y<=h;y++) {
map[x][y] = -1;
}
}
}
//utility
function brush (px, py, id, radius) {
var w = el('map_width').value;
var h = el('map_height').value;
for (i=0;i<= radius;i++) {
for (j=0;j<= radius;j++) {
if (px+i > -1 && px+i < w && py+j > -1 && py+j < h) {
map[px+i][py+j] = id;
}
}
}
}
//generator function
function shader (fn) {
var w = el('map_width').value;
var h = el('map_height').value;
var tile;
for (x=0;x<=w;x++) {
for (y=0;y<=h;y++) {
tile = fn(x,y);
if (tile != -1) { map[x][y] = tile; }
}
}
}
//draw function
function drawMap() {
var w = el('map_width').value;
var h = el('map_height').value;
var tile;
cv.setAttribute('width', w * 16);
cv.setAttribute('height', h * 16);
cv.style.width=((w*16)*2)+"px";
cv.style.height=((h*16)*2)+"px";
cv2.setAttribute('width', w * 16);
cv2.setAttribute('height', h * 16);
cv2.style.width=((w*16)/5)+"px";
cv2.style.height=((h*16)/5)+"px";
for (x=0;x<=w;x++) {
for (y=0;y<=h;y++) {
tile = map[x][y];
if (tile != -1) { c.drawImage(document.images[tile],x*16,y*16); }
}
}
}
//shaders (hint, you can read tiletype from the 'map' with map[x][y] in any shader)
function genGround(x,y) {
if (rand(10) > 4) { return 2;}
else { return 3; }
}
function genGrass(x,y) {
if (rand(10) > 6) { return 0;}
else { return -1; }
}
function genWater(x,y) {
if (rand(10) > 9) { brush(x, y, 4, rand(4)); }
return -1;
}
function genStone(x,y) {
if (map[x][y] == 3 && rand(10) > 9) { return 1; }
else { return -1; }
}
function genBeach(x,y) {
var w = el('map_width').value;
var h = el('map_height').value;
if (map[x][y] == 4 && rand(10) > 8) {
if (x+1 < w) {
if (map[x+1][y] != 4 ) { map[x+1][y] = 5; }
}
if (x-1 > -1) {
if (map[x-1][y] != 4 ) { map[x-1][y] = 5; }
}
if (y+1 < h) {
if (map[x][y+1] != 4 ) { map[x][y+1] = 5; }
}
if (y-1 > -1) {
if (map[x][y-1] != 4 ) { map[x][y-1] = 5; }
}
return -1;
}
else { return -1; }
}
//generate map
function generate () {
//create new map
createMap();
//use shaders
shader(genGround);
shader(genGrass);
shader(genStone);
shader(genWater);
shader(genBeach);
//draw generated map
drawMap();
c2.drawImage(cv, 0, 0);
}
function renderMap() {
var w = el('map_width').value;
var h = el('map_height').value;
var tile;
for (x=0;x<=w;x++) {
for (y=0;y<=h;y++) {
tile = map[x][y];
if (tile != -1) {
for (k=0;k<=4;k++)
c.drawImage(document.images[tile],x*16+(rand(24)-16),y*16+(rand(24)-16),rand(4),rand(4));
}
}
}
c2.drawImage(cv, 0, 0);
}
function render () {
var i = el('render_iterations').value;
for (j=0;j<=i;j++) {
renderMap();
}
}
window.onload = function() {
var smap = new SpryMap({id : "cv",
height: 400,
width: 600,
startX: 200,
startY: 200,
cssClass: "mappy"});
init();
}
</script>
<style type="text/css">
body, html {
font-family:verdana,tahoma,helvetica;
text-align:center;
}
#toolbar {
width:600px;
background:#efefef;
padding:5px;
margin-left:auto;
margin-right:auto;
text-align:left;
border-radius: 4px 4px 4px 4px;
}
#toolbar input {
width:40px;
border-radius: 4px 4px 4px 4px;
}
div.clear {
clear:both;
}
.button {
padding: 5px 10px;
display: inline;
background: #777 url(../images/button.png) repeat-x bottom;
border: none;
color: #fff;
cursor: pointer;
font-weight: bold;
border-radius: 5px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
text-shadow: 1px 1px #666;
text-decoration:none;
}
.button:hover {
background-position: 0 center;
}
.button:active {
background-position: 0 top;
position: relative;
top: 1px;
padding: 6px 10px 4px;
}
.button.red { background-color: #e50000; }
.button.purple { background-color: #9400bf; }
.button.green { background-color: #58aa00; }
.button.orange { background-color: #ff9c00; }
.button.blue { background-color: #2c6da0; }
.button.black { background-color: #333; }
.button.white { background-color: #fff; color: #000; text-shadow: 1px 1px #fff; }
.button.small { font-size: 75%; padding: 3px 7px; }
.button.small:active { padding: 4px 7px 2px; background-position: 0 top; }
.button.large { font-size: 125%; padding: 7px 12px; }
.button.large:active { padding: 8px 12px 6px; background-position: 0 top; }
</style>
</head>
<body>
<div id="toolbar">
<canvas id="cv2" style="float:right;width:100px;height:100px;background:#dedede;"></canvas>
tiles
<img src="data/grass.png"/>
<img src="data/rock.png"/>
<img src="data/gravel.png"/>
<img src="data/dirt.png"/>
<img src="data/water.png"/>
<img src="data/sand.png"/>
<p>
width <input id="map_width" type="text" value="30" maxlength="3"/> height <input id="map_height" type="text" value="30" maxlength="3"/> <a href="#" class="button blue small" onclick="generate()">generate map</a>
</p>
<p>
iterations <input id="render_iterations"type="text" value="20" maxlength="3"/> <a href="#" onclick="render();" class="button blue small">render map</a>
</p>
<div class="clear" />
<canvas id="cv"></canvas>
</div>
</body>
</html>