StarGazer.js 1.17 KB
Newer Older
Alexander Bazo committed
1
import Logger from "../utils/Logger.js";
2
import NetClient from "../com/NetClient.js";
Alexander Bazo committed
3 4
import GameManager from "./GameManager.js";

5
var stage, client, provider;
Alexander Bazo committed
6

7
function init(options) {
8
  Logger.log("Starting StarGazer game", "Game");
9 10 11 12 13 14
  initGazeProvider(options);
  initStage(options);
  initConnection(options);
}

function initGazeProvider(options) {
15
  provider = options.gazeDataProvider;
16 17
  provider.addEventListener("dataavailable", onGazeUpdate);
}
Alexander Bazo committed
18

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
function initStage(options) {
  stage = options.canvas;
  GameManager.init(stage);
}

function initConnection(options) {
  client = new NetClient();
  client.addEventListener("connect", onConnect);
  client.addEventListener("stateupdate", onStateUpdate)
  client.connect(options.gameServerURL);
  client.joinGame();
}

function onConnect(event) {
  Logger.log("Connected to game server", "Game");
  GameManager.start(event.ownID);
}

function onStateUpdate(event) {
  GameManager.syncState(event.state);
}

41 42
function onGazeUpdate(event) {
  let gazePoint = event.data;
43
  gazePoint.linkTo(stage);
44
  if (gazePoint.hasLink) {
45 46
    client.sendGazeData(gazePoint);
    //GameManager.addGazePoint(gazePoint);
Alexander Bazo committed
47
  }
Alexander Bazo committed
48 49
}

50 51 52
export default {
  init: init,
};