Animations now load dynamically and are cached

This commit is contained in:
2024-10-31 12:31:30 +01:00
parent d402744955
commit 74cbf3f2c8
2 changed files with 56 additions and 52 deletions

View File

@ -24,7 +24,6 @@ export async function loadTexture(scene: Phaser.Scene, assetData: AssetDataT): P
// If asset is found, add it to the scene
if (asset) {
return new Promise<boolean>((resolve) => {
console.log(asset.data)
scene.textures.addBase64(asset.key, asset.data)
scene.textures.once(`addtexture-${asset.key}`, () => {
gameStore.game.loadedAssets.push(asset)
@ -39,6 +38,7 @@ export async function loadTexture(scene: Phaser.Scene, assetData: AssetDataT): P
export async function loadSpriteTextures(scene: Phaser.Scene, sprite: Sprite) {
const sprite_actions = await fetch(config.server_endpoint + '/assets/list_sprite_actions/' + sprite?.id).then((response) => response.json())
for await (const sprite_action of sprite_actions) {
await loadTexture(scene, {
key: sprite_action.key,
data: sprite_action.data,
@ -49,14 +49,18 @@ export async function loadSpriteTextures(scene: Phaser.Scene, sprite: Sprite) {
frameHeight: sprite_action.frameHeight
} as AssetDataT)
if (sprite_action.isAnimated) {
scene.anims.create({
key: sprite_action.key,
frameRate: 7,
frames: scene.anims.generateFrameNumbers(sprite_action.key, { start: 0, end: sprite_action.frameCount! - 1 }),
repeat: -1
})
}
// If the sprite is not animated, skip
if (!sprite_action.isAnimated) continue
// Add the animation to the scene
const anim = scene.textures.get(sprite_action.key)
scene.textures.addSpriteSheet(sprite_action.key, anim, { frameWidth: sprite_action.frameWidth ?? 0, frameHeight: sprite_action.frameHeight ?? 0 })
scene.anims.create({
key: sprite_action.key,
frameRate: 7,
frames: scene.anims.generateFrameNumbers(sprite_action.key, { start: 0, end: sprite_action.frameCount! - 1 }),
repeat: -1
})
}
return true
}