-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
WorkspaceMesh.ts
351 lines (312 loc) · 10 KB
/
WorkspaceMesh.ts
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import { Object3D, Vector3, Color, Vector2 } from 'three';
import {
HexGridMesh,
HexGrid,
HexMesh,
keyToPos,
Axial,
realPosToGridPos,
} from './hex';
import { GridMesh } from './grid/GridMesh';
import {
DEFAULT_WORKSPACE_HEIGHT,
DEFAULT_WORKSPACE_SCALE,
DEFAULT_WORKSPACE_GRID_SCALE,
DEFAULT_MINI_WORKSPACE_SCALE,
AuxDomain,
BotCalculationContext,
calculateBotValue,
getContextSize,
getContextDefaultHeight,
getContextScale,
getBuilderContextGrid,
getContextGridScale,
isMinimized,
isContext,
getContextColor,
DEFAULT_WORKSPACE_COLOR,
hasValue,
getContextGridHeight,
calculateGridScale,
getContextVisualizeMode,
isUserBot,
Bot,
} from '@casual-simulation/aux-common';
import minBy from 'lodash/minBy';
import isEqual from 'lodash/isEqual';
import { GridChecker, GridCheckResults } from './grid/GridChecker';
import { GameObject } from './GameObject';
import { AuxBot3D } from './AuxBot3D';
import { calculateGridTileLocalCenter } from './grid/Grid';
/**
* Defines a mesh that represents a workspace.
*/
export class WorkspaceMesh extends GameObject {
private _debugMesh: Object3D;
private _debug: boolean;
private _debugInfo: WorkspaceMeshDebugInfo;
private _checker: GridChecker;
/**
* The hex grid for this workspace.
*/
hexGrid: HexGridMesh;
/**
* The square grid for this workspace.
*/
squareGrids: GridMesh[];
/**
* The workspace for this mesh.
*/
workspace: Bot;
/**
* The container for everything on the workspace.
*/
container: Object3D;
/**
* The mini hex that is shown when the mesh is in mini mode.
*/
miniHex: HexMesh;
/**
* The domain that this mesh should look at.
*/
domain: AuxDomain;
/**
* The number of bots on this mesh.
*/
botCount: number;
/**
* Sets the visibility of the grids on this workspace.
*/
set gridsVisible(visible: boolean) {
this.squareGrids.forEach(grid => {
grid.visible = visible;
});
}
/**
* Sets the GridChecker that this workspace should use to update its valid
* grid positions.
*/
set gridGhecker(val: GridChecker) {
this._checker = val;
}
/**
* Creates a new WorkspaceMesh.
*/
constructor(domain: AuxDomain) {
super();
this.squareGrids = [];
this.container = new Object3D();
this.domain = domain;
this.miniHex = new HexMesh(
new Axial(0, 0),
DEFAULT_MINI_WORKSPACE_SCALE,
DEFAULT_WORKSPACE_HEIGHT
);
this.miniHex.visible = false;
this.add(this.container);
this.add(this.miniHex);
this._debugInfo = {
id: this.id,
gridCheckResults: null,
};
this.botCount = 0;
}
/**
* Sets whether this mesh should display debug information.
* @param debug Whether the info should be shown.
*/
showDebugInfo(debug: boolean) {
this._debug = debug;
// TODO: Fix sometime
// this.update(undefined, true);
}
/**
* Gets the most recent debug info from the workspace.
*/
getDebugInfo() {
return this._debugInfo;
}
/**
* Calculates the GridTile that is the closest to the given world point.
* @param point The world point to test.
*/
closestTileToPoint(point: Vector3) {
const tiles = this.squareGrids
.map(g => g.closestTileToPoint(point))
.filter(t => !!t);
const closest = minBy(tiles, t => t.distance);
return closest;
}
/**
* Updates the mesh with the new workspace data and optionally updates the square grid using the given
* grid checker.
* @param calc The bot calculation context.
* @param workspace The new workspace data. If not provided the mesh will re-update using the existing data.
* @param force Whether to force the workspace to update everything, even aspects that have not changed.
*/
async update(
calc: BotCalculationContext,
workspace?: Bot,
bots?: AuxBot3D[],
force?: boolean
) {
if (!workspace) {
return;
}
const prev = this.workspace;
this.workspace = workspace || prev;
this.visible =
isContext(calc, this.workspace) &&
getContextVisualizeMode(calc, this.workspace) === 'surface';
this.container.visible = !isMinimized(calc, this.workspace);
this.miniHex.visible = !this.container.visible;
let gridUpdate: GridCheckResults = this._debugInfo.gridCheckResults;
if (bots.length > this.botCount) {
this.botCount = bots.length;
force = true;
}
if (this._gridChanged(this.workspace, prev, calc) || force) {
this._updateHexGrid(calc, bots);
if (this._checker) {
gridUpdate = await this._updateSquareGrids(this._checker, calc);
if (this._debugMesh) {
this.remove(this._debugMesh);
}
if (this._debug) {
this._debugMesh = new Object3D();
this._debugMesh.add(
GridChecker.createVisualization(gridUpdate)
);
this.add(this._debugMesh);
}
}
}
// Hex color.
const colorValue = getContextColor(calc, this.workspace);
const color: Color = hasValue(colorValue)
? new Color(colorValue)
: new Color(DEFAULT_WORKSPACE_COLOR);
const hexes = this.hexGrid.hexes;
hexes.forEach(h => {
h.color = color;
});
this.miniHex.color = color;
this.updateMatrixWorld(false);
if (this._debug) {
this._debugInfo = {
gridCheckResults: gridUpdate,
id: this.id,
};
}
}
public frameUpdate() {}
public dispose() {
super.dispose();
}
/**
* Updates the hex grid to match the workspace data.
*/
private _updateHexGrid(calc: BotCalculationContext, bots: AuxBot3D[]) {
if (this.hexGrid) {
this.hexGrid.dispose();
this.container.remove(this.hexGrid);
}
const size = getContextSize(calc, this.workspace);
let centerHeight: number = getContextGridHeight(
calc,
this.workspace,
'0:0'
);
const defaultHeight = getContextDefaultHeight(calc, this.workspace);
const scale = getContextScale(calc, this.workspace);
this.hexGrid = HexGridMesh.createFilledInHexGrid(
size,
centerHeight || DEFAULT_WORKSPACE_HEIGHT,
scale || DEFAULT_WORKSPACE_SCALE
);
bots.forEach(bot => {
if (isUserBot(bot.bot)) {
return;
}
let localPosition = calculateGridTileLocalCenter(
calculateBotValue(calc, bot.bot, bot.context + 'X'),
calculateBotValue(calc, bot.bot, bot.context + 'Y'),
calculateBotValue(calc, bot.bot, bot.context + 'Z'),
calculateGridScale(calc, bot.contextGroup.bot)
);
let axial: Axial = realPosToGridPos(
new Vector2(localPosition.x, localPosition.z),
scale
);
const hexgrid = this.hexGrid.addAt(axial);
hexgrid.height =
centerHeight || defaultHeight || DEFAULT_WORKSPACE_HEIGHT;
});
/*
const grid = getBuilderContextGrid(calc, this.workspace);
const positionsKeys = grid ? keys(grid) : [];
positionsKeys.forEach(key => {
const position = keyToPos(key);
const workspaceHex = grid[key];
if (grid['0:0'] != null) {
centerHeight = grid['0:0'].height;
}
const hex = this.hexGrid.addAt(position);
let nextHeight =
centerHeight || defaultHeight || DEFAULT_WORKSPACE_HEIGHT;
if (nextHeight < 0) {
nextHeight = defaultHeight || DEFAULT_WORKSPACE_HEIGHT;
}
hex.height = nextHeight;
});
*/
this.colliders = [...this.hexGrid.hexes, this.miniHex];
this.container.add(this.hexGrid);
}
/**
* Updates the square grid to match the workspace data.
* @param checker The grid checker to use.
*/
private async _updateSquareGrids(
checker: GridChecker,
calc: BotCalculationContext
) {
if (this.squareGrids && this.squareGrids.length > 0) {
this.squareGrids.forEach(g => g.dispose());
this.container.remove(...this.squareGrids);
}
const gridScale = getContextGridScale(calc, this.workspace);
checker.tileRatio = gridScale || DEFAULT_WORKSPACE_GRID_SCALE;
const results = await checker.check(this.hexGrid);
const levels = results.levels;
this.squareGrids = levels.map(l => new GridMesh(l));
this.squareGrids.forEach(grid => (grid.visible = false));
if (this.squareGrids && this.squareGrids.length > 0) {
this.container.add(...this.squareGrids);
}
return results;
}
private _gridChanged(
current: Bot,
previous: Bot,
calc: BotCalculationContext
) {
if (!previous) {
return true;
} else {
const currentSize = getContextSize(calc, current);
const previousSize = getContextSize(calc, previous);
if (currentSize !== previousSize) {
return true;
} else {
const currentGrid = getBuilderContextGrid(calc, current);
const previousGrid = getBuilderContextGrid(calc, previous);
return !isEqual(currentGrid, previousGrid);
}
}
}
}
export interface WorkspaceMeshDebugInfo {
id: number;
gridCheckResults: GridCheckResults;
}