115 lines
3.5 KiB
C#
115 lines
3.5 KiB
C#
using NEG.Utils.Achievments.AchievmentTypes;
|
|
#if STEAM_ACHIEVMENT_BACKEND
|
|
using Steamworks;
|
|
#endif
|
|
using UnityEngine;
|
|
|
|
namespace NEG.Utils.Achievments
|
|
{
|
|
[CreateAssetMenu(menuName = "Achievments/Config/Backend/Local")]
|
|
public class SteamBackendConfig : ScriptableObject, IAchievmentBackendConfig
|
|
{
|
|
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
|
|
private static void Init()
|
|
{
|
|
#if STEAM_ACHIEVMENT_BACKEND
|
|
Achievment.BackendLabel = "SteamAchievments";
|
|
#endif
|
|
}
|
|
|
|
public IAchievmentBackend ConstructBackend()
|
|
{
|
|
return new SteamBackend();
|
|
}
|
|
}
|
|
|
|
#if STEAM_ACHIEVMENT_BACKEND
|
|
public class SteamBackend : IAchievmentBackend
|
|
{
|
|
private bool isDirty = false;
|
|
|
|
public SteamBackend()
|
|
{
|
|
//
|
|
SteamUserStats.RequestCurrentStats();
|
|
}
|
|
|
|
public void AchievmentCompleted(AchievmentData achievment)
|
|
{
|
|
SteamUserStats.SetAchievement(achievment.Achievment.Id);
|
|
isDirty = true;
|
|
}
|
|
|
|
public void AchievmentStateChanged(AchievmentData achievment)
|
|
{
|
|
string id = achievment.Achievment.Id;
|
|
|
|
switch (achievment)
|
|
{
|
|
case IntAchievmentData intAchievment:
|
|
SteamUserStats.SetStat(id, intAchievment.CurrentProgress);
|
|
break;
|
|
case FloatAchievmentData floatAchievment:
|
|
SteamUserStats.SetStat(id, floatAchievment.CurrentProgress);
|
|
break;
|
|
case ToggleAchievmentData toggleAchievment:
|
|
//Do nothing, Achievment completed will also be called
|
|
break;
|
|
}
|
|
isDirty = true;
|
|
}
|
|
|
|
public AchievmentData GetStoredAchivment(AchievmentDefinition definition)
|
|
{
|
|
string id = definition.Id;
|
|
|
|
AchievmentData achievment = definition.Construct();
|
|
|
|
switch (achievment)
|
|
{
|
|
case IntAchievmentData intAchievment:
|
|
if (SteamUserStats.GetStat(id, out int statI))
|
|
{
|
|
intAchievment.CurrentProgress = statI;
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("Cannot get user stat, is steam initialised correctly?");
|
|
}
|
|
break;
|
|
case FloatAchievmentData floatAchievment:
|
|
if (SteamUserStats.GetStat(id, out float statF))
|
|
{
|
|
floatAchievment.CurrentProgress = statF;
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("Cannot get user stat, is steam initialised correctly?");
|
|
}
|
|
break;
|
|
case ToggleAchievmentData toggleAchievment:
|
|
if (SteamUserStats.GetAchievement(id, out bool ach))
|
|
{
|
|
toggleAchievment.CompletionState = ach;
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("Cannot get user stat, is steam initialised correctly?");
|
|
}
|
|
break;
|
|
}
|
|
|
|
return achievment;
|
|
}
|
|
|
|
public void Update()
|
|
{
|
|
if (isDirty)
|
|
{
|
|
//Reiterate on failure?
|
|
isDirty = !SteamUserStats.StoreStats();
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
} |