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
|
import { System, SystemNames } from ".";
import { Game } from "..";
import { Entity } from "../entities";
import { PhysicsConstants } from "../config";
import {
BoundingBox,
ComponentNames,
FacingDirection,
Highlight,
Grid as GridComponent,
} from "../components";
import { Coord2D, Direction, Dimension2D } from "../interfaces";
import { clamp } from "../utils";
export class Grid extends System {
private dimension: Dimension2D;
private grid: Set<string>[][] = [];
constructor(
{ width: columns, height: rows }: Dimension2D,
dimension: Dimension2D,
) {
super(SystemNames.Grid);
this.dimension = dimension;
this.grid = new Array(rows)
.fill(null)
.map(() => new Array(columns).fill(null).map(() => new Set()));
}
public update(dt: number, game: Game) {
this.putUninitializedEntitiesInGrid(game);
this.rebuildGrid(game);
this.highlightEntitiesLookedAt(game);
this.propogateEntityMovements(game);
this.updateMovingEntities(dt, game);
}
private highlightEntitiesLookedAt(game: Game) {
const highlightableEntities = new Set<string>();
game.forEachEntityWithComponent(
ComponentNames.FacingDirection,
(entity) => {
if (!entity.hasComponent(ComponentNames.Grid)) {
return;
}
const grid = entity.getComponent<GridComponent>(ComponentNames.Grid)!;
const facingDirection = entity.getComponent<FacingDirection>(
ComponentNames.FacingDirection,
)!;
const lookingAt = this.getNewGridPosition(
grid.gridPosition,
facingDirection.currentDirection,
);
if (
facingDirection.currentDirection === Direction.NONE ||
this.isOutOfBounds(lookingAt)
) {
return;
}
this.grid[lookingAt.y][lookingAt.x].forEach((id) => {
highlightableEntities.add(id);
});
},
);
highlightableEntities.forEach((id) => {
const entity = game.getEntity(id)!;
if (entity.hasComponent(ComponentNames.Highlight)) {
const highlight = entity.getComponent<Highlight>(
ComponentNames.Highlight,
)!;
highlight.highlight();
}
});
game.forEachEntityWithComponent(ComponentNames.Highlight, (entity) => {
if (!highlightableEntities.has(entity.id)) {
const highlight = entity.getComponent<Highlight>(
ComponentNames.Highlight,
)!;
highlight.unhighlight();
}
});
}
private propogateEntityMovements(game: Game) {
const movingEntities: Entity[] = [];
game.forEachEntityWithComponent(ComponentNames.Grid, (entity) => {
const grid = entity.getComponent<GridComponent>(ComponentNames.Grid)!;
if (grid.movingDirection !== Direction.NONE) {
movingEntities.push(entity);
}
});
// for each moving entity, check the entities in the grid cell it's moving to
// if they are pushable, move them in the same direction
// continue until no more pushable entities are found
for (const entity of movingEntities) {
const grid = entity.getComponent<GridComponent>(ComponentNames.Grid)!;
const { gridPosition, movingDirection } = grid;
grid.movingDirection = Direction.NONE;
entity.addComponent(grid); // default to not moving
let nextGridPosition = this.getNewGridPosition(
gridPosition,
movingDirection,
);
const moving = new Set<string>();
moving.add(entity.id);
while (!this.isOutOfBounds(nextGridPosition)) {
const { x, y } = nextGridPosition;
const entities = Array.from(this.grid[y][x]).map(
(id) => game.getEntity(id)!,
);
if (
entities.some((entity) =>
entity.hasComponent(ComponentNames.Colliding),
)
) {
moving.clear();
break;
}
const pushableEntities = entities.filter((entity) => {
if (!entity.hasComponent(ComponentNames.Grid)) return false;
const { movingDirection } = entity.getComponent<GridComponent>(
ComponentNames.Grid,
)!;
const pushable = entity.hasComponent(ComponentNames.Pushable);
return movingDirection === Direction.NONE && pushable;
});
if (pushableEntities.length === 0) {
break;
}
for (const pushableEntity of pushableEntities) {
moving.add(pushableEntity.id);
}
nextGridPosition = this.getNewGridPosition(
nextGridPosition,
movingDirection,
);
}
for (const id of moving) {
const entity = game.getEntity(id)!;
const grid = entity.getComponent<GridComponent>(ComponentNames.Grid)!;
grid.movingDirection = movingDirection;
entity.addComponent(grid);
}
}
}
private putUninitializedEntitiesInGrid(game: Game) {
game.forEachEntityWithComponent(ComponentNames.Grid, (entity) => {
const grid = entity.getComponent<GridComponent>(ComponentNames.Grid)!;
if (grid.initialized) {
return;
}
const hasBoundingBox = entity.hasComponent(ComponentNames.BoundingBox);
if (!hasBoundingBox) {
return;
}
const boundingBox = entity.getComponent<BoundingBox>(
ComponentNames.BoundingBox,
)!;
boundingBox.center = this.gridToScreenPosition(grid.gridPosition);
boundingBox.dimension = this.dimension;
entity.addComponent(boundingBox);
grid.initialized = true;
entity.addComponent(grid);
});
}
private updateMovingEntities(
dt: number,
game: Game,
velocity = PhysicsConstants.GRID_MOVEMENT_VELOCITY,
) {
game.forEachEntityWithComponent(ComponentNames.Grid, (entity) => {
const grid = entity.getComponent<GridComponent>(ComponentNames.Grid)!;
if (grid.movingDirection === Direction.NONE) {
return;
}
const boundingBox = entity.getComponent<BoundingBox>(
ComponentNames.BoundingBox,
)!;
const newGridPosition = this.getNewGridPosition(
grid.gridPosition,
grid.movingDirection,
);
if (this.isOutOfBounds(newGridPosition)) {
grid.movingDirection = Direction.NONE;
entity.addComponent(grid);
return;
}
let { dx, dy } = { dx: 0, dy: 0 };
switch (grid.movingDirection) {
case Direction.LEFT:
dx = -velocity * dt;
break;
case Direction.RIGHT:
dx = velocity * dt;
break;
case Direction.UP:
dy = -velocity * dt;
break;
case Direction.DOWN:
dy = velocity * dt;
break;
}
const { x, y } = boundingBox.center;
const nextPosition = { x: x + dx, y: y + dy };
const passedCenter = this.isEntityPastCenterWhenMoving(
grid.movingDirection,
newGridPosition,
nextPosition,
);
if (passedCenter) {
// re-align the entity to its new grid position
this.grid[grid.gridPosition.y][grid.gridPosition.x].delete(entity.id);
grid.gridPosition = newGridPosition;
grid.movingDirection = Direction.NONE;
this.grid[grid.gridPosition.y][grid.gridPosition.x].add(entity.id);
entity.addComponent(grid);
boundingBox.center = this.gridToScreenPosition(grid.gridPosition);
entity.addComponent(boundingBox);
return;
}
boundingBox.center = nextPosition;
entity.addComponent(boundingBox);
});
}
private getNewGridPosition(prev: Coord2D, direction: Direction) {
let { x: newX, y: newY } = prev;
switch (direction) {
case Direction.LEFT:
newX -= 1;
break;
case Direction.UP:
newY -= 1;
break;
case Direction.DOWN:
newY += 1;
break;
case Direction.RIGHT:
newX += 1;
break;
}
return { x: newX, y: newY };
}
private isEntityPastCenterWhenMoving(
direction: Direction,
gridPosition: Coord2D,
entityPosition: Coord2D,
) {
const { x, y } = this.gridToScreenPosition(gridPosition);
switch (direction) {
case Direction.LEFT:
return entityPosition.x <= x;
case Direction.RIGHT:
return entityPosition.x >= x;
case Direction.UP:
return entityPosition.y <= y;
case Direction.DOWN:
return entityPosition.y >= y;
}
return false;
}
private gridToScreenPosition(gridPosition: Coord2D) {
const { width, height } = this.dimension;
return {
x: gridPosition.x * width + width / 2,
y: gridPosition.y * height + height / 2,
};
}
private isOutOfBounds(position: Coord2D) {
const isOutOfBoundsX =
clamp(position.x, 0, this.grid[0].length - 1) !== position.x;
const isOutOfBoundsY =
clamp(position.y, 0, this.grid.length - 1) !== position.y;
return isOutOfBoundsX || isOutOfBoundsY;
}
private rebuildGrid(game: Game) {
const movedEntities = new Set<string>();
game.forEachEntityWithComponent(ComponentNames.Grid, (entity) => {
const grid = entity.getComponent<GridComponent>(ComponentNames.Grid)!;
const { x, y } = grid.gridPosition;
if (!this.grid[y][x].has(entity.id)) {
movedEntities.add(entity.id);
this.grid[y][x].add(entity.id);
}
});
this.grid.forEach((row) =>
row.forEach((cell) => {
for (const id of cell) {
if (movedEntities.has(id)) {
cell.delete(id);
}
}
}),
);
movedEntities.forEach((id) => {
const entity = game.getEntity(id)!;
const grid = entity.getComponent<GridComponent>(ComponentNames.Grid)!;
const { x, y } = grid.gridPosition;
this.grid[y][x].add(id);
});
}
}
|