Obstacle.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 "../IO/Log.h"
  26. #include "../Navigation/DynamicNavigationMesh.h"
  27. #include "../Navigation/Obstacle.h"
  28. #include "../Navigation/NavigationEvents.h"
  29. #include "../Scene/Scene.h"
  30. #include "../DebugNew.h"
  31. namespace Atomic
  32. {
  33. extern const char* NAVIGATION_CATEGORY;
  34. Obstacle::Obstacle(Context* context) :
  35. Component(context),
  36. height_(5.0f),
  37. radius_(5.0f),
  38. obstacleId_(0)
  39. {
  40. }
  41. Obstacle::~Obstacle()
  42. {
  43. if (obstacleId_ > 0 && ownerMesh_)
  44. ownerMesh_->RemoveObstacle(this);
  45. }
  46. void Obstacle::RegisterObject(Context* context)
  47. {
  48. context->RegisterFactory<Obstacle>(NAVIGATION_CATEGORY);
  49. ATOMIC_COPY_BASE_ATTRIBUTES(Component);
  50. ATOMIC_ACCESSOR_ATTRIBUTE("Radius", GetRadius, SetRadius, float, 5.0f, AM_DEFAULT);
  51. ATOMIC_ACCESSOR_ATTRIBUTE("Height", GetHeight, SetHeight, float, 5.0f, AM_DEFAULT);
  52. }
  53. void Obstacle::OnSetEnabled()
  54. {
  55. if (ownerMesh_)
  56. {
  57. if (IsEnabledEffective())
  58. ownerMesh_->AddObstacle(this);
  59. else
  60. ownerMesh_->RemoveObstacle(this);
  61. }
  62. }
  63. void Obstacle::SetHeight(float newHeight)
  64. {
  65. height_ = newHeight;
  66. if (ownerMesh_)
  67. ownerMesh_->ObstacleChanged(this);
  68. MarkNetworkUpdate();
  69. }
  70. void Obstacle::SetRadius(float newRadius)
  71. {
  72. radius_ = newRadius;
  73. if (ownerMesh_)
  74. ownerMesh_->ObstacleChanged(this);
  75. MarkNetworkUpdate();
  76. }
  77. void Obstacle::OnNodeSet(Node* node)
  78. {
  79. if (node)
  80. node->AddListener(this);
  81. }
  82. void Obstacle::OnSceneSet(Scene* scene)
  83. {
  84. if (scene)
  85. {
  86. if (scene == node_)
  87. {
  88. ATOMIC_LOGWARNING(GetTypeName() + " should not be created to the root scene node");
  89. return;
  90. }
  91. if (!ownerMesh_)
  92. ownerMesh_ = node_->GetParentComponent<DynamicNavigationMesh>(true);
  93. if (ownerMesh_)
  94. {
  95. ownerMesh_->AddObstacle(this);
  96. SubscribeToEvent(ownerMesh_, E_NAVIGATION_TILE_ADDED, ATOMIC_HANDLER(Obstacle, HandleNavigationTileAdded));
  97. }
  98. }
  99. else
  100. {
  101. if (obstacleId_ > 0 && ownerMesh_)
  102. ownerMesh_->RemoveObstacle(this);
  103. UnsubscribeFromEvent(E_NAVIGATION_TILE_ADDED);
  104. ownerMesh_.Reset();
  105. }
  106. }
  107. void Obstacle::OnMarkedDirty(Node* node)
  108. {
  109. if (IsEnabledEffective() && ownerMesh_)
  110. {
  111. Scene* scene = GetScene();
  112. /// \hack If scene already unassigned, or if it's being destroyed, do nothing
  113. if (!scene || scene->Refs() == 0)
  114. return;
  115. // If within threaded update, update later
  116. if (scene->IsThreadedUpdate())
  117. {
  118. scene->DelayedMarkedDirty(this);
  119. return;
  120. }
  121. ownerMesh_->ObstacleChanged(this);
  122. }
  123. }
  124. void Obstacle::HandleNavigationTileAdded(StringHash eventType, VariantMap& eventData)
  125. {
  126. // Re-add obstacle if it is intersected with newly added tile
  127. const IntVector2 tile = eventData[NavigationTileAdded::P_TILE].GetIntVector2();
  128. if (IsEnabledEffective() && ownerMesh_ && ownerMesh_->IsObstacleInTile(this, tile))
  129. ownerMesh_->ObstacleChanged(this);
  130. }
  131. void Obstacle::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  132. {
  133. if (debug && IsEnabledEffective())
  134. debug->AddCylinder(node_->GetWorldPosition(), radius_, height_, Color(0.0f, 1.0f, 1.0f), depthTest);
  135. }
  136. void Obstacle::DrawDebugGeometry(bool depthTest)
  137. {
  138. Scene* scene = GetScene();
  139. if (scene)
  140. DrawDebugGeometry(scene->GetComponent<DebugRenderer>(), depthTest);
  141. }
  142. }