GameManager.js 3.13 KB
Newer Older
1 2 3
import Logger from "../utils/Logger.js";
import Config from "./GameConfig.js";
import Time from "../utils/Time.js";
4
import GameRenderer from "./GameRenderer.js";
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
import Enemy from "./Enemy.js";

var gameLoop,
  gameArea,
  lastUpdate,
  lastEnemySpawn,
  options,
  enemies,
  gazePoints;

function onTick() {
  let now = Date.now(),
    delta = now - lastUpdate,
    adjustment = (Time.ONE_SECOND_IN_MS / options.frameRate) / delta;
  updateEnemies(now, adjustment);
  updateGazePoints(now);
  GameRenderer.render(getCurrentState(now));
  lastUpdate = Date.now();
}

function getCurrentState(now) {
  return {
    delta: now - lastUpdate,
    frameRate: options.frameRate,
    enemies: enemies,
    gazePoints: gazePoints,
    debug: options.debug,
  };
}

function updateEnemies(now, adjustment) {
  let currentGazePosition = gazePoints[gazePoints.length - 1];
  if ((now - lastEnemySpawn) >= Config.ENEMEY_SPAWN_DELAY) {
    if (enemies.length < Config.MAX_NUMBER_OF_ENEMIES) {
      Logger.log("Add new enemy", "Game");
      enemies.push(Enemy.createEnemy(gameArea));
      lastEnemySpawn = now;
    }
  }
  for (let i = enemies.length - 1; i >= 0; i--) {
    // Update enemy's position 
    enemies[i].update(gameArea.center, adjustment);
    // Check if enemy has hit target
    if (enemies[i].distanceTo(gameArea.center) < Config.TARGET_RADIUS) {
      onEnemyHitsTarget(enemies[i].damage);
      enemies.splice(i, 1);
    }
    // Skip other checks if no gaze data is available
    if (currentGazePosition === undefined) {
      continue;
    }
    // Check if enemy was hit
    if (enemies[i].isHitBy({
        x: currentGazePosition.targetX,
        y: currentGazePosition.targetY,
      })) {
      // Update enemy's health 
      enemies[i].health -= Config.DEFAULT_PLAYER_DAMAGE;
      // Remove enemy if destroyed
      if (enemies[i].health <= 0) {
        Logger.log(`Enemy destroyed by player`, "Game");
        enemies.splice(i, 1);
        // TODO: Add exposion at last position 
      }
    }
  }
}

function onEnemyHitsTarget(damage) {
  Logger.log(`Enemy hit target for ${damage} dmg`, "Game");
}

function updateGazePoints(now) {
  let lastIndexForRemoval = -1;
  for (let i = 0; i < gazePoints.length; i++) {
    gazePoints[i].age = now - gazePoints[i].createdAt;
    gazePoints[i].inversedAge = 1 - (gazePoints[i].age / Config.MAX_GAZE_POINT_AGE);
    if (gazePoints[i].age > Config.MAX_GAZE_POINT_AGE) {
      lastIndexForRemoval = i;
    }
  }
  if (lastIndexForRemoval !== -1) {
    gazePoints.splice(0, lastIndexForRemoval + 1);
  }
}
Alexander Bazo committed
90

91
class GameManager {
Alexander Bazo committed
92

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
  constructor() {
    enemies = [];
    gazePoints = [];
    lastEnemySpawn = 0;
  }

  setOptions(gameOptions) {
    options = gameOptions;
    gameArea = {
      width: options.width,
      height: options.height,
      center: {
        x: options.width / 2,
        y: options.height / 2,
      },
    };
  }
Alexander Bazo committed
110

111 112 113 114 115
  start() {
    GameRenderer.init(options.canvas, options.width, options.height, options.version);
    lastUpdate = Date.now();
    gameLoop = setInterval(onTick, (Time.ONE_SECOND_IN_MS / options.frameRate));
  }
Alexander Bazo committed
116

117 118 119
  addGazePoint(point) {
    gazePoints.push(point);
  }
Alexander Bazo committed
120

Alexander Bazo committed
121 122
}

123
export default new GameManager();