areadamagelooped.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. class AreaDamageLooped : AreaDamageManager
  2. {
  3. //! Current start time in seconds
  4. protected float m_CurrentTime = 0.0;
  5. //! Previous start time in seconds
  6. protected float m_PreviousTime = 0.0;
  7. //! How much time has accumulated
  8. protected float m_AccumulatedTime = 0.0;
  9. //! Loop interval in seconds
  10. protected float m_LoopInterval = 1.0;
  11. //! Caching of the amount of loops that will be performed in this frame
  12. protected int m_AmountOfLoops = 0;
  13. //! Decides if the looping will be using the Object as reference or the time since last update loop
  14. protected bool m_LoopByObject = true;
  15. void AreaDamageLooped(EntityAI parent, bool loopByObject = true)
  16. {
  17. m_LoopByObject = loopByObject;
  18. }
  19. //! Gets called when the trigger is spawned, so is the start and also a reset
  20. override void OnTriggerCreated()
  21. {
  22. super.OnTriggerCreated();
  23. m_PreviousTime = g_Game.GetTickTime();
  24. m_AccumulatedTime = 0.0;
  25. }
  26. override void SetLoopInterval( float time )
  27. {
  28. m_LoopInterval = time;
  29. }
  30. override void OnEnterServerEvent(TriggerInsider insider)
  31. {
  32. super.OnEnterServerEvent(insider);
  33. // Do direct damaging first time on entering
  34. if (m_LoopByObject)
  35. OnEvaluateDamageEx(insider, 1);
  36. }
  37. override void OnStayStartServerEvent(int nrOfInsiders)
  38. {
  39. super.OnStayStartServerEvent(nrOfInsiders);
  40. m_CurrentTime = g_Game.GetTickTime();
  41. m_AccumulatedTime += m_CurrentTime - m_PreviousTime;
  42. m_PreviousTime = m_CurrentTime;
  43. m_AmountOfLoops = m_AccumulatedTime / m_LoopInterval;
  44. }
  45. override void OnStayServerEvent(TriggerInsider insider, float deltaTime)
  46. {
  47. super.OnStayServerEvent(insider, deltaTime);
  48. // As we are using the Ex variant, we can put the logic in CalculateDamageScale
  49. OnEvaluateDamageEx(insider, deltaTime);
  50. }
  51. override void OnStayFinishServerEvent()
  52. {
  53. super.OnStayFinishServerEvent();
  54. m_AccumulatedTime -= m_AmountOfLoops * m_LoopInterval;
  55. }
  56. // TODO: EMH: OnLeaveServerEvent -> perform the remaining buffered damage to be done?
  57. override protected float CalculateDamageScale(TriggerInsider insider, float deltaTime)
  58. {
  59. // If we want the LoopInterval to be dependant on the Object, we will need their lastDamaged time
  60. if (m_LoopByObject)
  61. {
  62. float lastDamaged = 0;
  63. AreaDamageTriggerInsider dInsider;
  64. if ( CastTo( dInsider, insider ) )
  65. lastDamaged = dInsider.lastDamaged;
  66. // First time it is damaged, do full damage
  67. if (lastDamaged == 0)
  68. return 1;
  69. // This way, in case the server lags, it will scale the damage to catch up if necessary
  70. // We want to wait until it is at least above 1, to not spam the network with hit messages
  71. float damageCoeff = (m_CurrentTime - lastDamaged) / m_LoopInterval;
  72. if ( damageCoeff >= 1 )
  73. return damageCoeff;
  74. return 0;
  75. }
  76. else
  77. {
  78. return m_AmountOfLoops;
  79. }
  80. }
  81. }