forked from noxious/client
Showing placed map object works in both game and map editor, updated types, cleaned some logic
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
import config from '@/application/config'
|
||||
import type { Map as MapT, TextureData, Tile as TileT, UUID } from '@/application/types'
|
||||
import type { MapObject, Map as MapT, TextureData, Tile as TileT, UUID } from '@/application/types'
|
||||
import { unduplicateArray } from '@/application/utilities'
|
||||
import { loadTexture } from '@/services/textureService'
|
||||
import { MapStorage, TileStorage } from '@/storage/storages'
|
||||
@ -39,11 +39,6 @@ export function tileToWorldY(layer: TilemapLayer | Tilemap, positionX: number, p
|
||||
|
||||
/**
|
||||
* Can also be used to replace tiles
|
||||
* @param map
|
||||
* @param layer
|
||||
* @param positionX
|
||||
* @param positionY
|
||||
* @param tileName
|
||||
*/
|
||||
export function placeTile(map: Tilemap, layer: TilemapLayer, positionX: number, positionY: number, tileName: string) {
|
||||
let tileImg = map.getTileset(tileName) as Tileset
|
||||
@ -78,7 +73,6 @@ export const calculateIsometricDepth = (positionX: number, positionY: number, wi
|
||||
async function loadTileTextures(tiles: TileT[], scene: Phaser.Scene) {
|
||||
// Load each tile into the scene
|
||||
for (const tile of tiles) {
|
||||
if (!tile) continue
|
||||
const textureData = {
|
||||
key: tile.id,
|
||||
data: '/textures/tiles/' + tile.id + '.png',
|
||||
@ -89,9 +83,10 @@ async function loadTileTextures(tiles: TileT[], scene: Phaser.Scene) {
|
||||
}
|
||||
}
|
||||
|
||||
export async function loadTileTexturesFromMapTileArray(map_id: UUID, scene: Phaser.Scene) {
|
||||
const tileStorage = new TileStorage()
|
||||
export async function loadTileTexturesFromMapTileArray(map_id: string, scene: Phaser.Scene) {
|
||||
const mapStorage = new MapStorage()
|
||||
const tileStorage = new TileStorage()
|
||||
|
||||
const map = await mapStorage.get(map_id)
|
||||
if (!map) return
|
||||
|
||||
@ -101,19 +96,25 @@ export async function loadTileTexturesFromMapTileArray(map_id: UUID, scene: Phas
|
||||
await loadTileTextures(tiles, scene)
|
||||
}
|
||||
|
||||
export async function loadTileTexturesFromTileIds(tileIds: string[], scene: Phaser.Scene) {
|
||||
const tileStorage = new TileStorage()
|
||||
const tiles = await tileStorage.getByIds(tileIds)
|
||||
|
||||
await loadTileTextures(tiles, scene)
|
||||
}
|
||||
|
||||
export async function loadAllTileTextures(scene: Phaser.Scene) {
|
||||
const tileStorage = new TileStorage()
|
||||
const tiles = await tileStorage.getAll()
|
||||
|
||||
await loadTileTextures(tiles, scene)
|
||||
scene.load.start()
|
||||
}
|
||||
|
||||
export async function loadMapObjectTextures(mapObjects: MapObject[], scene: Phaser.Scene) {
|
||||
for (const mapObject of mapObjects) {
|
||||
const textureData = {
|
||||
key: mapObject.id,
|
||||
data: '/textures/map_objects/' + mapObject.id + '.png',
|
||||
group: 'map_objects',
|
||||
updatedAt: mapObject.updatedAt,
|
||||
frameWidth: mapObject.frameWidth,
|
||||
frameHeight: mapObject.frameHeight
|
||||
} as TextureData
|
||||
await loadTexture(scene, textureData)
|
||||
}
|
||||
}
|
||||
|
||||
export function createTileMap(scene: Phaser.Scene, map: MapT) {
|
||||
@ -129,19 +130,20 @@ export function createTileMap(scene: Phaser.Scene, map: MapT) {
|
||||
return new Phaser.Tilemaps.Tilemap(scene, mapConfig)
|
||||
}
|
||||
|
||||
export function createTileLayer(currentTileMap: Phaser.Tilemaps.Tilemap, mapData: MapT) {
|
||||
const tilesArray = unduplicateArray(mapData?.tiles.flat())
|
||||
|
||||
export function createTileLayer(tileMap: Phaser.Tilemaps.Tilemap, tilesArray: string[]) {
|
||||
// Load tiles into tileset
|
||||
const tilesetImages = tilesArray.map((tile: string, index: number) => {
|
||||
return currentTileMap.addTilesetImage(tile, tile, config.tile_size.width, config.tile_size.height, 1, 2, index + 1, { x: 0, y: -config.tile_size.height })
|
||||
return tileMap.addTilesetImage(tile, tile, config.tile_size.width, config.tile_size.height, 1, 2, index + 1, { x: 0, y: -config.tile_size.height })
|
||||
})
|
||||
|
||||
// Add blank tile
|
||||
tilesetImages.push(currentTileMap.addTilesetImage('blank_tile', 'blank_tile', config.tile_size.width, config.tile_size.height, 1, 2, 0, { x: 0, y: -config.tile_size.height }))
|
||||
tilesetImages.push(tileMap.addTilesetImage('blank_tile', 'blank_tile', config.tile_size.width, config.tile_size.height, 1, 2, 0, { x: 0, y: -config.tile_size.height }))
|
||||
|
||||
const layer = currentTileMap.createBlankLayer('tiles', tilesetImages as Tileset[], 0, config.tile_size.height) as Phaser.Tilemaps.TilemapLayer
|
||||
// Create layer
|
||||
const layer = tileMap.createBlankLayer('tiles', tilesetImages as Tileset[], 0, config.tile_size.height) as Phaser.Tilemaps.TilemapLayer
|
||||
|
||||
layer.setDepth(0)
|
||||
layer.setCullPadding(2, 2)
|
||||
|
||||
return layer
|
||||
}
|
||||
|
Reference in New Issue
Block a user