Add missing camera update call. Fix player flag being set on NPCs instead of the player. Add friction and movement.

main
sigonasr2 4 months ago
parent cf7121701a
commit 7df7b5cbec
  1. 16
      src/Hamster.cpp
  2. 9
      src/Hamster.h

@ -59,10 +59,12 @@ Hamster::Hamster(const vf2d spawnPos,const std::string_view img,const PlayerCont
void Hamster::UpdateHamsters(const float fElapsedTime){ void Hamster::UpdateHamsters(const float fElapsedTime){
for(Hamster&h:HAMSTER_LIST){ for(Hamster&h:HAMSTER_LIST){
h.animations.UpdateState(h.internalAnimState,fElapsedTime); h.animations.UpdateState(h.internalAnimState,fElapsedTime);
h.frictionEnabled=true;
if(h.IsPlayerControlled){ if(h.IsPlayerControlled){
h.HandlePlayerControls(); h.HandlePlayerControls();
} }
h.TurnTowardsTargetDirection(); h.TurnTowardsTargetDirection();
h.MoveHamster();
} }
} }
@ -113,9 +115,23 @@ void Hamster::HandlePlayerControls(){
} }
if(aimingDir!=vf2d{}){ if(aimingDir!=vf2d{}){
targetRot=aimingDir.norm().polar().y; 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(){ void Hamster::TurnTowardsTargetDirection(){
util::turn_towards_direction(rot,targetRot,turnSpd*HamsterGame::Game().GetElapsedTime()); 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
}

@ -43,8 +43,8 @@ All rights reserved.
class Hamster{ class Hamster{
enum PlayerControlled{ enum PlayerControlled{
PLAYER_CONTROLLED, PLAYER_CONTROLLED=true,
NPC, NPC=false,
}; };
static std::vector<Hamster>HAMSTER_LIST; static std::vector<Hamster>HAMSTER_LIST;
@ -60,6 +60,10 @@ class Hamster{
float rot{}; float rot{};
float targetRot{}; float targetRot{};
float turnSpd{2.f*geom2d::pi}; float turnSpd{2.f*geom2d::pi};
float maxSpd{128.f};
float timeToMaxSpd{0.5f};
float friction{400.f};
bool frictionEnabled{false};
std::string img; std::string img;
Animate2D::Animation<HamsterGame::AnimationState>animations; Animate2D::Animation<HamsterGame::AnimationState>animations;
Animate2D::AnimationState internalAnimState; Animate2D::AnimationState internalAnimState;
@ -75,4 +79,5 @@ public:
const vf2d&GetPos()const; const vf2d&GetPos()const;
void HandlePlayerControls(); void HandlePlayerControls();
void TurnTowardsTargetDirection(); void TurnTowardsTargetDirection();
void MoveHamster();
}; };
Loading…
Cancel
Save