forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.diamond.iterator.js
71 lines (63 loc) · 1.99 KB
/
util.diamond.iterator.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
/**
* A simple diamond pattern iterator which ends after one loop.
*
* Test case: [...load("util.diamond.iterator").loop({x:19,y:7},3)].map(i=>
* Game.rooms.W29N19.createConstructionSite(i.x,i.y,STRUCTURE_ROAD))
*/
class DiamondIterator {
// public y;
// public x;
// private _radius;
// private _dir
// private _step;
static loop(xy, radius) {
return {
[Symbol.iterator]: function() {
return new DiamondIterator(xy, radius);
}
}
}
constructor(xy, radius) {
this._radius = radius;
this.x = xy.x;
this.y = xy.y;
this._dir = TOP_RIGHT;
this._step = radius;
}
next() {
if (this._dir > TOP_LEFT) {
return {
done: true
}
}
const result = {
done: false,
value: {},
};
switch(this._dir) {
case TOP_RIGHT:
result.value.x = Math.round(this.x - this._step + 0.25);
result.value.y = Math.round(this.y + this._step - this._radius + 0.25);
break;
case BOTTOM_RIGHT:
result.value.x = Math.round(this.x + this._radius - this._step - 0.25);
result.value.y = Math.round(this.y - this._step + 0.25);
break;
case BOTTOM_LEFT:
result.value.x = Math.round(this.x + this._step - 0.25);
result.value.y = Math.round(this.y + this._radius - this._step - 0.25);
break;
default: // TOP_LEFT
result.value.x = Math.round(this.x + this._step - this._radius + 0.25);
result.value.y = Math.round(this.y + this._step - 0.25);
break;
}
this._step = this._step - 0.5;
if (this._step < 1) {
this._dir = this._dir + 2;
this._step = this._radius;
}
return result;
}
}
module.exports = DiamondIterator;