114 lines
2.7 KiB
C#
114 lines
2.7 KiB
C#
|
using System.Collections;
|
|||
|
using Cysharp.Threading.Tasks;
|
|||
|
using game;
|
|||
|
using UnityEngine;
|
|||
|
using UnityEngine.SceneManagement;
|
|||
|
|
|||
|
public class G : Singleton<G>
|
|||
|
{
|
|||
|
#if UNITY_EDITOR
|
|||
|
public static bool DebugFreezeLevel;
|
|||
|
#endif
|
|||
|
|
|||
|
public Configs Configs { get; private set; }
|
|||
|
public Session Session { get; private set; }
|
|||
|
public TimeRewards TimeRewards { get; } = new TimeRewards();
|
|||
|
public Level Level => Session != null ? Session.Level : null;
|
|||
|
|
|||
|
private GameData _gameData = null;
|
|||
|
private readonly SDK _sdk = new SDK();
|
|||
|
|
|||
|
private void Awake()
|
|||
|
{
|
|||
|
StartCoroutine(SafeInit());
|
|||
|
}
|
|||
|
|
|||
|
private IEnumerator SafeInit()
|
|||
|
{
|
|||
|
yield return new WaitUntil(() => SceneManager.GetActiveScene().name == "game");
|
|||
|
yield return Init();
|
|||
|
}
|
|||
|
|
|||
|
private IEnumerator Init()
|
|||
|
=> UniTask.ToCoroutine( async () =>
|
|||
|
{
|
|||
|
LoadConfigs();
|
|||
|
LoadGameSettings();
|
|||
|
LoadGameState();
|
|||
|
|
|||
|
CameraManager.Init();
|
|||
|
TimeRewards.Init();
|
|||
|
Sound.Init();
|
|||
|
Ads.Init();
|
|||
|
UI.Init();
|
|||
|
|
|||
|
ILoader loader = (ILoader)Loader.Instance ?? new NullLoader();
|
|||
|
await loader.Load(_sdk, 0.5f);
|
|||
|
|
|||
|
Session = Session.Create();
|
|||
|
Session.CreateLevel();
|
|||
|
|
|||
|
loader.Load(null, 1f);
|
|||
|
loader.Close();
|
|||
|
});
|
|||
|
|
|||
|
private void LoadConfigs()
|
|||
|
{
|
|||
|
var asset = Resources.Load<TextAsset>("ext_config/configs/packed_bundle");
|
|||
|
(Configs = new Configs()).Load(new System.IO.MemoryStream(asset.bytes));
|
|||
|
}
|
|||
|
|
|||
|
private void LoadGameSettings()
|
|||
|
{
|
|||
|
var levels = Configs.Get<ConfGameLevels>("@game_levels");
|
|||
|
var buffs = Configs.Get<ConfGameBuffs>("@game_buffs");
|
|||
|
GameSettings.Load(levels, buffs);
|
|||
|
}
|
|||
|
|
|||
|
private void LoadGameState()
|
|||
|
{
|
|||
|
_gameData = new GameData(Settings.VERSION);
|
|||
|
_gameData.UnregisterPersistentStructures();
|
|||
|
|
|||
|
_gameData.RegisterPersistentStructures(
|
|||
|
|
|||
|
TimeRewards.Daily,
|
|||
|
TimeRewards.Offline,
|
|||
|
Save.Progress,
|
|||
|
Save.Inventory,
|
|||
|
Save.Buffs
|
|||
|
);
|
|||
|
|
|||
|
_gameData.TryLoadOrCreateNew();
|
|||
|
}
|
|||
|
|
|||
|
private void OnApplicationQuit()
|
|||
|
{
|
|||
|
SaveGameState();
|
|||
|
}
|
|||
|
|
|||
|
private bool SaveGameState()
|
|||
|
{
|
|||
|
if (_gameData.SaveLocal().IsOk() == false)
|
|||
|
{
|
|||
|
Log.Error("G.SaveGameState: Save game state ended with error.");
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
Log.Debug("G.SaveGameState: Save game state loaded successful.");
|
|||
|
_gameData.RememberSuccessfulSave();
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
private void OnApplicationPause(bool isPaused)
|
|||
|
{
|
|||
|
if (isPaused)
|
|||
|
{
|
|||
|
SaveGameState();
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
SDK.Reconnect();
|
|||
|
}
|
|||
|
}
|