diff --git a/src/components/utilities/assetManager/partials/ObjectDetails.vue b/src/components/utilities/assetManager/partials/ObjectDetails.vue index 17e1a25..ab98bf1 100644 --- a/src/components/utilities/assetManager/partials/ObjectDetails.vue +++ b/src/components/utilities/assetManager/partials/ObjectDetails.vue @@ -29,11 +29,14 @@ import type { Object } from '@/types' import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue' import { useAssetManagerStore } from '@/stores/assetManager' +import { useZoneEditorStore } from '@/stores/zoneEditor' import { useSocketStore } from '@/stores/socket' import config from '@/config' const socket = useSocketStore() const assetManagerStore = useAssetManagerStore() +const zoneEditorStore = useZoneEditorStore() + const selectedObject = computed(() => assetManagerStore.selectedObject) const objectName = ref('') @@ -71,6 +74,11 @@ function refreshObjectList() { socket.connection.emit('gm:object:list', {}, (response: Object[]) => { assetManagerStore.setObjectList(response) assetManagerStore.setSelectedObject(null) + + if (zoneEditorStore.active) { + console.log('Refreshing object list for zone store') + zoneEditorStore.setObjectList(response) + } }) } diff --git a/src/components/utilities/zoneEditor/Objects.vue b/src/components/utilities/zoneEditor/Objects.vue index d54b971..cca77c5 100644 --- a/src/components/utilities/zoneEditor/Objects.vue +++ b/src/components/utilities/zoneEditor/Objects.vue @@ -13,7 +13,7 @@ @@ -24,14 +24,13 @@ import { useSocketStore } from '@/stores/socket' import Modal from '@/components/utilities/Modal.vue' const socket = useSocketStore() -const tiles = ref([]) const isModalOpen = ref(false) const zoneEditorStore = useZoneEditorStore() onMounted(async () => { isModalOpen.value = true socket.connection.emit('gm:tile:list', {}, (response: string[]) => { - tiles.value = response + zoneEditorStore.tileList = response }) }) diff --git a/src/components/utilities/zoneEditor/ZoneEditor.vue b/src/components/utilities/zoneEditor/ZoneEditor.vue index 8b8b64c..e76174f 100644 --- a/src/components/utilities/zoneEditor/ZoneEditor.vue +++ b/src/components/utilities/zoneEditor/ZoneEditor.vue @@ -17,7 +17,7 @@ import config from '@/config' import Tileset = Phaser.Tilemaps.Tileset import TilemapLayer = Phaser.Tilemaps.TilemapLayer 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 { useSocketStore } from '@/stores/socket' import Toolbar from '@/components/utilities/zoneEditor/Toolbar.vue' @@ -28,6 +28,7 @@ import { placeTile, tileToWorldXY } from '@/services/zone' import { useAssetStore } from '@/stores/assets' import Objects from '@/components/utilities/zoneEditor/Objects.vue' import type { Object } from '@/types' +import { storeToRefs } from 'pinia' const scene = useScene() const socket = useSocketStore() @@ -95,10 +96,9 @@ function pencil(tile: Phaser.Tilemaps.Tile) { if (zoneEditorStore.drawMode === 'object') { if (zoneEditorStore.selectedObject === null) return - console.log('zoneObjects.value', zoneObjects.value) zoneObjects.value.push({ id: Math.random().toString(10), - object: zoneEditorStore.selectedObject, + object: toRaw(zoneEditorStore.selectedObject), position_x: tileToWorldXY(tiles, tile.x, tile.y).position_x, 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(() => { 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 }) diff --git a/src/stores/assets.ts b/src/stores/assets.ts index 96d1018..5694a5d 100644 --- a/src/stores/assets.ts +++ b/src/stores/assets.ts @@ -1,10 +1,12 @@ import { defineStore } from 'pinia' -import { type Asset } from '@/types' +import { type Asset, type Object } from '@/types' import config from '@/config' export const useAssetStore = defineStore('assets', { state: () => ({ - assets: [] as Asset[] + assets: [] as Asset[], + tiles: [] as string[], + objects: [] as Object[], }), actions: { setAssets(assets: Asset[]) { @@ -13,6 +15,12 @@ export const useAssetStore = defineStore('assets', { addAsset(asset: Asset) { this.assets.push(asset) }, + setTiles(tiles: string[]) { + this.tiles = tiles + }, + setObjects(objects: Object[]) { + this.objects = objects + }, fetchAssets() { fetch(config.server_endpoint + '/assets') .then((response) => response.json()) diff --git a/src/stores/zoneEditor.ts b/src/stores/zoneEditor.ts index 6eac1b1..59389b7 100644 --- a/src/stores/zoneEditor.ts +++ b/src/stores/zoneEditor.ts @@ -1,5 +1,5 @@ import { defineStore } from 'pinia' -import type { Object } from '@/types' +import type { Object, ZoneObject } from '@/types' export const useZoneEditorStore = defineStore('zoneEditor', { state: () => ({ @@ -7,8 +7,10 @@ export const useZoneEditorStore = defineStore('zoneEditor', { name: '', width: 10, height: 10, + tileList: [] as string[], tiles: [] as string[][], - objects: [] as Object[], + objectList: [] as Object[], + objects: [] as ZoneObject[], tool: 'move', drawMode: 'tile', selectedTile: '', @@ -28,16 +30,22 @@ export const useZoneEditorStore = defineStore('zoneEditor', { setHeight(height: number) { this.height = height }, + setTileList(tiles: string[]) { + this.tileList = tiles + }, setTiles(tiles: string[][]) { this.tiles = tiles }, updateTile(x: number, y: number, tile: string) { this.tiles[y][x] = tile }, - setObjects(objects: Object[]) { + setObjectList(objects: Object[]) { + this.objectList = objects + }, + setObjects(objects: ZoneObject[]) { this.objects = objects }, - updateObject(index: number, object: Object) { + updateObject(index: number, object: ZoneObject) { this.objects[index] = object }, setTool(tool: string) { @@ -59,7 +67,9 @@ export const useZoneEditorStore = defineStore('zoneEditor', { this.name = '' this.width = 10 this.height = 10 + this.tileList = [] this.tiles = [] + this.objectList = [] this.objects = [] this.tool = 'move' this.drawMode = 'tile'