OffMeshConnection.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../Core/Context.h"
  5. #include "../Graphics/DebugRenderer.h"
  6. #include "../Navigation/OffMeshConnection.h"
  7. #include "../Scene/Scene.h"
  8. #include "../DebugNew.h"
  9. namespace Urho3D
  10. {
  11. extern const char* NAVIGATION_CATEGORY;
  12. static const float DEFAULT_RADIUS = 1.0f;
  13. static const unsigned DEFAULT_MASK_FLAG = 1;
  14. static const unsigned DEFAULT_AREA = 1;
  15. OffMeshConnection::OffMeshConnection(Context* context) :
  16. Component(context),
  17. endPointID_(0),
  18. radius_(DEFAULT_RADIUS),
  19. bidirectional_(true),
  20. endPointDirty_(false),
  21. mask_(DEFAULT_MASK_FLAG),
  22. areaId_(DEFAULT_AREA)
  23. {
  24. }
  25. OffMeshConnection::~OffMeshConnection() = default;
  26. void OffMeshConnection::RegisterObject(Context* context)
  27. {
  28. context->RegisterFactory<OffMeshConnection>(NAVIGATION_CATEGORY);
  29. URHO3D_ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, true, AM_DEFAULT);
  30. URHO3D_ATTRIBUTE_EX("Endpoint NodeID", endPointID_, MarkEndPointDirty, 0, AM_DEFAULT | AM_NODEID);
  31. URHO3D_ATTRIBUTE("Radius", radius_, DEFAULT_RADIUS, AM_DEFAULT);
  32. URHO3D_ATTRIBUTE("Bidirectional", bidirectional_, true, AM_DEFAULT);
  33. URHO3D_ATTRIBUTE("Flags Mask", mask_, DEFAULT_MASK_FLAG, AM_DEFAULT);
  34. URHO3D_ATTRIBUTE("Area Type", areaId_, DEFAULT_AREA, AM_DEFAULT);
  35. }
  36. void OffMeshConnection::ApplyAttributes()
  37. {
  38. if (endPointDirty_)
  39. {
  40. Scene* scene = GetScene();
  41. endPoint_ = scene ? scene->GetNode(endPointID_) : nullptr;
  42. endPointDirty_ = false;
  43. }
  44. }
  45. void OffMeshConnection::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  46. {
  47. if (!node_ || !endPoint_)
  48. return;
  49. Vector3 start = node_->GetWorldPosition();
  50. Vector3 end = endPoint_->GetWorldPosition();
  51. debug->AddSphere(Sphere(start, radius_), Color::WHITE, depthTest);
  52. debug->AddSphere(Sphere(end, radius_), Color::WHITE, depthTest);
  53. debug->AddLine(start, end, Color::WHITE, depthTest);
  54. }
  55. void OffMeshConnection::SetRadius(float radius)
  56. {
  57. radius_ = radius;
  58. MarkNetworkUpdate();
  59. }
  60. void OffMeshConnection::SetBidirectional(bool enabled)
  61. {
  62. bidirectional_ = enabled;
  63. MarkNetworkUpdate();
  64. }
  65. void OffMeshConnection::SetMask(unsigned newMask)
  66. {
  67. mask_ = newMask;
  68. MarkNetworkUpdate();
  69. }
  70. void OffMeshConnection::SetAreaID(unsigned newAreaID)
  71. {
  72. areaId_ = newAreaID;
  73. MarkNetworkUpdate();
  74. }
  75. void OffMeshConnection::SetEndPoint(Node* node)
  76. {
  77. endPoint_ = node;
  78. endPointID_ = node ? node->GetID() : 0;
  79. MarkNetworkUpdate();
  80. }
  81. Node* OffMeshConnection::GetEndPoint() const
  82. {
  83. return endPoint_;
  84. }
  85. }