NetworkPriority.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // Copyright (c) 2008-2020 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 "../Network/NetworkPriority.h"
  25. #include "../DebugNew.h"
  26. namespace Urho3D
  27. {
  28. const char* NETWORK_CATEGORY = "Network";
  29. static const float DEFAULT_BASE_PRIORITY = 100.0f;
  30. static const float DEFAULT_DISTANCE_FACTOR = 0.0f;
  31. static const float DEFAULT_MIN_PRIORITY = 0.0f;
  32. static const float UPDATE_THRESHOLD = 100.0f;
  33. NetworkPriority::NetworkPriority(Context* context) :
  34. Component(context),
  35. basePriority_(DEFAULT_BASE_PRIORITY),
  36. distanceFactor_(DEFAULT_DISTANCE_FACTOR),
  37. minPriority_(DEFAULT_MIN_PRIORITY),
  38. alwaysUpdateOwner_(true)
  39. {
  40. }
  41. NetworkPriority::~NetworkPriority() = default;
  42. void NetworkPriority::RegisterObject(Context* context)
  43. {
  44. context->RegisterFactory<NetworkPriority>(NETWORK_CATEGORY);
  45. URHO3D_ATTRIBUTE("Base Priority", float, basePriority_, DEFAULT_BASE_PRIORITY, AM_DEFAULT);
  46. URHO3D_ATTRIBUTE("Distance Factor", float, distanceFactor_, DEFAULT_DISTANCE_FACTOR, AM_DEFAULT);
  47. URHO3D_ATTRIBUTE("Minimum Priority", float, minPriority_, DEFAULT_MIN_PRIORITY, AM_DEFAULT);
  48. URHO3D_ATTRIBUTE("Always Update Owner", bool, alwaysUpdateOwner_, true, AM_DEFAULT);
  49. }
  50. void NetworkPriority::SetBasePriority(float priority)
  51. {
  52. basePriority_ = Max(priority, 0.0f);
  53. MarkNetworkUpdate();
  54. }
  55. void NetworkPriority::SetDistanceFactor(float factor)
  56. {
  57. distanceFactor_ = Max(factor, 0.0f);
  58. MarkNetworkUpdate();
  59. }
  60. void NetworkPriority::SetMinPriority(float priority)
  61. {
  62. minPriority_ = Max(priority, 0.0f);
  63. MarkNetworkUpdate();
  64. }
  65. void NetworkPriority::SetAlwaysUpdateOwner(bool enable)
  66. {
  67. alwaysUpdateOwner_ = enable;
  68. MarkNetworkUpdate();
  69. }
  70. bool NetworkPriority::CheckUpdate(float distance, float& accumulator)
  71. {
  72. float currentPriority = Max(basePriority_ - distanceFactor_ * distance, minPriority_);
  73. accumulator += currentPriority;
  74. if (accumulator >= UPDATE_THRESHOLD)
  75. {
  76. accumulator = fmodf(accumulator, UPDATE_THRESHOLD);
  77. return true;
  78. }
  79. else
  80. return false;
  81. }
  82. }