diff --git a/assets/hamster.png b/assets/hamster.png index 0582080..404137c 100644 Binary files a/assets/hamster.png and b/assets/hamster.png differ diff --git a/assets/hamster.xcf b/assets/hamster.xcf new file mode 100644 index 0000000..27240c8 Binary files /dev/null and b/assets/hamster.xcf differ diff --git a/src/Hamster.cpp b/src/Hamster.cpp index a83e41b..edd6579 100644 --- a/src/Hamster.cpp +++ b/src/Hamster.cpp @@ -93,8 +93,15 @@ void Hamster::LoadHamsters(const vf2d startingLoc){ void Hamster::DrawHamsters(TransformedView&tv){ for(Hamster&h:HAMSTER_LIST){ - const Animate2D::Frame&img{h.GetCurrentAnimation()}; + const Animate2D::FrameSequence&anim{h.animations.GetFrames(h.internalAnimState)}; + const Animate2D::FrameSequence&wheelTopAnim{h.animations.GetFrames(HamsterGame::WHEEL_TOP)}; + const Animate2D::FrameSequence&wheelBottomAnim{h.animations.GetFrames(HamsterGame::WHEEL_BOTTOM)}; + const Animate2D::Frame&img{h.animations.GetState(h.internalAnimState)==HamsterGame::DEFAULT?anim.GetFrame(h.distanceTravelled/100.f):h.GetCurrentAnimation()}; + const Animate2D::Frame&wheelTopImg{wheelTopAnim.GetFrame(h.distanceTravelled/80.f)}; + const Animate2D::Frame&wheelBottomImg{wheelBottomAnim.GetFrame(h.distanceTravelled/80.f)}; + tv.DrawPartialRotatedDecal(h.pos,wheelBottomImg.GetSourceImage()->Decal(),h.rot,wheelBottomImg.GetSourceRect().size/2,wheelBottomImg.GetSourceRect().pos,wheelBottomImg.GetSourceRect().size); tv.DrawPartialRotatedDecal(h.pos,img.GetSourceImage()->Decal(),h.rot,img.GetSourceRect().size/2,img.GetSourceRect().pos,img.GetSourceRect().size); + tv.DrawPartialRotatedDecal(h.pos,wheelTopImg.GetSourceImage()->Decal(),h.rot,wheelTopImg.GetSourceRect().size/2,wheelTopImg.GetSourceRect().pos,wheelTopImg.GetSourceRect().size,{1.f,1.f},{255,255,255,192}); } } @@ -140,6 +147,8 @@ void Hamster::TurnTowardsTargetDirection(){ void Hamster::MoveHamster(){ pos+=vel*HamsterGame::Game().GetElapsedTime(); + + distanceTravelled+=vel.mag()*HamsterGame::Game().GetElapsedTime(); #pragma region Handle Friction if(frictionEnabled){ diff --git a/src/Hamster.h b/src/Hamster.h index cbc30a9..2f0c659 100644 --- a/src/Hamster.h +++ b/src/Hamster.h @@ -48,7 +48,7 @@ class Hamster{ NPC=false, }; - enum PlayerState{ + enum HamsterState{ NORMAL, BUMPED, }; @@ -74,12 +74,13 @@ class Hamster{ bool frictionEnabled{false}; float collisionRadius{12.f}; float bumpTimer{}; + double distanceTravelled{}; std::string img; Animate2D::Animationanimations; Animate2D::AnimationState internalAnimState; PlayerControlled IsPlayerControlled; static std::optionalplayerHamster; - PlayerState state{NORMAL}; + HamsterState state{NORMAL}; public: Hamster(const vf2d spawnPos,const std::string_view img,const PlayerControlled IsPlayerControlled=NPC); static const Hamster&GetPlayer(); diff --git a/src/HamsterGame.cpp b/src/HamsterGame.cpp index f446547..d604f0d 100644 --- a/src/HamsterGame.cpp +++ b/src/HamsterGame.cpp @@ -57,6 +57,8 @@ void HamsterGame::LoadAnimations(){ }; LoadAnimation(DEFAULT,"hamster.png",{{0,32},{32,32}},0.3f); + LoadAnimation(WHEEL_TOP,"hamster.png",{{0,96},{32,96}},0.1f); + LoadAnimation(WHEEL_BOTTOM,"hamster.png",{{64,96},{96,96}},0.1f); Animate2D::FrameSequence&waterAnimFrames{(*ANIMATED_TILE_IDS.insert({1384,Animate2D::FrameSequence{0.2f}}).first).second}; for(vf2d&sourcePos:std::vector{{192+16*0,784},{192+16*1,784},{192+16*2,784},{192+16*3,784},{192+16*4,784},{192+16*5,784},{192+16*6,784},{192+16*7,784}}){ waterAnimFrames.AddFrame(Animate2D::Frame{&GetGFX("gametiles.png"),{sourcePos,{16,16}}}); diff --git a/src/HamsterGame.h b/src/HamsterGame.h index bead40a..a9edb7d 100644 --- a/src/HamsterGame.h +++ b/src/HamsterGame.h @@ -52,6 +52,8 @@ class HamsterGame : public olc::PixelGameEngine public: enum AnimationState{ DEFAULT, + WHEEL_TOP, + WHEEL_BOTTOM, }; HamsterGame(); diff --git a/src/olcUTIL_Animate2D.h b/src/olcUTIL_Animate2D.h index 580b68f..21961d0 100644 --- a/src/olcUTIL_Animate2D.h +++ b/src/olcUTIL_Animate2D.h @@ -117,6 +117,11 @@ namespace olc::utils::Animate2D m_nStyle = nStyle; } + inline void ChangeFrameDuration(const float fFrameDuration){ + m_fFrameDuration = fFrameDuration; + m_fFrameRate = 1.0f / m_fFrameDuration; + } + // Adds a frame to this sequence inline void AddFrame(const Frame& frame) { @@ -165,6 +170,7 @@ namespace olc::utils::Animate2D // that are animated. Under normal circumstances, it is never updated manually struct AnimationState { + private: size_t nIndex = 0; float fTime = 0.0f; @@ -212,10 +218,24 @@ namespace olc::utils::Animate2D { m_vSequences.emplace_back(sequence); m_mapStateIndices[sStateName] = m_vSequences.size() - 1; + m_mapIndexStates[m_vSequences.size() - 1] = sStateName; + } + + inline const FrameSequence&GetFrames(const StatesEnum&sStateName)const{ + return m_vSequences.at(m_mapStateIndices.at(sStateName)); + } + + inline const FrameSequence&GetFrames(const AnimationState& state)const{ + return m_vSequences.at(state.nIndex); + } + + inline const StatesEnum&GetState(const AnimationState& state)const{ + return m_mapIndexStates.at(state.nIndex); } private: std::vector m_vSequences; std::unordered_map m_mapStateIndices; + std::unordered_map m_mapIndexStates; }; } \ No newline at end of file