Improved map tile initialising

This commit is contained in:
Dennis Postma 2025-01-10 23:22:32 +01:00
parent 4067ec2585
commit 9de7af961e
5 changed files with 31 additions and 45 deletions

View File

@ -184,7 +184,7 @@ export type Character = {
export type MapCharacter = { export type MapCharacter = {
character: Character character: Character
isMoving?: boolean isMoving: boolean
} }
export type CharacterItem = { export type CharacterItem = {

View File

@ -90,7 +90,9 @@ const updatePosition = (positionX: number, positionY: number, direction: Directi
} }
}, },
onUpdate: (tween) => { onUpdate: (tween) => {
// @ts-ignore
currentPositionX.value = tween.targets[0].x currentPositionX.value = tween.targets[0].x
// @ts-ignore
currentPositionY.value = tween.targets[0].y currentPositionY.value = tween.targets[0].y
}, },
onComplete: () => { onComplete: () => {
@ -149,8 +151,6 @@ watch(
} }
) )
watch(() => props.mapCharacter, updateSprite)
const characterTypeStorage = new CharacterTypeStorage() const characterTypeStorage = new CharacterTypeStorage()
characterTypeStorage.getSpriteId(props.mapCharacter.character.characterType!).then((spriteId) => { characterTypeStorage.getSpriteId(props.mapCharacter.character.characterType!).then((spriteId) => {
console.log(spriteId) console.log(spriteId)
@ -172,8 +172,7 @@ onMounted(() => {
mapStore.setCharacterLoaded(true) mapStore.setCharacterLoaded(true)
// #146 : Set camera position to character, need to be improved still // #146 : Set camera position to character, need to be improved still
// scene.cameras.main.startFollow(charContainer.value as Phaser.GameObjects.Container) scene.cameras.main.startFollow(charContainer.value as Phaser.GameObjects.Container)
// scene.cameras.main.stopFollow()
} }
updatePosition(props.mapCharacter.character.positionX, props.mapCharacter.character.positionY, props.mapCharacter.character.rotation) updatePosition(props.mapCharacter.character.positionX, props.mapCharacter.character.positionY, props.mapCharacter.character.rotation)

View File

@ -1,26 +1,20 @@
<template> <template>
<MapTiles :key="mapStore.mapId" @tileMap:create="tileMap = $event" /> <MapTiles :key="mapStore.mapId" @tileMap:create="tileMap = $event" />
<MapObjects v-if="tileMap" :tilemap="tileMap as Phaser.Tilemaps.Tilemap" /> <!-- <MapObjects v-if="tileMap" :tilemap="tileMap as Phaser.Tilemaps.Tilemap" />-->
<Characters v-if="tileMap" :tilemap="tileMap as Phaser.Tilemaps.Tilemap" /> <Characters v-if="tileMap" :tilemap="tileMap as Phaser.Tilemaps.Tilemap" />
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import type { Map, MapCharacter, mapLoadData, UUID } from '@/application/types' import type { MapCharacter, mapLoadData, UUID } from '@/application/types'
import Characters from '@/components/game/map/Characters.vue' import Characters from '@/components/game/map/Characters.vue'
import MapTiles from '@/components/game/map/MapTiles.vue' import MapTiles from '@/components/game/map/MapTiles.vue'
import MapObjects from '@/components/game/map/PlacedMapObjects.vue' import MapObjects from '@/components/game/map/PlacedMapObjects.vue'
import { loadMapTilesIntoScene } from '@/composables/mapComposable'
import { MapStorage } from '@/storage/storages'
import { useGameStore } from '@/stores/gameStore' import { useGameStore } from '@/stores/gameStore'
import { useMapStore } from '@/stores/mapStore' import { useMapStore } from '@/stores/mapStore'
import { useScene } from 'phavuer' import { onUnmounted, shallowRef } from 'vue'
import { onUnmounted, ref, shallowRef } from 'vue'
const scene = useScene()
const gameStore = useGameStore() const gameStore = useGameStore()
const mapStore = useMapStore() const mapStore = useMapStore()
const mapStorage = new MapStorage()
const mapData = ref<Map>()
const tileMap = shallowRef<Phaser.Tilemaps.Tilemap>() const tileMap = shallowRef<Phaser.Tilemaps.Tilemap>()

View File

@ -1,5 +1,5 @@
<template> <template>
<Controls v-if="isInitialized" :layer="tileLayer" :depth="0" /> <Controls v-if="tileLayer" :layer="tileLayer" :depth="0" />
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
@ -11,17 +11,17 @@ import { FlattenMapArray, loadMapTilesIntoScene, setLayerTiles } from '@/composa
import { MapStorage } from '@/storage/storages' import { MapStorage } from '@/storage/storages'
import { useMapStore } from '@/stores/mapStore' import { useMapStore } from '@/stores/mapStore'
import { useScene } from 'phavuer' import { useScene } from 'phavuer'
import { onBeforeUnmount, onMounted, ref } from 'vue' import { onBeforeUnmount, onMounted, shallowRef } from 'vue'
import Tileset = Phaser.Tilemaps.Tileset
const emit = defineEmits(['tileMap:create']) const emit = defineEmits(['tileMap:create'])
const scene = useScene() const scene = useScene()
const mapStore = useMapStore() const mapStore = useMapStore()
const mapStorage = new MapStorage() const mapStorage = new MapStorage()
let tileMap: Phaser.Tilemaps.Tilemap const tileMap = shallowRef<Phaser.Tilemaps.Tilemap>()
let tileLayer: Phaser.Tilemaps.TilemapLayer const tileLayer = shallowRef<Phaser.Tilemaps.TilemapLayer>()
let isInitialized = ref(false)
function createTileMap(mapData: any) { function createTileMap(mapData: any) {
const mapConfig = new Phaser.Tilemaps.MapData({ const mapConfig = new Phaser.Tilemaps.MapData({
@ -35,48 +35,41 @@ function createTileMap(mapData: any) {
const newTileMap = new Phaser.Tilemaps.Tilemap(scene, mapConfig) const newTileMap = new Phaser.Tilemaps.Tilemap(scene, mapConfig)
emit('tileMap:create', newTileMap) emit('tileMap:create', newTileMap)
return newTileMap return newTileMap
} }
function createTileLayer(mapData: any) { function createTileLayer(currentTileMap: Phaser.Tilemaps.Tilemap, mapData: any) {
const tilesArray = unduplicateArray(FlattenMapArray(mapData?.tiles ?? [])) const tilesArray = unduplicateArray(FlattenMapArray(mapData?.tiles ?? []))
const tilesetImages = Array.from(tilesArray).map((tile: any, index: number) => { const tilesetImages = tilesArray.map((tile: any, index: number) => {
return tileMap.addTilesetImage(tile, tile, config.tile_size.width, config.tile_size.height, 1, 2, index + 1, { x: 0, y: -config.tile_size.height }) return currentTileMap.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 // Add blank tile
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 })) 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 }))
const layer = tileMap.createBlankLayer('tiles', tilesetImages as any, 0, config.tile_size.height) as Phaser.Tilemaps.TilemapLayer
const layer = currentTileMap.createBlankLayer('tiles', tilesetImages as Tileset[], 0, config.tile_size.height) as Phaser.Tilemaps.TilemapLayer
layer.setDepth(0) layer.setDepth(0)
layer.setCullPadding(2, 2) layer.setCullPadding(2, 2)
return layer return layer
} }
async function initialize() {
try {
await loadMapTilesIntoScene(mapStore.mapId as UUID, scene)
const mapData = await mapStorage.get(mapStore.mapId)
tileMap = createTileMap(mapData)
tileLayer = createTileLayer(mapData)
setLayerTiles(tileMap, tileLayer, mapData?.tiles)
isInitialized.value = true
} catch (error) {
console.error('Failed to initialize map:', error)
}
}
onMounted(() => { onMounted(() => {
initialize() loadMapTilesIntoScene(mapStore.mapId as UUID, scene)
.then(() => mapStorage.get(mapStore.mapId))
.then((mapData) => {
tileMap.value = createTileMap(mapData)
tileLayer.value = createTileLayer(tileMap.value, mapData)
setLayerTiles(tileMap.value, tileLayer.value, mapData?.tiles)
})
.catch((error) => console.error('Failed to initialize map:', error))
}) })
onBeforeUnmount(() => { onBeforeUnmount(() => {
if (!tileMap) return if (!tileMap.value) return
tileMap.destroyLayer('tiles') tileMap.value.destroyLayer('tiles')
tileMap.removeAllLayers() tileMap.value.removeAllLayers()
tileMap.destroy() tileMap.value.destroy()
}) })
</script> </script>

View File