StarGazerState.js 4 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 26 27 28
    addGazePoint(gazePoint) {
        this.gazePoints.push(gazePoint);
    }

Alexander Bazo committed
29
    update(deltaTime) {
30 31 32 33 34
        let now = Date.now(),
            delta = now - this.lastUpdate;
        this.updateGazePoints(now);
        this.updateEnemies(now, delta);
        this.lastUpdate = Date.now();
Alexander Bazo committed
35 36
    }

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

48 49 50 51 52 53 54 55
    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
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 97 98 99 100
        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
101 102 103 104 105 106 107
    }
}

schema.defineTypes(StarGazerState, {
    stateType: "string",
    width: "number",
    height: "number",
108 109
    planet: Planet,
    gazePoints: [GazePoint],
Alexander Bazo committed
110 111 112 113 114
    enemies: [Enemy],
});


module.exports = StarGazerState;