index.js 1.78 KB
Newer Older
1
import Config from "./Config.js";
Alexander Bazo committed
2 3
import Logger from "./utils/Logger.js";
import StarGazer from "./game/StarGazer.js";
4 5
import FakeGazeDataProvider from "./gaze/FakeGazeDataProvider.js";
import GazeDataProvider from "./gaze/GazeDataProvider.js";
Alexander Bazo committed
6

7
var canvas = document.querySelector("canvas"),
8 9
  starScreen = document.querySelector("#startScreen"),
  options = {
10 11 12
    useMouse: false, // Defauls set in HTML document with checked attribute
    showDebug: false, // Defauls set in HTML document with checked attribute
    logToConsole: false, // Defauls set in HTML document with checked attribute
13
  };
Alexander Bazo committed
14

Alexander Bazo committed
15
function init() {
16
  loadOptions();
17
  if (options.logToConsole === true) {
18 19
    Logger.enable();
  }
20
  document.querySelector("#versionInfo").innerHTML = `build ${Config.GAME_VERSION}`;
21 22
  document.querySelector("#startButton").addEventListener("click",
    prepareGame);
Alexander Bazo committed
23 24
}

25 26
function loadOptions() {
  options.useMouse = document.querySelector("#useMouseInput").checked;
27 28
  options.showDebug = document.querySelector("#showDebugInfo").checked;
  options.logToConsole = document.querySelector("#logToConsole").checked;
29 30
}

31
function prepareGame() {
32
  loadOptions();
Alexander Bazo committed
33
  StarGazer.init({
34 35
    canvas: canvas,
    fps: Config.TARGET_FPS,
36
    version: `Star Gazer, build ${Config.GAME_VERSION}`,
37
    showDebug: options.showDebug,
38 39
    width: Config.SCREEN_WIDTH,
    height: Config.SCREEN_HEIGHT,
40
    gazeDataProvider: getDataProvider(),
Alexander Bazo committed
41
  });
42
  canvas.requestFullscreen().then(startGame);
Alexander Bazo committed
43 44
}

45 46 47
function startGame() {
	starScreen.classList.add("hidden");
	canvas.classList.remove("hidden");
Alexander Bazo committed
48 49
}

50 51
function getDataProvider() {
  let provider;
52
  if (options.useMouse === true) {
53 54 55 56 57 58
    provider = new FakeGazeDataProvider();
  } else {
    provider = new GazeDataProvider();
  }
  provider.start(Config.GAZE_SERVER_URL);
  return provider;
Alexander Bazo committed
59 60 61
}

init();