feat: add save system

This commit is contained in:
cuqmbr 2022-07-22 14:01:41 +03:00
parent 9fba37c78a
commit 8c7a68cc6b
7 changed files with 90 additions and 1 deletions

View File

@ -0,0 +1,17 @@
using UnityEngine;
public class SaveManager : MonoBehaviour
{
[SerializeField] private string ApiUrl;
private void Awake()
{
SessionStore.ApiUrl = ApiUrl;
SessionStore.Load();
}
private void OnApplicationQuit()
{
SessionStore.Save();
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0d152cc19f7d546e382626f05f73467d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -23,12 +23,13 @@ public class ScoreManager : MonoBehaviour
PlayerEvents.OnBallTouched += AddScore;
PlayerEvents.OnBallTouched += AddExperience;
PlayerEvents.OnWallTouched += ResetExperienceAndRewardMultiplier;
PlayerEvents.OnDeath += SaveHighScore;
GameStateManager.Instance.OnGameStateChange += OnGameStateChange;
}
private void Start()
{
_highScore = SessionStore.HighScore;
LoadHighScore();
}
private void Update()
@ -94,6 +95,22 @@ public class ScoreManager : MonoBehaviour
ResetExperienceAndRewardMultiplier();
}
private void SaveHighScore()
{
if (_currentScore < _highScore)
{
return;
}
_highScore = _currentScore;
SessionStore.HighScore = _highScore;
}
private void LoadHighScore()
{
_highScore = SessionStore.HighScore;
}
private void OnGameStateChange(GameState newGameState)
{
switch (newGameState)

View File

@ -0,0 +1,33 @@
using System.Linq;
using DatabaseModels.DataTransferObjets;
using JetBrains.Annotations;
public static class SessionStore
{
public static string ApiUrl { get; set; }
public static UserData UserData { get; private set; }
public static int HighScore { get; set; }
public static ScoreboardRecordDto[] ScoreboardRecords;
public static async void Save()
{
await SaveSystem.SaveToJsonAsync("userData.json", UserData);
await SaveSystem.SaveToBinaryAsync("HighScore.bin", HighScore);
}
public static async void Load()
{
UserData = await SaveSystem.LoadFromJsonAsync<UserData>("userData.json");
HighScore = await SaveSystem.LoadFromBinaryAsync<int>("HighScore.bin");
ScoreboardRecords = await HttpClient.Get<ScoreboardRecordDto[]>($"{ApiUrl}/scoreboard");
int? dbHighScore = ScoreboardRecords?.FirstOrDefault(sbr => sbr.User.Username == UserData.Username)?.Score;
if (dbHighScore != null && dbHighScore > HighScore)
{
HighScore = (int) dbHighScore;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 64ad7cad6b2d4b6c8e5bf9684216b484
timeCreated: 1658237964

View File

@ -0,0 +1,5 @@
public class UserData
{
public string Username { get; set; }
public string Password { get; set; }
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 8fe7c657c4e04ed5998db05dc8771aae
timeCreated: 1658238022