Hot reload for objects when updated inside asset manager so no page reload is required

This commit is contained in:
Dennis Postma 2024-07-06 22:24:29 +02:00
parent e96b9c9ee9
commit 067478055e
6 changed files with 53 additions and 15 deletions

View File

@ -29,11 +29,14 @@
import type { Object } from '@/types' import type { Object } from '@/types'
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue' import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
import { useAssetManagerStore } from '@/stores/assetManager' import { useAssetManagerStore } from '@/stores/assetManager'
import { useZoneEditorStore } from '@/stores/zoneEditor'
import { useSocketStore } from '@/stores/socket' import { useSocketStore } from '@/stores/socket'
import config from '@/config' import config from '@/config'
const socket = useSocketStore() const socket = useSocketStore()
const assetManagerStore = useAssetManagerStore() const assetManagerStore = useAssetManagerStore()
const zoneEditorStore = useZoneEditorStore()
const selectedObject = computed(() => assetManagerStore.selectedObject) const selectedObject = computed(() => assetManagerStore.selectedObject)
const objectName = ref('') const objectName = ref('')
@ -71,6 +74,11 @@ function refreshObjectList() {
socket.connection.emit('gm:object:list', {}, (response: Object[]) => { socket.connection.emit('gm:object:list', {}, (response: Object[]) => {
assetManagerStore.setObjectList(response) assetManagerStore.setObjectList(response)
assetManagerStore.setSelectedObject(null) assetManagerStore.setSelectedObject(null)
if (zoneEditorStore.active) {
console.log('Refreshing object list for zone store')
zoneEditorStore.setObjectList(response)
}
}) })
} }

View File

@ -13,7 +13,7 @@
<template #modalBody> <template #modalBody>
<div class="my-[15px] mx-auto"> <div class="my-[15px] mx-auto">
<div class="columns-2 sm:columns-3 md:columns-4 lg:columns-5 gap-4"> <div class="columns-2 sm:columns-3 md:columns-4 lg:columns-5 gap-4">
<div v-for="(object, index) in objects" :key="index" class="mb-4"> <div v-for="(object, index) in zoneEditorStore.objectList" :key="index" class="mb-4">
<img <img
:src="`${config.server_endpoint}/assets/objects/${object.id}.png`" :src="`${config.server_endpoint}/assets/objects/${object.id}.png`"
alt="Object" alt="Object"
@ -41,14 +41,13 @@ import Modal from '@/components/utilities/Modal.vue'
import type { Object } from '@/types' import type { Object } from '@/types'
const socket = useSocketStore() const socket = useSocketStore()
const objects = ref<Object[]>([])
const isModalOpen = ref(false) const isModalOpen = ref(false)
const zoneEditorStore = useZoneEditorStore() const zoneEditorStore = useZoneEditorStore()
onMounted(async () => { onMounted(async () => {
isModalOpen.value = true isModalOpen.value = true
socket.connection.emit('gm:object:list', {}, (response: Object[]) => { socket.connection.emit('gm:object:list', {}, (response: Object[]) => {
objects.value = response zoneEditorStore.setObjectList(response)
}) })
}) })
</script> </script>

View File

@ -8,7 +8,7 @@
<div class="m-[15px]"> <div class="m-[15px]">
<div class="flex flex-wrap gap-2.5"> <div class="flex flex-wrap gap-2.5">
<img class="w-[64px] h-[32px] cursor-pointer border-2 border-solid border-transparent transition ease duration-300" src="/assets/zone/blank_tile.png" alt="Blank tile" @click="zoneEditorStore.setSelectedTile('blank_tile')" :class="{ selected: zoneEditorStore.selectedTile && zoneEditorStore.selectedTile === 'blank_tile' }" /> <img class="w-[64px] h-[32px] cursor-pointer border-2 border-solid border-transparent transition ease duration-300" src="/assets/zone/blank_tile.png" alt="Blank tile" @click="zoneEditorStore.setSelectedTile('blank_tile')" :class="{ selected: zoneEditorStore.selectedTile && zoneEditorStore.selectedTile === 'blank_tile' }" />
<img class="w-[64px] h-[32px] cursor-pointer border-2 border-solid border-transparent transition ease duration-300" v-for="(tile, index) in tiles" :key="index" :src="`${config.server_endpoint}/assets/tiles/${tile}.png`" alt="Tile" @click="zoneEditorStore.setSelectedTile(tile)" :class="{ selected: zoneEditorStore.selectedTile && zoneEditorStore.selectedTile === tile }" /> <img class="w-[64px] h-[32px] cursor-pointer border-2 border-solid border-transparent transition ease duration-300" v-for="(tile, index) in zoneEditorStore.tileList" :key="index" :src="`${config.server_endpoint}/assets/tiles/${tile}.png`" alt="Tile" @click="zoneEditorStore.setSelectedTile(tile)" :class="{ selected: zoneEditorStore.selectedTile && zoneEditorStore.selectedTile === tile }" />
</div> </div>
</div> </div>
</template> </template>
@ -24,14 +24,13 @@ import { useSocketStore } from '@/stores/socket'
import Modal from '@/components/utilities/Modal.vue' import Modal from '@/components/utilities/Modal.vue'
const socket = useSocketStore() const socket = useSocketStore()
const tiles = ref<string[]>([])
const isModalOpen = ref(false) const isModalOpen = ref(false)
const zoneEditorStore = useZoneEditorStore() const zoneEditorStore = useZoneEditorStore()
onMounted(async () => { onMounted(async () => {
isModalOpen.value = true isModalOpen.value = true
socket.connection.emit('gm:tile:list', {}, (response: string[]) => { socket.connection.emit('gm:tile:list', {}, (response: string[]) => {
tiles.value = response zoneEditorStore.tileList = response
}) })
}) })
</script> </script>

View File

@ -17,7 +17,7 @@ import config from '@/config'
import Tileset = Phaser.Tilemaps.Tileset import Tileset = Phaser.Tilemaps.Tileset
import TilemapLayer = Phaser.Tilemaps.TilemapLayer import TilemapLayer = Phaser.Tilemaps.TilemapLayer
import { Container, TilemapLayer as TilemapLayerC, useScene, Image } from 'phavuer' import { Container, TilemapLayer as TilemapLayerC, useScene, Image } from 'phavuer'
import { onBeforeMount, onBeforeUnmount, ref, toRaw } from 'vue' import { onBeforeMount, onBeforeUnmount, ref, toRaw, watch } from 'vue'
import Controls from '@/components/utilities/Controls.vue' import Controls from '@/components/utilities/Controls.vue'
import { useSocketStore } from '@/stores/socket' import { useSocketStore } from '@/stores/socket'
import Toolbar from '@/components/utilities/zoneEditor/Toolbar.vue' import Toolbar from '@/components/utilities/zoneEditor/Toolbar.vue'
@ -28,6 +28,7 @@ import { placeTile, tileToWorldXY } from '@/services/zone'
import { useAssetStore } from '@/stores/assets' import { useAssetStore } from '@/stores/assets'
import Objects from '@/components/utilities/zoneEditor/Objects.vue' import Objects from '@/components/utilities/zoneEditor/Objects.vue'
import type { Object } from '@/types' import type { Object } from '@/types'
import { storeToRefs } from 'pinia'
const scene = useScene() const scene = useScene()
const socket = useSocketStore() const socket = useSocketStore()
@ -95,10 +96,9 @@ function pencil(tile: Phaser.Tilemaps.Tile) {
if (zoneEditorStore.drawMode === 'object') { if (zoneEditorStore.drawMode === 'object') {
if (zoneEditorStore.selectedObject === null) return if (zoneEditorStore.selectedObject === null) return
console.log('zoneObjects.value', zoneObjects.value)
zoneObjects.value.push({ zoneObjects.value.push({
id: Math.random().toString(10), id: Math.random().toString(10),
object: zoneEditorStore.selectedObject, object: toRaw(zoneEditorStore.selectedObject),
position_x: tileToWorldXY(tiles, tile.x, tile.y).position_x, position_x: tileToWorldXY(tiles, tile.x, tile.y).position_x,
position_y: tileToWorldXY(tiles, tile.x, tile.y).position_y position_y: tileToWorldXY(tiles, tile.x, tile.y).position_y
}) })
@ -121,6 +121,20 @@ function save() {
}) })
} }
const { objectList } = storeToRefs(zoneEditorStore)
watch(objectList, (newObjects) => {
// update objects inside zoneObjects and keep the position
zoneObjects.value = zoneObjects.value.map((object) => {
const newObject = newObjects.find((newObject) => newObject.id === object.object.id)
if (!newObject) return object
return {
...object,
object: newObject
}
})
});
onBeforeMount(() => { onBeforeMount(() => {
exampleTilesArray.forEach((row, y) => row.forEach((tile, x) => placeTile(zone, tiles, x, y, 'blank_tile'))) exampleTilesArray.forEach((row, y) => row.forEach((tile, x) => placeTile(zone, tiles, x, y, 'blank_tile')))
socket.connection.emit('gm:zone_editor:zone:request', { zoneId: socket.character.zoneId }) socket.connection.emit('gm:zone_editor:zone:request', { zoneId: socket.character.zoneId })

View File

@ -1,10 +1,12 @@
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
import { type Asset } from '@/types' import { type Asset, type Object } from '@/types'
import config from '@/config' import config from '@/config'
export const useAssetStore = defineStore('assets', { export const useAssetStore = defineStore('assets', {
state: () => ({ state: () => ({
assets: [] as Asset[] assets: [] as Asset[],
tiles: [] as string[],
objects: [] as Object[],
}), }),
actions: { actions: {
setAssets(assets: Asset[]) { setAssets(assets: Asset[]) {
@ -13,6 +15,12 @@ export const useAssetStore = defineStore('assets', {
addAsset(asset: Asset) { addAsset(asset: Asset) {
this.assets.push(asset) this.assets.push(asset)
}, },
setTiles(tiles: string[]) {
this.tiles = tiles
},
setObjects(objects: Object[]) {
this.objects = objects
},
fetchAssets() { fetchAssets() {
fetch(config.server_endpoint + '/assets') fetch(config.server_endpoint + '/assets')
.then((response) => response.json()) .then((response) => response.json())

View File

@ -1,5 +1,5 @@
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
import type { Object } from '@/types' import type { Object, ZoneObject } from '@/types'
export const useZoneEditorStore = defineStore('zoneEditor', { export const useZoneEditorStore = defineStore('zoneEditor', {
state: () => ({ state: () => ({
@ -7,8 +7,10 @@ export const useZoneEditorStore = defineStore('zoneEditor', {
name: '', name: '',
width: 10, width: 10,
height: 10, height: 10,
tileList: [] as string[],
tiles: [] as string[][], tiles: [] as string[][],
objects: [] as Object[], objectList: [] as Object[],
objects: [] as ZoneObject[],
tool: 'move', tool: 'move',
drawMode: 'tile', drawMode: 'tile',
selectedTile: '', selectedTile: '',
@ -28,16 +30,22 @@ export const useZoneEditorStore = defineStore('zoneEditor', {
setHeight(height: number) { setHeight(height: number) {
this.height = height this.height = height
}, },
setTileList(tiles: string[]) {
this.tileList = tiles
},
setTiles(tiles: string[][]) { setTiles(tiles: string[][]) {
this.tiles = tiles this.tiles = tiles
}, },
updateTile(x: number, y: number, tile: string) { updateTile(x: number, y: number, tile: string) {
this.tiles[y][x] = tile this.tiles[y][x] = tile
}, },
setObjects(objects: Object[]) { setObjectList(objects: Object[]) {
this.objectList = objects
},
setObjects(objects: ZoneObject[]) {
this.objects = objects this.objects = objects
}, },
updateObject(index: number, object: Object) { updateObject(index: number, object: ZoneObject) {
this.objects[index] = object this.objects[index] = object
}, },
setTool(tool: string) { setTool(tool: string) {
@ -59,7 +67,9 @@ export const useZoneEditorStore = defineStore('zoneEditor', {
this.name = '' this.name = ''
this.width = 10 this.width = 10
this.height = 10 this.height = 10
this.tileList = []
this.tiles = [] this.tiles = []
this.objectList = []
this.objects = [] this.objects = []
this.tool = 'move' this.tool = 'move'
this.drawMode = 'tile' this.drawMode = 'tile'