1
0
forked from noxious/client

POC working new caching method - moved controllers folder, renamed assets to textures, fixed HTTP bug, formatted code

This commit is contained in:
2025-01-07 03:59:08 +01:00
parent 6e30a8530a
commit c2db9b5469
26 changed files with 246 additions and 175 deletions

View File

@ -1,58 +1,58 @@
import { Assets } from '@/dexie/assets'
import config from '@/application/config'
import type { AssetDataT, HttpResponse, Sprite, SpriteAction } from '@/application/types'
import type { HttpResponse, Sprite, SpriteAction, TextureData } from '@/application/types'
import { Textures } from '@/dexie/textures'
import { useGameStore } from '@/stores/gameStore'
const textureLoadingPromises = new Map<string, Promise<boolean>>()
export async function loadTexture(scene: Phaser.Scene, assetData: AssetDataT): Promise<boolean> {
export async function loadTexture(scene: Phaser.Scene, textureData: TextureData): Promise<boolean> {
const gameStore = useGameStore()
const assetStorage = new Assets()
const textures = new Textures()
// Check if the texture is already loaded in Phaser
if (gameStore.game.loadedAssets.find((asset) => asset.key === assetData.key)) {
if (gameStore.game.loadedAssets.find((asset) => asset.key === textureData.key)) {
return Promise.resolve(true)
}
// If there's already a loading promise for this texture, return it
if (textureLoadingPromises.has(assetData.key)) {
return await textureLoadingPromises.get(assetData.key)!
if (textureLoadingPromises.has(textureData.key)) {
return await textureLoadingPromises.get(textureData.key)!
}
// Create new loading promise
const loadingPromise = (async () => {
// Check if the asset is already cached
let asset = await assetStorage.get(assetData.key)
let texture = await textures.get(textureData.key)
// If asset is not found, download it
if (!asset) {
await assetStorage.download(assetData)
asset = await assetStorage.get(assetData.key)
if (!texture) {
await textures.download(textureData)
texture = await textures.get(textureData.key)
}
// If asset is found, add it to the scene
if (asset) {
if (texture) {
return new Promise<boolean>((resolve) => {
// Remove existing texture if it exists
if (scene.textures.exists(asset.key)) {
scene.textures.remove(asset.key)
if (scene.textures.exists(texture.key)) {
scene.textures.remove(texture.key)
}
scene.textures.addBase64(asset.key, asset.data)
scene.textures.once(`addtexture-${asset.key}`, () => {
gameStore.game.loadedAssets.push(assetData)
textureLoadingPromises.delete(assetData.key) // Clean up the promise
scene.textures.addBase64(texture.key, texture.data)
scene.textures.once(`addtexture-${texture.key}`, () => {
gameStore.game.loadedAssets.push(textureData)
textureLoadingPromises.delete(textureData.key) // Clean up the promise
resolve(true)
})
})
}
textureLoadingPromises.delete(assetData.key) // Clean up the promise
textureLoadingPromises.delete(textureData.key) // Clean up the promise
return Promise.resolve(false)
})()
// Store the loading promise
textureLoadingPromises.set(assetData.key, loadingPromise)
textureLoadingPromises.set(textureData.key, loadingPromise)
return loadingPromise
}
@ -74,7 +74,7 @@ export async function loadSpriteTextures(scene: Phaser.Scene, sprite_id: string)
frameWidth: sprite_action.frameWidth,
frameHeight: sprite_action.frameHeight,
frameRate: sprite_action.frameRate
} as AssetDataT)
} as TextureData)
// If the sprite is not animated, skip
if (!sprite_action.isAnimated) continue

View File

@ -1,6 +1,8 @@
import config from '@/application/config'
import type { AssetDataT, HttpResponse, UUID } from '@/application/types'
import type { HttpResponse, TextureData, UUID } from '@/application/types'
import { unduplicateArray } from '@/application/utilities'
import { loadTexture } from '@/composables/gameComposable'
import { MapStorage } from '@/dexie/maps'
import Tilemap = Phaser.Tilemaps.Tilemap
import TilemapLayer = Phaser.Tilemaps.TilemapLayer
@ -86,11 +88,22 @@ export function FlattenMapArray(tiles: string[][]) {
}
export async function loadMapTilesIntoScene(map_id: UUID, scene: Phaser.Scene) {
// Fetch the list of tiles from the server
const tileArray: HttpResponse<AssetDataT[]> = await fetch(config.server_endpoint + '/assets/list_tiles/' + map_id).then((response) => response.json())
const mapStorage = new MapStorage()
const map = await mapStorage.get(map_id)
if (!map) return
const tileArray = unduplicateArray(FlattenMapArray(map.tiles))
console.log(tileArray)
// Load each tile into the scene
for (const tile of tileArray.data ?? []) {
await loadTexture(scene, tile)
for (const tile of tileArray) {
const textureData = {
key: tile,
data: '/textures/tiles/' + tile + '.png',
group: 'tiles',
updatedAt: map.updatedAt // @TODO: Fix this
} as TextureData
await loadTexture(scene, textureData)
}
}