Hinzufügen der Playground- und GameLoop-Klassen zur Spielverwaltung

This commit is contained in:
2025-04-03 20:04:09 +02:00
parent 74ba1730f5
commit cb62a4f82c
3 changed files with 74 additions and 6 deletions

View File

@@ -0,0 +1,41 @@
class Playground {
constructor() {
// Spielgröße (width * height) Felder
this.width = 20;
this.height = 20;
/** @type {Array<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;

View File

@@ -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<SocketUser>} */
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 = "";

View File

@@ -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;