using System; using UnityEngine; public class GlobalUpdate : MonoBehaviour { int m_updatesToHandleGetFocus = -1; bool m_focus = true; bool m_safeAreaInited = false; //----------------------------------------------------------------------------------------- public void OnDestroy() { OnDestroySave(); } //----------------------------------------------------------------------------------------- void OnDestroySave() { if (GlobalsVar.gUser != null) { GlobalsVar.gUser.SaveEquipedItems(); GlobalsVar.gUser.SaveUnlockedItems(); GlobalsVar.gUser.Save(false); } } //----------------------------------------------------------------------------------------- void Awake () { DontDestroyOnLoad(this); } //------------------------------------------------------------------- void OnApplicationFocus(bool hasFocus) { if (m_focus != hasFocus) { if (hasFocus) { m_updatesToHandleGetFocus = 2; } else { DateTime nowTime = UnbiasedTime.Instance.Now(); GlobalsVar.gLostFocusTime = (nowTime.Ticks / TimeSpan.TicksPerMillisecond); m_updatesToHandleGetFocus = -1; OnDestroySave(); } m_focus = hasFocus; } } //----------------------------------------------------------------------------------------- void TryInitSafeArea() { if (m_safeAreaInited) return; if (Screen.safeArea.width != Screen.width) return; m_safeAreaInited = true; GlobalsVar.gSafeAreaHudShift = (int)Math.Abs((Screen.safeArea.height - Screen.height) / 2.0f); if (GlobalsVar.gSafeAreaHudShift > 50) // уж больно много, на баг похоже.. GlobalsVar.gSafeAreaHudShift = 50; Debug.Log("Safe area = " + GlobalsVar.gSafeAreaHudShift.ToString()); if (GlobalsVar.gBoard != null) GlobalsVar.gBoard.ApplaySafeAreaHudShift(); } //----------------------------------------------------------------------------------------- void Update () { if (!m_focus) return; UpdateUser(); UpdateSkipClickTime(); if (m_focus && m_updatesToHandleGetFocus > 0) { m_updatesToHandleGetFocus -= 1; if (m_updatesToHandleGetFocus <= 0) { DateTime nowTime = UnbiasedTime.Instance.Now(); long timeNow = nowTime.Ticks / TimeSpan.TicksPerMillisecond; GlobalsVar.gDeltaTimeFromLostFocus = (timeNow - GlobalsVar.gLostFocusTime) / 1000f; if (GlobalsVar.gDeltaTimeFromLostFocus < 0.0f) GlobalsVar.gDeltaTimeFromLostFocus = 1.0f; if (GlobalsVar.gLostFocusTime > 0) { UpdateEnergyTimer(GlobalsVar.gDeltaTimeFromLostFocus); CommonFunctions.UpdateTimeToFreeCoins(GlobalsVar.gDeltaTimeFromLostFocus); } } } TryInitSafeArea(); UpdateEnergyTimer(Time.unscaledDeltaTime); CommonFunctions.UpdateTimeToFreeCoins(Time.unscaledDeltaTime); } //------------------------------------------------------------------- void UpdateSkipClickTime() { if (GlobalsVar.gSkipClickTime <= 0f) return; GlobalsVar.gSkipClickTime -= Time.unscaledDeltaTime; if (GlobalsVar.gSkipClickTime < 0f) GlobalsVar.gSkipClickTime = 0f; } //------------------------------------------------------------------- void UpdateEnergyTimer(float timeDelta) { if (GlobalsVar.gUser == null) return; if (GlobalsVar.gUser.Energy >= GlobalsVar.gMaxEnergyCount) return; while (timeDelta > 0.0f) { float valueToDec = timeDelta; if (valueToDec < GlobalsVar.gCurrentTimeToRegenerateEnergy) { timeDelta = 0.0f; } else { timeDelta -= GlobalsVar.gCurrentTimeToRegenerateEnergy; valueToDec = GlobalsVar.gCurrentTimeToRegenerateEnergy; } GlobalsVar.gCurrentTimeToRegenerateEnergy -= valueToDec; if (GlobalsVar.gCurrentTimeToRegenerateEnergy <= 0) { GlobalsVar.gUser.IncEnergy(); if (GlobalsVar.gUser.Energy < GlobalsVar.gMaxEnergyCount) GlobalsVar.gCurrentTimeToRegenerateEnergy = GlobalsVar.gTimeToRegenerateEnergy; if (GlobalsVar.gUser.Energy >= GlobalsVar.gMaxEnergyCount) break; } } MainMenu mainMenu = GlobalsVar.gBoard as MainMenu; if (mainMenu) mainMenu.UpdateTimerLabel(); } //----------------------------------------------------------------------------------------- void UpdateUser() { if (GlobalsVar.gUser != null) GlobalsVar.gUser.Update(); } }