Skip to content
Permalink
Browse files

don't use extra optional<state> type, just return unchanged value or …

…a different state and assign to mState
  • Loading branch information...
fenbf committed Jun 7, 2019
1 parent 95323f1 commit f9fffc3e4bac3d5c2a50525d0411e09e930d261c
Showing with 9 additions and 15 deletions.
  1. +9 −15 state-machine/enemy.cpp
@@ -142,9 +142,8 @@ bool Enemy::playerInOuterZone() const

void Enemy::updateLogic(const double timeDelta, const bool reachedCurrentTarget)
{
using MaybeNextState = std::optional<State>;
auto maybeNextState = match(mState,
[=](Circling& state) -> MaybeNextState
mState = match(mState,
[=](Circling& state) -> State
{
if (reachedCurrentTarget)
{
@@ -166,20 +165,20 @@ void Enemy::updateLogic(const double timeDelta, const bool reachedCurrentTarget)
return State{FlyToCenter{}};
}

return std::nullopt;
return state; // no change
},

[=](const FlyToCenter&) -> MaybeNextState
[=](const FlyToCenter& state) -> State
{
if (reachedCurrentTarget)
{
return State{ShootingFromCenter{}};
}

return std::nullopt;
return state;
},

[=](ShootingFromCenter& state) -> MaybeNextState
[=](ShootingFromCenter& state) -> State
{
state.mTimeSpentInCenter += timeDelta;
const auto spentEnoughTimeInCenter =
@@ -199,25 +198,20 @@ void Enemy::updateLogic(const double timeDelta, const bool reachedCurrentTarget)

shootAtPlayer(state, timeDelta);

return std::nullopt;
return state;
},

[=](const FlyOut& state) -> MaybeNextState
[=](const FlyOut& state) -> State
{
if (reachedCurrentTarget)
{
flyTo(CIRCLE_POSITIONS[state.mTargetCornerIndex]);
return State{Circling{state.mTargetCornerIndex}};
}

return std::nullopt;
return state;
}
);

if (maybeNextState)
{
mState = *maybeNextState;
}
}


0 comments on commit f9fffc3

Please sign in to comment.
You can’t perform that action at this time.