This repository has been archived by the owner on Feb 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
position.js
113 lines (90 loc) · 2.6 KB
/
position.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
"use strict";
var typify = require("./typify.js");
function make(x, y) {
typify.assert("number", x);
typify.assert("number", y);
return {
x: x,
y: y
};
}
// Max-distance
function distance(a, b) {
typify.assert("position", a);
typify.assert("position", b);
var dx = Math.abs(a.x - b.x);
var dy = Math.abs(a.y - b.y);
var dz = Math.abs(a.x + a.y - b.x - b.y);
return Math.max(dx, dy, dz);
}
function eq(a, b) {
typify.assert("position", a);
typify.assert("position", b);
return a.x === b.x && a.y === b.y;
}
function roundCubeToNearestHex(fc) {
// typify.assert("floatcube", fc);
// Simply rounding would work in most of the cases.
// The extra logic is there to handle few special cases,
// where a position would round to wrong hex and possibly out of area.
// The logic is explained here:
// http://www.redblobgames.com/grids/hexagons/#rounding
var rx = Math.round(fc.x);
var ry = Math.round(fc.y);
var rz = Math.round(fc.z);
var xDiff = Math.abs(rx - fc.x);
var yDiff = Math.abs(ry - fc.y);
var zDiff = Math.abs(rz - fc.z);
if (xDiff > yDiff && xDiff > zDiff) {
rx = -ry - rz;
} else if (yDiff > zDiff) {
ry = -rx - rz;
} else {
rz = -rx - ry;
}
return {x: rx, y: rz};
}
function clampPosition(pos, radius) {
var d = distance({x: 0, y: 0}, pos);
if (d === 0 || d <= radius) {
// When in radius, we can simply return the position
return pos;
} else {
// We clip the position to nearest in radius
var t = 1.0 * radius / d;
var cx = pos.x * t;
var cy = (-pos.x - pos.y) * t;
var cz = pos.y * t;
// We need to round the floating point location to nearest hex
return roundCubeToNearestHex({x: cx, y: cy, z: cz});
}
}
// clamp
function clamp(pos, fieldRadius) {
typify.assert("position", pos);
typify.assert("nat", fieldRadius);
return clampPosition(pos, fieldRadius);
}
var origo = make(0, 0);
function neighbours(pos, radius) {
typify.assert("position", pos);
typify.assert("nat", radius);
var result = [];
for (var x = pos.x - radius; x <= pos.x + radius; x++) {
for (var y = pos.y - radius; y <= pos.y + radius; y++) {
var newPos = make(x, y);
if (distance(pos, newPos) <= radius && !eq(pos, newPos)) {
result.push(newPos);
}
}
}
return result;
}
module.exports = {
make: make,
distance: distance,
eq: eq,
clamp: clamp,
origo: origo,
neighbours: neighbours
};