diff --git a/src/Hamster.cpp b/src/Hamster.cpp index 556bb06..fca843f 100644 --- a/src/Hamster.cpp +++ b/src/Hamster.cpp @@ -59,10 +59,12 @@ Hamster::Hamster(const vf2d spawnPos,const std::string_view img,const PlayerCont void Hamster::UpdateHamsters(const float fElapsedTime){ for(Hamster&h:HAMSTER_LIST){ h.animations.UpdateState(h.internalAnimState,fElapsedTime); + h.frictionEnabled=true; if(h.IsPlayerControlled){ h.HandlePlayerControls(); } h.TurnTowardsTargetDirection(); + h.MoveHamster(); } } @@ -113,9 +115,23 @@ void Hamster::HandlePlayerControls(){ } if(aimingDir!=vf2d{}){ targetRot=aimingDir.norm().polar().y; + const vf2d currentVel{vel}; + vel=vf2d{std::min(maxSpd,currentVel.polar().x+(maxSpd*HamsterGame::Game().GetElapsedTime())/timeToMaxSpd),currentVel.polar().y}.cart(); + frictionEnabled=false; } } void Hamster::TurnTowardsTargetDirection(){ util::turn_towards_direction(rot,targetRot,turnSpd*HamsterGame::Game().GetElapsedTime()); +} + +void Hamster::MoveHamster(){ + pos+=vel*HamsterGame::Game().GetElapsedTime(); + + #pragma region Handle Friction + if(frictionEnabled){ + const vf2d currentVel{vel}; + vel=vf2d{std::max(0.f,currentVel.polar().x-friction*HamsterGame::Game().GetElapsedTime()),currentVel.polar().y}.cart(); + } + #pragma endregion } \ No newline at end of file diff --git a/src/Hamster.h b/src/Hamster.h index 1f912b1..1f22d67 100644 --- a/src/Hamster.h +++ b/src/Hamster.h @@ -43,8 +43,8 @@ All rights reserved. class Hamster{ enum PlayerControlled{ - PLAYER_CONTROLLED, - NPC, + PLAYER_CONTROLLED=true, + NPC=false, }; static std::vectorHAMSTER_LIST; @@ -60,6 +60,10 @@ class Hamster{ float rot{}; float targetRot{}; float turnSpd{2.f*geom2d::pi}; + float maxSpd{128.f}; + float timeToMaxSpd{0.5f}; + float friction{400.f}; + bool frictionEnabled{false}; std::string img; Animate2D::Animationanimations; Animate2D::AnimationState internalAnimState; @@ -75,4 +79,5 @@ public: const vf2d&GetPos()const; void HandlePlayerControls(); void TurnTowardsTargetDirection(); + void MoveHamster(); }; \ No newline at end of file