GameManager.js 1.2 KB
Newer Older
1
import GameConfig from "../config/GameConfiguration.js";
2 3
import Logger from "../utils/Logger.js";
import Time from "../utils/Time.js";
4
import GameRenderer from "./GameRenderer.js";
5
import Enemy from "./objects/Enemy.js";
6
import Planet from "./objects/Planet.js";
7 8

var gameLoop,
9
playerID,
10
  stage,
11 12
  state,
  lastUpdate = 0,
13
  lastEnemySpawn;
14 15 16

function onTick() {
  let now = Date.now(),
17 18 19 20 21 22 23
    delta = now - lastUpdate,
    adjustment = (Time.ONE_SECOND_IN_MS / GameConfig.getFPS()) / delta;
  if (state) {
    predictStateChanges();
    GameRenderer.render(state, delta)
  }
  lastUpdate = now;
24 25 26
}


27 28
function predictStateChanges() {
  // TODO: Predict local changes to enemy positions
29 30
}

31

32
function onWorldDestroyed() {
33
  Logger.log(`World destroyed [Current health ${planet.health}]`, "Game");
34
}
Alexander Bazo committed
35

36
class GameManager {
Alexander Bazo committed
37

38 39
  init(canvasStage) {
    stage = canvasStage;
40
    state = null;
41
  }
Alexander Bazo committed
42

43 44
  start(newPlayerID) {
    playerID = newPlayerID;
45
    GameRenderer.init(stage);
46
    lastUpdate = Date.now();
47
    gameLoop = setInterval(onTick, (Time.ONE_SECOND_IN_MS / GameConfig.getFPS()));
48
  }
Alexander Bazo committed
49

50 51 52
  addGazePoint(point) {
    gazePoints.push(point);
  }
Alexander Bazo committed
53

54 55 56 57
  syncState(serverState) {
    state = serverState;
  }

Alexander Bazo committed
58 59
}

60
export default new GameManager();