diff --git a/src/Hamster.cpp b/src/Hamster.cpp index ceffa1a..177b1ea 100644 --- a/src/Hamster.cpp +++ b/src/Hamster.cpp @@ -72,7 +72,7 @@ void Hamster::UpdateHamsters(const float fElapsedTime){ if(h.IsPlayerControlled){ h.HandlePlayerControls(); }else{ - //TODO: NPC controls. + h.HandleNPCControls(); } } }break; @@ -664,4 +664,58 @@ const bool Hamster::CanMove()const{ const bool Hamster::FlyingInTheAir()const{ return GetState()==FLYING&&hamsterJet.value().GetZ()>0.5f&&GetZ()>0.5f; +} + +void Hamster::HandleNPCControls(){ + vf2d aimingDir{}; + + const HamsterAI::ActionOptRef¤tAction{ai.GetCurrentAction()}; + if(!currentAction.has_value())return; + const HamsterAI::Action&action{currentAction.value().get()}; + + vf2d diff{action.pos-GetPos()}; + if(diff.mag()<16.f){ + if(action.type==HamsterAI::Action::LAUNCH_JET){ + if(HasPowerup(Powerup::JET)){ + SetState(FLYING); + lastSafeLocation.reset(); + if(IsPlayerControlled)HamsterAI::OnJetLaunch(this->pos); + hamsterJet.emplace(*this); + }else{ + //TODO + //If we don't have a Jet and it's required at this point, we must backtrack nodes until we get to a location where one is placed... + //This will be a separate behavioral AI node later on... + } + } + ai.AdvanceToNextAction(); + } + const float moveThreshold{4.f}; + + if(diff.y<-moveThreshold){ + aimingDir+=vf2d{0,-1}; + } + if(diff.x>moveThreshold){ + aimingDir+=vf2d{1,0}; + } + if(diff.y>moveThreshold){ + aimingDir+=vf2d{0,1}; + } + if(diff.x<-moveThreshold){ + aimingDir+=vf2d{-1,0}; + } + if(aimingDir!=vf2d{}){ + targetRot=aimingDir.norm().polar().y; + const vf2d currentVel{vel}; + vel=vf2d{currentVel.polar().x+((GetMaxSpeed()/GetTimeToMaxSpeed())*HamsterGame::Game().GetElapsedTime()),rot}.cart(); + vel=vf2d{std::min(GetMaxSpeed(),vel.polar().x),vel.polar().y}.cart(); + frictionEnabled=false; + } + /* Initiating Flight + if(HasPowerup(Powerup::JET)){ + SetState(FLYING); + lastSafeLocation.reset(); + if(IsPlayerControlled)HamsterAI::OnJetLaunch(this->pos); + hamsterJet.emplace(*this); + } + */ } \ No newline at end of file diff --git a/src/Hamster.h b/src/Hamster.h index 75030dd..a15967f 100644 --- a/src/Hamster.h +++ b/src/Hamster.h @@ -133,6 +133,7 @@ public: const vf2d&GetPos()const; const float&GetZ()const; void HandlePlayerControls(); + void HandleNPCControls(); void TurnTowardsTargetDirection(); void MoveHamster(); void HandleCollision(); diff --git a/src/HamsterAI.cpp b/src/HamsterAI.cpp index 1a269c8..98b9720 100644 --- a/src/HamsterAI.cpp +++ b/src/HamsterAI.cpp @@ -88,7 +88,7 @@ void HamsterAI::OnTextEntryComplete(const std::string&enteredText){ const HamsterAI::ActionOptRef HamsterAI::GetCurrentAction(){ if(actionIndtype=type; file.close(); +} + +const HamsterAI::AIType HamsterAI::GetAIType(){ + return type; } \ No newline at end of file diff --git a/src/HamsterAI.h b/src/HamsterAI.h index a503a5b..84ce289 100644 --- a/src/HamsterAI.h +++ b/src/HamsterAI.h @@ -57,11 +57,12 @@ public: SMART, NORMAL, DUMB, - END, //NOTE: Not at AI type, just used for iteration detection. + END, //NOTE: Not an AI type, just used for iteration detection. }; using ActionOptRef=std::optional>; const ActionOptRef GetCurrentAction(); const ActionOptRef AdvanceToNextAction(); + const AIType GetAIType(); void LoadAI(const std::string&mapName,AIType type); static void OnMove(const vi2d pos); @@ -80,4 +81,5 @@ private: std::vectoractionsToPerform; size_t actionInd{}; static std::optionallastTileWalkedOn; //In World Coords + AIType type{END}; }; \ No newline at end of file