-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConstrainedTokenBorder.js
229 lines (196 loc) · 6.79 KB
/
ConstrainedTokenBorder.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* globals
ClockwiseSweepPolygon,
foundry,
PIXI
*/
/* eslint no-unused-vars: ["error", { "argsIgnorePattern": "^_" }] */
"use strict";
export const PATCHES = {};
PATCHES.CONSTRAINED_TOKEN_BORDER = {};
// ----- NOTE: Hooks ----- //
function canvasInit() { ConstrainedTokenBorder._wallsID++; }
PATCHES.CONSTRAINED_TOKEN_BORDER.HOOKS = { canvasInit };
/**
* Generate a polygon of the token bounds with portions intersected by walls stripped out.
* Use line-of-sight from the center point to determine the resulting token shape.
* This border represents the physical bounds of the token, so the move restriction is
* used for walls (which thus don't have limited restriction walls).
*/
export class ConstrainedTokenBorder extends ClockwiseSweepPolygon {
/**
* Cache shape by token.
*/
static _cache = new WeakMap();
/**
* Retrieve the constrained token shape for the given wall restriction type.
* @param {Token} token
* @param {string} type Corresponds to wall restriction: sight, sound, light, move
*/
static get(token) {
let polygon = this._cache.get(token);
if ( !polygon ) this._cache.set(token, polygon = new this(token));
polygon.initialize();
polygon.compute();
return polygon;
}
/** Indicator of wall/edge changes
* @type {number}
*/
static _wallsID = 0;
/**
* Properties to test if relevant token characterics have changed.
* @type {object}
*/
_tokenProperties = {
visionHeight: null
};
/**
* More properties to test if relevant token characterics have changed, specific to the document.
* @type {object}
*/
_tokenDocumentProperties = {
x: null,
y: null,
elevation: null,
width: null,
height: null
}
/** @type {Token} */
_token;
/** @type {number} */
#wallsID = -1;
/**
* If true, no walls constrain token.
* @type {boolean}
*/
_unrestricted;
/** @type {boolean} */
#dirty = true;
constructor(token) {
super();
this._token = token;
}
/** @override */
initialize() {
const { _token, _tokenProperties, _tokenDocumentProperties } = this;
// Determine if the token has changed.
// Could use getProperty/setProperty, but may be a bit slow and unnecessary, given
// that all properties are either on the token or the document.
let tokenMoved = false;
for ( const key of Object.keys(_tokenProperties) ) {
const value = _token[key];
tokenMoved ||= _tokenProperties[key] !== value;
_tokenProperties[key] = value;
}
const doc = _token.document;
for ( const key of Object.keys(_tokenDocumentProperties) ) {
const value = doc[key];
tokenMoved ||= _tokenDocumentProperties[key] !== value;
_tokenDocumentProperties[key] = value;
}
if ( tokenMoved || this.#wallsID !== ConstrainedTokenBorder._wallsID ) {
this.#wallsID = ConstrainedTokenBorder._wallsID;
this.#dirty = true;
const config = {
source: _token.vision,
type: "move",
boundaryShapes: [_token.tokenBorder] // [_token.tokenBorder.toPolygon()] }; // Avoid WeilerAtherton.
};
super.initialize(_token.center, config);
}
}
/** @override */
getBounds() {
return this._token.bounds;
}
/** @override */
compute() {
if ( this.#dirty ) {
this.#dirty = false;
super.compute();
}
}
/** @override */
_compute() {
this.points.length = 0;
if ( this._identifyEdges() ) {
this._identifyVertices();
this._executeSweep();
this._constrainBoundaryShapes();
this._unrestricted = false;
} else {
this._unrestricted = true;
}
this.vertices.clear();
this.edges.clear();
this.rays.length = 0;
// If we screwed up, fall back on unrestricted.
if ( this.points.length < 6 ) this._unrestricted = true;
}
/**
* Reject walls collinear to the bounding shape.
* Test whether a wall should be included in the computed polygon for a given origin and type
* @param {Edge} edge The Edge being considered
* @param {Record<EdgeTypes, 0|1|2>} edgeTypes Which types of edges are being used? 0=no, 1=maybe, 2=always
* @param {PIXI.Rectangle} bounds The overall bounding box
* @returns {boolean} Should the edge be included?
* @protected
*/
_testEdgeInclusion(edge, edgeTypes, bounds) {
// Need to include scene boundaries in case we need to run sweep.
const m = edgeTypes[edge.type];
if ( !m ) return false;
if ( m === 2 ) return true;
// Drop edges collinear to the border.
if ( this.#edgeIsCollinearToBoundary(edge) ) return false;
return super._testEdgeInclusion(edge, edgeTypes, bounds);
}
/**
* Test whether a given edge lies precisely on a boundary edge.
* @param {Edge} edge The Edge being considered
* @returns {boolean}
*/
#edgeIsCollinearToBoundary(edge) {
const boundary = this.config.boundaryShapes[0]; // Always a single shape b/c set in initialize.
if ( boundary instanceof PIXI.Rectangle ) {
const delta = edge.b.subtract(edge.a, PIXI.Point._tmp);
if ( !delta.x && (edge.a.x.almostEqual(boundary.left) || edge.a.x.almostEqual(boundary.right)) ) return true;
if ( !delta.y && (edge.a.y.almostEqual(boundary.top) || edge.a.y.almostEqual(boundary.bottom)) ) return true;
} else if ( boundary instanceof PIXI.Polygon ) {
const orient2d = foundry.utils.orient2dFast;
for ( const boundaryEdge of boundary.iterateEdges() ) {
// Works b/c the boundary polygon is simple.
if ( orient2d(boundaryEdge.A, boundaryEdge.B, edge.a, edge.b).almostEqual(0) ) return true;
}
}
return false;
}
/**
* If all edges are collinear to the token border, then we can just use the token border.
* @returns {boolean} True if sweep should be run (edges not all collinear or outside the token border).
@override */
_identifyEdges() {
super._identifyEdges();
// Can skip sweep if only border edges left and those edges don't intersect the boundary.
const boundary = this.config.boundaryShapes[0];
for ( const edge of this.edges ) {
if ( !(edge.type === "innerBounds" || edge.type === "outerBounds") ) return true;
if ( boundary.lineSegmentIntersects(edge.a, edge.b, { inside: true }) &&
!this.#edgeIsCollinearToBoundary(edge) ) return true;
}
return false; // Can skip the sweep.
}
/** @override */
contains(x, y) {
const inBounds = this._token.bounds.contains(x, y);
if ( this._unrestricted || !inBounds ) return inBounds;
return PIXI.Polygon.prototype.contains.call(this, x, y);
}
/**
* Return either this polygon or the underlying token border if possible.
* @returns {ConstrainedTokenShape|PIXI.Rectangle}
*/
constrainedBorder() {
return this._unrestricted ? this._token.tokenBorder : this;
}
}