From 7db2ba322c9d496db07e8593fb8fe5c877005c68 Mon Sep 17 00:00:00 2001 From: Dennis Postma Date: Sat, 26 Oct 2024 02:41:16 +0200 Subject: [PATCH] Minor improvements, more work on dynamic asset loading --- .../gameMaster/zoneEditor/EventTiles.vue | 6 +- .../gameMaster/zoneEditor/Objects.vue | 6 +- .../gameMaster/zoneEditor/Tiles.vue | 6 +- src/components/utilities/Notifications.vue | 6 +- src/components/zone/Zone.vue | 8 +-- src/components/zone/ZoneTiles.vue | 31 +++++++--- src/components/zone/partials/ZoneObject.vue | 25 +++------ src/composables/zoneComposable.ts | 29 ++++++++++ src/screens/Game.vue | 56 +++++++++---------- 9 files changed, 105 insertions(+), 68 deletions(-) diff --git a/src/components/gameMaster/zoneEditor/EventTiles.vue b/src/components/gameMaster/zoneEditor/EventTiles.vue index 76ecc63..00ffdb8 100644 --- a/src/components/gameMaster/zoneEditor/EventTiles.vue +++ b/src/components/gameMaster/zoneEditor/EventTiles.vue @@ -8,7 +8,7 @@ import { useZoneEditorStore } from '@/stores/zoneEditorStore' import { Image, useScene } from 'phavuer' import { getTile, tileToWorldX, tileToWorldY } from '@/composables/zoneComposable' import { uuidv4 } from '@/utilities' -import { onBeforeMount, onBeforeUnmount } from 'vue' +import { onMounted, onUnmounted } from 'vue' const scene = useScene() const zoneEditorStore = useZoneEditorStore() @@ -102,14 +102,14 @@ function eraser(pointer: Phaser.Input.Pointer) { zoneEditorStore.zone.zoneEventTiles = zoneEditorStore.zone.zoneEventTiles.filter((eventTile) => eventTile.id !== existingEventTile.id) } -onBeforeMount(() => { +onMounted(() => { scene.input.on(Phaser.Input.Events.POINTER_DOWN, pencil) scene.input.on(Phaser.Input.Events.POINTER_MOVE, pencil) scene.input.on(Phaser.Input.Events.POINTER_DOWN, eraser) scene.input.on(Phaser.Input.Events.POINTER_MOVE, eraser) }) -onBeforeUnmount(() => { +onUnmounted(() => { scene.input.off(Phaser.Input.Events.POINTER_DOWN, pencil) scene.input.off(Phaser.Input.Events.POINTER_MOVE, pencil) scene.input.off(Phaser.Input.Events.POINTER_DOWN, eraser) diff --git a/src/components/gameMaster/zoneEditor/Objects.vue b/src/components/gameMaster/zoneEditor/Objects.vue index 8eacc87..ce0d3aa 100644 --- a/src/components/gameMaster/zoneEditor/Objects.vue +++ b/src/components/gameMaster/zoneEditor/Objects.vue @@ -10,7 +10,7 @@ import { Image, useScene } from 'phavuer' import { useZoneEditorStore } from '@/stores/zoneEditorStore' import type { ZoneObject } from '@/types' import SelectedZoneObject from '@/components/gameMaster/zoneEditor/partials/SelectedZoneObject.vue' -import { onBeforeMount, onBeforeUnmount, ref, watch } from 'vue' +import { onMounted, onUnmounted, ref, watch } from 'vue' const scene = useScene() const zoneEditorStore = useZoneEditorStore() @@ -155,14 +155,14 @@ function deleteZoneObject(id: string) { selectedZoneObject.value = null } -onBeforeMount(() => { +onMounted(() => { scene.input.on(Phaser.Input.Events.POINTER_DOWN, pencil) scene.input.on(Phaser.Input.Events.POINTER_MOVE, pencil) scene.input.on(Phaser.Input.Events.POINTER_DOWN, eraser) scene.input.on(Phaser.Input.Events.POINTER_MOVE, eraser) }) -onBeforeUnmount(() => { +onUnmounted(() => { scene.input.off(Phaser.Input.Events.POINTER_DOWN, pencil) scene.input.off(Phaser.Input.Events.POINTER_MOVE, pencil) scene.input.off(Phaser.Input.Events.POINTER_DOWN, eraser) diff --git a/src/components/gameMaster/zoneEditor/Tiles.vue b/src/components/gameMaster/zoneEditor/Tiles.vue index cfd6c67..b4ad75a 100644 --- a/src/components/gameMaster/zoneEditor/Tiles.vue +++ b/src/components/gameMaster/zoneEditor/Tiles.vue @@ -7,7 +7,7 @@ import config from '@/config' import { useScene } from 'phavuer' import { useGameStore } from '@/stores/gameStore' import { useZoneEditorStore } from '@/stores/zoneEditorStore' -import { onBeforeMount, onBeforeUnmount } from 'vue' +import { onMounted, onUnmounted } from 'vue' import { createTileArray, getTile, placeTile, setLayerTiles } from '@/composables/zoneComposable' import Controls from '@/components/utilities/Controls.vue' @@ -123,7 +123,7 @@ function paint(pointer: Phaser.Input.Pointer) { zoneEditorStore.zone.tiles = createTileArray(zoneTilemap.width, zoneTilemap.height, zoneEditorStore.selectedTile.id) } -onBeforeMount(() => { +onMounted(() => { if (!zoneEditorStore.zone?.tiles) { return } @@ -134,7 +134,7 @@ onBeforeMount(() => { scene.input.on(Phaser.Input.Events.POINTER_DOWN, paint) }) -onBeforeUnmount(() => { +onUnmounted(() => { scene.input.off(Phaser.Input.Events.POINTER_MOVE, pencil) scene.input.off(Phaser.Input.Events.POINTER_MOVE, eraser) scene.input.off(Phaser.Input.Events.POINTER_DOWN, paint) diff --git a/src/components/utilities/Notifications.vue b/src/components/utilities/Notifications.vue index c964e11..f188299 100644 --- a/src/components/utilities/Notifications.vue +++ b/src/components/utilities/Notifications.vue @@ -12,7 +12,7 @@ diff --git a/src/composables/zoneComposable.ts b/src/composables/zoneComposable.ts index 6204483..b8161a4 100644 --- a/src/composables/zoneComposable.ts +++ b/src/composables/zoneComposable.ts @@ -52,6 +52,8 @@ export function placeTile(zone: Tilemap, layer: TilemapLayer, x: number, y: numb } export function setLayerTiles(zone: Tilemap, layer: TilemapLayer, tiles: string[][]) { + if (!tiles) return + tiles.forEach((row: string[], y: number) => { row.forEach((tile: string, x: number) => { placeTile(zone, layer, x, y, tile) @@ -71,6 +73,33 @@ export const calculateIsometricDepth = (x: number, y: number, width: number = 0, return baseDepth + (width + height) / (2 * config.tile_size.x) } +export async function loadZoneTileTexture(scene: Phaser.Scene, textureId: string, updatedAt: Date): Promise { + const assetManager = useAssetManager + + // Check if the texture is already loaded in Phaser + if (scene.textures.exists(textureId)) { + return true + } + + let assetData = await assetManager.getAsset(textureId) + + if (!assetData) { + await assetManager.downloadAsset(textureId, `/assets/tiles/${textureId}.png`, 'tiles', updatedAt) + assetData = await assetManager.getAsset(textureId) + } + + if (assetData) { + return new Promise((resolve) => { + scene.textures.addBase64(textureId, assetData.data) + scene.textures.once(`addtexture-${textureId}`, () => { + resolve(true) + }) + }) + } + + return false +} + export async function loadZoneObjectTexture(scene: Phaser.Scene, textureId: string, updatedAt: Date): Promise { const assetManager = useAssetManager diff --git a/src/screens/Game.vue b/src/screens/Game.vue index a77d9a2..c9cf98d 100644 --- a/src/screens/Game.vue +++ b/src/screens/Game.vue @@ -83,24 +83,24 @@ function preloadScene(scene: Phaser.Scene) { * We're using rex-await-loader to load assets asynchronously * Phaser does not support this out of the box, so we're using this plugin */ - scene.load.rexAwait(async function (successCallback) { - await assetManager.getAssetsByGroup('tiles').then((assets) => { - assets.forEach((asset) => { - if (scene.load.textureManager.exists(asset.key)) return - scene.textures.addBase64(asset.key, asset.data) - }) - }) - - // Load objects - await assetManager.getAssetsByGroup('objects').then((assets) => { - assets.forEach((asset) => { - if (scene.load.textureManager.exists(asset.key)) return - scene.textures.addBase64(asset.key, asset.data) - }) - }) - - successCallback() - }) + // scene.load.rexAwait(async function (successCallback) { + // await assetManager.getAssetsByGroup('tiles').then((assets) => { + // assets.forEach((asset) => { + // if (scene.load.textureManager.exists(asset.key)) return + // scene.textures.addBase64(asset.key, asset.data) + // }) + // }) + // + // // Load objects + // await assetManager.getAssetsByGroup('objects').then((assets) => { + // assets.forEach((asset) => { + // if (scene.load.textureManager.exists(asset.key)) return + // scene.textures.addBase64(asset.key, asset.data) + // }) + // }) + // + // successCallback() + // }) } function createScene(scene: Phaser.Scene) { @@ -108,15 +108,15 @@ function createScene(scene: Phaser.Scene) { * Create sprite animations * This is done here because phaser forces us to */ - assetManager.getAssetsByGroup('sprite_animations').then((assets) => { - assets.forEach((asset) => { - scene.anims.create({ - key: asset.key, - frameRate: 7, - frames: scene.anims.generateFrameNumbers(asset.key, { start: 0, end: asset.frameCount! - 1 }), - repeat: -1 - }) - }) - }) + // assetManager.getAssetsByGroup('sprite_animations').then((assets) => { + // assets.forEach((asset) => { + // scene.anims.create({ + // key: asset.key, + // frameRate: 7, + // frames: scene.anims.generateFrameNumbers(asset.key, { start: 0, end: asset.frameCount! - 1 }), + // repeat: -1 + // }) + // }) + // }) }