Obstacle.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // Copyright (c) 2008-2015 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. COPY_BASE_ATTRIBUTES(Component);
  50. ACCESSOR_ATTRIBUTE("Radius", GetRadius, SetRadius, float, 5.0f, AM_DEFAULT);
  51. 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::OnSceneSet(Scene* scene)
  78. {
  79. if (scene)
  80. {
  81. if (scene == node_)
  82. {
  83. LOGWARNING(GetTypeName() + " should not be created to the root scene node");
  84. return;
  85. }
  86. if (!ownerMesh_)
  87. ownerMesh_ = scene->GetComponent<DynamicNavigationMesh>();
  88. if (ownerMesh_)
  89. ownerMesh_->AddObstacle(this);
  90. }
  91. else
  92. {
  93. if (obstacleId_ > 0 && ownerMesh_)
  94. ownerMesh_->RemoveObstacle(this);
  95. }
  96. }
  97. void Obstacle::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  98. {
  99. if (debug && IsEnabledEffective())
  100. debug->AddCylinder(node_->GetWorldPosition(), radius_, height_, Color(0.0f, 1.0f, 1.0f), depthTest);
  101. }
  102. void Obstacle::DrawDebugGeometry(bool depthTest)
  103. {
  104. Scene* scene = GetScene();
  105. if (scene)
  106. DrawDebugGeometry(scene->GetComponent<DebugRenderer>(), depthTest);
  107. }
  108. }