summaryrefslogtreecommitdiff
path: root/src/game.js
blob: 07ea67b974775633e9f650ec643bfc1f872a8fb3 (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
let lastTimeStamp;
game.loop = (timeStamp) => {
  let elapsedTime = timeStamp - lastTimeStamp;
  lastTimeStamp = timeStamp;

  game.systemOrder.map((i) => {
    game.systems[i].update(elapsedTime, game.entities);
  });

  requestAnimationFrame(game.loop);
}

game.initialize = () => {
  game.systemOrder = ["render", "physics", "gridSystem", "keyboardInput"];
  game.systems = {
    render: game.system.Render(game.graphics),
    physics: game.system.Physics(),
    gridSystem: game.system.GridSystem({
      xDim: 15,
      yDim: 15,
      canvasWidth: game.canvas.width,
      canvasHeight: game.canvas.height,
    }),
    keyboardInput: game.system.KeyboardInput(),
  };

  game.entities = {};

  Array(400).fill(null).forEach((_, i) => {
    const bigBlue = game.createBigBlue();
    bigBlue.addComponent(game.components.GridPosition({x: Math.floor(Math.random() * 15), y: Math.floor(Math.random() * 13)}));
		bigBlue.addComponent(game.components.Pushable());
    game.entities[bigBlue.id] = bigBlue;
  });

  game.rock = game.createRock();
  game.rock.addComponent(game.components.Position({x: 200, y: 200}));
  game.rock.addComponent(game.components.GridPosition({x: 0, y: 0}));
  game.rock.addComponent(game.components.Controllable({controls: ['left', 'right', 'up', 'down']}));
	game.rock.addComponent(game.components.Pushable());
  game.entities[game.rock.id] = game.rock;

  lastTimeStamp = performance.now()
  requestAnimationFrame(game.loop);
}