diff --git a/index.js b/index.js index 0bd7c56..94371e2 100644 --- a/index.js +++ b/index.js @@ -14,7 +14,7 @@ var app = express(), server = http.createServer(app), gameServer; -Logger.enable(); +// Logger.enable(); app.use(cors()); app.use(express.json()); @@ -29,7 +29,9 @@ gameServer.define(Config.getGameRoomName(), StarGazerRoom); app.use(Config.getSocialRoute(), socialRoutes); app.use(Config.getMonitorRoute(), monitor(gameServer)); // Prrobably shoud be set after game server is created -app.use(Config.getGameRoute(), express.static(Config.getGamePath())); +app.use(Config.getGameRoute(), express.static(Config.getGamePath(), { + maxAge: 100 +})); gameServer.listen(Config.getPort()); Logger.log(`Listening on ws://localhost:${ Config.getPort() }`, "Server"); \ No newline at end of file diff --git a/lib/game/GazePoint.js b/lib/game/GazePoint.js index a0b52c8..80e35eb 100644 --- a/lib/game/GazePoint.js +++ b/lib/game/GazePoint.js @@ -3,11 +3,18 @@ const Schema = schema.Schema; class GazePoint extends Schema { - constructor(screenX, screenY) { + constructor(screenX, screenY, createdAt, player, id) { + super(); this.screenX = screenX; this.screenY = screenY; - this.createdAt = Date.now(); - this.id = this.createdAt; + this.createdAt = createdAt; + this.player = player; + this.id = id; + this.relativeAge = 0; + } + + static fromClientData(data) { + return new GazePoint(data.screenX, data.screenY, data.createdAt, data.player, data.id); } } @@ -16,7 +23,8 @@ schema.defineTypes(GazePoint, { screenX: "number", screenY: "number", createdAt: "number", - player: "text", + relativeAge: "number", + player: "string", id: "number", }); diff --git a/lib/game/StarGazerState.js b/lib/game/StarGazerState.js index 4ba0024..86b96ef 100644 --- a/lib/game/StarGazerState.js +++ b/lib/game/StarGazerState.js @@ -22,6 +22,10 @@ class StarGazerState extends Schema { this.lastEnemySpawn = 0; } + addGazePoint(gazePoint) { + this.gazePoints.push(gazePoint); + } + update(deltaTime) { let now = Date.now(), delta = now - this.lastUpdate; @@ -32,7 +36,7 @@ class StarGazerState extends Schema { updateGazePoints(now) { for (let i = this.gazePoints.length - 1; i >= 0; i--) { - let age = now - gazePoints[i].createdAt; + let age = now - this.gazePoints[i].createdAt; if (age > GameConfig.getMaxGazePointAge()) { this.gazePoints.splice(i, 1); } else { diff --git a/lib/rooms/StarGazerRoom.js b/lib/rooms/StarGazerRoom.js index 9ac0d72..89496fa 100644 --- a/lib/rooms/StarGazerRoom.js +++ b/lib/rooms/StarGazerRoom.js @@ -1,4 +1,6 @@ const colyseus = require("colyseus"), + Logger = require("../utils/Logger.js"); + GazePoint = require("../game/GazePoint.js"); StarGazerState = require("../game/StarGazerState.js"); var deltaTime; @@ -6,26 +8,28 @@ var deltaTime; class StarGazerRoom extends colyseus.Room { onCreate(options) { - console.log("in: onCreate"); - console.log(this); + Logger.log("Creating StarGazerRoom", "Game Room"); this.setSimulationInterval(this.update.bind(this)); this.setState(new StarGazerState()); } onJoin(client, options) { - console.log("in: onJoin"); + Logger.log(`Player ${client.sessionId} joined StarGazerRoom`, "Game Room"); } onMessage(client, message) { - console.log("in: onMessage"); + if(message.type === "gaze") { + message.data.player = client.id; + this.state.addGazePoint(GazePoint.fromClientData(message.data)); + } } onLeave(client, consented) { - console.log("in: onLeave"); + Logger.log(`Player ${client.sessionId} left StarGazerRoom`, "Game Room"); } onDispose() { - console.log("in: onDispose"); + Logger.log("Disposing StarGazerRoom", "Game Room"); } startGame() {