summaryrefslogtreecommitdiff
path: root/src/systems/gridSystem.js
blob: e8a6c82fe767433ed5e1bfb30bb49417e9ba8fea (plain)
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
game.system.GridSystem = ({ xDim, yDim }) => {
  const entitiesGrid = Array(yDim).fill(null).map(() => Array(xDim).fill(null).map(() => new Map()));

  let gridWidth = game.canvas.width / xDim;
  let gridHeight = game.canvas.height / yDim;

  const gameCoordsToGrid = ({ x, y }) => {
    return { x: Math.floor((x+gridWidth/2) / game.canvas.width * xDim), y: Math.floor((y+gridHeight/2) / game.canvas.height * 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 => {
      const entity = entities[id];
      const { x, y } = entity.components.gridPosition;
      entitiesGrid[y][x].set(entity.id, entity);
    });
  }

  const update = (_elapsedTime, entities) => {
    const gridEntities = Object.keys(entities).filter((x) => entities[x].hasComponent("gridPosition")).map((x) => entities[x]);
    rebuildGrid(gridEntities);
    gridEntities.map((entity) => {
      if (entity.hasComponent("appearance")) {
        entity.components.appearance.width = gridWidth;
        entity.components.appearance.height = gridHeight;
      }
      if (entity.hasComponent("position")) {
        let newGridCoords = gameCoordsToGrid(entity.components.position);
        const oldGridCoords = entity.components.gridPosition;
        if (!equivalence(newGridCoords, oldGridCoords)) {
          const momentumVector = unitize({ 
            dx: newGridCoords.x - oldGridCoords.x,
            dy: newGridCoords.y - oldGridCoords.y,
          });

          const proposed = {...newGridCoords};
          if (entity.hasComponent("controllable")) {
            let found = false;
            do {
              found = false;
              const entitiesInCell = entitiesGrid[proposed.y][proposed.x];
              for (let entity of entitiesInCell.values()) {
                if (entity.hasComponent("pushable")) {
                  found = true;
                  entity.addComponent(game.components.Momentum({...momentumVector}));
                }
              }
              proposed.x += momentumVector.dx;
              proposed.y += momentumVector.dy;
              const proposedClampedInBounds = clamp(proposed, xDim-1, yDim-1);
              if (!equivalence(proposedClampedInBounds, proposed)) {
                found = false;
              }
            } while (found);
          }

          if (entity.hasComponent("pushable") || entity.hasComponent("controllable")) {
            for (let e of entitiesGrid[newGridCoords.y][newGridCoords.x].values()) {
              if (e.hasComponent("stop")) {
                newGridCoords = oldGridCoords;
                break;
              }
            }
            entity.addComponent(game.components.Momentum({...momentumVector}));
          }

          entity.components.gridPosition = {...entity.components.gridPosition, ...newGridCoords};

          entity.components.position = {
            ...entity.components.position,
            ...gridCoordsToGame(entity.components.gridPosition)
          };

          if (entity.hasComponent("momentum")) {
            entity.components.momentum.dx = 0;
            entity.components.momentum.dy = 0;
          }
        }
      } else {
        entity.addComponent(game.components.Position({...gridCoordsToGame(entity.components.gridPosition)}));    
      };
    });
  };

  return { entitiesGrid, gameCoordsToGrid, gridCoordsToGame, update, gridWidth, gridHeight };
};