diff --git a/Achievments/AchievmentManager.cs b/Achievments/AchievmentManager.cs index 38b46f0..244e384 100644 --- a/Achievments/AchievmentManager.cs +++ b/Achievments/AchievmentManager.cs @@ -18,7 +18,7 @@ namespace NEG.Utils.Achievments public const string DefaultAchivmentsConfigLabel = "Achivments"; private AchievmentManager manager = new AchievmentManager(); - private IAchivmentStorage storage; + private IAchievmentBackend backend; public static Builder FromDefaultConfig() { @@ -47,20 +47,8 @@ namespace NEG.Utils.Achievments return builder; } - - public Builder WithLabeledDefinitions(string label) - { - var handle = Addressables.LoadAssetsAsync((IEnumerable)new string[] { label }, delegate { }, Addressables.MergeMode.Union, false); - - var achivmentCollections = handle.WaitForCompletion(); - - foreach (var achivmentCollection in achivmentCollections) - WithDefinitionsFrom(achivmentCollection); - - return this; - } - public Builder WithDefinitionsFrom(AchivmentDefinitionCollection collection) + public Builder WithDefinitionsFrom(AchievmentManagerConfig collection) { foreach (var def in collection.Achivments) { @@ -70,14 +58,26 @@ namespace NEG.Utils.Achievments return this; } - public Builder LoadedFrom(IAchivmentStorage storageIn) + public Builder WithLabeledBackend(string label) { - if (storage != null) + var backendConfigHandle = Addressables.LoadAssetAsync(label); + + var backendConfig = backendConfigHandle.WaitForCompletion(); + + WithBackend(backendConfig.ConstructBackend()); + + Addressables.Release(backendConfigHandle); + return this; + } + + public Builder WithBackend(IAchievmentBackend backendIn) + { + if (backend != null) { - throw new ApplicationException("Cannot Load achivment data from multiple storage instances"); + throw new ApplicationException("There can only be one Achievment Backend at a time"); } - this.storage = storageIn; + this.backend = backendIn; return this; } @@ -90,11 +90,13 @@ namespace NEG.Utils.Achievments public AchievmentManager Build() { - //TODO - /*if (storage != null) + if (backend != null) { - manager.LoadFrom(storage); - }*/ + manager.InitBackend(backend); + }else + { + Debug.LogWarning("No AchievmentBackend selected. Is this intended?"); + } return manager; } } @@ -127,15 +129,27 @@ namespace NEG.Utils.Achievments } } - /*private void LoadFrom(IAchivmentStorage achivmentStorage) + /// + /// Initializes a backend syncing achievments data with it and redistering it as a callback reciever + /// + /// Resets all achievments data + private void InitBackend(IAchievmentBackend achievmentBackend) { - foreach (var entry in definitionCache) + foreach (var definition in definitionCache.Values) { - var storedProgress = achivmentStorage.GetStoredAchivmentProgress(entry.Key); + var storedProgress = achievmentBackend.GetStoredAchivment(definition); - dataCache[entry.Value].ProgressLeft = storedProgress; + if (storedProgress != null) + { + dataCache[definition] = storedProgress; + } + else + { + dataCache[definition] = definition.Construct(); + } } - }*/ + AddCallbackReciever(achievmentBackend); + } public void AddCallbackRecievers(IEnumerable initialCallbacks) { diff --git a/Achievments/AchievmentManagerConfig.cs b/Achievments/AchievmentManagerConfig.cs index b6584f4..95dc214 100644 --- a/Achievments/AchievmentManagerConfig.cs +++ b/Achievments/AchievmentManagerConfig.cs @@ -1,3 +1,4 @@ +using NEG.Utils.Achievments.AchievmentTypes; using System.Collections; using System.Collections.Generic; using UnityEngine; @@ -7,14 +8,12 @@ namespace NEG.Utils.Achievments [CreateAssetMenu(menuName = "Achivments/BaseConfig")] public class AchievmentManagerConfig : ScriptableObject, IAchivmentManagerConfig { - public const string DefaultAchivmentsCollectionLabel = "Achivments"; - [field: SerializeField] - public string AchivmentCollectionAssetLabel { get; private set; } = DefaultAchivmentsCollectionLabel; + public List Achivments { get; private set; } = new List(); public void Apply(AchievmentManager.Builder builder) { - builder.WithLabeledDefinitions(AchivmentCollectionAssetLabel); + builder.WithDefinitionsFrom(this); } public void ApplyLast(AchievmentManager.Builder builder) diff --git a/Achievments/Achievments.cs b/Achievments/Achievments.cs index fcaa854..866c6b0 100644 --- a/Achievments/Achievments.cs +++ b/Achievments/Achievments.cs @@ -1,3 +1,5 @@ +using NEG.Utils.Achievments.AchievmentTypes; +using System; using System.Collections; using System.Collections.Generic; using UnityEngine; @@ -6,17 +8,144 @@ namespace NEG.Utils.Achievments { public static class Achievments { - public static AchievmentManager Instance + public static AchievmentManager Instance { get { if (instance == null) { - instance = + instance = AchievmentManager.Builder.FromDefaultConfig() + .WithLabeledBackend(BackendLabel) + .Build(); } + return instance; } } + + internal static string BackendLabel + { + get => backendLabel; + set + { + if (backendLabel != null) + { + throw new ApplicationException("Multiple AchievmentBackends enabled, this is not allowed"); + } + backendLabel = value; + } + } + private static string backendLabel; + private static AchievmentManager instance; + #region Achievment Manipulation (Sets, Gets) + + /// + /// Returns if an achivment at a given id is completed + /// + /// + /// + /// + /// throws an if there is no achievment under id + public static bool IsCompleted(string id) + { + return instance.IsCompleted(id); + } + + #region Toggle + /// + /// Sets a as completed.
+ ///
+ /// + /// + /// throws an if there is no achievment under id or an if achievment under id is of a different type + public static bool SetToggleAchivment(string id) + { + return instance.SetToggleAchivment(id); + } + + /// + /// Gets a completion state from a .
+ ///
+ /// + /// + /// throws an if there is no achievment under id or an if achievment under id is of a different type + public static bool GetToggleState(string id) + { + return instance.GetToggleState(id); + } + #endregion + + #region Int + /// + /// Sets progress of a given to
+ ///
+ /// + /// + /// throws an if there is no achievment under id or an if achievment under id is of a different type + public static bool SetIntProgress(string id, int progress) + { + return instance.SetIntProgress(id, progress); + } + + /// + /// Changes progress of a given by
+ ///
+ /// \ + /// + /// throws an if there is no achievment under id or an if achievment under id is of a different type + public static bool ChangeIntProgress(string id, int delta) + { + return instance.ChangeIntProgress(id, delta); + } + + /// + /// Gets current progress from a .
+ ///
+ /// + /// + /// throws an if there is no achievment under id or an if achievment under id is of a different type + public static int GetIntProgress(string id) + { + return instance.GetIntProgress(id); + } + #endregion + + #region Float + /// + /// Sets progress of a given to
+ ///
+ /// + /// + /// throws an if there is no achievment under id or an if achievment under id is of a different type + public static bool SetFloatProgress(string id, float progress) + { + return instance.SetFloatProgress(id, progress); + } + + /// + /// Changes progress of a given by
+ ///
+ /// + /// + /// throws an if there is no achievment under id or an if achievment under id is of a different type + public static bool ChangeFloatProgress(string id, float delta) + { + return instance.ChangeFloatProgress(id, delta); + } + + /// + /// Gets current progress from a .
+ ///
+ /// + /// + /// throws an if there is no achievment under id or an if achievment under id is of a different type + public static float GetFloatProgress(string id) + { + return instance.GetFloatProgress(id); + } + #endregion + #endregion + } } \ No newline at end of file diff --git a/Achievments/AchivmentDefinitionCollection.cs b/Achievments/AchivmentDefinitionCollection.cs index fb804d6..ec914c7 100644 --- a/Achievments/AchivmentDefinitionCollection.cs +++ b/Achievments/AchivmentDefinitionCollection.cs @@ -9,7 +9,6 @@ namespace NEG.Utils.Achievments [CreateAssetMenu(menuName = "Achivments/Collection")] public class AchivmentDefinitionCollection : ScriptableObject { - [field: SerializeField] - public List Achivments { get; private set; } = new List(); + } } \ No newline at end of file diff --git a/Achievments/IAchievmentBackend.cs b/Achievments/IAchievmentBackend.cs new file mode 100644 index 0000000..b97d863 --- /dev/null +++ b/Achievments/IAchievmentBackend.cs @@ -0,0 +1,26 @@ +using NEG.Utils.Achievments.AchievmentTypes; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace NEG.Utils.Achievments +{ + /// + /// Used to construct instance + /// + public interface IAchievmentBackendConfig + { + /// Constructed backend + public IAchievmentBackend ConstructBackend(); + } + + + public interface IAchievmentBackend : IAchievmentCallbackReciever + { + /// + /// Constructs an AchievmentData for given + /// + /// May return null if there is no stored data for this achievment + public AchievmentData GetStoredAchivment(AchievmentDefinition definition); + } +} \ No newline at end of file diff --git a/Achievments/IAchivmentStorage.cs.meta b/Achievments/IAchievmentBackend.cs.meta similarity index 100% rename from Achievments/IAchivmentStorage.cs.meta rename to Achievments/IAchievmentBackend.cs.meta diff --git a/Achievments/IAchivmentStorage.cs b/Achievments/IAchivmentStorage.cs deleted file mode 100644 index 00f0799..0000000 --- a/Achievments/IAchivmentStorage.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -namespace NEG.Utils.Achievments -{ - public interface IAchivmentStorage - { - public int GetStoredAchivmentProgress(string id); - } -} \ No newline at end of file diff --git a/Achievments/TODO.txt b/Achievments/TODO.txt index bf02039..bce4482 100644 --- a/Achievments/TODO.txt +++ b/Achievments/TODO.txt @@ -1,5 +1,5 @@ Static Achievments class -Implement Storage again +Implement Storage again API (done) Fix typos -Merge AchievmentCollection with AchievmentManagerConfig +Merge AchievmentCollection with AchievmentManagerConfig (done) Static backend constructors \ No newline at end of file diff --git a/Achievments/Tests/ConfigTests.cs b/Achievments/Tests/ConfigTests.cs index 9a995f2..50bca12 100644 --- a/Achievments/Tests/ConfigTests.cs +++ b/Achievments/Tests/ConfigTests.cs @@ -12,7 +12,7 @@ namespace NEG.Utils.Achievments.Tests public class ConfigTests { - public const string AchievmentsLabel = "TestAchivments"; + public const string AchievmentsLabel = "TestAchievments"; public const string AchievmentIdToggle = "TOGGLE"; public const string AchievmentIdInt = "INT"; diff --git a/Achievments/Tests/TestAssets/AchivmentCollection.asset b/Achievments/Tests/TestAssets/AchivmentCollection.asset deleted file mode 100644 index 25bd3ce..0000000 --- a/Achievments/Tests/TestAssets/AchivmentCollection.asset +++ /dev/null @@ -1,18 +0,0 @@ -%YAML 1.1 -%TAG !u! tag:unity3d.com,2011: ---- !u!114 &11400000 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 0} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: ef37a873be859d042bda22dee624e429, type: 3} - m_Name: AchivmentCollection - m_EditorClassIdentifier: - k__BackingField: - - {fileID: 11400000, guid: 7734df2e5d4033346aac56f0a2b2a836, type: 2} - - {fileID: 11400000, guid: c704b1ea2247ad540842a9caff628211, type: 2} - - {fileID: 11400000, guid: c71840de74e747e45afc82ecf8922dcd, type: 2} diff --git a/Achievments/Tests/TestAssets/AchivmentCollection.asset.meta b/Achievments/Tests/TestAssets/AchivmentCollection.asset.meta deleted file mode 100644 index 3747185..0000000 --- a/Achievments/Tests/TestAssets/AchivmentCollection.asset.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 4d95138fe57571c4299aa325a378e4ea -NativeFormatImporter: - externalObjects: {} - mainObjectFileID: 11400000 - userData: - assetBundleName: - assetBundleVariant: diff --git a/Achievments/Tests/TestAssets/BaseConfig.asset b/Achievments/Tests/TestAssets/BaseConfig.asset index 467b029..1511dcb 100644 --- a/Achievments/Tests/TestAssets/BaseConfig.asset +++ b/Achievments/Tests/TestAssets/BaseConfig.asset @@ -12,4 +12,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 88120b6e616164f489387a6a32a25dee, type: 3} m_Name: BaseConfig m_EditorClassIdentifier: - k__BackingField: TestAchivments + k__BackingField: + - {fileID: 11400000, guid: 7734df2e5d4033346aac56f0a2b2a836, type: 2} + - {fileID: 11400000, guid: c704b1ea2247ad540842a9caff628211, type: 2} + - {fileID: 11400000, guid: c71840de74e747e45afc82ecf8922dcd, type: 2}