GameManager.js 3.35 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
import Enemy from "./objects/Enemy.js";
import World from "./objects/World.js";
7 8 9 10 11 12

var gameLoop,
  gameArea,
  lastUpdate,
  lastEnemySpawn,
  options,
13
  world,
14 15 16 17 18
  enemies,
  gazePoints;

function onTick() {
  let now = Date.now(),
19
    delta = now - lastUpdate;
20
  updateGazePoints(now);
21 22
  updateEnemies(now, delta);
  GameRenderer.render(getCurrentState(delta));
23 24 25
  lastUpdate = Date.now();
}

26
function getCurrentState(delta) {
27
  return {
28
    delta: delta,
29
    frameRate: options.frameRate,
30
    objects: [...enemies, world],
31 32 33 34 35
    gazePoints: gazePoints,
    debug: options.debug,
  };
}

36 37 38 39 40 41 42 43 44 45 46 47
function updateGazePoints(now) {
  for (let i = gazePoints.length - 1; i >= 0; i--) {
    let age = now - gazePoints[i].createdAt;
    if (age > Config.MAX_GAZE_POINT_AGE) {
      gazePoints.splice(i, 1);
    } else {
      gazePoints[i].relativeAge = age / Config.MAX_GAZE_POINT_AGE;
    }
  }
}

function updateEnemies(now, delta) {
48 49 50 51
  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");
52 53
      enemies.push(Enemy.createEnemy(gameArea.width, -50, gameArea.center.x,
        gameArea.center.y));
54 55 56 57 58
      lastEnemySpawn = now;
    }
  }
  for (let i = enemies.length - 1; i >= 0; i--) {
    // Update enemy's position 
59
    enemies[i].update(delta);
60
    // Check if enemy has hit target
61 62 63 64 65 66 67 68
    if (world.isHitBy(enemies[i])) {
      Logger.log(`Enemy hit target for ${enemies[i].damage} dmg`, "Game");
      // Reduce world health after enemy has hit
      world.health -= enemies[i].damage;
      if (world.health <= 0) {
        onWorldDestroyed();
      }
      // Remove enemy after it has hit target
69
      enemies.splice(i, 1);
70 71 72
      // Skip other checks if enemy has hit target
      // TODO: Add exposion at last position 
      continue;
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    }
    // 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 
      }
    }
  }
}

95 96
function onWorldDestroyed() {
  Logger.log(`World destroyed [Current health ${world.health}]`, "Game");
97
}
Alexander Bazo committed
98

99
class GameManager {
Alexander Bazo committed
100

101
  init(gameOptions) {
102 103 104 105 106 107 108 109 110
    options = gameOptions;
    gameArea = {
      width: options.width,
      height: options.height,
      center: {
        x: options.width / 2,
        y: options.height / 2,
      },
    };
111 112 113 114
    enemies = [];
    gazePoints = [];
    world = new World(gameArea.center.x, gameArea.center.y);
    lastEnemySpawn = 0;
115
  }
Alexander Bazo committed
116

117
  start() {
118

119 120 121 122
    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
123

124 125 126
  addGazePoint(point) {
    gazePoints.push(point);
  }
Alexander Bazo committed
127

Alexander Bazo committed
128 129
}

130
export default new GameManager();