-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo.html
67 lines (63 loc) · 2.67 KB
/
demo.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
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="./RandomDungeon.js"></script>
</head>
<body>
<canvas width="600" height="600" style="border: 1px solid blue; display: inline-block; vertical-align: top;" id="canvas"></canvas>
<script type="text/javascript">
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var dungeon = new RandomDungeon({
'seed' : 1, // int for randomize dungeon
'n_rows' : 50, // must be an odd number
'n_cols' : 50, // must be an odd number
'dungeon_layout' : 'Normal', // "U", "Box", "Cross" or Array of Array
'room_min' : 6, // minimum room size
'room_max' : 10, // maximum room size
'room_layout' : "Packed", // Packed, Scattered
'corridor_layout' : 0, // Labyrinth: 0, Straight: 100
'remove_deadends' : 0, // percentage
'add_stairs' : 2, // number of stairs
'map_style' : { // "Standard", "Black" or object
//'background' : '#000000',
'wall' : null,
'fill' : '#000000',
'open' : '#FFFFFF',
'open_grid' : "#999999",
'door' : '#ff0000',
'stair' : '#0000ff',
},
'cell_size' : 10, // pixels
});
console.log(dungeon);
dungeon.render(canvas, {'grid': false});
var _id, _is_room, _is_corridor;
canvas.addEventListener('mousemove', function render (event) {
var col = Math.floor(event.offsetX/dungeon.data.cell_size-0.5);
var row = Math.floor(event.offsetY/dungeon.data.cell_size-0.5);
var cell = dungeon.cells[row] && dungeon.cells[row][col];
var id = cell >> 15;
var is_room = !!(cell & 2);
var is_corridor = !!(cell & 4);
if ((is_corridor || is_room) && (_id != id || _is_room != is_room || _is_corridor != is_corridor)) {
_id = id;
_is_room = is_room;
is_corridor = is_corridor;
dungeon.render(canvas, {
'hightlight': {
'room': is_room ? id : null,
'corridor': is_corridor ? id : null,
},
'grid': false,
});
}
});
canvas.addEventListener('click', function render (event) {
var col = Math.floor(event.offsetX/dungeon.data.cell_size-0.5);
var row = Math.floor(event.offsetY/dungeon.data.cell_size-0.5);
console.log(dungeon.get_cell(row, col) || dungeon.cells[row][col]);
});
</script>
</body>
</html>