alarmclock.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. class AlarmClock_ColorBase: ClockBase
  2. {
  3. const string RINGING_SOUND = "AlarmClock_Ring_Loop_SoundSet";
  4. const string TURN_TOGGLE_SOUND = "AlarmClock_Turn_Off_SoundSet";
  5. const string DESTROYED_SOUND = "AlarmClock_Destroyed_SoundSet";
  6. const string HIT_SOUND = "AlarmClock_Hit_SoundSet";
  7. static ref NoiseParams m_NoisePar;
  8. static NoiseSystem m_NoiseSystem;
  9. override void Init()
  10. {
  11. super.Init();
  12. if ( GetGame().IsServer() )
  13. {
  14. m_NoiseSystem = GetGame().GetNoiseSystem();
  15. if ( m_NoiseSystem && !m_NoisePar )
  16. {
  17. // Create and load noise parameters
  18. m_NoisePar = new NoiseParams;
  19. m_NoisePar.LoadFromPath("cfgVehicles " + GetType() + " NoiseAlarmClock");
  20. }
  21. }
  22. }
  23. void ~AlarmClock_ColorBase()
  24. {
  25. #ifndef SERVER
  26. OnRingingStopClient();
  27. #endif
  28. }
  29. override void SetActions()
  30. {
  31. super.SetActions();
  32. AddAction(ActionSetAlarmClock);
  33. AddAction(ActionTurnOnAlarmClock);
  34. AddAction(ActionTurnOffAlarmClock);
  35. }
  36. override string GetToggleSound()
  37. {
  38. return TURN_TOGGLE_SOUND;
  39. }
  40. override string GetRingingSound()
  41. {
  42. return RINGING_SOUND;
  43. }
  44. override string GetDestroyedSound()
  45. {
  46. return DESTROYED_SOUND;
  47. }
  48. override string GetHitSound()
  49. {
  50. return HIT_SOUND;
  51. }
  52. override string GetExplosiveTriggerSlotName()
  53. {
  54. return "TriggerAlarmClock";
  55. }
  56. override void GetDebugActions(out TSelectableActionInfoArrayEx outputList)
  57. {
  58. outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.ACTIVATE_ENTITY, "SetAlarmAhead1Min", FadeColors.LIGHT_GREY));
  59. outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.SEPARATOR, "___________________________", FadeColors.LIGHT_GREY));
  60. super.GetDebugActions(outputList);
  61. }
  62. override bool OnAction(int action_id, Man player, ParamsReadContext ctx)
  63. {
  64. if (super.OnAction(action_id, player, ctx))
  65. return true;
  66. if (GetGame().IsServer() || !GetGame().IsMultiplayer())
  67. {
  68. if (action_id == EActions.ACTIVATE_ENTITY)
  69. {
  70. SetAlarmInXMins(1);
  71. }
  72. }
  73. return false;
  74. }
  75. override string GetDebugText()
  76. {
  77. string debug_output;
  78. if( GetGame().IsDedicatedServer())
  79. {
  80. debug_output = "alarm in: " + GetAlarmInMin().ToString() + " mins" + "\n";
  81. debug_output += "current state: " + typename.EnumToString(EAlarmClockState, m_State) + "\n";;
  82. debug_output += "ringing for " + m_RingingDuration.ToString()+ " secs" + "\n";
  83. debug_output += "ringing max " + GetRingingDurationMax().ToString()+ " secs" + "\n";
  84. }
  85. else
  86. {
  87. debug_output = "this is client";
  88. }
  89. return debug_output;
  90. }
  91. void OnUpdate()
  92. {
  93. if ( IsAlarmOn() )
  94. {
  95. //due to variable server time flow(day-night time accel), it's not possible to simply set a timer for X secs without some convoluted math/code, so we need to check at regular intervals
  96. int alarm_hand_in_minutes = ConvertAlarmHand01ToMins12h(m_AlarmTime01);
  97. int pass, hour, minute;
  98. GetGame().GetWorld().GetDate(pass, pass, pass, hour, minute);
  99. int curr_time_in_minutes = ConvertTimeToMins12h(hour, minute);
  100. //Print(GetAlarmInMin());
  101. if ( alarm_hand_in_minutes == curr_time_in_minutes )
  102. {
  103. MakeRingingStart();
  104. }
  105. }
  106. if ( IsRinging())
  107. {
  108. m_RingingDuration += UPDATE_TICK_RATE;
  109. if (m_RingingDuration >= GetRingingDurationMax())
  110. {
  111. TurnOff();
  112. }
  113. else if ( m_NoiseSystem )
  114. {
  115. m_NoiseSystem.AddNoiseTarget( GetPosition(), UPDATE_TICK_RATE, m_NoisePar, NoiseAIEvaluate.GetNoiseReduction(GetGame().GetWeather()));
  116. }
  117. }
  118. }
  119. protected void AnimateAlarmHand(float value)
  120. {
  121. SetAnimationPhaseNow("ClockAlarm", value);
  122. }
  123. override bool OnStoreLoad( ParamsReadContext ctx, int version )
  124. {
  125. if (!super.OnStoreLoad(ctx, version))
  126. return false;
  127. if (version < 126)
  128. {
  129. return true;
  130. }
  131. EAlarmClockState state;
  132. if ( !ctx.Read( state ) )
  133. {
  134. return false;
  135. }
  136. float time;
  137. if ( !ctx.Read( time ) )
  138. {
  139. return false;
  140. }
  141. SetAlarmTimeServer(time);
  142. SetState(state);
  143. if ( state == EAlarmClockState.SET )
  144. {
  145. TurnOn();
  146. }
  147. else if (state == EAlarmClockState.RINGING )
  148. {
  149. MakeRingingStart();
  150. }
  151. return true;
  152. }
  153. override void OnStoreSave(ParamsWriteContext ctx)
  154. {
  155. super.OnStoreSave(ctx);
  156. ctx.Write(m_State);
  157. ctx.Write(m_AlarmTime01);
  158. }
  159. override void OnDebugSpawn()
  160. {
  161. TurnOn();
  162. MakeRingingStart();
  163. }
  164. };
  165. class AlarmClock_Red : AlarmClock_ColorBase {};
  166. class AlarmClock_Blue : AlarmClock_ColorBase {};
  167. class AlarmClock_Green : AlarmClock_ColorBase {};