-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdungeon.php
377 lines (331 loc) · 15.3 KB
/
dungeon.php
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
<?php
// dungeon crawler sample by Andreas Ganje (https://ganje.de)
// legend:
// w - wall
// p - passage
$level = [
['w', 'w', 'w', 'w', 'w', 'w'],
['w', 'p', 'p', 'p', 'w', 'w'],
['w', 'p', 'w', 'p', 'w', 'w'],
['w', 'p', 'p', 'p', 'p', 'w'],
['w', 'p', 'w', 'p', 'w', 'w'],
['w', 'p', 'w', 'p', 'w', 'w'],
['w', 'w', 'w', 'w', 'w', 'w'],
];
$x = isset($_GET['x']) ? $_GET['x'] : 2;
$y = isset($_GET['y']) ? $_GET['y'] : 2;
$d = isset($_GET['d']) ? $_GET['d'] : 's';
// -1 because of array index starting from 0
$x = $x - 1;
$y = $y - 1;
// determine current position
function getCurrentPosition($level, $y, $x, $d)
{
// important: first draw the far images, then the nearer (overdraw)
// middle (always at the end, to overdraw left and right)
// consider initial placeholders (array keys) to make sure the correct order of drawing
if ($d === 'n') {
// players view n
// L M R
// 3 . . .
// 2 . . .
// 1 . P .
$layer = [
'S4' => 'w',
'LLL4' => isset($level[$y - 3][$x - 3]) ? $level[$y - 3][$x - 3] : 'w',
'LL4' => isset($level[$y - 3][$x - 2]) ? $level[$y - 3][$x - 2] : 'w',
'L4' => isset($level[$y - 3][$x - 1]) ? $level[$y - 3][$x - 1] : 'w',
'RRR4' => isset($level[$y - 3][$x + 3]) ? $level[$y - 3][$x + 3] : 'w',
'RR4' => isset($level[$y - 3][$x + 2]) ? $level[$y - 3][$x + 2] : 'w',
'R4' => isset($level[$y - 3][$x + 1]) ? $level[$y - 3][$x + 1] : 'w',
'M4' => isset($level[$y - 3][$x]) ? $level[$y - 3][$x] : 'w',
'LL3' => isset($level[$y - 2][$x - 2]) ? $level[$y - 2][$x - 2] : 'w',
'L3' => isset($level[$y - 2][$x - 1]) ? $level[$y - 2][$x - 1] : 'w',
'RR3' => isset($level[$y - 2][$x + 2]) ? $level[$y - 2][$x + 2] : 'w',
'R3' => isset($level[$y - 2][$x + 1]) ? $level[$y - 2][$x + 1] : 'w',
'S3' => 'w',
'M3' => isset($level[$y - 2][$x]) ? $level[$y - 2][$x] : 'w',
'LL2' => isset($level[$y - 1][$x - 2]) ? $level[$y - 1][$x - 2] : 'w',
'L2' => isset($level[$y - 1][$x - 1]) ? $level[$y - 1][$x - 1] : 'w',
'RR2' => isset($level[$y - 1][$x + 2]) ? $level[$y - 1][$x + 2] : 'w',
'R2' => isset($level[$y - 1][$x + 1]) ? $level[$y - 1][$x + 1] : 'w',
'S2' => 'w',
'M2' => isset($level[$y - 1][$x]) ? $level[$y - 1][$x] : 'w',
'L1' => isset($level[$y][$x - 1]) ? $level[$y][$x - 1] : 'w',
'R1' => isset($level[$y][$x + 1]) ? $level[$y][$x + 1] : 'w',
'M1' => isset($level[$y][$x]) ? $level[$y][$x] : 'w',
];
} elseif ($d === 'e') {
// players view e
// 1 2 3
// L . . .
// M P . .
// R . . .
$layer = [
'S4' => 'w',
'LLL4' => isset($level[$y - 3][$x + 3]) ? $level[$y - 3][$x + 3] : 'w',
'LL4' => isset($level[$y - 2][$x + 3]) ? $level[$y - 2][$x + 3] : 'w',
'L4' => isset($level[$y - 1][$x + 3]) ? $level[$y - 1][$x + 3] : 'w',
'RRR4' => isset($level[$y + 3][$x + 3]) ? $level[$y + 3][$x + 3] : 'w',
'RR4' => isset($level[$y + 2][$x + 3]) ? $level[$y + 2][$x + 3] : 'w',
'R4' => isset($level[$y + 1][$x + 3]) ? $level[$y + 1][$x + 3] : 'w',
'M4' => isset($level[$y][$x + 3]) ? $level[$y][$x + 3] : 'w',
'LL3' => isset($level[$y - 2][$x + 2]) ? $level[$y - 2][$x + 2] : 'w',
'L3' => isset($level[$y - 1][$x + 2]) ? $level[$y - 1][$x + 2] : 'w',
'RR3' => isset($level[$y + 2][$x + 2]) ? $level[$y + 2][$x + 2] : 'w',
'R3' => isset($level[$y + 1][$x + 2]) ? $level[$y + 1][$x + 2] : 'w',
'S3' => 'w',
'M3' => isset($level[$y][$x + 2]) ? $level[$y][$x + 2] : 'w',
'LL2' => isset($level[$y - 2][$x + 1]) ? $level[$y - 2][$x + 1] : 'w',
'L2' => isset($level[$y - 1][$x + 1]) ? $level[$y - 1][$x + 1] : 'w',
'RR2' => isset($level[$y + 2][$x + 1]) ? $level[$y + 2][$x + 1] : 'w',
'R2' => isset($level[$y + 1][$x + 1]) ? $level[$y + 1][$x + 1] : 'w',
'S2' => 'w',
'M2' => isset($level[$y][$x + 1]) ? $level[$y][$x + 1] : 'w',
'L1' => isset($level[$y - 1][$x]) ? $level[$y - 1][$x] : 'w',
'R1' => isset($level[$y + 1][$x]) ? $level[$y + 1][$x] : 'w',
'M1' => isset($level[$y][$x]) ? $level[$y][$x] : 'w',
];
} elseif ($d === 's') {
// players view s
// R M L
// 1 . P .
// 2 . . .
// 3 . . .
$layer = [
'S4' => 'w',
'LLL4' => isset($level[$y + 3][$x + 3]) ? $level[$y + 3][$x + 3] : 'w',
'LL4' => isset($level[$y + 3][$x + 2]) ? $level[$y + 3][$x + 2] : 'w',
'L4' => isset($level[$y + 3][$x + 1]) ? $level[$y + 3][$x + 1] : 'w',
'RRR4' => isset($level[$y + 3][$x - 3]) ? $level[$y + 3][$x - 3] : 'w',
'RR4' => isset($level[$y + 3][$x - 2]) ? $level[$y + 3][$x - 2] : 'w',
'R4' => isset($level[$y + 3][$x - 1]) ? $level[$y + 3][$x - 1] : 'w',
'M4' => isset($level[$y + 3][$x]) ? $level[$y + 3][$x] : 'w',
'LL3' => isset($level[$y + 2][$x + 2]) ? $level[$y + 2][$x + 2] : 'w',
'L3' => isset($level[$y + 2][$x + 1]) ? $level[$y + 2][$x + 1] : 'w',
'RR3' => isset($level[$y + 2][$x - 2]) ? $level[$y + 2][$x - 2] : 'w',
'R3' => isset($level[$y + 2][$x - 1]) ? $level[$y + 2][$x - 1] : 'w',
'S3' => 'w',
'M3' => isset($level[$y + 2][$x]) ? $level[$y + 2][$x] : 'w',
'LL2' => isset($level[$y + 1][$x + 2]) ? $level[$y + 1][$x + 2] : 'w',
'L2' => isset($level[$y + 1][$x + 1]) ? $level[$y + 1][$x + 1] : 'w',
'RR2' => isset($level[$y + 1][$x - 2]) ? $level[$y + 1][$x - 2] : 'w',
'R2' => isset($level[$y + 1][$x - 1]) ? $level[$y + 1][$x - 1] : 'w',
'S2' => 'w',
'M2' => isset($level[$y + 1][$x]) ? $level[$y + 1][$x] : 'w',
'L1' => isset($level[$y][$x + 1]) ? $level[$y][$x + 1] : 'w',
'R1' => isset($level[$y][$x - 1]) ? $level[$y][$x - 1] : 'w',
'M1' => isset($level[$y][$x]) ? $level[$y][$x] : 'w',
];
} elseif ($d === 'w') {
// players view w
// 3 2 1
// R . . .
// M . . P
// L . . .
$layer = [
'S4' => 'w',
'LLL4' => isset($level[$y + 3][$x - 3]) ? $level[$y + 3][$x - 3] : 'w',
'LL4' => isset($level[$y + 2][$x - 3]) ? $level[$y + 2][$x - 3] : 'w',
'L4' => isset($level[$y + 1][$x - 3]) ? $level[$y + 1][$x - 3] : 'w',
'RRR4' => isset($level[$y - 3][$x - 3]) ? $level[$y - 3][$x - 3] : 'w',
'RR4' => isset($level[$y - 2][$x - 3]) ? $level[$y - 2][$x - 3] : 'w',
'R4' => isset($level[$y - 1][$x - 3]) ? $level[$y - 1][$x - 3] : 'w',
'M4' => isset($level[$y][$x - 3]) ? $level[$y][$x - 3] : 'w',
'LL3' => isset($level[$y + 2][$x - 2]) ? $level[$y + 2][$x - 2] : 'w',
'L3' => isset($level[$y + 1][$x - 2]) ? $level[$y + 1][$x - 2] : 'w',
'RR3' => isset($level[$y - 2][$x - 2]) ? $level[$y - 2][$x - 2] : 'w',
'R3' => isset($level[$y - 1][$x - 2]) ? $level[$y - 1][$x - 2] : 'w',
'S3' => 'w',
'M3' => isset($level[$y][$x - 2]) ? $level[$y][$x - 2] : 'w',
'LL2' => isset($level[$y + 2][$x - 1]) ? $level[$y + 2][$x - 1] : 'w',
'L2' => isset($level[$y + 1][$x - 1]) ? $level[$y + 1][$x - 1] : 'w',
'RR2' => isset($level[$y - 2][$x - 1]) ? $level[$y - 2][$x - 1] : 'w',
'R2' => isset($level[$y - 1][$x - 1]) ? $level[$y - 1][$x - 1] : 'w',
'S2' => 'w',
'M2' => isset($level[$y][$x - 2]) ? $level[$y][$x - 1] : 'w',
'L1' => isset($level[$y + 1][$x]) ? $level[$y + 1][$x] : 'w',
'R1' => isset($level[$y - 1][$x]) ? $level[$y - 1][$x] : 'w',
'M1' => isset($level[$y][$x]) ? $level[$y][$x] : 'w',
];
} else {
$layer = [];
}
return $layer;
}
$canvas = imagecreatetruecolor(350, 350);
$colorLines = imagecolorallocate($canvas, 150, 150, 150);
$colorWalls = imagecolorallocate($canvas, 50, 50, 50);
$colorFloor = imagecolorallocate($canvas, 0, 0, 0);
$background = imagefilledrectangle($canvas, 0, 0, 701, 701, $colorFloor);
$layer = getCurrentPosition($level, $y, $x, $d);
// get transparency for darken the far steps
function getTransparency($canvas)
{
return imagecolorallocatealpha(
$canvas,
0,
0,
0,
100
);
}
// functions for drawing walls
function drawL1W($canvas, $foregroundColor, $backgroundColor)
{
imagefilledpolygon($canvas, [-1, 0, 0, 350, 50, 300, 50, 50], 4, $backgroundColor);
imagepolygon($canvas, [-1, 0, 0, 350, 50, 300, 50, 50], 4, $foregroundColor);
}
function drawL2W($canvas, $foregroundColor, $backgroundColor)
{
imagefilledpolygon($canvas, [50, 50, 50, 300, 125, 225, 125, 125], 4, $backgroundColor);
imagepolygon($canvas, [50, 50, 50, 300, 125, 225, 125, 125], 4, $foregroundColor);
imagefilledrectangle($canvas, -1, 50, 50, 300, $backgroundColor);
imagerectangle($canvas, -1, 50, 50, 300, $foregroundColor);
}
function drawL2P($canvas, $foregroundColor, $backgroundColor)
{
imagefilledrectangle($canvas, 0, 50, 49, 300, getTransparency($canvas));
}
function drawLL2W($canvas, $foregroundColor, $backgroundColor)
{
imagefilledpolygon($canvas, [-1, 117, -1, 233, 25, 225, 25, 125], 4, $backgroundColor);
imagepolygon($canvas, [-1, 117, -1, 233, 25, 225, 25, 125], 4, $foregroundColor);
}
function drawL3W($canvas, $foregroundColor, $backgroundColor)
{
imagefilledrectangle($canvas, 25, 125, 125, 225, $backgroundColor);
imagerectangle($canvas, 25, 125, 125, 225, $foregroundColor);
imagefilledpolygon($canvas, [125, 125, 125, 225, 150, 200, 150, 150], 4, $backgroundColor);
imagepolygon($canvas, [125, 125, 125, 225, 150, 200, 150, 150], 4, $foregroundColor);
}
function drawL3P($canvas, $foregroundColor, $backgroundColor)
{
imagefilledrectangle($canvas, 26, 125, 124, 225, getTransparency($canvas));
}
function drawLL3W($canvas, $foregroundColor, $backgroundColor)
{
imagefilledrectangle($canvas, -1, 125, 25, 225, $backgroundColor);
imagerectangle($canvas, -1, 125, 25, 225, $foregroundColor);
imagefilledpolygon($canvas, [25, 125, 25, 225, 100, 200, 100, 150], 4, $backgroundColor);
imagepolygon($canvas, [25, 125, 25, 225, 100, 200, 100, 150], 4, $foregroundColor);
}
function drawL4W($canvas, $foregroundColor, $backgroundColor)
{
imagefilledrectangle($canvas, 100, 150, 150, 200, $backgroundColor);
imagerectangle($canvas, 100, 150, 150, 200, $foregroundColor);
}
function drawLL4W($canvas, $foregroundColor, $backgroundColor)
{
imagefilledrectangle($canvas, 50, 150, 100, 200, $backgroundColor);
imagerectangle($canvas, 50, 150, 100, 200, $foregroundColor);
}
function drawLLL4W($canvas, $foregroundColor, $backgroundColor)
{
imagefilledrectangle($canvas, -1, 150, 50, 200, $backgroundColor);
imagerectangle($canvas, -1, 150, 50, 200, $foregroundColor);
}
function drawR1W($canvas, $foregroundColor, $backgroundColor)
{
imagefilledpolygon($canvas, [300, 50, 300, 300, 350, 350, 350, 0], 4, $backgroundColor);
imagepolygon($canvas, [300, 50, 300, 300, 350, 350, 350, 0], 4, $foregroundColor);
}
function drawR2W($canvas, $foregroundColor, $backgroundColor)
{
imagefilledpolygon($canvas, [225, 125, 225, 225, 300, 300, 300, 50], 4, $backgroundColor);
imagepolygon($canvas, [225, 125, 225, 225, 300, 300, 300, 50], 4, $foregroundColor);
imagefilledrectangle($canvas, 300, 50, 350, 300, $backgroundColor);
imagerectangle($canvas, 300, 50, 350, 300, $foregroundColor);
}
function drawR2P($canvas, $foregroundColor, $backgroundColor)
{
imagefilledrectangle($canvas, 301, 50, 350, 300, getTransparency($canvas));
}
function drawRR2W($canvas, $foregroundColor, $backgroundColor)
{
imagefilledpolygon($canvas, [325, 125, 325, 225, 350, 233, 350, 117], 4, $backgroundColor);
imagepolygon($canvas, [325, 125, 325, 225, 350, 233, 350, 117], 4, $foregroundColor);
}
function drawR3W($canvas, $foregroundColor, $backgroundColor)
{
imagefilledrectangle($canvas, 225, 125, 325, 225, $backgroundColor);
imagerectangle($canvas, 225, 125, 325, 225, $foregroundColor);
imagefilledpolygon($canvas, [200, 150, 200, 200, 225, 225, 225, 125], 4, $backgroundColor);
imagepolygon($canvas, [200, 150, 200, 200, 225, 225, 225, 125], 4, $foregroundColor);
}
function drawR3P($canvas, $foregroundColor, $backgroundColor)
{
imagefilledrectangle($canvas, 226, 125, 350, 225, getTransparency($canvas));
}
function drawRR3W($canvas, $foregroundColor, $backgroundColor)
{
imagefilledrectangle($canvas, 325, 125, 350, 225, $backgroundColor);
imagerectangle($canvas, 325, 125, 350, 225, $foregroundColor);
imagefilledpolygon($canvas, [250, 150, 250, 200, 325, 225, 325, 125], 4, $backgroundColor);
imagepolygon($canvas, [250, 150, 250, 200, 325, 225, 325, 125], 4, $foregroundColor);
}
function drawR4W($canvas, $foregroundColor, $backgroundColor)
{
imagefilledrectangle($canvas, 200, 150, 250, 200, $backgroundColor);
imagerectangle($canvas, 200, 150, 250, 200, $foregroundColor);
}
function drawRR4W($canvas, $foregroundColor, $backgroundColor)
{
imagefilledrectangle($canvas, 250, 150, 300, 200, $backgroundColor);
imagerectangle($canvas, 250, 150, 300, 200, $foregroundColor);
}
function drawRRR4W($canvas, $foregroundColor, $backgroundColor)
{
imagefilledrectangle($canvas, 300, 150, 350, 200, $backgroundColor);
imagerectangle($canvas, 300, 150, 350, 200, $foregroundColor);
}
function drawM1W($canvas, $foregroundColor, $backgroundColor)
{
imagefilledrectangle($canvas, -1, 0, 350, 350, $backgroundColor);
imagerectangle($canvas, -1, 0, 350, 350, $foregroundColor);
}
function drawM2W($canvas, $foregroundColor, $backgroundColor)
{
imagefilledrectangle($canvas, 50, 50, 300, 300, $backgroundColor);
imagerectangle($canvas, 50, 50, 300, 300, $foregroundColor);
}
function drawM3W($canvas, $foregroundColor, $backgroundColor)
{
imagefilledrectangle($canvas, 125, 125, 225, 225, $backgroundColor);
imagerectangle($canvas, 125, 125, 225, 225, $foregroundColor);
}
function drawM4W($canvas, $foregroundColor, $backgroundColor)
{
imagefilledrectangle($canvas, 150, 150, 200, 200, $backgroundColor);
imagerectangle($canvas, 150, 150, 200, 200, $foregroundColor);
}
function drawS4W($canvas, $foregroundColor, $backgroundColor)
{
imagefilledrectangle($canvas, 0, 150, 350, 200, getTransparency($canvas));
}
function drawS3W($canvas, $foregroundColor, $backgroundColor)
{
imagefilledrectangle($canvas, 125, 125, 225, 225, getTransparency($canvas));
}
function drawS2W($canvas, $foregroundColor, $backgroundColor)
{
imagefilledrectangle($canvas, 50, 50, 300, 300, getTransparency($canvas));
}
// draw image layers
foreach ($layer as $key => $value) {
if ($value) {
$functionName = 'draw' . $key . strtoupper($value);
// functions are only available if there something has to be drawn
if (function_exists($functionName)) {
$functionName(
$canvas,
$colorLines,
$colorWalls
);
}
}
}
// provide image
header('Content-Type: image/jpg');
imagejpeg($canvas);
imagedestroy($canvas);