pluginobjectsinteractionmanager.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. class PluginObjectsInteractionManager extends PluginBase
  2. {
  3. private ref array<Object> m_LockedObjects;
  4. private ref array<float> m_LockedObjectsDecay;
  5. private const float TIME_TO_FORCED_UNLOCK = 60;
  6. private const float TICK_RATE = 10;
  7. private ref Timer m_DecayTimer;
  8. void PluginObjectsInteractionManager()
  9. {
  10. m_LockedObjects = new array<Object>;
  11. m_LockedObjectsDecay = new array<float>;
  12. //TIMERDEPRECATED - timer for decaying objects
  13. m_DecayTimer = new Timer();
  14. m_DecayTimer.Run(TICK_RATE, this, "Decay", NULL,true);
  15. }
  16. bool IsFree(Object target)
  17. {
  18. if ( target && m_LockedObjects.Count() > 0 )
  19. {
  20. for ( int i = 0; i < m_LockedObjects.Count(); i++ )
  21. {
  22. if ( m_LockedObjects.Get(i) == target )
  23. {
  24. return false;
  25. }
  26. }
  27. }
  28. return true;
  29. }
  30. void Lock(Object target)
  31. {
  32. if ( target && !IsFree(target) )
  33. {
  34. m_LockedObjects.Insert(target);
  35. m_LockedObjectsDecay.Insert(0);
  36. }
  37. }
  38. void Release(Object target)
  39. {
  40. if ( target && m_LockedObjects.Count() > 0 )
  41. {
  42. for ( int i = 0; i < m_LockedObjects.Count(); i++ )
  43. {
  44. if ( m_LockedObjects.Get(i) == target )
  45. {
  46. m_LockedObjects.Remove(i);
  47. m_LockedObjectsDecay.Remove(i);
  48. break;
  49. }
  50. }
  51. }
  52. }
  53. //FAILSAFE - checks periodically locked objects and releases them automaticaly if given time has passed
  54. void Decay()
  55. {
  56. if ( m_LockedObjectsDecay.Count() > 0 )
  57. {
  58. for ( int i = 0; i < m_LockedObjectsDecay.Count(); i++ )
  59. {
  60. if ( m_LockedObjectsDecay.Get(i) >= TIME_TO_FORCED_UNLOCK )
  61. {
  62. m_LockedObjects.Remove(i);
  63. m_LockedObjectsDecay.Remove(i);
  64. }
  65. else
  66. {
  67. m_LockedObjectsDecay.Get(i) += TICK_RATE;
  68. }
  69. }
  70. }
  71. }
  72. };