diff --git a/backend/src/SocketIO/GameManager/Game/Classes/Playground/Playground.js b/backend/src/SocketIO/GameManager/Game/Classes/Playground/Playground.js new file mode 100644 index 0000000..27f37e5 --- /dev/null +++ b/backend/src/SocketIO/GameManager/Game/Classes/Playground/Playground.js @@ -0,0 +1,41 @@ +class Playground { + constructor() { + // Spielgröße (width * height) Felder + this.width = 20; + this.height = 20; + + /** @type {Array} */ + this.tiles = this.createPlayground(); + } + + createPlayground() { + const tilesArray = []; + + for (let i = 0; i < this.width; i++) { + const column = []; + for (let j = 0; j < this.height; j++) { + column.push(null); + } + tilesArray.push(column); + } + + return tilesArray; + } + + getTile(x, y) { + if (x < 0 || x >= this.width || y < 0 || y >= this.height) { + return null; // Ungültiges feld + } + return this.tiles[x][y]; + } + + setTile(x, y, object) { + if (x < 0 || x >= this.width || y < 0 || y >= this.height) { + return false; // Ungültiges feld + } + this.tiles[x][y] = object; + return true; + } +} + +module.exports = Playground; \ No newline at end of file diff --git a/backend/src/SocketIO/GameManager/Game/Game.js b/backend/src/SocketIO/GameManager/Game/Game.js index cb6a61e..eee5bc4 100644 --- a/backend/src/SocketIO/GameManager/Game/Game.js +++ b/backend/src/SocketIO/GameManager/Game/Game.js @@ -1,6 +1,7 @@ const socketIO = require("socket.io"); const SocketUser = require("../../Classes/SocketUser"); const GameManager = require("../GameManager"); +const GameLoop = require("./GameLoop"); class Game{ /** @param {socketIO.Server} io @param {GameManager} gameManager @param {number} code */ @@ -10,24 +11,33 @@ class Game{ this.code = code; this.waitingSeconds = 5; - this.gameStart = false; + this.gameStarted = false; /**@type {Array} */ - this.players = [] + this.players = []; - setTimeout(() => { this.waitingForPlayers(true) }, 100) + this.gameLoop = new GameLoop(io, this); + + setTimeout(() => { this.waitingForPlayers(true) }, 100); + } + + startGame(){ + + this.gameStarted = true; } waitingForPlayers(changeTime = false){ if(this.waitingSeconds === 0){ if(this.players.length < 2) { - this.io.to(`game-${this.code}`).emit("gameEnd", "Das Spiel ist zu Ende, da nicht genug Spieler beigetreten sind!"); + this.io.to(`game-${this.code}`).emit( + "gameEnd", + "Das Spiel ist zu Ende, da nicht genug Spieler beigetreten sind!" + ); this.gameManager.games.delete(this.code); return; } - this.gameStart = true; - return this.io.to(`game-${this.code}`).emit("gameStart"); + return this.startGame(); } let msg = ""; diff --git a/backend/src/SocketIO/GameManager/Game/GameLoop.js b/backend/src/SocketIO/GameManager/Game/GameLoop.js new file mode 100644 index 0000000..6c67935 --- /dev/null +++ b/backend/src/SocketIO/GameManager/Game/GameLoop.js @@ -0,0 +1,17 @@ +const socketIO = require("socket.io"); +const GameManager = require("../GameManager"); +const Playground = require("./Classes/Playground/Playground"); + +class GameLoop{ + /** @param {socketIO.Server} io @param {GameManager} game */ + constructor(io, game) { + this.io = io; + this.game = game; + + this.playground = new Playground(); + } + + +} + +module.exports = GameLoop; \ No newline at end of file