afxForceSet.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  2. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  3. // Copyright (C) 2015 Faust Logic, Inc.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //
  23. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  24. #include "afx/arcaneFX.h"
  25. #include "afx/forces/afxForceSet.h"
  26. #include "afx/forces/afxForce.h"
  27. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  28. afxForceSet::afxForceSet(const char* name)
  29. {
  30. mName = (name) ? StringTable->insert(name) : ST_NULLSTRING;
  31. mUpdate_dt = 10.0f; // seems like an ok maximum, force-xmods will probably lower it.
  32. mElapsed_dt = 0.0f;
  33. mElapsed_ms = 0;
  34. mNum_updates = 0;
  35. mLast_num_updates = 0;
  36. }
  37. void afxForceSet::remove(afxForce* force)
  38. {
  39. for (S32 i = 0; i < mForce_v.size(); i++)
  40. {
  41. if (mForce_v[i] == force)
  42. {
  43. mForce_v.erase(i);
  44. return;
  45. }
  46. }
  47. }
  48. S32 afxForceSet::updateDT(F32 dt)
  49. {
  50. U32 now = Platform::getVirtualMilliseconds();
  51. if (mElapsed_ms == now)
  52. return mLast_num_updates;
  53. mElapsed_ms = now;
  54. mElapsed_dt += dt;
  55. if (mElapsed_dt < mUpdate_dt)
  56. {
  57. mLast_num_updates = 0;
  58. return 0;
  59. }
  60. mNum_updates = mFloor(mElapsed_dt/mUpdate_dt);
  61. mElapsed_dt -= mUpdate_dt*mNum_updates;
  62. mLast_num_updates = mNum_updates;
  63. return mNum_updates;
  64. }
  65. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  66. afxForceSetMgr::afxForceSetMgr()
  67. {
  68. }
  69. afxForceSetMgr::~afxForceSetMgr()
  70. {
  71. for (S32 i = 0; i < forces_sets.size(); i++)
  72. {
  73. if (forces_sets[i])
  74. delete forces_sets[i];
  75. }
  76. }
  77. afxForceSet* afxForceSetMgr::findForceSet(StringTableEntry forces_set_name)
  78. {
  79. for (S32 i = 0; i < forces_sets.size(); i++)
  80. {
  81. if (forces_sets[i] && forces_sets[i]->getName() == forces_set_name)
  82. return forces_sets[i];
  83. }
  84. return 0;
  85. }
  86. void afxForceSetMgr::registerForce(StringTableEntry forces_set_name, afxForce* force)
  87. {
  88. if (!force)
  89. return;
  90. // find forceSet by name
  91. afxForceSet* fset = findForceSet(forces_set_name);
  92. // create forceSet if it does not already exist
  93. if (!fset)
  94. {
  95. fset = new afxForceSet(forces_set_name);
  96. forces_sets.push_back(fset);
  97. }
  98. // add force to set
  99. fset->add(force);
  100. }
  101. void afxForceSetMgr::unregisterForce(StringTableEntry forces_set_name, afxForce* force)
  102. {
  103. if (!force)
  104. return;
  105. afxForceSet* fset = findForceSet(forces_set_name);
  106. if (!fset)
  107. return;
  108. fset->remove(force);
  109. }