Dungeon mode

This commit is contained in:
2023-11-01 03:01:57 +01:00
parent d729c8dcbb
commit 6dc6bc5db8
16 changed files with 322 additions and 66 deletions

25
Classes/dungeonData.ts Normal file
View File

@@ -0,0 +1,25 @@
import { game } from "../game"
import { levelState } from "./levelState"
export class dungeonData{
entranceId : string
rooms : levelState[] = []
playerCount : number = 0
constructor(id:string) {
this.entranceId = id
}
getRoom(roomId : string, create : boolean){
let room = this.rooms.find(r=>r.id == roomId)
if(room == null && create){
room = new levelState(roomId, -1)
room.isDungeon = true
this.rooms.push(room)
game.lobbyState.rooms.push(room)
}
return room
}
}

View File

@@ -1,6 +1,7 @@
export class enemyDamageInfo{
sourceId : number
targetId : number
sourceId : number //player/enemy
targetId : number //player/enemy
targetName : string //physics objects
relativePosition : number[]
damage : number
knockback : number

View File

@@ -1,11 +1,78 @@
import { enemyData } from "./enemyData";
import { physicsObject } from "./physicsObject";
import { playerData } from "./playerData";
import { userData } from "./userData";
export class levelState{
id : string
seed : number = -1
hostId : number = -1
enemies : enemyData[] = [];
objects : physicsObject[] = [];
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
}
}

View File

@@ -1,4 +1,5 @@
import { playerData } from "../Classes/playerData";
import { dungeonData } from "./dungeonData";
import { levelState } from "./levelState";
import { userData } from "./userData";
@@ -6,6 +7,7 @@ export class lobbyState{
users : userData[] = [];
players : playerData[] = [];
rooms : levelState[] = []
dungeons : dungeonData[] = []
copyLight(){
let lobby = Object.assign({}, this);
@@ -15,6 +17,7 @@ export class lobbyState{
lobby.players.push(this.players[i].copyLight());
}
lobby.rooms = this.rooms
lobby.dungeons = this.dungeons
return lobby
}
@@ -39,11 +42,112 @@ export class lobbyState{
idx = this.players.findIndex(u=>u.id == id)
if(idx != -1){
let player = this.players[idx]
this.players.splice(idx, 1)
this.roomExit(player.room, player)
}
}
findPlayer(id: number){
return this.players.find(p=>p.id == id)
}
getDungeonRoot(name){
let split = name.split("_")
return split[0] + "_" + split[1] + "_0_0"
}
getDungeon(id : string, create : boolean){
let dungeon = this.dungeons.find(r=>r.entranceId == id)
if(dungeon == null && create){
dungeon = new dungeonData(id)
this.dungeons.push(dungeon)
}
return dungeon
}
getRoom(id: string, create : boolean){
let room = this.rooms.find(r=>r.id == id)
if(room == null && create){
room = new levelState(id, -1)
this.rooms.push(room)
}
return room
}
createRoom(id : string, host : number){
let room = new levelState(id, host)
this.rooms.push(room)
return room
}
roomEnter(targetRoom : levelState, player: playerData){
if(!targetRoom.isDungeon){
let room = this.getRoom(targetRoom.id, true)
room.addPlayer(player)
return room
}
else{
let dungeon = this.getDungeon(this.getDungeonRoot(targetRoom.id), true);
let room = dungeon.getRoom(targetRoom.id, true)
if(room.addPlayer(player)){
dungeon.playerCount += 1;
}
return room
}
}
roomComplete(roomId: string){
let room = this.getRoom(roomId, false)
if(room != null){
room.completed = true
return room
}
return null
}
roomExit(roomId : string, player: playerData){
let room = this.getRoom(roomId, false)
if(room != null){
if(room.removePlayer(player.id)){
if(room.isDungeon){
let dungeon = this.getDungeon(this.getDungeonRoot(roomId), false)
dungeon.playerCount -= 1;
this.tryRemoveDungeon(room.id)
}
else if(room.hostId == -1){
let roomIdx = this.rooms.indexOf(room)
this.rooms.splice(roomIdx, 1)
}
}
return room
}
return null
}
tryRemoveDungeon(roomId : string){
let dungeonIdx = this.dungeons.findIndex(r=>r.entranceId == this.getDungeonRoot(roomId))
if(dungeonIdx == -1) return false
let dungeon = this.dungeons[dungeonIdx]
if(dungeon.playerCount == 0){
this.dungeons.splice(dungeonIdx, 1)
dungeon.rooms.forEach(room=>{
let roomIdx = this.rooms.findIndex(r=>r.id == room.id)
this.rooms.splice(roomIdx, 1)
})
}
return true
}
}

View File

@@ -1,8 +1,16 @@
export class physicsObject{
name : string
templateId : number
//array of floats [3]
kinematic : boolean
position : number[] = [0,0,0]
rotation : number[] = [0,0,0]
velocity : number[] = [0,0,0]
angularVelocity : number[] = [0,0,0]
static copyLight(o: physicsObject) {
let pObject = Object.assign({}, o)
return pObject
}
}

View File

@@ -1,4 +1,3 @@
import { game } from "../game";
import { enemyDamageInfo } from "./enemyDamageInfo";
import { physicsObject } from "./physicsObject";
import { playerInventory } from "./playerInventory";
@@ -15,8 +14,8 @@ export class playerData{
copyLight(){
let player : any = {}
player.id = this.id
player.room = this.room
player.rigidbody = this.rigidbody
//player.room = this.room
player.rigidbody = physicsObject.copyLight(this.rigidbody)
player.damageInfo = this.damageInfo
return player
}

View File

@@ -0,0 +1,6 @@
import { levelState } from "./levelState"
export class roomChangeData{
previousRoom : string
targetRoom : levelState
}

View File

@@ -0,0 +1,8 @@
import { levelState } from "./levelState"
import { playerData } from "./playerData"
export class roomChangeResponse{
player : playerData
newRoom : levelState
previousRoom : levelState
}