Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.11 KB | None | 0 0
  1. #include "StdAfx.h"
  2. #include "CharacterPath.h"
  3. #include "Character/Character.h"
  4. #include "Character/Movement/CharacterMovement.h"
  5.  
  6.  
  7. CCharacterPath::CCharacterPath()
  8.     : m_movementRequestId(0)
  9.     , m_pPathFollower(nullptr)
  10.     , m_pFoundPath(nullptr)
  11. {
  12. }
  13.  
  14. CCharacterPath::~CCharacterPath()
  15. {
  16.     Reset();
  17. }
  18.  
  19. void CCharacterPath::PostInit(IGameObject *pGameObject)
  20. {
  21.     InitAI();
  22. }
  23.  
  24. void CCharacterPath::Reset()
  25. {
  26.     gEnv->pAISystem->GetMovementSystem()->UnregisterEntity(GetEntityId());
  27.     m_pPathFollower.reset();
  28.     SAFE_RELEASE(m_pFoundPath);
  29. }
  30.  
  31. void CCharacterPath::InitAI()
  32. {
  33.     Reset();
  34.  
  35.     m_pCharacter = static_cast<CCharacter *>(GetGameObject()->QueryExtension("Character"));
  36.  
  37.     m_navigationAgentTypeId = gEnv->pAISystem->GetNavigationSystem()->GetAgentTypeID("MediumSizedCharacters");
  38.  
  39.     m_callbacks.queuePathRequestFunction = functor(*this, &CCharacterPath::RequestPathTo);
  40.     m_callbacks.checkOnPathfinderStateFunction = functor(*this, &CCharacterPath::GetPathfinderState);
  41.     m_callbacks.getPathFollowerFunction = functor(*this, &CCharacterPath::GetPathFollower);
  42.     m_callbacks.getPathFunction = functor(*this, &CCharacterPath::GetINavPath);
  43.    
  44.     gEnv->pAISystem->GetMovementSystem()->RegisterEntity(GetEntityId(), m_callbacks, *this);
  45.  
  46.     if (m_pPathFollower == nullptr)
  47.     {
  48.         PathFollowerParams params;
  49.         params.maxAccel = m_pCharacter->GetCVars().m_moveSpeed;
  50.         params.maxSpeed = m_pCharacter->GetCVars().m_moveSpeed;
  51.         params.minSpeed = 0.0f;
  52.         params.normalSpeed = params.maxSpeed;
  53.  
  54.         params.use2D = false;
  55.  
  56.         m_pPathFollower = gEnv->pAISystem->CreateAndReturnNewDefaultPathFollower(params, m_pathObstacles);
  57.     }
  58.  
  59.     m_movementAbility.b3DMove = true;
  60. }
  61.  
  62. void CCharacterPath::RequestMoveTo(const Vec3 &position)
  63. {
  64.     CRY_ASSERT_MESSAGE(m_movementRequestId.id == 0, "RequestMoveTo can not be called while another request is being handled!");
  65.  
  66.     MovementRequest movementRequest;
  67.     movementRequest.entityID = GetEntityId();
  68.     movementRequest.destination = position;
  69.     movementRequest.callback = functor(*this, &CCharacterPath::MovementRequestCallback);
  70.     movementRequest.style.SetSpeed(MovementStyle::Walk);
  71.  
  72.     movementRequest.type = MovementRequest::Type::MoveTo;
  73.  
  74.     m_state = Movement::StillFinding;
  75.  
  76.     m_movementRequestId = gEnv->pAISystem->GetMovementSystem()->QueueRequest(movementRequest);
  77. }
  78.  
  79. void CCharacterPath::RequestPathTo(MNMPathRequest &request)
  80. {
  81.     m_state = Movement::StillFinding;
  82.  
  83.     request.resultCallback = functor(*this, &CCharacterPath::OnMNMPathResult);
  84.     request.agentTypeID = m_navigationAgentTypeId;
  85.  
  86.     m_pathFinderRequestId = gEnv->pAISystem->GetMNMPathfinder()->RequestPathTo(this, request);
  87. }
  88.  
  89. void CCharacterPath::CancelCurrentRequest()
  90. {
  91.     CRY_ASSERT(m_movementRequestId.id != 0);
  92.  
  93.     gEnv->pAISystem->GetMovementSystem()->CancelRequest(m_movementRequestId);
  94.     m_movementRequestId = 0;
  95.  
  96.     if (m_pathFinderRequestId != 0)
  97.     {
  98.         gEnv->pAISystem->GetMNMPathfinder()->CancelPathRequest(m_pathFinderRequestId);
  99.  
  100.         m_pathFinderRequestId = 0;
  101.     }
  102. }
  103.  
  104. void CCharacterPath::OnMNMPathResult(const MNM::QueuedPathID& requestId, MNMPathRequestResult& result)
  105. {
  106.     m_pathFinderRequestId = 0;
  107.  
  108.     if (result.HasPathBeenFound())
  109.     {
  110.         m_state = Movement::FoundPath;
  111.  
  112.         SAFE_DELETE(m_pFoundPath);
  113.         m_pFoundPath = result.pPath->Clone();
  114.  
  115.         // Bump version
  116.         m_pFoundPath->SetVersion(m_pFoundPath->GetVersion() + 1);
  117.  
  118.         m_pPathFollower->Reset();
  119.         m_pPathFollower->AttachToPath(m_pFoundPath);
  120.     }
  121.     else
  122.     {
  123.         m_state = Movement::CouldNotFindPath;
  124.     }
  125. }
  126.  
  127.  
  128. Vec3 CCharacterPath::GetVelocity() const
  129. {
  130.     return m_pCharacter->GetMovement()->GetVelocity();
  131. }
  132.  
  133. void CCharacterPath::SetMovementOutputValue(const PathFollowResult& result)
  134. {
  135.     float frameTime = gEnv->pTimer->GetFrameTime();
  136.     float moveStep = m_pCharacter->GetCVars().m_moveSpeed * frameTime;
  137.  
  138.     auto &playerMovement = *m_pCharacter->GetMovement();
  139.  
  140.     if (result.velocityOut.GetLength() > moveStep)
  141.     {
  142.         playerMovement.RequestMove(result.velocityOut.GetNormalized() * moveStep);
  143.     }
  144.     else
  145.     {
  146.         playerMovement.SetVelocity(result.velocityOut);
  147.     }
  148. }
  149.  
  150. void CCharacterPath::ClearMovementState()
  151. {
  152.     m_pCharacter->GetMovement()->SetVelocity(ZERO);
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement