forked from noxious/server
Renamed zone > map
This commit is contained in:
58
src/models/loadedMap.ts
Normal file
58
src/models/loadedMap.ts
Normal file
@ -0,0 +1,58 @@
|
||||
import MapCharacter from './mapCharacter'
|
||||
|
||||
import { UUID } from '#application/types'
|
||||
import { Character } from '#entities/character'
|
||||
import { Map } from '#entities/map'
|
||||
import mapEventTileRepository from '#repositories/mapEventTileRepository'
|
||||
|
||||
class LoadedMap {
|
||||
private readonly map: Map
|
||||
private characters: MapCharacter[] = []
|
||||
|
||||
constructor(map: Map) {
|
||||
this.map = map
|
||||
}
|
||||
|
||||
public getMap(): Map {
|
||||
return this.map
|
||||
}
|
||||
|
||||
public addCharacter(character: Character) {
|
||||
const mapCharacter = new MapCharacter(character)
|
||||
this.characters.push(mapCharacter)
|
||||
}
|
||||
|
||||
public async removeCharacter(id: UUID) {
|
||||
const mapCharacter = this.getCharacterById(id)
|
||||
if (mapCharacter) {
|
||||
await mapCharacter.savePosition()
|
||||
this.characters = this.characters.filter((c) => c.character.id !== id)
|
||||
}
|
||||
}
|
||||
|
||||
public getCharacterById(id: UUID): MapCharacter | undefined {
|
||||
return this.characters.find((c) => c.character.id === id)
|
||||
}
|
||||
|
||||
public getCharactersInMap(): MapCharacter[] {
|
||||
console.log(this.characters)
|
||||
return this.characters
|
||||
}
|
||||
|
||||
public async getGrid(): Promise<number[][]> {
|
||||
let grid: number[][] = Array.from({ length: this.map.height }, () => Array.from({ length: this.map.width }, () => 0))
|
||||
|
||||
const eventTiles = await mapEventTileRepository.getAll(this.map.id)
|
||||
|
||||
// Set the grid values based on the event tiles, these are strings
|
||||
eventTiles.forEach((eventTile) => {
|
||||
if (eventTile.type === 'BLOCK') {
|
||||
grid[eventTile.positionY][eventTile.positionX] = 1
|
||||
}
|
||||
})
|
||||
|
||||
return grid
|
||||
}
|
||||
}
|
||||
|
||||
export default LoadedMap
|
@ -1,58 +0,0 @@
|
||||
import ZoneCharacter from './zoneCharacter'
|
||||
|
||||
import { UUID } from '#application/types'
|
||||
import { Character } from '#entities/character'
|
||||
import { Zone } from '#entities/zone'
|
||||
import zoneEventTileRepository from '#repositories/zoneEventTileRepository'
|
||||
|
||||
class LoadedZone {
|
||||
private readonly zone: Zone
|
||||
private characters: ZoneCharacter[] = []
|
||||
|
||||
constructor(zone: Zone) {
|
||||
this.zone = zone
|
||||
}
|
||||
|
||||
public getZone(): Zone {
|
||||
return this.zone
|
||||
}
|
||||
|
||||
public addCharacter(character: Character) {
|
||||
const zoneCharacter = new ZoneCharacter(character)
|
||||
this.characters.push(zoneCharacter)
|
||||
}
|
||||
|
||||
public async removeCharacter(id: UUID) {
|
||||
const zoneCharacter = this.getCharacterById(id)
|
||||
if (zoneCharacter) {
|
||||
await zoneCharacter.savePosition()
|
||||
this.characters = this.characters.filter((c) => c.character.id !== id)
|
||||
}
|
||||
}
|
||||
|
||||
public getCharacterById(id: UUID): ZoneCharacter | undefined {
|
||||
return this.characters.find((c) => c.character.id === id)
|
||||
}
|
||||
|
||||
public getCharactersInZone(): ZoneCharacter[] {
|
||||
console.log(this.characters)
|
||||
return this.characters
|
||||
}
|
||||
|
||||
public async getGrid(): Promise<number[][]> {
|
||||
let grid: number[][] = Array.from({ length: this.zone.height }, () => Array.from({ length: this.zone.width }, () => 0))
|
||||
|
||||
const eventTiles = await zoneEventTileRepository.getAll(this.zone.id)
|
||||
|
||||
// Set the grid values based on the event tiles, these are strings
|
||||
eventTiles.forEach((eventTile) => {
|
||||
if (eventTile.type === 'BLOCK') {
|
||||
grid[eventTile.positionY][eventTile.positionX] = 1
|
||||
}
|
||||
})
|
||||
|
||||
return grid
|
||||
}
|
||||
}
|
||||
|
||||
export default LoadedZone
|
@ -3,10 +3,10 @@ import { Server } from 'socket.io'
|
||||
import { TSocket } from '#application/types'
|
||||
import { Character } from '#entities/character'
|
||||
import SocketManager from '#managers/socketManager'
|
||||
import ZoneManager from '#managers/zoneManager'
|
||||
import MapManager from '#managers/mapManager'
|
||||
import TeleportService from '#services/teleportService'
|
||||
|
||||
class ZoneCharacter {
|
||||
class MapCharacter {
|
||||
public readonly character: Character
|
||||
public isMoving: boolean = false
|
||||
public currentPath: Array<{ x: number; y: number }> | null = null
|
||||
@ -16,12 +16,12 @@ class ZoneCharacter {
|
||||
}
|
||||
|
||||
public async savePosition() {
|
||||
await this.character.setPositionX(this.character.positionX).setPositionY(this.character.positionY).setRotation(this.character.rotation).setZone(this.character.zone).update()
|
||||
await this.character.setPositionX(this.character.positionX).setPositionY(this.character.positionY).setRotation(this.character.rotation).setMap(this.character.map).update()
|
||||
}
|
||||
|
||||
public async teleport(zoneId: number, targetX: number, targetY: number): Promise<void> {
|
||||
public async teleport(mapId: number, targetX: number, targetY: number): Promise<void> {
|
||||
await TeleportService.teleportCharacter(this.character.id, {
|
||||
targetZoneId: zoneId,
|
||||
targetMapId: mapId,
|
||||
targetX,
|
||||
targetY
|
||||
})
|
||||
@ -34,13 +34,13 @@ class ZoneCharacter {
|
||||
this.currentPath = null
|
||||
await this.savePosition()
|
||||
|
||||
// Leave zone and remove from manager
|
||||
if (this.character.zone) {
|
||||
socket.leave(this.character.zone.id)
|
||||
ZoneManager.removeCharacter(this.character.id)
|
||||
// Leave map and remove from manager
|
||||
if (this.character.map) {
|
||||
socket.leave(this.character.map.id)
|
||||
MapManager.removeCharacter(this.character.id)
|
||||
|
||||
// Notify zone players
|
||||
io.in(this.character.zone.id).emit('zone:character:leave', this.character.id)
|
||||
// Notify map players
|
||||
io.in(this.character.map.id).emit('map:character:leave', this.character.id)
|
||||
}
|
||||
|
||||
// Notify all players
|
||||
@ -51,4 +51,4 @@ class ZoneCharacter {
|
||||
}
|
||||
}
|
||||
|
||||
export default ZoneCharacter
|
||||
export default MapCharacter
|
Reference in New Issue
Block a user