AchievementsModule.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using KairoEngine.Core;
  6. using KairoEngine.Core.ModuleSystem;
  7. using Sirenix.OdinInspector;
  8. namespace KairoEngine.Achievements
  9. {
  10. [Serializable, HideReferenceObjectPicker]
  11. public class AchievementsModule : GameModuleBase
  12. {
  13. public override string name => "Achievements Module";
  14. public bool CheckOnStart = true;
  15. public bool checkOnInterval = true;
  16. [ShowIf("@checkOnInterval"), SuffixLabel("ms")] public int checkInterval = 3000;
  17. [SuffixLabel("ms")] public int startDelay = 3000;
  18. public bool debug = false;
  19. [NonSerialized, ShowInInspector, InlineEditor(InlineEditorObjectFieldModes.Boxed)] public AchievementsLibrary library;
  20. public AchievementsModule(GameConfig config) : base(config)
  21. {
  22. this.gameConfig = config;
  23. this.className = this.GetType().AssemblyQualifiedName;
  24. this.typeName = "AchievementsModule";
  25. }
  26. public override void Load(Transform parent)
  27. {
  28. if(library == null)
  29. {
  30. Debug.LogError("Missing Achievements Library.\nPlease configure the Achievements module in the Game config file.");
  31. return;
  32. }
  33. var obj = new GameObject();
  34. obj.transform.parent = parent;
  35. obj.name = "Achievements";
  36. var comp = obj.AddComponent<AchievementsController>();
  37. comp.library = this.library;
  38. comp.CheckOnStart = this.CheckOnStart;
  39. comp.checkOnInterval = this.checkOnInterval;
  40. comp.checkInterval = this.checkInterval;
  41. comp.startDelay = this.startDelay;
  42. comp.debug = this.debug;
  43. }
  44. public override void Reset()
  45. {
  46. this.library = null;
  47. this.CheckOnStart = true;
  48. this.checkOnInterval = true;
  49. this.checkInterval = 3000;
  50. this.startDelay = 3000;
  51. this.debug = false;
  52. }
  53. public override void Destroy() { }
  54. public static AchievementsModule JSONToAchievementsModule(string data)
  55. {
  56. try
  57. {
  58. return JsonUtility.FromJson<AchievementsModule>(data);
  59. }
  60. catch (System.Exception e)
  61. {
  62. Debug.LogError($"Could not deserialize AchievementsModule: \n{e}");
  63. return new AchievementsModule(null);
  64. }
  65. }
  66. public override void OnBeforeSerialize(ObjectSerializer serializer)
  67. {
  68. if(library != null) serializer.AddScriptableObject("Achievements_Library", library);
  69. }
  70. public override void OnBeforeDeserialize(ObjectSerializer serializer)
  71. {
  72. library = (AchievementsLibrary)serializer.GetScriptableObject("Achievements_Library");
  73. }
  74. }
  75. }