GameManager.js 2.53 KB
Newer Older
Alexander Bazo committed
1 2 3 4 5
import Logger from "../utils/Logger.js";

const BACKGROUND_COLOR = "rgba(30,30,30,0.5)",
  DEFAULT_GAZE_POINT_RADIUS = 10,
  DEFAULT_GAZE_POINT_COLOR = "#ff007b",
Alexander Bazo committed
6
  FPS_COLOR = "#ffbb00",
Alexander Bazo committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
  MAX_GAZE_POINT_AGE = 500;

var lastUpdate,
  lastDelta;

class GameManger {

  constructor() {
    this.gazePoints = new Map();
  }

  setCanvas(canvas) {
    this.canvas = canvas;
    this.context = this.canvas.getContext("2d");
    this.canvas.style.backgroundColor = BACKGROUND_COLOR;
  }

  setFrameRate(fps) {
    this.fps = fps;
  }

Alexander Bazo committed
28 29 30 31 32 33 34 35
  showFPS() {
    this.shouldDisplayFrameRate = true;
  }

  hideFPS() {
    this.shouldDisplayFrameRate = false;
  }

Alexander Bazo committed
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
  setSize(width, height) {
    this.canvas.width = width;
    this.canvas.height = height;
    this.canvas.style.width = `${width}px`;
    this.canvas.style.height = `${height}px`;
  }

  start() {
    let targetFrameTime = 1000 / this.fps;
    this.setUpdateTime();
    this.loop = setInterval(this.onTick.bind(this), targetFrameTime);
  }

  setUpdateTime() {
    lastUpdate = Date.now();
  }

  setDelta() {
    lastDelta = Date.now() - lastUpdate;
  }

  addGazePoint(point) {
    this.gazePoints.set(point.id, point);
  }

  removeGazePoint(point) {
    this.gazePoints.delete(point.id);
  }

  onTick() {
    this.setDelta();
    this.updateGazePoints();
    this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
    this.drawGazePoints();
Alexander Bazo committed
70 71 72
    if (this.shouldDisplayFrameRate === true) {
      this.drawFrameRate();
    }
Alexander Bazo committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    this.setUpdateTime();
  }

  updateGazePoints() {
    let now = Date.now();
    for (let item of this.gazePoints) {
      let point = item[1];
      if (now - point.createdAt > MAX_GAZE_POINT_AGE) {
        this.removeGazePoint(point);
      }
    }
  }

  drawGazePoints() {
    let now = Date.now();
    this.context.fillStyle = DEFAULT_GAZE_POINT_COLOR;
    for (let item of this.gazePoints) {
      this.context.save();
Alexander Bazo committed
91 92
      this.context.globalAlpha = 1 - (now - item[1].createdAt) /
        MAX_GAZE_POINT_AGE;
Alexander Bazo committed
93 94 95 96 97 98 99 100 101 102
      this.context.beginPath();
      this.context.ellipse(item[1].targetX, item[1].targetY,
        DEFAULT_GAZE_POINT_RADIUS, DEFAULT_GAZE_POINT_RADIUS, Math.PI / 4,
        0, 2 * Math.PI);
      this.context.fill();
      this.context.closePath();
      this.context.restore();
    }
  }

Alexander Bazo committed
103
  drawFrameRate() {
Alexander Bazo committed
104
    let fps = parseInt(1000 / lastDelta);
Alexander Bazo committed
105
    this.context.beginPath();
Alexander Bazo committed
106
    this.context.font = "20px Arial";
Alexander Bazo committed
107
    this.context.fillStyle = FPS_COLOR;
Alexander Bazo committed
108
    this.context.fillText(`FPS: ${fps}`, 30, 30);
Alexander Bazo committed
109 110 111
    this.context.closePath();
  }

Alexander Bazo committed
112 113 114
}

export default GameManger;