StorySystemModule.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using Sirenix.OdinInspector;
  6. using KairoEngine.Core;
  7. using KairoEngine.Core.ModuleSystem;
  8. using Ink.Runtime;
  9. namespace KairoEngine.StorySystem
  10. {
  11. public enum StoryInitType
  12. {
  13. OnLoad,
  14. OnEvent,
  15. None
  16. }
  17. [Serializable, HideReferenceObjectPicker]
  18. public class StoryObject : System.Object
  19. {
  20. [HorizontalGroup("line", 0.3f), HideLabel] public string name;
  21. [HorizontalGroup("line", 0.7f), HideLabel] public GameObject prefab;
  22. [HideInInspector, NonSerialized] public GameObject instance;
  23. }
  24. [Serializable, HideReferenceObjectPicker]
  25. public class StorySystemModule : GameModuleBase
  26. {
  27. public override string name => "Story Module";
  28. [FoldoutGroup("@name"), ShowInInspector, NonSerialized] public TextAsset inkJSONAsset = null;
  29. [FoldoutGroup("@name")] public string storyName = "Storyline";
  30. [FoldoutGroup("@name")] public bool showStoryLog = false;
  31. [FoldoutGroup("@name")] public int lineInverval = 250;
  32. [FoldoutGroup("@name"), LabelText("Start")] public StoryInitType startType;
  33. [FoldoutGroup("@name"), ShowIf("@startType == StoryInitType.OnEvent")] public string eventStartName = "StartStory";
  34. [FoldoutGroup("@name"), Space] public List<StoryObject> storyObjects = new List<StoryObject>();
  35. [SerializeField, HideInInspector] private int storyObjectsCount = 0;
  36. private StoryController storyController;
  37. private Transform storyObjectsContainer = null;
  38. public StorySystemModule(GameConfig config) : base(config)
  39. {
  40. this.gameConfig = config;
  41. this.className = this.GetType().AssemblyQualifiedName;
  42. this.typeName = "StorySystemModule";
  43. }
  44. public override void Load(Transform parent)
  45. {
  46. if(storyObjects.Count > 0) storyObjectsContainer = new GameObject("StoryObjects").transform;
  47. if(storyObjectsContainer != null) storyObjectsContainer.transform.parent = parent;
  48. }
  49. public override void Start()
  50. {
  51. if(startType == StoryInitType.OnLoad) LoadStory(storyObjectsContainer);
  52. else if(startType == StoryInitType.OnEvent) GenericEvents.StartListening(eventStartName, () => LoadStory(storyObjectsContainer));
  53. }
  54. private void LoadStory(Transform parent)
  55. {
  56. if(inkJSONAsset != null) storyController = new StoryController(inkJSONAsset, storyName, storyObjects, parent, showStoryLog, lineInverval);
  57. else Debug.LogError($"Missing Ink Story asset in StorySystemModule");
  58. if(storyController != null) storyController.Start();
  59. }
  60. public override void Reset()
  61. {
  62. showStoryLog = false;
  63. inkJSONAsset = null;
  64. storyController = null;
  65. }
  66. public override void Destroy()
  67. {
  68. if(storyController != null) storyController.Stop();
  69. if(startType == StoryInitType.OnEvent) GenericEvents.StopListening(eventStartName, () => LoadStory(storyObjectsContainer));
  70. }
  71. public static StorySystemModule JSONToStorySystemModule(string data)
  72. {
  73. try
  74. {
  75. return JsonUtility.FromJson<StorySystemModule>(data);
  76. }
  77. catch (System.Exception e)
  78. {
  79. Debug.LogError($"Could not deserialize StorySystemModule: \n{e}");
  80. return new StorySystemModule(null);
  81. }
  82. }
  83. public override void OnBeforeSerialize(ObjectSerializer serializer)
  84. {
  85. if(inkJSONAsset != null) serializer.AddUnityObject("inkJSONAsset", inkJSONAsset);
  86. if(storyObjectsContainer != null) serializer.AddGameObject("storyObjectsContainer", storyObjectsContainer.gameObject);
  87. if(storyController != null) serializer.AddObject("storyController", storyController);
  88. storyObjectsCount = storyObjects.Count;
  89. for (int i = 0; i < storyObjects.Count; i++)
  90. {
  91. serializer.AddObject($"storyObjects_{i}", storyObjects[i]);
  92. }
  93. }
  94. public override void OnBeforeDeserialize(ObjectSerializer serializer)
  95. {
  96. // inkJSONAsset
  97. inkJSONAsset = (TextAsset)serializer.GetUnityObject("inkJSONAsset");
  98. // storyObjectsContainer
  99. GameObject gameObject = serializer.GetGameObject("storyObjectsContainer");
  100. if(gameObject != null) storyObjectsContainer = gameObject.transform;
  101. // storyController
  102. storyController = (StoryController)serializer.GetObject("storyController");
  103. // storyObjects
  104. for (int i = 0; i < storyObjectsCount; i++)
  105. {
  106. var obj = serializer.GetObject($"storyObjects_{i}");
  107. if(obj != null) storyObjects.Add((StoryObject)obj);
  108. }
  109. }
  110. }
  111. }