NavArea.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 "../Scene/Component.h"
  24. #include "../Core/Context.h"
  25. #include "../Graphics/DebugRenderer.h"
  26. #include "../IO/Log.h"
  27. #include "../Navigation/NavArea.h"
  28. #include "../Scene/Node.h"
  29. #include "../Container/Str.h"
  30. namespace Atomic
  31. {
  32. static const unsigned MAX_NAV_AREA_ID = 255;
  33. static const Vector3 DEFAULT_BOUNDING_BOX_MIN(-10.0f, -10.0f, -10.0f);
  34. static const Vector3 DEFAULT_BOUNDING_BOX_MAX(10.0f, 10.0f, 10.0f);
  35. static const unsigned DEFAULT_MASK_FLAG = 0;
  36. static const unsigned DEFAULT_AREA_ID = 0;
  37. extern const char* NAVIGATION_CATEGORY;
  38. NavArea::NavArea(Context* context) :
  39. Component(context),
  40. areaID_(DEFAULT_AREA_ID),
  41. boundingBox_(DEFAULT_BOUNDING_BOX_MIN, DEFAULT_BOUNDING_BOX_MAX)
  42. {
  43. }
  44. NavArea::~NavArea()
  45. {
  46. }
  47. void NavArea::RegisterObject(Context* context)
  48. {
  49. context->RegisterFactory<NavArea>(NAVIGATION_CATEGORY);
  50. COPY_BASE_ATTRIBUTES(Component);
  51. ATTRIBUTE("Bounding Box Min", Vector3, boundingBox_.min_, DEFAULT_BOUNDING_BOX_MIN, AM_DEFAULT);
  52. ATTRIBUTE("Bounding Box Max", Vector3, boundingBox_.max_, DEFAULT_BOUNDING_BOX_MAX, AM_DEFAULT);
  53. ACCESSOR_ATTRIBUTE("Area ID", GetAreaID, SetAreaID, unsigned, DEFAULT_AREA_ID, AM_DEFAULT);
  54. }
  55. void NavArea::SetAreaID(unsigned newID)
  56. {
  57. if (newID > MAX_NAV_AREA_ID)
  58. LOGERRORF("NavArea Area ID %u exceeds maximum value of %u", newID, MAX_NAV_AREA_ID);
  59. areaID_ = (unsigned char)newID;
  60. MarkNetworkUpdate();
  61. }
  62. BoundingBox NavArea::GetWorldBoundingBox() const
  63. {
  64. Matrix3x4 mat;
  65. mat.SetTranslation(node_->GetWorldPosition());
  66. return boundingBox_.Transformed(mat);
  67. }
  68. void NavArea::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  69. {
  70. if (debug && IsEnabledEffective())
  71. {
  72. Matrix3x4 mat;
  73. mat.SetTranslation(node_->GetWorldPosition());
  74. debug->AddBoundingBox(boundingBox_, mat, Color::GREEN, depthTest);
  75. }
  76. }
  77. }