From 36341b5d6ca0553b55352b2ea6be45dcbbf07091 Mon Sep 17 00:00:00 2001 From: wraitii Date: Mon, 10 Jun 2019 19:33:24 +0000 Subject: [PATCH] UnitMotion - Handle obstruction moving flag similarly to m_CurSpeed The obstruction manager keeps a flag of moving units. This should be updated in the same location as the entity's current speed, since they have similar properties. Differential Revision: https://code.wildfiregames.com/D1895 This was SVN commit r22362. --- .../simulation2/components/CCmpUnitMotion.cpp | 58 +++++++++---------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/source/simulation2/components/CCmpUnitMotion.cpp b/source/simulation2/components/CCmpUnitMotion.cpp index e9fd072d7d..c6c62c1f6e 100644 --- a/source/simulation2/components/CCmpUnitMotion.cpp +++ b/source/simulation2/components/CCmpUnitMotion.cpp @@ -563,24 +563,40 @@ private: void MoveFailed() { - CmpPtr cmpObstruction(GetEntityHandle()); - if (cmpObstruction) - cmpObstruction->SetMovingFlag(false); - CMessageMotionChanged msg(true); GetSimContext().GetComponentManager().PostMessage(GetEntityId(), msg); } void MoveSucceeded() { - CmpPtr cmpObstruction(GetEntityHandle()); - if (cmpObstruction) - cmpObstruction->SetMovingFlag(false); - CMessageMotionChanged msg(false); GetSimContext().GetComponentManager().PostMessage(GetEntityId(), msg); } + /** + * Update other components on our speed. + * This doesn't use messages for efficiency. + * This should only be called when speed changes. + */ + void UpdateMovementState(entity_pos_t speed) + { + CmpPtr cmpObstruction(GetEntityHandle()); + // Moved last turn, didn't this turn. + if (speed == fixed::Zero() && m_CurSpeed > fixed::Zero()) + { + if (cmpObstruction) + cmpObstruction->SetMovingFlag(false); + } + // Moved this turn, didn't last turn + else if (speed > fixed::Zero() && m_CurSpeed == fixed::Zero()) + { + if (cmpObstruction) + cmpObstruction->SetMovingFlag(true); + } + + m_CurSpeed = speed; + } + bool MoveToPointRange(entity_pos_t x, entity_pos_t z, entity_pos_t minRange, entity_pos_t maxRange, entity_id_t target); /** @@ -681,11 +697,6 @@ REGISTER_COMPONENT_TYPE(UnitMotion) void CCmpUnitMotion::PathResult(u32 ticket, const WaypointPath& path) { - // reset our state for sanity. - CmpPtr cmpObstruction(GetEntityHandle()); - if (cmpObstruction) - cmpObstruction->SetMovingFlag(false); - // Ignore obsolete path requests if (ticket != m_ExpectedPathTicket) return; @@ -718,9 +729,6 @@ void CCmpUnitMotion::PathResult(u32 ticket, const WaypointPath& path) m_LongPath.m_Waypoints.emplace_back(Waypoint{ m_FinalGoal.x, m_FinalGoal.z }); m_PathState = PATHSTATE_FOLLOWING; - - if (cmpObstruction) - cmpObstruction->SetMovingFlag(true); } else if (m_PathState == PATHSTATE_WAITING_REQUESTING_SHORT || m_PathState == PATHSTATE_FOLLOWING_REQUESTING_SHORT) { @@ -767,9 +775,6 @@ void CCmpUnitMotion::PathResult(u32 ticket, const WaypointPath& path) m_Tries = 0; m_PathState = PATHSTATE_FOLLOWING; - - if (cmpObstruction) - cmpObstruction->SetMovingFlag(true); } else LOGWARNING("unexpected PathResult (%u %d %d)", GetEntityId(), m_State, m_PathState); @@ -784,6 +789,9 @@ void CCmpUnitMotion::Move(fixed dt) m_State = STATE_IDLE; MoveSucceeded(); m_CurSpeed = fixed::Zero(); + CmpPtr cmpObstruction(GetEntityHandle()); + if (cmpObstruction) + cmpObstruction->SetMovingFlag(false); return; } @@ -898,7 +906,7 @@ void CCmpUnitMotion::Move(fixed dt) // Update our speed over this turn so that the visual actor shows the correct animation. if (pos == initialPos) - m_CurSpeed = fixed::Zero(); + UpdateMovementState(fixed::Zero()); else { // Update the Position component after our movement (if we actually moved anywhere) @@ -909,7 +917,7 @@ void CCmpUnitMotion::Move(fixed dt) cmpPosition->MoveAndTurnTo(pos.X,pos.Y, angle); // Calculate the mean speed over this past turn. - m_CurSpeed = cmpPosition->GetDistanceTravelled() / dt; + UpdateMovementState(offset.Length() / dt); } if (wasObstructed) @@ -991,10 +999,6 @@ void CCmpUnitMotion::Move(fixed dt) CmpPtr cmpUnitMotion(GetSimContext(), m_MoveRequest.m_Entity); if (cmpUnitMotion && !cmpUnitMotion->IsMoving()) { - CmpPtr cmpObstruction(GetEntityHandle()); - if (cmpObstruction) - cmpObstruction->SetMovingFlag(false); - CMessageMotionChanged msg(false); GetSimContext().GetComponentManager().PostMessage(GetEntityId(), msg); } @@ -1240,10 +1244,6 @@ void CCmpUnitMotion::BeginPathing(const CFixedVector2D& from, const PathGoal& go // reset our state for sanity. m_ExpectedPathTicket = 0; - CmpPtr cmpObstruction(GetEntityHandle()); - if (cmpObstruction) - cmpObstruction->SetMovingFlag(false); - m_PathState = PATHSTATE_NONE; #if DISABLE_PATHFINDER