From 6c7864b4d47e9f3e10513c9b73d94f99812ab3f8 Mon Sep 17 00:00:00 2001 From: Dennis Postma Date: Wed, 5 Feb 2025 18:27:33 +0100 Subject: [PATCH] Moved map character network event logic into characters component, added playAnimation function to characterComposable, finished attack animation --- src/components/game/character/Character.vue | 13 ++++++-- src/components/game/map/Characters.vue | 32 +++++++++++++++++++ src/components/game/map/Map.vue | 27 +--------------- .../useCharacterSpriteComposable.ts | 21 ++++++++++++ src/stores/mapStore.ts | 2 +- 5 files changed, 66 insertions(+), 29 deletions(-) diff --git a/src/components/game/character/Character.vue b/src/components/game/character/Character.vue index 795e0c5..c38f109 100644 --- a/src/components/game/character/Character.vue +++ b/src/components/game/character/Character.vue @@ -28,7 +28,7 @@ const gameStore = useGameStore() const mapStore = useMapStore() const scene = useScene() -const { characterContainer, characterSprite, currentPositionX, currentPositionY, isometricDepth, isFlippedX, updatePosition, calcDirection, updateSprite, initializeSprite, cleanup } = useCharacterSpriteComposable(scene, props.tileMap, props.mapCharacter) +const { characterContainer, characterSprite, currentPositionX, currentPositionY, isometricDepth, isFlippedX, updatePosition, playAnimation, calcDirection, updateSprite, initializeSprite, cleanup } = useCharacterSpriteComposable(scene, props.tileMap, props.mapCharacter) const handlePositionUpdate = (newValues: any, oldValues: any) => { if (!newValues) return @@ -51,7 +51,16 @@ watch( rotation: props.mapCharacter.character.rotation, isAttacking: props.mapCharacter.isAttacking }), - handlePositionUpdate + (oldValues, newValues) => { + handlePositionUpdate(oldValues, newValues) + + if (props.mapCharacter.isAttacking) { + // Play attack animation + playAnimation('attack') + // Disable attack immediately after playing the animation + mapStore.updateCharacterProperty(props.mapCharacter.character.id, 'isAttacking', false) + } + } ) onMounted(async () => { diff --git a/src/components/game/map/Characters.vue b/src/components/game/map/Characters.vue index 3f9172c..611cc88 100644 --- a/src/components/game/map/Characters.vue +++ b/src/components/game/map/Characters.vue @@ -3,12 +3,44 @@ diff --git a/src/components/game/map/Map.vue b/src/components/game/map/Map.vue index 752ad90..832c49e 100644 --- a/src/components/game/map/Map.vue +++ b/src/components/game/map/Map.vue @@ -5,7 +5,7 @@ diff --git a/src/composables/useCharacterSpriteComposable.ts b/src/composables/useCharacterSpriteComposable.ts index fe583f9..ae93e98 100644 --- a/src/composables/useCharacterSpriteComposable.ts +++ b/src/composables/useCharacterSpriteComposable.ts @@ -59,6 +59,26 @@ export function useCharacterSpriteComposable(scene: Phaser.Scene, tilemap: Phase }) } + const playAnimation = (animation: string, loop = false) => { + if (!characterSprite.value || !characterSpriteId.value) return + + const fullAnimationName = `${characterSpriteId.value}-${animation}_${currentDirection.value}` + + // Remove any existing animation complete listeners + characterSprite.value.off(Phaser.Animations.Events.ANIMATION_COMPLETE) + + // Add new listener + characterSprite.value.on(Phaser.Animations.Events.ANIMATION_COMPLETE, () => { + characterSprite.value!.setFrame(0) + characterSprite.value!.setTexture(charTexture.value) + }) + + characterSprite.value.anims.play({ + key: fullAnimationName, + repeat: loop ? -1 : 0 + }) + } + const calcDirection = (oldPositionX: number, oldPositionY: number, newPositionX: number, newPositionY: number): Direction => { if (newPositionY < oldPositionY || newPositionX < oldPositionX) return Direction.NEGATIVE if (newPositionX > oldPositionX || newPositionY > oldPositionY) return Direction.POSITIVE @@ -125,6 +145,7 @@ export function useCharacterSpriteComposable(scene: Phaser.Scene, tilemap: Phase isometricDepth, isFlippedX, updatePosition, + playAnimation, calcDirection, updateSprite, initializeSprite, diff --git a/src/stores/mapStore.ts b/src/stores/mapStore.ts index da2a01f..8537f9a 100644 --- a/src/stores/mapStore.ts +++ b/src/stores/mapStore.ts @@ -27,7 +27,7 @@ export const useMapStore = defineStore('map', { addCharacter(character: MapCharacter) { this.characters.push(character) }, - updateCharacterProperty(characterId: UUID, property: K, value: MapCharacter[K]) { + updateCharacterProperty(characterId: string, property: K, value: MapCharacter[K]) { const character = this.characters.find((char) => char.character.id === characterId) if (character) { character[property] = value