GameManager.js 3.12 KB
Newer Older
1
import GameConfig from "../config/GameConfiguration.js";
2 3
import Logger from "../utils/Logger.js";
import Time from "../utils/Time.js";
4
import GameRenderer from "./GameRenderer.js";
5
import Enemy from "./objects/Enemy.js";
6
import Planet from "./objects/Planet.js";
7 8

var gameLoop,
9 10
  stage,
  planet,
11
  enemies,
12 13 14
  gazePoints,
  lastUpdate,
  lastEnemySpawn;
15 16 17

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

25
function getCurrentState(delta) {
26
  return {
27
    delta: delta,
28
    objects: [...enemies, planet],
29 30 31 32
    gazePoints: gazePoints,
  };
}

33 34 35
function updateGazePoints(now) {
  for (let i = gazePoints.length - 1; i >= 0; i--) {
    let age = now - gazePoints[i].createdAt;
36
    if (age > GameConfig.getMaxGazePointAge()) {
37 38
      gazePoints.splice(i, 1);
    } else {
39
      gazePoints[i].relativeAge = age / GameConfig.getMaxGazePointAge();
40 41 42 43 44
    }
  }
}

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

91
function onWorldDestroyed() {
92
  Logger.log(`World destroyed [Current health ${planet.health}]`, "Game");
93
}
Alexander Bazo committed
94

95
class GameManager {
Alexander Bazo committed
96

97 98
  init(canvasStage) {
    stage = canvasStage;
99 100
    enemies = [];
    gazePoints = [];
101
    planet = new Planet(GameConfig.getScreenWidth()/2, GameConfig.getScreenHeight());
102
    lastEnemySpawn = 0;
103
  }
Alexander Bazo committed
104

105
  start() {
106
    GameRenderer.init(stage);
107
    lastUpdate = Date.now();
108
    gameLoop = setInterval(onTick, (Time.ONE_SECOND_IN_MS / GameConfig.getFPS()));
109
  }
Alexander Bazo committed
110

111 112 113
  addGazePoint(point) {
    gazePoints.push(point);
  }
Alexander Bazo committed
114

Alexander Bazo committed
115 116
}

117
export default new GameManager();