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
|
game.system.Grid = (entitiesGrid) => {
let gridWidth = game.canvas.width / game.config.xDim;
let gridHeight = game.canvas.height / game.config.yDim;
const gameCoordsToGrid = ({ x, y }) => {
return { x: Math.floor((x+gridWidth/2) / game.canvas.width * game.config.yDim), y: Math.floor((y+gridHeight/2) / game.canvas.height * game.config.yDim) };
};
const gridCoordsToGame = ({ x, y }) => {
return { x: x * gridWidth, y: y * gridHeight };
};
const rebuildGrid = (entities) => {
let changedIds = new Set();
entities.map(entity => {
const { x, y } = entity.components.gridPosition;
if (!entitiesGrid[y][x].has(entity.id)) {
changedIds.add(entity.id);
}
});
entitiesGrid.forEach((row) => row.forEach((entitiesInCell) => {
for (let id of entitiesInCell.keys()) {
if (changedIds.has(id)) {
entitiesInCell.delete(id);
}
}
}));
changedIds.forEach(id => {
// TODO: Figure out why we HAVE to use game.entities rather than entities
// Hint: it breaks when changing a level in the menu
const entity = game.entities[id];
const { x, y } = entity.components.gridPosition;
entitiesGrid[y][x].set(entity.id, entity);
});
};
const update = (_elapsedTime, entities, changedIds) => {
gridEntities = Object.keys(entities).filter((x) => entities[x].hasComponent("gridPosition")).map((x) => entities[x]);
const thisChangedIds = new Set();
gridEntities.map((entity) => {
if (entity.hasComponent("appearance")) {
entity.components.appearance.width = gridWidth;
entity.components.appearance.height = gridHeight;
}
if (entity.hasComponent("gridPosition")) {
const oldGridCoords = entity.components.gridPosition;
if (entity.hasComponent("position")) {
const gameCoords = gridCoordsToGame(entity.components.gridPosition);
if (Math.abs(entity.components.position.x - gameCoords.x) >= gridWidth/2 || Math.abs(entity.components.position.y - gameCoords.y) >= gridHeight/2) {
entity.components.gridPosition = gameCoordsToGrid(entity.components.position);
if (entity.hasComponent("momentum")) {
entity.removeComponent("momentum");
}
}
}
if (!entity.hasComponent("position") || !equivalence(entity.components.gridPosition, oldGridCoords)) {
entity.components.position = {
...entity.components.position,
...gridCoordsToGame(entity.components.gridPosition)
};
thisChangedIds.add(entity.id);
}
}
});
rebuildGrid(gridEntities);
return thisChangedIds;
};
return { gameCoordsToGrid, gridCoordsToGame, update, gridWidth, gridHeight };
};
|