const schema = require("@colyseus/schema"), Logger = require("../utils/Logger.js"), GazePoint = require("./GazePoint.js"), Enemy = require("./Enemy.js"), Planet = require("./Planet.js"), Schema = schema.Schema, ArraySchema = schema.ArraySchema, GameConfig = require("../config/GameConfig.js"); class StarGazerState extends Schema { constructor() { Logger.log("Initalizing game state", "Game"); super(); this.stateType = "idle"; this.width = GameConfig.getMapWidth(); this.height = GameConfig.getMapHeight(); this.gazePoints = new ArraySchema(); this.planet = new Planet(GameConfig.getMapWidth()/2, GameConfig.getMapHeight()); this.enemies = new ArraySchema(); this.lastUpdate = Date.now(); this.lastEnemySpawn = 0; } addGazePoint(gazePoint) { this.gazePoints.push(gazePoint); } update(deltaTime) { let now = Date.now(), delta = now - this.lastUpdate; this.updateGazePoints(now); this.updateEnemies(now, delta); this.lastUpdate = Date.now(); } updateGazePoints(now) { for (let i = this.gazePoints.length - 1; i >= 0; i--) { let age = now - this.gazePoints[i].createdAt; if (age > GameConfig.getMaxGazePointAge()) { this.gazePoints.splice(i, 1); } else { this.gazePoints[i].relativeAge = age / GameConfig.getMaxGazePointAge(); } } } 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; } } 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() { } } schema.defineTypes(StarGazerState, { stateType: "string", width: "number", height: "number", planet: Planet, gazePoints: [GazePoint], enemies: [Enemy], }); module.exports = StarGazerState;