123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- enum EAlarmClockState
- {
- UNSET,
- SET,
- RINGING,
- //-----------
- COUNT
- }
- class ClockBase : Inventory_Base
- {
- float m_AlarmTime01;
- int m_State = EAlarmClockState.UNSET;
- static const float UPDATE_TICK_RATE = 1; // Clock update tick frequency
- ref Timer m_TimerUpdate;
- float m_RingingDuration;
- int m_StatePrev = -1;
-
- EffectSound m_RingingSoundLoop;
- EffectSound m_TurnOnSound;
- EffectSound m_DestoryedSound;
- EffectSound m_HitSound;
- EffectSound m_WorkingSound;
- const float RINGING_DURATION_MAX = 60;//in secs, at or past this value, the clock stops ringing
- void ClockBase()
- {
- Init();
- }
-
- void ~ClockBase()
- {
- SEffectManager.DestroyEffect(m_WorkingSound);
- SEffectManager.DestroyEffect(m_HitSound);
- SEffectManager.DestroyEffect(m_DestoryedSound);
- SEffectManager.DestroyEffect(m_TurnOnSound);
- SEffectManager.DestroyEffect(m_RingingSoundLoop);
- }
-
- void Init()
- {
- RegisterNetSyncVariableInt("m_State", 0, EAlarmClockState.COUNT - 1);
- }
-
- override void SetActions()
- {
- super.SetActions();
-
- AddAction(ActionAttachExplosivesTrigger);
- }
-
- protected int GetAlarmInMin()
- {
- int alarm_hand_in_mins = ConvertAlarmHand01ToMins12h(m_AlarmTime01);
-
- int pass, hour, minute;
- GetGame().GetWorld().GetDate(pass, pass, pass, hour, minute);
-
- int curr_time_in_minutes = ConvertTimeToMins12h(hour, minute);
- int ring_in_mins = GetTimeDiffInMins12h(curr_time_in_minutes, alarm_hand_in_mins);
- return ring_in_mins;
- }
-
- static int ConvertAlarmHand01ToMins12h(float time01)
- {
- return Math.Lerp(0,12*60,time01);
- }
-
- static int ConvertAlarmHand01ToMins(float time01, int mins_max/*what's the upper range in minutes we are trying to map the time01 to*/)
- {
- return Math.Lerp(0,mins_max,time01);
- }
-
- static float ConvertMins12hToAlarmHand01(int mins)
- {
- return Math.InverseLerp(0,12*60,mins);
- }
-
- static int ConvertTimeToMins12h(int hour, int minute)
- {
- if (hour >= 12)
- hour -= 12;
- return hour * 60 + minute;
- }
-
- static int GetTimeDiffInMins12h(int from_mins, int to_mins)
- {
- if (to_mins > from_mins)
- {
- return to_mins - from_mins;
- }
- else if (to_mins < from_mins)
- {
- return ((12 * 60) - from_mins) + to_mins;
- }
- else return 0;
- }
-
- string GetToggleSound()
- {
- return "";
- }
- string GetRingingSound()
- {
- return "";
- }
- string GetHitSound()
- {
- return "";
- }
- string GetDestroyedSound()
- {
- return "";
- }
- string GetWorkingSound()
- {
- return "";
- }
-
- override void EEKilled(Object killer)
- {
- super.EEKilled(killer);
- TurnOff();
- }
-
- override void EEHitByRemote(int damageType, EntityAI source, int component, string dmgZone, string ammo, vector modelPos)
- {
- super.EEHitByRemote(damageType, source, component, dmgZone, ammo, modelPos);
- PlaySoundSet( m_HitSound, GetHitSound(), 0, 0 );
- }
-
- override void OnDamageDestroyed(int oldLevel)
- {
- super.OnDamageDestroyed(oldLevel);
-
- if (GetGame().IsClient())
- {
- OnRingingStopClient();
- if (oldLevel != -1)
- {
- PlaySoundSet(m_DestoryedSound, GetDestroyedSound(), 0, 0);
- }
- }
- }
- protected void OnRingingStartServer()
- {
- ActivateParent();
- }
- protected void OnRingingStartClient()
- {
- PlaySoundSetLoop( m_RingingSoundLoop, GetRingingSound(), 0, 0 );
- if (m_WorkingSound)
- {
- SEffectManager.DestroyEffect(m_WorkingSound);
- }
- }
-
- protected void OnRingingStopServer();
-
- protected void OnRingingStopClient()
- {
- SEffectManager.DestroyEffect(m_RingingSoundLoop);
- }
-
- void SetAlarmInXMins(int in_mins)
- {
- int pass, hour, minute;
- GetGame().GetWorld().GetDate(pass, pass, pass, hour, minute);
- int mins12h = ConvertTimeToMins12h(hour, minute) + in_mins;
- float time01 = ConvertMins12hToAlarmHand01(mins12h);
- SetAlarmTimeServer(time01);
- Arm();
- }
-
- float GetRingingDurationMax()
- {
- return RINGING_DURATION_MAX;
- }
-
-
- protected void SetupTimerServer()
- {
- m_TimerUpdate = new Timer();
- m_TimerUpdate.Run(UPDATE_TICK_RATE , this, "OnUpdate", null, true);
- }
-
- protected void SetState(EAlarmClockState state)
- {
- m_State = state;
- SetSynchDirty();
- }
- protected void Disarm()
- {
- SetState(EAlarmClockState.UNSET);
- }
-
- protected void Arm()
- {
- SetupTimerServer();
- SetState(EAlarmClockState.SET);
- m_RingingDuration = 0;
- }
- protected void ActivateParent()
- {
- if (GetHierarchyParent())
- {
- ItemBase parent = ItemBase.Cast(GetHierarchyParent());
- if (parent)
- {
- parent.OnActivatedByItem(this);
- }
- }
- }
-
- protected void MakeRingingStart()
- {
- if (!m_TimerUpdate)
- SetupTimerServer();
- SetState(EAlarmClockState.RINGING);
-
- OnRingingStartServer();
- }
-
- protected void MakeRingingStop()
- {
- SetState(EAlarmClockState.UNSET);
-
- OnRingingStopServer();
- }
- void TurnOnClient();
- void TurnOffClient();
-
- override void OnVariablesSynchronized()
- {
- super.OnVariablesSynchronized();
-
- if (m_State != m_StatePrev)//state changed
- {
- if (m_StatePrev == EAlarmClockState.RINGING)
- {
- OnRingingStopClient();
- }
- else if (m_State == EAlarmClockState.RINGING)
- {
- OnRingingStartClient();
- }
- if (m_State == EAlarmClockState.SET)
- {
- if (m_StatePrev != -1 || IsInitialized())
- {
- PlaySoundSet( m_TurnOnSound, GetToggleSound(), 0, 0 );
- }
- if (GetWorkingSound())
- {
- PlaySoundSet( m_WorkingSound, GetWorkingSound(), 0, 0, true );
- }
- }
- else if (m_State == EAlarmClockState.UNSET)
- {
- if (m_StatePrev == EAlarmClockState.SET)
- {
- if (m_WorkingSound)
- {
- SEffectManager.DestroyEffect(m_WorkingSound);
- }
- if (m_StatePrev != -1 || IsInitialized())
- {
- PlaySoundSet( m_TurnOnSound, GetToggleSound(), 0, 0 );
- }
- }
- }
- m_StatePrev = m_State;
- }
- }
-
-
- //---------------------------------------------------------------------------------------------------------
- //---------------------------------------------- Public methods -------------------------------------------
- //---------------------------------------------------------------------------------------------------------
-
- bool IsRinging()
- {
- return (m_State == EAlarmClockState.RINGING);
- }
-
- bool IsAlarmOn()
- {
- return (m_State == EAlarmClockState.SET);
- }
-
- void TurnOn()
- {
- Arm();
- }
- void TurnOff()
- {
- if ( IsRinging() )
- {
- MakeRingingStop();
- }
- else
- {
- Disarm();
- }
-
- m_TimerUpdate = null;
- }
- void SetAlarmTimeServer(float time01)
- {
- SetAnimationPhaseNow("ClockAlarm", time01);
- m_AlarmTime01 = time01;
- }
- }
|