Load textures using cache data instead of data sent from server
This commit is contained in:
@ -1,5 +1,4 @@
|
||||
import config from '@/application/config'
|
||||
import type { HttpResponse, Sprite, SpriteAction, TextureData } from '@/application/types'
|
||||
import type { TextureData } from '@/application/types'
|
||||
import { TextureStorage } from '@/storage/textureStorage'
|
||||
import { useGameStore } from '@/stores/gameStore'
|
||||
import { SpriteStorage } from '@/storage/storages'
|
||||
@ -11,7 +10,7 @@ export async function loadTexture(scene: Phaser.Scene, textureData: TextureData)
|
||||
const textureStorage = new TextureStorage()
|
||||
|
||||
// Check if the texture is already loaded in Phaser
|
||||
if (gameStore.game.loadedAssets.find((asset) => asset.key === textureData.key)) {
|
||||
if (gameStore.game.loadedTextures.find((texture) => texture === textureData.key)) {
|
||||
return Promise.resolve(true)
|
||||
}
|
||||
|
||||
@ -41,7 +40,7 @@ export async function loadTexture(scene: Phaser.Scene, textureData: TextureData)
|
||||
|
||||
scene.textures.addBase64(texture.key, texture.data)
|
||||
scene.textures.once(`addtexture-${texture.key}`, () => {
|
||||
gameStore.game.loadedAssets.push(textureData)
|
||||
gameStore.game.loadedTextures.push(textureData.key)
|
||||
textureLoadingPromises.delete(textureData.key) // Clean up the promise
|
||||
resolve(true)
|
||||
})
|
||||
@ -60,20 +59,24 @@ export async function loadTexture(scene: Phaser.Scene, textureData: TextureData)
|
||||
export async function loadSpriteTextures(scene: Phaser.Scene, sprite_id: string) {
|
||||
if (!sprite_id) return
|
||||
|
||||
console.log(sprite_id)
|
||||
// @TODO: Fix this
|
||||
const spriteStorage = new SpriteStorage()
|
||||
const sprite = await spriteStorage.get(sprite_id)
|
||||
|
||||
if (!sprite) return
|
||||
if (!sprite) {
|
||||
console.error('Failed to load sprite:', sprite_id)
|
||||
return
|
||||
}
|
||||
|
||||
for await (const sprite_action of sprite.spriteActions) {
|
||||
await loadTexture(scene, {
|
||||
key: sprite_action.key,
|
||||
data: sprite_action.data,
|
||||
key: sprite_action.id + '_' + sprite_action.action,
|
||||
data: '/textures/sprites/' + sprite.id + '/' + sprite_action.action + '.png',
|
||||
group: sprite_action.isAnimated ? 'sprite_animations' : 'sprites',
|
||||
updatedAt: sprite_action.updatedAt,
|
||||
originX: sprite_action.originX ?? 0,
|
||||
originY: sprite_action.originY ?? 0,
|
||||
originX: sprite_action.originX,
|
||||
originY: sprite_action.originY,
|
||||
isAnimated: sprite_action.isAnimated,
|
||||
frameWidth: sprite_action.frameWidth,
|
||||
frameHeight: sprite_action.frameHeight,
|
||||
|
Reference in New Issue
Block a user