StarGazerState.js 3.92 KB
Newer Older
Alexander Bazo committed
1
const schema = require("@colyseus/schema"),
2 3
    Logger = require("../utils/Logger.js"),
    GazePoint = require("./GazePoint.js"),
Alexander Bazo committed
4
    Enemy = require("./Enemy.js"),
5
    Planet = require("./Planet.js"),
Alexander Bazo committed
6 7
    Schema = schema.Schema,
    ArraySchema = schema.ArraySchema,
8
    GameConfig = require("../config/GameConfig.js");
Alexander Bazo committed
9 10 11 12

class StarGazerState extends Schema {

    constructor() {
13
        Logger.log("Initalizing game state", "Game");
Alexander Bazo committed
14 15
        super();
        this.stateType = "idle";
16 17 18 19
        this.width = GameConfig.getMapWidth();
        this.height = GameConfig.getMapHeight();
        this.gazePoints = new ArraySchema();
        this.planet = new Planet(GameConfig.getMapWidth()/2, GameConfig.getMapHeight());
Alexander Bazo committed
20
        this.enemies = new ArraySchema();
21 22
        this.lastUpdate = Date.now();
        this.lastEnemySpawn = 0;
Alexander Bazo committed
23 24 25
    }

    update(deltaTime) {
26 27 28 29 30
        let now = Date.now(),
            delta = now - this.lastUpdate;
        this.updateGazePoints(now);
        this.updateEnemies(now, delta);
        this.lastUpdate = Date.now();
Alexander Bazo committed
31 32
    }

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

44 45 46 47 48 49 50 51
    updateEnemies(now, delta) {
        let currentGazePosition = this.gazePoints[this.gazePoints.length - 1];
        if ((now - this.lastEnemySpawn) >= GameConfig.getEnemySpawnDelay()) {
            if (this.enemies.length < GameConfig.getMaxNumberOfEnemies()) {
                Logger.log("Add new enemy", "Game");
                this.enemies.push(Enemy.createEnemy(GameConfig.getMapWidth(), 100, this.planet.x, this.planet.y));
                this.lastEnemySpawn = now;
            }
Alexander Bazo committed
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 90 91 92 93 94 95 96
        for (let i = this.enemies.length - 1; i >= 0; i--) {
            // Remove focus from enemy
            this.enemies[i].removeFocus();
            // Update enemy's position 
            this.enemies[i].update(delta);
            // Check if enemy has hit target
            if (this.planet.isHitBy(this.enemies[i])) {
                Logger.log(`Enemy hit target for ${this.enemies[i].damage} dmg`, "Game");
                // Reduce planet health after enemy has hit
                this.planet.health -= this.enemies[i].damage;
                if (this.planet.health <= 0) {
                    this.onWorldDestroyed();
                }
                // Remove enemy after it has hit target
                this.enemies.splice(i, 1);
                // Skip other checks if enemy has hit target
                // TODO: Add exposion at last position 
                continue;
            }
            // Skip other checks if no gaze data is available
            if (currentGazePosition === undefined) {
                continue;
            }
            // Check if enemy was hit
            if (this.enemies[i].isHitBy({
                    x: currentGazePosition.targetX,
                    y: currentGazePosition.targetY,
                })) {
                // Set focus on currently watched enemy
                this.enemies[i].setFocus();
                // Update enemy's health 
                this.enemies[i].health -= GameConfig.getPlayerDamage() / (1000 / delta);
                // Remove enemy if destroyed
                if (this.enemies[i].health <= 0) {
                    Logger.log(`Enemy destroyed by player`, "Game");
                    this.enemies.splice(i, 1);
                    // TODO: Add exposion at last position 
                }
            }
        }
    }

    onWorldDestroyed() {

Alexander Bazo committed
97 98 99 100 101 102 103
    }
}

schema.defineTypes(StarGazerState, {
    stateType: "string",
    width: "number",
    height: "number",
104 105
    planet: Planet,
    gazePoints: [GazePoint],
Alexander Bazo committed
106 107 108 109 110
    enemies: [Enemy],
});


module.exports = StarGazerState;