122 lines
3.1 KiB
C#
122 lines
3.1 KiB
C#
|
using System.Collections;
|
|||
|
using Cysharp.Threading.Tasks;
|
|||
|
using game;
|
|||
|
using RND.SDK;
|
|||
|
using UnityEngine;
|
|||
|
using UnityEngine.SceneManagement;
|
|||
|
using RND;
|
|||
|
|
|||
|
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();
|
|||
|
|
|||
|
CameraManager.Init();
|
|||
|
Sound.Init();
|
|||
|
Ads.Init();
|
|||
|
UI.Init();
|
|||
|
|
|||
|
TimeRewards.Init();
|
|||
|
ILoader loader = (ILoader)Loader.Instance ?? new NullLoader();
|
|||
|
await loader.Load(_sdk, 0.5f);
|
|||
|
|
|||
|
LoadGameState();
|
|||
|
Session = Session.Create();
|
|||
|
Session.CreateLevel();
|
|||
|
|
|||
|
Session.OnLevelStarted += () =>
|
|||
|
ProductStatistics.ProgressionEvent(EnumProductProgressStatus.START, (int)Save.Progress.LevelId);
|
|||
|
Session.OnLevelEnded += status =>
|
|||
|
ProductStatistics.ProgressionEvent(status == EnumLevelResult.WIN
|
|||
|
? EnumProductProgressStatus.COMPLETE : EnumProductProgressStatus.FAIL, (int)Save.Progress.LevelId);
|
|||
|
|
|||
|
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 == null || _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();
|
|||
|
}
|
|||
|
}
|