39 lines
1.1 KiB
JavaScript
39 lines
1.1 KiB
JavaScript
|
import { Player } from "./player.js";
|
||
|
import { roomManager } from "./roomManager.js";
|
||
|
import { World } from "./world/world.js";
|
||
|
export class Game {
|
||
|
constructor(id, options) {
|
||
|
this.id = id;
|
||
|
this.players = [];
|
||
|
this.oldWorld = [];
|
||
|
this.world = new World(options);
|
||
|
}
|
||
|
addPlayer(id, name, color) {
|
||
|
if (this.getAllNames().includes(name))
|
||
|
return false;
|
||
|
const player = new Player(id, name, color);
|
||
|
this.players.push(player);
|
||
|
return player;
|
||
|
}
|
||
|
removePlayerByID(id) {
|
||
|
this.players = this.players.filter(obj => obj.id != id);
|
||
|
if (this.players.length < 1) {
|
||
|
roomManager.removeGameByID(this.id);
|
||
|
}
|
||
|
}
|
||
|
getAllIDs() {
|
||
|
let ids = [];
|
||
|
for (let i = 0; i < this.players.length; i++) {
|
||
|
ids.push(this.players[i].id);
|
||
|
}
|
||
|
return ids;
|
||
|
}
|
||
|
getAllNames() {
|
||
|
let names = [];
|
||
|
for (let i = 0; i < this.players.length; i++) {
|
||
|
names.push(this.players[i].name);
|
||
|
}
|
||
|
return names;
|
||
|
}
|
||
|
}
|