Skip to content

Commit

Permalink
Refactor local playerprefs profile service.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zesix committed Sep 21, 2018
1 parent 6e9d840 commit b8d4bd5
Show file tree
Hide file tree
Showing 20 changed files with 282 additions and 306 deletions.
4 changes: 2 additions & 2 deletions Assets/01_Scenes/MainMenu.unity
Expand Up @@ -1796,8 +1796,8 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 21379fd1b0a1f604fa697b2c26ea7384, type: 3}
m_Name:
m_EditorClassIdentifier:
_dropdownSystem: {fileID: 1728350269}
_resolutionOptions:
DropdownSystem: {fileID: 1728350269}
ResolutionOptions:
- 1024x768
- 1280x800
- 1360x768
Expand Down
27 changes: 0 additions & 27 deletions Assets/Scripts/MainSystem/PresentationManager.cs

This file was deleted.

27 changes: 27 additions & 0 deletions Assets/Scripts/MainSystem/PresentationService.cs
@@ -0,0 +1,27 @@
using UnityEngine;

namespace Impulse
{
/// <summary>
/// Manages the presentation for the game
/// </summary>
public class PresentationService : Singleton<PresentationService>
{
public void Initialize()
{
SetResolution(LocalPlayerProfileService.Instance.GetLocalData().Resolution);
}

/// <summary>
/// Set display resolution.
/// </summary>
public void SetResolution(string resolutionString)
{
var words = resolutionString.Split('x');
var width = int.Parse(words[0]);
var height = int.Parse(words[1]);

Screen.SetResolution(width, height, Screen.fullScreen);
}
}
}
14 changes: 7 additions & 7 deletions Assets/Scripts/MainSystem/Zenject/SaveControllerInstaller.cs
@@ -1,12 +1,12 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Zenject;
using Zenject;

public class SaveControllerInstaller : MonoInstaller<SaveControllerInstaller>
namespace Impulse
{
public override void InstallBindings()
public class SaveControllerInstaller : MonoInstaller<SaveControllerInstaller>
{
transform.GetComponent<GameObjectContext>().Container.BindInstance(GetComponent<ILocalDataManager>());
public override void InstallBindings()
{
transform.GetComponent<GameObjectContext>().Container.BindInstance(GetComponent<ILocalPlayerProfileService>());
}
}
}
82 changes: 41 additions & 41 deletions Assets/Scripts/MainSystem/Zenject/ZenjectMainSystemStartup.cs
@@ -1,57 +1,57 @@
using System.Collections;
using System.Collections.Generic;
using System.Collections.Generic;
using UnityEngine;
using Zenject;
/// <summary>
/// Use this class for initialization of core systems (core singleton manager instatiation)
/// </summary>
public class ZenjectMainSystemStartup : MonoInstaller
{
[SerializeField]
[Tooltip("List of singletons to instantiate")]
private List<GameObject> _singletonsToSpawn = new List<GameObject>();

namespace Impulse
{
/// <summary>
/// Use this to install the zenject bindings
/// Use this class for initialization of core systems (core singleton service instantiation)
/// </summary>
public override void InstallBindings()
public class ZenjectMainSystemStartup : MonoInstaller
{
// Deactivate install warning, we don't need to worry about installation order as every singleton initialization should be order independant
Container.ShouldCheckForInstallWarning = false;
[SerializeField] [Tooltip("List of singleton services to instantiate")]
private List<GameObject> _singletonsToSpawn = new List<GameObject>();

foreach (GameObject singleton in _singletonsToSpawn)
/// <summary>
/// Use this to install the Zenject bindings
/// </summary>
public override void InstallBindings()
{
// Zenject based singletons (objects in need of reference injection)
if (singleton.GetComponent<GameObjectContext>() != null)
{
Container.InstantiatePrefab(singleton);
}
// Regular based singletons
else
// Deactivate install warning - we don't need to worry about installation order as every singleton initialization should be order independent.
Container.ShouldCheckForInstallWarning = false;

foreach (var singleton in _singletonsToSpawn)
{
GameObject instancedSingleton = Instantiate(singleton);
instancedSingleton.transform.parent = this.transform;
instancedSingleton.transform.localPosition = Vector3.zero;
instancedSingleton.transform.localRotation = Quaternion.identity;
instancedSingleton.transform.localScale = Vector3.one;
// Zenject based singletons (objects in need of reference injection)
if (singleton.GetComponent<GameObjectContext>() != null)
{
Container.InstantiatePrefab(singleton);
}
// Regular based singletons
else
{
var instancedSingleton = Instantiate(singleton);
instancedSingleton.transform.parent = transform;
instancedSingleton.transform.localPosition = Vector3.zero;
instancedSingleton.transform.localRotation = Quaternion.identity;
instancedSingleton.transform.localScale = Vector3.one;
}
}
}
}

/// <summary>
/// Use this for initialization
/// </summary>
public override void Start()
{
// Initialize core managers
SaveManager.Instance.Initialize();
/// <summary>
/// Use this for initialization
/// </summary>
public override void Start()
{
// Initialize core managers
LocalPlayerProfileService.Instance.Initialize();

// Load player profile
SaveManager.Instance.LoadData();
// Load player profile
LocalPlayerProfileService.Instance.LoadData();

// Use presentation data
PresentationManager.Instance.Initialize();
// Use presentation data
PresentationService.Instance.Initialize();
}
}


}
35 changes: 15 additions & 20 deletions Assets/Scripts/Menu/OptionsMenuScreen.cs
@@ -1,43 +1,38 @@
using System.Collections;
using System.Collections.Generic;
using System.Collections.Generic;
using Impulse;
using UnityEngine;
using UnityEngine.UI;

public class OptionsMenuScreen : InterfaceScreen
{

[SerializeField]
protected Dropdown _dropdownSystem;
protected Dropdown DropdownSystem;

[SerializeField]
protected List<string> _resolutionOptions = new List<string>()
protected List<string> ResolutionOptions = new List<string>
{
"1024x768", "1280x800","1360x768","1366x768","1440x900","1600x900","1680x1050","1920x1080","2560x1080","2560x1440","2560x1600"
};

/// <summary>
/// Use this for initialization
/// </summary>
protected override void Start()
{
_dropdownSystem.ClearOptions();
_dropdownSystem.AddOptions(_resolutionOptions);
DropdownSystem.ClearOptions();
DropdownSystem.AddOptions(ResolutionOptions);

// Load saved resolution
string savedResolution = SaveManager.Instance.GetLocalData().Resolution;
int indexOfSavedResolution = _resolutionOptions.IndexOf(savedResolution);
_dropdownSystem.value = indexOfSavedResolution;
_dropdownSystem.RefreshShownValue();
var savedResolution = LocalPlayerProfileService.Instance.GetLocalData().Resolution;
var indexOfSavedResolution = ResolutionOptions.IndexOf(savedResolution);
DropdownSystem.value = indexOfSavedResolution;
DropdownSystem.RefreshShownValue();
}

public void SetResolution()
{
// Notify resolution manager
PresentationManager.Instance.SetResolution(_resolutionOptions[_dropdownSystem.value]);
PresentationService.Instance.SetResolution(ResolutionOptions[DropdownSystem.value]);

// Update player prefs
string resolutionToSave = _resolutionOptions[_dropdownSystem.value];
SaveManager.Instance.GetLocalData().Resolution = resolutionToSave;
SaveManager.Instance.SaveData();
// Update player profile settings.
var resolutionToSave = ResolutionOptions[DropdownSystem.value];
LocalPlayerProfileService.Instance.GetLocalData().Resolution = resolutionToSave;
LocalPlayerProfileService.Instance.SaveData();
}
}
24 changes: 0 additions & 24 deletions Assets/Scripts/PlayerProfile/ILocalDataManager.cs

This file was deleted.

23 changes: 23 additions & 0 deletions Assets/Scripts/PlayerProfile/ILocalPlayerProfileService.cs
@@ -0,0 +1,23 @@
namespace Impulse
{
/// <summary>
/// Interface for local profile management.
/// </summary>
public interface ILocalPlayerProfileService
{
/// <summary>
/// Requests the profile data to be reset.
/// </summary>
void ResetProfile();

/// <summary>
/// Save the current values of the player profile data.
/// </summary>
void SaveProfile(PlayerProfile playerProfile);

/// <summary>
/// Load player profile.
/// </summary>
PlayerProfile LoadProfile();
}
}
45 changes: 45 additions & 0 deletions Assets/Scripts/PlayerProfile/LocalPlayerProfileService.cs
@@ -0,0 +1,45 @@
using Zenject;

namespace Impulse
{
public class LocalPlayerProfileService : Singleton<LocalPlayerProfileService>
{

private PersistentLocalProfileService _localSaveService;
[Inject] private ILocalPlayerProfileService _localPlayerProfileService;

public void Initialize()
{
_localSaveService = new PersistentLocalProfileService();
_localSaveService.Initialize(_localPlayerProfileService);
}

public PlayerProfile GetPlayerProfile()
{
return _localSaveService.LocalPlayerProfile;
}

public void SaveData()
{
_localSaveService.SaveProfile();
}

public void ResetData()
{
_localSaveService.ResetProfile();
}

public void LoadData()
{
_localSaveService.LoadProfile();
}

public PlayerProfile GetLocalData()
{
if (_localSaveService.LocalPlayerProfile == null)
return new PlayerProfile();

return _localSaveService.LocalPlayerProfile;
}
}
}
48 changes: 0 additions & 48 deletions Assets/Scripts/PlayerProfile/PersistentLocalDataService.cs

This file was deleted.

0 comments on commit b8d4bd5

Please sign in to comment.