-
Notifications
You must be signed in to change notification settings - Fork 0
/
fov.js
33 lines (30 loc) · 848 Bytes
/
fov.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
var FOV_UNKNOWN = 0;
var FOV_KNOWN = 1;
var FOV_SEEN = 2;
function fovUpdate(p) {
var d = this.dungeon;
function index(x, y) {
return y * d.width + x;
}
function isSeen(x, y) {
return x >= (p.x - r) && x <= (p.x + r) && y >= (p.y - r) && y <= (p.y + r);
}
var r = CreatureAttrs.player.sightRange;
for (var x = 0, w = d.width; x < w; ++x) {
for (var y = 0, h = d.height; y < h; ++y) {
var i = index(x, y);
var v = this.fovMap[i];
var location = {};
location.x = x; location.y = y;
this.fovMap[i] = p.canSee(location) ? FOV_SEEN : (v !== FOV_UNKNOWN) ? FOV_KNOWN : FOV_UNKNOWN;
}
}
}
function FovMap(dungeon) {
this.dungeon = dungeon;
this.fovMap = new Array(dungeon.width * dungeon.height);
for (var i = 0, len = this.fovMap.length; i < len; ++i) {
this.fovMap[i] = FOV_UNKNOWN;
}
this.update = fovUpdate;
}