Added Gamestart Functionality
This commit is contained in:
@@ -22,12 +22,13 @@ class Game{
|
|||||||
}
|
}
|
||||||
|
|
||||||
startGame(){
|
startGame(){
|
||||||
|
this.io.to(`game-${this.code}`).emit("startGame");
|
||||||
this.gameStarted = true;
|
this.gameStarted = true;
|
||||||
|
this.gameLoop.loop();
|
||||||
}
|
}
|
||||||
|
|
||||||
waitingForPlayers(changeTime = false){
|
waitingForPlayers(changeTime = false){
|
||||||
if(this.waitingSeconds === 0){
|
if(this.waitingSeconds <= 0){
|
||||||
if(this.players.length < 2) {
|
if(this.players.length < 2) {
|
||||||
this.io.to(`game-${this.code}`).emit(
|
this.io.to(`game-${this.code}`).emit(
|
||||||
"gameEnd",
|
"gameEnd",
|
||||||
@@ -55,8 +56,10 @@ class Game{
|
|||||||
waitingSeconds: this.waitingSeconds
|
waitingSeconds: this.waitingSeconds
|
||||||
});
|
});
|
||||||
|
|
||||||
if (changeTime) this.waitingSeconds--;
|
if (changeTime){
|
||||||
setTimeout(() => { this.waitingForPlayers(true) }, 1000)
|
this.waitingSeconds--;
|
||||||
|
setTimeout(() => { this.waitingForPlayers(true) }, 1000);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
const socketIO = require("socket.io");
|
const socketIO = require("socket.io");
|
||||||
const GameManager = require("../GameManager");
|
|
||||||
const Playground = require("./Classes/Playground/Playground");
|
const Playground = require("./Classes/Playground/Playground");
|
||||||
|
const Game = require("./Game");
|
||||||
|
|
||||||
class GameLoop{
|
class GameLoop{
|
||||||
/** @param {socketIO.Server} io @param {GameManager} game */
|
/** @param {socketIO.Server} io @param {Game} game */
|
||||||
constructor(io, game) {
|
constructor(io, game) {
|
||||||
this.io = io;
|
this.io = io;
|
||||||
this.game = game;
|
this.game = game;
|
||||||
@@ -11,7 +11,16 @@ class GameLoop{
|
|||||||
this.playground = new Playground();
|
this.playground = new Playground();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
loop(){
|
||||||
|
this.io.to(`game-${this.game.code}`).emit("loop", {
|
||||||
|
code: this.game.code,
|
||||||
|
playground: this.playground
|
||||||
|
});
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
this.loop();
|
||||||
|
}, 250);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = GameLoop;
|
module.exports = GameLoop;
|
||||||
@@ -3,7 +3,7 @@ class ServerConnectionManager {
|
|||||||
/**@type {import("../../../backend/node_modules/socket.io-client".Socket} für Autocompletions VSC*/
|
/**@type {import("../../../backend/node_modules/socket.io-client".Socket} für Autocompletions VSC*/
|
||||||
this.socket = io(`${window.location.protocol}//${window.location.hostname}:${window.location.port}`);
|
this.socket = io(`${window.location.protocol}//${window.location.hostname}:${window.location.port}`);
|
||||||
|
|
||||||
this.basicSetup();
|
this.body = document.getElementsByTagName("body")[0];
|
||||||
|
|
||||||
// Socket.on Handler und Routen
|
// Socket.on Handler und Routen
|
||||||
this.socket.on("waitingForPlayers", (msg) => {
|
this.socket.on("waitingForPlayers", (msg) => {
|
||||||
@@ -13,6 +13,13 @@ class ServerConnectionManager {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.socket.on("gameEnd", (msg) => { this.gameEnd(msg) });
|
this.socket.on("gameEnd", (msg) => { this.gameEnd(msg) });
|
||||||
|
this.socket.on("startGame", () => { this.startGame() });
|
||||||
|
|
||||||
|
this.basicSetup();
|
||||||
|
}
|
||||||
|
|
||||||
|
startGame(){
|
||||||
|
this.body.innerHTML = "<h1>Spielfeld</h1>";
|
||||||
}
|
}
|
||||||
|
|
||||||
gameEnd(msg){
|
gameEnd(msg){
|
||||||
|
|||||||
Reference in New Issue
Block a user