100 lines
2.8 KiB
TypeScript
100 lines
2.8 KiB
TypeScript
import { enemyData } from "./enemyData";
|
|
import { playerData } from "./playerData";
|
|
import { propData } from "./propData";
|
|
|
|
export class levelState{
|
|
id : string
|
|
seed : number = -1
|
|
hostId : number = -1
|
|
completed : boolean = false
|
|
isDungeon : boolean = false
|
|
enemies : enemyData[] = []
|
|
objects : propData[] = []
|
|
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
|
|
}
|
|
|
|
copyStart(){
|
|
let level : any = {}
|
|
level.id = this.id
|
|
level.seed = this.seed
|
|
level.hostId = this.hostId
|
|
level.completed = this.completed
|
|
level.isDungeon = this.isDungeon
|
|
if(this.enemies.length > 0) level.enemies = this.enemies.map(e => enemyData.copyStart(e))
|
|
if(this.objects.length > 0) level.objects = this.objects.map(e => propData.copyPropStart(e))
|
|
if(this.players.length > 0) level.players = this.players.map(pl=> pl.copyUpdate())
|
|
return level
|
|
}
|
|
|
|
copyUpdate(){
|
|
let level : any = {}
|
|
level.id = this.id
|
|
level.seed = this.seed
|
|
level.hostId = this.hostId
|
|
level.completed = this.completed
|
|
if(this.enemies.length > 0) level.enemies = this.enemies.map(e => enemyData.copyUpdate(e))
|
|
if(this.objects.length > 0) level.objects = this.objects.map(e => propData.copyPropUpdate(e))
|
|
if(this.players.length > 0) level.players = this.players.map(pl=> pl.copyUpdate())
|
|
return level
|
|
}
|
|
|
|
copyState(){
|
|
let level : any = {}
|
|
level.id = this.id
|
|
level.seed = this.seed
|
|
level.hostId = this.hostId
|
|
level.completed = this.completed
|
|
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
|
|
}
|
|
} |