NetworkPriority.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // Copyright (c) 2008-2013 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 "Context.h"
  24. #include "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. OBJECTTYPESTATIC(NetworkPriority);
  34. NetworkPriority::NetworkPriority(Context* context) :
  35. Component(context),
  36. basePriority_(DEFAULT_BASE_PRIORITY),
  37. distanceFactor_(DEFAULT_DISTANCE_FACTOR),
  38. minPriority_(DEFAULT_MIN_PRIORITY),
  39. alwaysUpdateOwner_(true)
  40. {
  41. }
  42. NetworkPriority::~NetworkPriority()
  43. {
  44. }
  45. void NetworkPriority::RegisterObject(Context* context)
  46. {
  47. context->RegisterComponentFactory<NetworkPriority>(NETWORK_CATEGORY);
  48. ATTRIBUTE(NetworkPriority, VAR_FLOAT, "Base Priority", basePriority_, DEFAULT_BASE_PRIORITY, AM_DEFAULT);
  49. ATTRIBUTE(NetworkPriority, VAR_FLOAT, "Distance Factor", distanceFactor_, DEFAULT_DISTANCE_FACTOR, AM_DEFAULT);
  50. ATTRIBUTE(NetworkPriority, VAR_FLOAT, "Minimum Priority", minPriority_, DEFAULT_MIN_PRIORITY, AM_DEFAULT);
  51. ATTRIBUTE(NetworkPriority, VAR_BOOL, "Always Update Owner", alwaysUpdateOwner_, true, AM_DEFAULT);
  52. }
  53. void NetworkPriority::SetBasePriority(float priority)
  54. {
  55. basePriority_ = Max(priority, 0.0f);
  56. MarkNetworkUpdate();
  57. }
  58. void NetworkPriority::SetDistanceFactor(float factor)
  59. {
  60. distanceFactor_ = Max(factor, 0.0f);
  61. MarkNetworkUpdate();
  62. }
  63. void NetworkPriority::SetMinPriority(float priority)
  64. {
  65. minPriority_ = Max(priority, 0.0f);
  66. MarkNetworkUpdate();
  67. }
  68. void NetworkPriority::SetAlwaysUpdateOwner(bool enable)
  69. {
  70. alwaysUpdateOwner_ = enable;
  71. MarkNetworkUpdate();
  72. }
  73. bool NetworkPriority::CheckUpdate(float distance, float& accumulator)
  74. {
  75. float currentPriority = Max(basePriority_ - distanceFactor_ * distance, minPriority_);
  76. accumulator += currentPriority;
  77. if (accumulator >= UPDATE_THRESHOLD)
  78. {
  79. accumulator = fmodf(accumulator, UPDATE_THRESHOLD);
  80. return true;
  81. }
  82. else
  83. return false;
  84. }
  85. }