-
Notifications
You must be signed in to change notification settings - Fork 0
/
createdungeon_main.js
189 lines (159 loc) · 6.15 KB
/
createdungeon_main.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* global define */
(function(){
"use strict";
// preface ======================================================== {{{
var api = {};
var listener = [];
var listener_id = 0;
function add_listener(callback) {
var id = ++listener_id;
listener.push({
id: id,
fn: callback,
obj: {
id: id,
destroy: function() {
remove_listener(id);
}
}
});
return id;
}
function remove_listener(id) {
for (var i = 0; i < listener.length; i++) {
if (id === listener[i].id) {
listener.splice(i, 1);
return;
}
}
}
function trigger_listener(data) {
listener.forEach(function(listen) {
listen.fn.call(listen.obj, data);
});
}
// ---------------------------------------------------------------- }}}
function Dungeon(data, uuid, design, seed) { // {{{
Object.defineProperties(this, {
uuid: { enumerable: true, value: uuid },
design: { enumerable: true, value: design },
seed: { enumerable: true, value: seed },
width: { enumerable: true, value: data[0] },
height: { enumerable: true, value: data[1] },
data: { value: data } });
}
api.Dungeon = Dungeon;
Dungeon.prototype.get = function(x, y) {
return this.data[(y*this.width) + x + 2];
};
Dungeon.prototype.set = function(x, y, val) {
this.data[(y*this.width) + x + 2] = val;
return val;
};
// }}}
api.createDungeon = (function(){
var worker, workerReady;
return function(design, seed) {
if (!worker) {
worker = new Worker(api.createDungeon.workerUrl);
workerReady = new Promise(function(resolve){
worker.addEventListener('message', function(e) {
if (e.data === 'Ready.') {
resolve();
} else {
trigger_listener(new Dungeon(e.data.data, e.data.uuid, e.data.design, e.data.seed));
}
}, false);
});
}
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c === 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
return new Promise(function(resolve){
api.onCreateDungeon(function(dungeon){
if (dungeon.uuid === uuid) {
resolve(dungeon);
this.destroy();
}
});
if (typeof seed !== 'number') {
seed = (Math.random() * 10000000)|0;
}
workerReady.then(function(){
worker.postMessage({ design: design, seed: seed, uuid: uuid });
});
});
};
})();
api.createDungeon.workerUrl = 'createdungeon_worker.js';
api.onCreateDungeon = function(callback) {
return add_listener(callback);
};
api.showDungeon = function(dungeon, element){
if (!dungeon) return;
if (!element) element = document.body;
var PIXEL_ZOOM = 2;
var COLOR = [
"#0000ff",
/* OPEN */ "#f0f0f0",
/* CLOSED */ "#303030",
/* G_CLOSED */ "#303030", // GUARANTEED-OPEN AND GUARANTEED-CLOSED
/* G_OPEN */ "#f0f0f0",
/* NJ_OPEN */ "#f0f0f0", // NV = non-join, these cannot be joined br Builders
/* NJ_G_OPEN */ "#f0f0f0", // with others of their own kind
/* NJ_CLOSED */ "#303030",
/* NJ_G_CLOSED */ "#303030",
/* IR_OPEN */ "#f0f0f0", // inside-room, open
/* IT_OPEN */ "#e8e8e8", // inside-tunnel, open
/* IA_OPEN */ "#e0e0e0", // inside anteroom, open
/* H_DOOR */ "#a04000", // horizontal door
/* V_DOOR */ "#a04000", // vertical door
/* MOB1 */ "#ff0040", // MOBs of different level - higher is better
/* MOB2 */ "#f00030",
/* MOB3 */ "#e00020",
/* TREAS1 */ "#ffff00", // treasure of different level
/* TREAS2 */ "#f0f000",
/* TREAS3 */ "#e0e000",
/* COLUMN */ "#00ff00"
];
var canvas = document.createElement('canvas');
var dpr = window.devicePixelRatio || 1;
var pixelSize = dpr * PIXEL_ZOOM;
canvas.width = pixelSize * dungeon.width;
canvas.height = pixelSize * dungeon.height;
canvas.style.width = (dungeon.width * PIXEL_ZOOM) + "px";
canvas.style.height = (dungeon.height * PIXEL_ZOOM) + "px";
var ctx = canvas.getContext('2d');
var x, y, val;
for (y= 0; y < dungeon.height; y++) {
for (x= 0; x < dungeon.width; x++) {
val = dungeon.get(x, y);
ctx.fillStyle = COLOR[val+1];
ctx.fillRect(x*pixelSize, y*pixelSize, pixelSize, pixelSize);
}
}
if (element !== false) {
element.appendChild(canvas);
}
dungeon.canvas = canvas;
return dungeon;
};
// define.amd or exports ======================================================== {{{
if (typeof define === 'function' && define.amd) {
define(function(){
return api;
});
} else {
var root = (typeof exports !== 'undefined' && null !== exports) ? exports : this;
if (typeof root.dungeonmaker === 'undefined') {
root.dungeonmaker = Object.create(null);
}
for (var key in api) {
if (api.hasOwnProperty(key)) {
root.dungeonmaker[key] = api[key];
}
}
}
// ------------------------------------------------------------------------------ }}}
}).call(this);