clockbase.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. enum EAlarmClockState
  2. {
  3. UNSET,
  4. SET,
  5. RINGING,
  6. //-----------
  7. COUNT
  8. }
  9. class ClockBase : Inventory_Base
  10. {
  11. float m_AlarmTime01;
  12. int m_State = EAlarmClockState.UNSET;
  13. static const float UPDATE_TICK_RATE = 1; // Clock update tick frequency
  14. ref Timer m_TimerUpdate;
  15. float m_RingingDuration;
  16. int m_StatePrev = -1;
  17. EffectSound m_RingingSoundLoop;
  18. EffectSound m_TurnOnSound;
  19. EffectSound m_DestoryedSound;
  20. EffectSound m_HitSound;
  21. EffectSound m_WorkingSound;
  22. const float RINGING_DURATION_MAX = 60;//in secs, at or past this value, the clock stops ringing
  23. void ClockBase()
  24. {
  25. Init();
  26. }
  27. void ~ClockBase()
  28. {
  29. SEffectManager.DestroyEffect(m_WorkingSound);
  30. SEffectManager.DestroyEffect(m_HitSound);
  31. SEffectManager.DestroyEffect(m_DestoryedSound);
  32. SEffectManager.DestroyEffect(m_TurnOnSound);
  33. SEffectManager.DestroyEffect(m_RingingSoundLoop);
  34. }
  35. void Init()
  36. {
  37. RegisterNetSyncVariableInt("m_State", 0, EAlarmClockState.COUNT - 1);
  38. }
  39. override void SetActions()
  40. {
  41. super.SetActions();
  42. AddAction(ActionAttachExplosivesTrigger);
  43. }
  44. protected int GetAlarmInMin()
  45. {
  46. int alarm_hand_in_mins = ConvertAlarmHand01ToMins12h(m_AlarmTime01);
  47. int pass, hour, minute;
  48. GetGame().GetWorld().GetDate(pass, pass, pass, hour, minute);
  49. int curr_time_in_minutes = ConvertTimeToMins12h(hour, minute);
  50. int ring_in_mins = GetTimeDiffInMins12h(curr_time_in_minutes, alarm_hand_in_mins);
  51. return ring_in_mins;
  52. }
  53. static int ConvertAlarmHand01ToMins12h(float time01)
  54. {
  55. return Math.Lerp(0,12*60,time01);
  56. }
  57. static int ConvertAlarmHand01ToMins(float time01, int mins_max/*what's the upper range in minutes we are trying to map the time01 to*/)
  58. {
  59. return Math.Lerp(0,mins_max,time01);
  60. }
  61. static float ConvertMins12hToAlarmHand01(int mins)
  62. {
  63. return Math.InverseLerp(0,12*60,mins);
  64. }
  65. static int ConvertTimeToMins12h(int hour, int minute)
  66. {
  67. if (hour >= 12)
  68. hour -= 12;
  69. return hour * 60 + minute;
  70. }
  71. static int GetTimeDiffInMins12h(int from_mins, int to_mins)
  72. {
  73. if (to_mins > from_mins)
  74. {
  75. return to_mins - from_mins;
  76. }
  77. else if (to_mins < from_mins)
  78. {
  79. return ((12 * 60) - from_mins) + to_mins;
  80. }
  81. else return 0;
  82. }
  83. string GetToggleSound()
  84. {
  85. return "";
  86. }
  87. string GetRingingSound()
  88. {
  89. return "";
  90. }
  91. string GetHitSound()
  92. {
  93. return "";
  94. }
  95. string GetDestroyedSound()
  96. {
  97. return "";
  98. }
  99. string GetWorkingSound()
  100. {
  101. return "";
  102. }
  103. override void EEKilled(Object killer)
  104. {
  105. super.EEKilled(killer);
  106. TurnOff();
  107. }
  108. override void EEHitByRemote(int damageType, EntityAI source, int component, string dmgZone, string ammo, vector modelPos)
  109. {
  110. super.EEHitByRemote(damageType, source, component, dmgZone, ammo, modelPos);
  111. PlaySoundSet( m_HitSound, GetHitSound(), 0, 0 );
  112. }
  113. override void OnDamageDestroyed(int oldLevel)
  114. {
  115. super.OnDamageDestroyed(oldLevel);
  116. if (GetGame().IsClient())
  117. {
  118. OnRingingStopClient();
  119. if (oldLevel != -1)
  120. {
  121. PlaySoundSet(m_DestoryedSound, GetDestroyedSound(), 0, 0);
  122. }
  123. }
  124. }
  125. protected void OnRingingStartServer()
  126. {
  127. ActivateParent();
  128. }
  129. protected void OnRingingStartClient()
  130. {
  131. PlaySoundSetLoop( m_RingingSoundLoop, GetRingingSound(), 0, 0 );
  132. if (m_WorkingSound)
  133. {
  134. SEffectManager.DestroyEffect(m_WorkingSound);
  135. }
  136. }
  137. protected void OnRingingStopServer();
  138. protected void OnRingingStopClient()
  139. {
  140. SEffectManager.DestroyEffect(m_RingingSoundLoop);
  141. }
  142. void SetAlarmInXMins(int in_mins)
  143. {
  144. int pass, hour, minute;
  145. GetGame().GetWorld().GetDate(pass, pass, pass, hour, minute);
  146. int mins12h = ConvertTimeToMins12h(hour, minute) + in_mins;
  147. float time01 = ConvertMins12hToAlarmHand01(mins12h);
  148. SetAlarmTimeServer(time01);
  149. Arm();
  150. }
  151. float GetRingingDurationMax()
  152. {
  153. return RINGING_DURATION_MAX;
  154. }
  155. protected void SetupTimerServer()
  156. {
  157. m_TimerUpdate = new Timer();
  158. m_TimerUpdate.Run(UPDATE_TICK_RATE , this, "OnUpdate", null, true);
  159. }
  160. protected void SetState(EAlarmClockState state)
  161. {
  162. m_State = state;
  163. SetSynchDirty();
  164. }
  165. protected void Disarm()
  166. {
  167. SetState(EAlarmClockState.UNSET);
  168. }
  169. protected void Arm()
  170. {
  171. SetupTimerServer();
  172. SetState(EAlarmClockState.SET);
  173. m_RingingDuration = 0;
  174. }
  175. protected void ActivateParent()
  176. {
  177. if (GetHierarchyParent())
  178. {
  179. ItemBase parent = ItemBase.Cast(GetHierarchyParent());
  180. if (parent)
  181. {
  182. parent.OnActivatedByItem(this);
  183. }
  184. }
  185. }
  186. protected void MakeRingingStart()
  187. {
  188. if (!m_TimerUpdate)
  189. SetupTimerServer();
  190. SetState(EAlarmClockState.RINGING);
  191. OnRingingStartServer();
  192. }
  193. protected void MakeRingingStop()
  194. {
  195. SetState(EAlarmClockState.UNSET);
  196. OnRingingStopServer();
  197. }
  198. void TurnOnClient();
  199. void TurnOffClient();
  200. override void OnVariablesSynchronized()
  201. {
  202. super.OnVariablesSynchronized();
  203. if (m_State != m_StatePrev)//state changed
  204. {
  205. if (m_StatePrev == EAlarmClockState.RINGING)
  206. {
  207. OnRingingStopClient();
  208. }
  209. else if (m_State == EAlarmClockState.RINGING)
  210. {
  211. OnRingingStartClient();
  212. }
  213. if (m_State == EAlarmClockState.SET)
  214. {
  215. if (m_StatePrev != -1 || IsInitialized())
  216. {
  217. PlaySoundSet( m_TurnOnSound, GetToggleSound(), 0, 0 );
  218. }
  219. if (GetWorkingSound())
  220. {
  221. PlaySoundSet( m_WorkingSound, GetWorkingSound(), 0, 0, true );
  222. }
  223. }
  224. else if (m_State == EAlarmClockState.UNSET)
  225. {
  226. if (m_StatePrev == EAlarmClockState.SET)
  227. {
  228. if (m_WorkingSound)
  229. {
  230. SEffectManager.DestroyEffect(m_WorkingSound);
  231. }
  232. if (m_StatePrev != -1 || IsInitialized())
  233. {
  234. PlaySoundSet( m_TurnOnSound, GetToggleSound(), 0, 0 );
  235. }
  236. }
  237. }
  238. m_StatePrev = m_State;
  239. }
  240. }
  241. //---------------------------------------------------------------------------------------------------------
  242. //---------------------------------------------- Public methods -------------------------------------------
  243. //---------------------------------------------------------------------------------------------------------
  244. bool IsRinging()
  245. {
  246. return (m_State == EAlarmClockState.RINGING);
  247. }
  248. bool IsAlarmOn()
  249. {
  250. return (m_State == EAlarmClockState.SET);
  251. }
  252. void TurnOn()
  253. {
  254. Arm();
  255. }
  256. void TurnOff()
  257. {
  258. if ( IsRinging() )
  259. {
  260. MakeRingingStop();
  261. }
  262. else
  263. {
  264. Disarm();
  265. }
  266. m_TimerUpdate = null;
  267. }
  268. void SetAlarmTimeServer(float time01)
  269. {
  270. SetAnimationPhaseNow("ClockAlarm", time01);
  271. m_AlarmTime01 = time01;
  272. }
  273. }