Refactor assetManager, renamed assetManager to assetStorage, renamed AssetT to AssetDataT, added better error handling in authentication service, continued working on dynamic asset loading for both maps and map editor
This commit is contained in:
@ -3,8 +3,9 @@ import Tilemap = Phaser.Tilemaps.Tilemap
|
||||
import TilemapLayer = Phaser.Tilemaps.TilemapLayer
|
||||
import Tileset = Phaser.Tilemaps.Tileset
|
||||
import Tile = Phaser.Tilemaps.Tile
|
||||
import { useAssetManager } from '@/managers/assetManager'
|
||||
import type { AssetT, Zone as ZoneT } from '@/types'
|
||||
import type { AssetDataT, Zone as ZoneT } from '@/types'
|
||||
import { AssetStorage } from '@/storage/assetStorage'
|
||||
import { useGameStore } from '@/stores/gameStore'
|
||||
|
||||
export function getTile(layer: TilemapLayer | Tilemap, x: number, y: number): Tile | undefined {
|
||||
const tile = layer.getTileAtWorldXY(x, y)
|
||||
@ -86,59 +87,40 @@ export function FlattenZoneArray(tiles: string[][]) {
|
||||
}
|
||||
|
||||
export async function loadZoneTilesIntoScene(zone: ZoneT, scene: Phaser.Scene) {
|
||||
const tileArray: AssetT[] = await fetch(config.server_endpoint + '/assets/list_tiles/' + zone.id).then((response) => response.json())
|
||||
console.log(tileArray)
|
||||
// Fetch the list of tiles from the server
|
||||
const tileArray: AssetDataT[] = await fetch(config.server_endpoint + '/assets/list_tiles/' + zone.id).then((response) => response.json())
|
||||
|
||||
// Load each tile into the scene
|
||||
for (const tile of tileArray) {
|
||||
await loadZoneTileTexture(scene, tile.key, tile.updatedAt)
|
||||
await loadTexture(scene, tile)
|
||||
}
|
||||
}
|
||||
|
||||
export async function loadZoneTileTexture(scene: Phaser.Scene, textureId: string, updatedAt: Date): Promise<boolean> {
|
||||
const assetManager = useAssetManager
|
||||
export async function loadTexture(scene: Phaser.Scene, assetData: AssetDataT): Promise<boolean> {
|
||||
const gameStore = useGameStore()
|
||||
const assetStorage = new AssetStorage()
|
||||
|
||||
// Check if the texture is already loaded in Phaser
|
||||
if (scene.textures.exists(textureId)) {
|
||||
if (scene.textures.exists(assetData.key)) {
|
||||
return true
|
||||
}
|
||||
|
||||
let assetData = await assetManager.getAsset(textureId)
|
||||
// Check if the asset is already cached
|
||||
let asset = await assetStorage.get(assetData.key)
|
||||
|
||||
if (!assetData) {
|
||||
await assetManager.downloadAsset(textureId, `/assets/tiles/${textureId}.png`, 'tiles', updatedAt)
|
||||
assetData = await assetManager.getAsset(textureId)
|
||||
// If asset is not found, download it
|
||||
if (!asset) {
|
||||
await assetStorage.download(assetData)
|
||||
asset = await assetStorage.get(assetData.key)
|
||||
}
|
||||
|
||||
if (assetData) {
|
||||
// If asset is found, add it to the scene
|
||||
if (asset) {
|
||||
return new Promise<boolean>((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<boolean> {
|
||||
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/objects/${textureId}.png`, 'objects', updatedAt)
|
||||
assetData = await assetManager.getAsset(textureId)
|
||||
}
|
||||
|
||||
if (assetData) {
|
||||
return new Promise<boolean>((resolve) => {
|
||||
scene.textures.addBase64(textureId, assetData.data)
|
||||
scene.textures.once(`addtexture-${textureId}`, () => {
|
||||
console.log(asset.data)
|
||||
scene.textures.addBase64(asset.key, asset.data)
|
||||
scene.textures.once(`addtexture-${asset.key}`, () => {
|
||||
gameStore.game.loadedAssets.push(asset)
|
||||
resolve(true)
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user