OffMeshConnection.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // Copyright (c) 2008-2013 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 "Context.h"
  24. #include "DebugRenderer.h"
  25. #include "OffMeshConnection.h"
  26. #include "Scene.h"
  27. #include "DebugNew.h"
  28. namespace Urho3D
  29. {
  30. extern const char* NAVIGATION_CATEGORY;
  31. OBJECTTYPESTATIC(OffMeshConnection);
  32. static const float DEFAULT_RADIUS = 1.0f;
  33. OffMeshConnection::OffMeshConnection(Context* context) :
  34. Component(context),
  35. endPointID_(0),
  36. radius_(DEFAULT_RADIUS),
  37. bidirectional_(true),
  38. endPointDirty_(false)
  39. {
  40. }
  41. OffMeshConnection::~OffMeshConnection()
  42. {
  43. }
  44. void OffMeshConnection::RegisterObject(Context* context)
  45. {
  46. context->RegisterFactory<OffMeshConnection>(NAVIGATION_CATEGORY);
  47. ACCESSOR_ATTRIBUTE(OffMeshConnection, VAR_BOOL, "Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  48. ATTRIBUTE(OffMeshConnection, VAR_INT, "Endpoint NodeID", endPointID_, 0, AM_DEFAULT | AM_NODEID);
  49. ATTRIBUTE(OffMeshConnection, VAR_FLOAT, "Radius", radius_, DEFAULT_RADIUS, AM_DEFAULT);
  50. ATTRIBUTE(OffMeshConnection, VAR_BOOL, "Bidirectional", bidirectional_, true, AM_DEFAULT);
  51. }
  52. void OffMeshConnection::OnSetAttribute(const AttributeInfo& attr, const Variant& src)
  53. {
  54. Component::OnSetAttribute(attr, src);
  55. if (attr.offset_ == offsetof(OffMeshConnection, endPointID_))
  56. endPointDirty_ = true;
  57. }
  58. void OffMeshConnection::ApplyAttributes()
  59. {
  60. if (endPointDirty_)
  61. {
  62. Scene* scene = GetScene();
  63. endPoint_ = scene ? scene->GetNode(endPointID_) : (Node*)0;
  64. endPointDirty_ = false;
  65. }
  66. }
  67. void OffMeshConnection::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  68. {
  69. if (!node_ || !endPoint_)
  70. return;
  71. Vector3 start = node_->GetWorldPosition();
  72. Vector3 end = endPoint_->GetWorldPosition();
  73. debug->AddSphere(Sphere(start, radius_), Color::WHITE, depthTest);
  74. debug->AddSphere(Sphere(end, radius_), Color::WHITE, depthTest);
  75. debug->AddLine(start, end, Color::WHITE, depthTest);
  76. }
  77. void OffMeshConnection::SetRadius(float radius)
  78. {
  79. radius_ = radius;
  80. MarkNetworkUpdate();
  81. }
  82. void OffMeshConnection::SetBidirectional(bool enabled)
  83. {
  84. bidirectional_ = enabled;
  85. MarkNetworkUpdate();
  86. }
  87. void OffMeshConnection::SetEndPoint(Node* node)
  88. {
  89. endPoint_ = node;
  90. endPointID_ = node ? node->GetID() : 0;
  91. MarkNetworkUpdate();
  92. }
  93. Node* OffMeshConnection::GetEndPoint() const
  94. {
  95. return endPoint_;
  96. }
  97. }