KF-MMO-Server/Classes/levelState.ts

78 lines
1.9 KiB
TypeScript

import { enemyData } from "./enemyData";
import { physicsObject } from "./physicsObject";
import { playerData } from "./playerData";
export class levelState{
id : string
seed : number = -1
hostId : number = -1
completed : boolean = false
isDungeon : boolean = false
enemies : enemyData[] = []
objects : physicsObject[] = []
players : playerData[] = []
getRandomInt(max) {
return Math.floor(Math.random() * max);
}
constructor(id, hostId){
this.id = id
this.hostId = hostId
this.seed = this.getRandomInt(1000000)
this.completed = false
}
copyLight() {
let level : any = {}
level.id = this.id
level.seed = this.seed
level.hostId = this.hostId
level.completed = this.completed
level.enemies = this.enemies
level.objects = this.objects
level.players = this.players.map(pl=>pl.copyLight())
return level
}
isHostValid(){
if(this.hostId == -1) return false
if(this.players.length == 0) return false
let playerIdx = this.players.findIndex(p=>p.id==this.hostId)
if(playerIdx == -1) return false
return true
}
addPlayer(player : playerData) : boolean{
let playerIdx = this.players.findIndex(p=>p.id==player.id)
if(playerIdx != -1) return false
this.players.push(player)
if(this.hostId == -1){
this.hostId = this.players[0].id
}
return true
}
removePlayer(id) : boolean{
let playerIdx = this.players.findIndex(p=>p.id==id)
if(playerIdx == -1) return false
this.players.splice(playerIdx,1)
if(this.hostId == id){
if(this.players.length > 0){
this.hostId = this.players[0].id
}
else{
this.hostId = -1;
}
}
return true
}
}