From 956dcc7fc9459a2720427e6b7b5fd154b07f4fef Mon Sep 17 00:00:00 2001 From: ephphatha Date: Sat, 28 Aug 2021 17:52:21 +1000 Subject: [PATCH] Add helper for selecting animation sprites from Direction The use on PlayerAnimationData could be further simplified by adding a helper to PlayerStruct but that'd be introducing another changed file to the PR... Something for later. The monster version is only used once thanks to the helpers added in previous commits. This was mainly to highlight the similarity between PlayerAnimationData and AnimStruct, AnimStruct could inherit/extend PlayerAnimationData without much trouble. --- Source/items.cpp | 4 ++-- Source/monster.h | 8 +++++++- Source/player.h | 5 +++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Source/items.cpp b/Source/items.cpp index fe96f378d..b17a3e0a5 100644 --- a/Source/items.cpp +++ b/Source/items.cpp @@ -2966,10 +2966,10 @@ void CalcPlrItemVals(Player &player, bool loadgfx) SetPlrAnims(player); if (player._pmode == PM_STAND) { LoadPlrGFX(player, player_graphic::Stand); - player.AnimInfo.ChangeAnimationData(&*player.AnimationData[static_cast(player_graphic::Stand)].CelSpritesForDirections[static_cast(player._pdir)], player._pNFrames, 4); + player.AnimInfo.ChangeAnimationData(&*player.AnimationData[static_cast(player_graphic::Stand)].GetCelSpritesForDirection(player._pdir), player._pNFrames, 4); } else { LoadPlrGFX(player, player_graphic::Walk); - player.AnimInfo.ChangeAnimationData(&*player.AnimationData[static_cast(player_graphic::Walk)].CelSpritesForDirections[static_cast(player._pdir)], player._pWFrames, 1); + player.AnimInfo.ChangeAnimationData(&*player.AnimationData[static_cast(player_graphic::Walk)].GetCelSpritesForDirection(player._pdir), player._pWFrames, 1); } } else { player._pgfxnum = gfxNum; diff --git a/Source/monster.h b/Source/monster.h index 2c8443088..a226a2ecf 100644 --- a/Source/monster.h +++ b/Source/monster.h @@ -133,6 +133,12 @@ enum class LeaderRelation : uint8_t { struct AnimStruct { std::unique_ptr CMem; std::array, 8> CelSpritesForDirections; + + inline const std::optional &GetCelSpritesForDirection(Direction direction) const + { + return CelSpritesForDirections[static_cast(direction)]; + } + int Frames; int Rate; }; @@ -220,7 +226,7 @@ struct Monster { // note: missing field _mAFNum */ void ActivateAnimation(MonsterGraphic graphic, Direction direction) { - auto &celSprite = this->MType->GetAnimData(graphic).CelSpritesForDirections[static_cast(direction)]; + auto &celSprite = this->MType->GetAnimData(graphic).GetCelSpritesForDirection(direction); this->AnimInfo.pCelSprite = celSprite ? &*celSprite : nullptr; } diff --git a/Source/player.h b/Source/player.h index bc6c0fc06..10d780a08 100644 --- a/Source/player.h +++ b/Source/player.h @@ -159,6 +159,11 @@ struct PlayerAnimationData { * Is referenced from CelSprite in CelSpritesForDirections */ std::unique_ptr RawData; + + inline const std::optional &GetCelSpritesForDirection(Direction direction) const + { + return CelSpritesForDirections[static_cast(direction)]; + } }; struct Player {