Add in wheel and extra Animate2D helper functions.

main
sigonasr2 3 months ago
parent 13908da158
commit 7cf2bb7c3a
  1. BIN
      assets/hamster.png
  2. BIN
      assets/hamster.xcf
  3. 11
      src/Hamster.cpp
  4. 5
      src/Hamster.h
  5. 2
      src/HamsterGame.cpp
  6. 2
      src/HamsterGame.h
  7. 20
      src/olcUTIL_Animate2D.h

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

@ -93,8 +93,15 @@ void Hamster::LoadHamsters(const vf2d startingLoc){
void Hamster::DrawHamsters(TransformedView&tv){ void Hamster::DrawHamsters(TransformedView&tv){
for(Hamster&h:HAMSTER_LIST){ 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,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});
} }
} }
@ -141,6 +148,8 @@ void Hamster::TurnTowardsTargetDirection(){
void Hamster::MoveHamster(){ void Hamster::MoveHamster(){
pos+=vel*HamsterGame::Game().GetElapsedTime(); pos+=vel*HamsterGame::Game().GetElapsedTime();
distanceTravelled+=vel.mag()*HamsterGame::Game().GetElapsedTime();
#pragma region Handle Friction #pragma region Handle Friction
if(frictionEnabled){ if(frictionEnabled){
const vf2d currentVel{vel}; const vf2d currentVel{vel};

@ -48,7 +48,7 @@ class Hamster{
NPC=false, NPC=false,
}; };
enum PlayerState{ enum HamsterState{
NORMAL, NORMAL,
BUMPED, BUMPED,
}; };
@ -74,12 +74,13 @@ class Hamster{
bool frictionEnabled{false}; bool frictionEnabled{false};
float collisionRadius{12.f}; float collisionRadius{12.f};
float bumpTimer{}; float bumpTimer{};
double distanceTravelled{};
std::string img; std::string img;
Animate2D::Animation<HamsterGame::AnimationState>animations; Animate2D::Animation<HamsterGame::AnimationState>animations;
Animate2D::AnimationState internalAnimState; Animate2D::AnimationState internalAnimState;
PlayerControlled IsPlayerControlled; PlayerControlled IsPlayerControlled;
static std::optional<Hamster*>playerHamster; static std::optional<Hamster*>playerHamster;
PlayerState state{NORMAL}; HamsterState state{NORMAL};
public: public:
Hamster(const vf2d spawnPos,const std::string_view img,const PlayerControlled IsPlayerControlled=NPC); Hamster(const vf2d spawnPos,const std::string_view img,const PlayerControlled IsPlayerControlled=NPC);
static const Hamster&GetPlayer(); static const Hamster&GetPlayer();

@ -57,6 +57,8 @@ void HamsterGame::LoadAnimations(){
}; };
LoadAnimation(DEFAULT,"hamster.png",{{0,32},{32,32}},0.3f); 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}; Animate2D::FrameSequence&waterAnimFrames{(*ANIMATED_TILE_IDS.insert({1384,Animate2D::FrameSequence{0.2f}}).first).second};
for(vf2d&sourcePos:std::vector<vf2d>{{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}}){ for(vf2d&sourcePos:std::vector<vf2d>{{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}}}); waterAnimFrames.AddFrame(Animate2D::Frame{&GetGFX("gametiles.png"),{sourcePos,{16,16}}});

@ -52,6 +52,8 @@ class HamsterGame : public olc::PixelGameEngine
public: public:
enum AnimationState{ enum AnimationState{
DEFAULT, DEFAULT,
WHEEL_TOP,
WHEEL_BOTTOM,
}; };
HamsterGame(); HamsterGame();

@ -117,6 +117,11 @@ namespace olc::utils::Animate2D
m_nStyle = nStyle; m_nStyle = nStyle;
} }
inline void ChangeFrameDuration(const float fFrameDuration){
m_fFrameDuration = fFrameDuration;
m_fFrameRate = 1.0f / m_fFrameDuration;
}
// Adds a frame to this sequence // Adds a frame to this sequence
inline void AddFrame(const Frame& frame) 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 // that are animated. Under normal circumstances, it is never updated manually
struct AnimationState struct AnimationState
{ {
private: private:
size_t nIndex = 0; size_t nIndex = 0;
float fTime = 0.0f; float fTime = 0.0f;
@ -212,10 +218,24 @@ namespace olc::utils::Animate2D
{ {
m_vSequences.emplace_back(sequence); m_vSequences.emplace_back(sequence);
m_mapStateIndices[sStateName] = m_vSequences.size() - 1; 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: private:
std::vector<FrameSequence> m_vSequences; std::vector<FrameSequence> m_vSequences;
std::unordered_map<StatesEnum, size_t> m_mapStateIndices; std::unordered_map<StatesEnum, size_t> m_mapStateIndices;
std::unordered_map<size_t,StatesEnum> m_mapIndexStates;
}; };
} }
Loading…
Cancel
Save