AI basic movement integrated.

sigonasr2 3 months ago
parent 337b9ecc07
commit ad3b2e433b
  1. 56
      src/Hamster.cpp
  2. 1
      src/Hamster.h
  3. 7
      src/HamsterAI.cpp
  4. 4
      src/HamsterAI.h

@ -72,7 +72,7 @@ void Hamster::UpdateHamsters(const float fElapsedTime){
if(h.IsPlayerControlled){ if(h.IsPlayerControlled){
h.HandlePlayerControls(); h.HandlePlayerControls();
}else{ }else{
//TODO: NPC controls. h.HandleNPCControls();
} }
} }
}break; }break;
@ -665,3 +665,57 @@ const bool Hamster::CanMove()const{
const bool Hamster::FlyingInTheAir()const{ const bool Hamster::FlyingInTheAir()const{
return GetState()==FLYING&&hamsterJet.value().GetZ()>0.5f&&GetZ()>0.5f; return GetState()==FLYING&&hamsterJet.value().GetZ()>0.5f&&GetZ()>0.5f;
} }
void Hamster::HandleNPCControls(){
vf2d aimingDir{};
const HamsterAI::ActionOptRef&currentAction{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);
}
*/
}

@ -133,6 +133,7 @@ public:
const vf2d&GetPos()const; const vf2d&GetPos()const;
const float&GetZ()const; const float&GetZ()const;
void HandlePlayerControls(); void HandlePlayerControls();
void HandleNPCControls();
void TurnTowardsTargetDirection(); void TurnTowardsTargetDirection();
void MoveHamster(); void MoveHamster();
void HandleCollision(); void HandleCollision();

@ -88,7 +88,7 @@ void HamsterAI::OnTextEntryComplete(const std::string&enteredText){
const HamsterAI::ActionOptRef HamsterAI::GetCurrentAction(){ const HamsterAI::ActionOptRef HamsterAI::GetCurrentAction(){
if(actionInd<actionsToPerform.size())return ActionOptRef{actionsToPerform.at(actionInd)}; if(actionInd<actionsToPerform.size())return ActionOptRef{actionsToPerform.at(actionInd)};
return {};
} }
const HamsterAI::ActionOptRef HamsterAI::AdvanceToNextAction(){ const HamsterAI::ActionOptRef HamsterAI::AdvanceToNextAction(){
actionInd++; actionInd++;
@ -129,5 +129,10 @@ void HamsterAI::LoadAI(const std::string&mapName,AIType type){
newAction.type=HamsterAI::Action::ActionType(typeNum); newAction.type=HamsterAI::Action::ActionType(typeNum);
actionsToPerform.emplace_back(newAction); actionsToPerform.emplace_back(newAction);
} }
this->type=type;
file.close(); file.close();
} }
const HamsterAI::AIType HamsterAI::GetAIType(){
return type;
}

@ -57,11 +57,12 @@ public:
SMART, SMART,
NORMAL, NORMAL,
DUMB, 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<std::reference_wrapper<HamsterAI::Action>>; using ActionOptRef=std::optional<std::reference_wrapper<HamsterAI::Action>>;
const ActionOptRef GetCurrentAction(); const ActionOptRef GetCurrentAction();
const ActionOptRef AdvanceToNextAction(); const ActionOptRef AdvanceToNextAction();
const AIType GetAIType();
void LoadAI(const std::string&mapName,AIType type); void LoadAI(const std::string&mapName,AIType type);
static void OnMove(const vi2d pos); static void OnMove(const vi2d pos);
@ -80,4 +81,5 @@ private:
std::vector<Action>actionsToPerform; std::vector<Action>actionsToPerform;
size_t actionInd{}; size_t actionInd{};
static std::optional<vi2d>lastTileWalkedOn; //In World Coords static std::optional<vi2d>lastTileWalkedOn; //In World Coords
AIType type{END};
}; };
Loading…
Cancel
Save