kitchentimer.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. class KitchenTimer : ClockBase
  2. {
  3. const string RINGING_SOUND = "KitchenTimer_Ring_Loop_SoundSet";
  4. const string DESTROYED_SOUND = "AlarmClock_Destroyed_SoundSet";
  5. const string HIT_SOUND = "AlarmClock_Hit_SoundSet";
  6. const string WORKING_SOUND = "KitchenTimer_Ticking_Loop_SoundSet";
  7. EffectSound m_RingingStopSound;
  8. static ref NoiseParams m_NoisePar;
  9. static NoiseSystem m_NoiseSystem;
  10. int m_AlarmInSecs;
  11. override void Init()
  12. {
  13. super.Init();
  14. if (GetGame().IsServer())
  15. {
  16. m_NoiseSystem = GetGame().GetNoiseSystem();
  17. if ( m_NoiseSystem && !m_NoisePar)
  18. {
  19. // Create and load noise parameters
  20. m_NoisePar = new NoiseParams;
  21. m_NoisePar.LoadFromPath("cfgVehicles " + GetType() + " NoiseKitchenTimer");
  22. }
  23. }
  24. }
  25. override void SetActions()
  26. {
  27. super.SetActions();
  28. AddAction(ActionSetKitchenTimer);
  29. AddAction(ActionResetKitchenTimer);
  30. }
  31. override string GetExplosiveTriggerSlotName()
  32. {
  33. return "TriggerKitchenTimer";
  34. }
  35. override string GetToggleSound()
  36. {
  37. return "";
  38. }
  39. override string GetRingingSound()
  40. {
  41. return RINGING_SOUND;
  42. }
  43. string GetRingingStopSound()
  44. {
  45. return "KitchenTimer_Ring_End_SoundSet";
  46. }
  47. override string GetDestroyedSound()
  48. {
  49. return DESTROYED_SOUND;
  50. }
  51. override string GetHitSound()
  52. {
  53. return HIT_SOUND;
  54. }
  55. override string GetWorkingSound()
  56. {
  57. return WORKING_SOUND;
  58. }
  59. int GetMinutesMax()
  60. {
  61. return 45;
  62. }
  63. int Time01ToSeconds(float time01)
  64. {
  65. return Math.Lerp(0,GetMinutesMax() * 60, time01);
  66. }
  67. float SecondsTo01(int seconds)
  68. {
  69. return Math.InverseLerp(0,GetMinutesMax() * 60, seconds);
  70. }
  71. override float GetRingingDurationMax()
  72. {
  73. return 60;
  74. }
  75. override void TurnOff()
  76. {
  77. super.TurnOff();
  78. SEffectManager.DestroyEffect(m_WorkingSound);
  79. }
  80. void OnUpdate()
  81. {
  82. if (m_AlarmInSecs > 0)
  83. {
  84. m_AlarmInSecs -= UPDATE_TICK_RATE;
  85. float time01 = SecondsTo01(m_AlarmInSecs);
  86. SetAnimationPhaseNow("ClockAlarm", time01);
  87. if (IsRinging())
  88. {
  89. MakeRingingStop();
  90. }
  91. }
  92. else if (!IsRinging())
  93. {
  94. MakeRingingStart();
  95. }
  96. if (IsRinging())
  97. {
  98. m_RingingDuration += UPDATE_TICK_RATE;
  99. if (m_RingingDuration >= GetRingingDurationMax())
  100. {
  101. TurnOff();
  102. }
  103. else if (m_NoiseSystem)
  104. {
  105. m_NoiseSystem.AddNoiseTarget(GetPosition(), UPDATE_TICK_RATE, m_NoisePar, NoiseAIEvaluate.GetNoiseReduction(GetGame().GetWeather()));
  106. }
  107. }
  108. }
  109. override protected void Disarm()
  110. {
  111. super.Disarm();
  112. SetAlarmTimeServerSecs(0);
  113. }
  114. override protected void OnRingingStopClient()
  115. {
  116. if (m_RingingSoundLoop)
  117. PlaySoundSet(m_RingingStopSound, GetRingingStopSound(), 0, 0);
  118. super.OnRingingStopClient();
  119. }
  120. override bool OnStoreLoad(ParamsReadContext ctx, int version)
  121. {
  122. if (!super.OnStoreLoad(ctx, version))
  123. return false;
  124. if (version < 128)
  125. {
  126. return true;
  127. }
  128. EAlarmClockState state;
  129. if (!ctx.Read(state))
  130. {
  131. return false;
  132. }
  133. int time;
  134. if (!ctx.Read(time))
  135. {
  136. return false;
  137. }
  138. SetState(state);
  139. if (state == EAlarmClockState.SET)
  140. {
  141. SetAlarmTimeServerSecs(time);
  142. }
  143. else if (state == EAlarmClockState.RINGING)
  144. {
  145. MakeRingingStart();
  146. }
  147. return true;
  148. }
  149. override void OnStoreSave(ParamsWriteContext ctx)
  150. {
  151. super.OnStoreSave(ctx);
  152. ctx.Write(m_State);
  153. ctx.Write(m_AlarmInSecs);
  154. }
  155. //---------------------------------------------------------------------------------------------------------
  156. //---------------------------------------------- Public methods -------------------------------------------
  157. //---------------------------------------------------------------------------------------------------------
  158. override void SetAlarmTimeServer(float time01)
  159. {
  160. SetAnimationPhaseNow("ClockAlarm", time01);
  161. m_AlarmInSecs = Time01ToSeconds(time01);
  162. if (m_AlarmInSecs > 0)
  163. {
  164. TurnOn();
  165. }
  166. }
  167. void SetAlarmTimeServerSecs(int inSecs)
  168. {
  169. SetAlarmTimeServer(SecondsTo01(inSecs));
  170. }
  171. //----------------------------------
  172. //------------- DEBUG --------------
  173. //----------------------------------
  174. override void GetDebugActions(out TSelectableActionInfoArrayEx outputList)
  175. {
  176. outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.ALARM_SET_AHEAD, "Set Alarm Ahead", FadeColors.LIGHT_GREY));
  177. outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.SEPARATOR, "___________________________", FadeColors.LIGHT_GREY));
  178. super.GetDebugActions(outputList);
  179. }
  180. override bool OnAction(int action_id, Man player, ParamsReadContext ctx)
  181. {
  182. if (super.OnAction(action_id, player, ctx))
  183. return true;
  184. if (GetGame().IsServer() || !GetGame().IsMultiplayer())
  185. {
  186. if (action_id == EActions.ALARM_SET_AHEAD)
  187. {
  188. SetAlarmTimeServerSecs(20);
  189. }
  190. }
  191. return false;
  192. }
  193. override string GetDebugText()
  194. {
  195. string debug_output;
  196. if (GetGame().IsDedicatedServer())
  197. {
  198. debug_output = "alarm in: " + m_AlarmInSecs.ToString() + " secs" + "\n";
  199. debug_output += "current state: " + typename.EnumToString(EAlarmClockState, m_State) + "\n";
  200. debug_output += "ringing for " + m_RingingDuration.ToString()+ " secs" + "\n";
  201. debug_output += "ringing max " + GetRingingDurationMax().ToString()+ " secs" + "\n";
  202. }
  203. else
  204. {
  205. debug_output = "this is client";
  206. }
  207. return debug_output;
  208. }
  209. };