OffMeshConnection.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Core/Context.h"
  24. #include "../Graphics/DebugRenderer.h"
  25. #include "../Navigation/OffMeshConnection.h"
  26. #include "../Scene/Scene.h"
  27. #include "../DebugNew.h"
  28. namespace Atomic
  29. {
  30. extern const char* NAVIGATION_CATEGORY;
  31. static const float DEFAULT_RADIUS = 1.0f;
  32. static const unsigned DEFAULT_MASK_FLAG = 1;
  33. static const unsigned DEFAULT_AREA = 1;
  34. OffMeshConnection::OffMeshConnection(Context* context) :
  35. Component(context),
  36. endPointID_(0),
  37. radius_(DEFAULT_RADIUS),
  38. bidirectional_(true),
  39. endPointDirty_(false),
  40. mask_(DEFAULT_MASK_FLAG),
  41. areaId_(DEFAULT_AREA)
  42. {
  43. }
  44. OffMeshConnection::~OffMeshConnection()
  45. {
  46. }
  47. void OffMeshConnection::RegisterObject(Context* context)
  48. {
  49. context->RegisterFactory<OffMeshConnection>(NAVIGATION_CATEGORY);
  50. ATOMIC_ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  51. ATOMIC_ATTRIBUTE("Endpoint NodeID", int, endPointID_, 0, AM_DEFAULT | AM_NODEID);
  52. ATOMIC_ATTRIBUTE("Radius", float, radius_, DEFAULT_RADIUS, AM_DEFAULT);
  53. ATOMIC_ATTRIBUTE("Bidirectional", bool, bidirectional_, true, AM_DEFAULT);
  54. ATOMIC_ATTRIBUTE("Flags Mask", unsigned, mask_, DEFAULT_MASK_FLAG, AM_DEFAULT);
  55. ATOMIC_ATTRIBUTE("Area Type", unsigned, areaId_, DEFAULT_AREA, AM_DEFAULT);
  56. }
  57. void OffMeshConnection::OnSetAttribute(const AttributeInfo& attr, const Variant& src)
  58. {
  59. Serializable::OnSetAttribute(attr, src);
  60. if (attr.offset_ == offsetof(OffMeshConnection, endPointID_))
  61. endPointDirty_ = true;
  62. }
  63. void OffMeshConnection::ApplyAttributes()
  64. {
  65. if (endPointDirty_)
  66. {
  67. Scene* scene = GetScene();
  68. endPoint_ = scene ? scene->GetNode(endPointID_) : (Node*)0;
  69. endPointDirty_ = false;
  70. }
  71. }
  72. void OffMeshConnection::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  73. {
  74. if (!node_ || !endPoint_)
  75. return;
  76. Vector3 start = node_->GetWorldPosition();
  77. Vector3 end = endPoint_->GetWorldPosition();
  78. debug->AddSphere(Sphere(start, radius_), Color::WHITE, depthTest);
  79. debug->AddSphere(Sphere(end, radius_), Color::WHITE, depthTest);
  80. debug->AddLine(start, end, Color::WHITE, depthTest);
  81. }
  82. void OffMeshConnection::SetRadius(float radius)
  83. {
  84. radius_ = radius;
  85. MarkNetworkUpdate();
  86. }
  87. void OffMeshConnection::SetBidirectional(bool enabled)
  88. {
  89. bidirectional_ = enabled;
  90. MarkNetworkUpdate();
  91. }
  92. void OffMeshConnection::SetMask(unsigned newMask)
  93. {
  94. mask_ = newMask;
  95. MarkNetworkUpdate();
  96. }
  97. void OffMeshConnection::SetAreaID(unsigned newAreaID)
  98. {
  99. areaId_ = newAreaID;
  100. MarkNetworkUpdate();
  101. }
  102. void OffMeshConnection::SetEndPoint(Node* node)
  103. {
  104. endPoint_ = node;
  105. endPointID_ = node ? node->GetID() : 0;
  106. MarkNetworkUpdate();
  107. }
  108. Node* OffMeshConnection::GetEndPoint() const
  109. {
  110. return endPoint_;
  111. }
  112. }