Obstacle.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 "../Navigation/Obstacle.h"
  23. #include "../Core/Context.h"
  24. #include "../Graphics/DebugRenderer.h"
  25. #include "../Navigation/DynamicNavigationMesh.h"
  26. #include "../IO/Log.h"
  27. #include "../Navigation/NavigationEvents.h"
  28. #include "../Scene/Scene.h"
  29. #include "../DebugNew.h"
  30. namespace Atomic
  31. {
  32. extern const char* NAVIGATION_CATEGORY;
  33. Obstacle::Obstacle(Context* context) :
  34. Component(context),
  35. height_(5.0f),
  36. radius_(5.0f),
  37. obstacleId_(0)
  38. {
  39. }
  40. Obstacle::~Obstacle()
  41. {
  42. if (obstacleId_ > 0 && ownerMesh_)
  43. ownerMesh_->RemoveObstacle(this);
  44. }
  45. void Obstacle::RegisterObject(Context* context)
  46. {
  47. context->RegisterFactory<Obstacle>(NAVIGATION_CATEGORY);
  48. COPY_BASE_ATTRIBUTES(Component);
  49. ACCESSOR_ATTRIBUTE("Radius", GetRadius, SetRadius, float, 5.0f, AM_DEFAULT);
  50. ACCESSOR_ATTRIBUTE("Height", GetHeight, SetHeight, float, 5.0f, AM_DEFAULT);
  51. }
  52. void Obstacle::OnSetEnabled()
  53. {
  54. if (ownerMesh_)
  55. {
  56. if (IsEnabledEffective())
  57. ownerMesh_->AddObstacle(this);
  58. else
  59. ownerMesh_->RemoveObstacle(this);
  60. }
  61. }
  62. void Obstacle::SetHeight(float newHeight)
  63. {
  64. height_ = newHeight;
  65. if (ownerMesh_)
  66. ownerMesh_->ObstacleChanged(this);
  67. MarkNetworkUpdate();
  68. }
  69. void Obstacle::SetRadius(float newRadius)
  70. {
  71. radius_ = newRadius;
  72. if (ownerMesh_)
  73. ownerMesh_->ObstacleChanged(this);
  74. MarkNetworkUpdate();
  75. }
  76. void Obstacle::OnNodeSet(Node* node)
  77. {
  78. if (node)
  79. {
  80. if (GetScene() == node)
  81. {
  82. LOGWARNING(GetTypeName() + " should not be created to the root scene node");
  83. return;
  84. }
  85. if (!ownerMesh_)
  86. ownerMesh_ = GetScene()->GetComponent<DynamicNavigationMesh>();
  87. if (ownerMesh_)
  88. ownerMesh_->AddObstacle(this);
  89. }
  90. }
  91. void Obstacle::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  92. {
  93. if (debug && IsEnabledEffective())
  94. debug->AddCylinder(node_->GetWorldPosition(), radius_, height_, Color(0.0f, 1.0f, 1.0f), depthTest);
  95. }
  96. void Obstacle::DrawDebugGeometry(bool depthTest)
  97. {
  98. Scene* scene = GetScene();
  99. if (scene)
  100. DrawDebugGeometry(scene->GetComponent<DebugRenderer>(), depthTest);
  101. }
  102. }