StorySystemModule.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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, NonSerialized, ShowInInspector] 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. [ShowInInspector, NonSerialized] public TextAsset inkJSONAsset = null;
  29. public string storyName = "Storyline";
  30. public bool showStoryLog = false;
  31. public int lineInverval = 250;
  32. [LabelText("Start")] public StoryInitType startType;
  33. [ShowIf("@startType == StoryInitType.OnEvent")] public string eventStartName = "StartStory";
  34. [Space] public List<StoryObject> storyObjects = new List<StoryObject>();
  35. private StoryController storyController;
  36. private Transform storyObjectsContainer = null;
  37. public StorySystemModule(GameConfig config) : base(config)
  38. {
  39. this.gameConfig = config;
  40. this.className = this.GetType().AssemblyQualifiedName;
  41. this.typeName = "StorySystemModule";
  42. }
  43. public override void Load(Transform parent)
  44. {
  45. if(storyObjects.Count > 0) storyObjectsContainer = new GameObject("StoryObjects").transform;
  46. if(storyObjectsContainer != null) storyObjectsContainer.transform.parent = parent;
  47. }
  48. public override void Start()
  49. {
  50. if(startType == StoryInitType.OnLoad) LoadStory(storyObjectsContainer);
  51. else if(startType == StoryInitType.OnEvent) GenericEvents.StartListening(eventStartName, () => LoadStory(storyObjectsContainer));
  52. }
  53. private void LoadStory(Transform parent)
  54. {
  55. if(inkJSONAsset != null) storyController = new StoryController(inkJSONAsset, storyName, storyObjects, parent, showStoryLog, lineInverval);
  56. else Debug.LogError($"Missing Ink Story asset in StorySystemModule");
  57. if(storyController != null) storyController.Start();
  58. }
  59. public override void Reset()
  60. {
  61. showStoryLog = false;
  62. inkJSONAsset = null;
  63. storyController = null;
  64. }
  65. public override void Destroy()
  66. {
  67. if(storyController != null) storyController.Stop();
  68. if(startType == StoryInitType.OnEvent) GenericEvents.StopListening(eventStartName, () => LoadStory(storyObjectsContainer));
  69. }
  70. public static StorySystemModule JSONToStorySystemModule(string data)
  71. {
  72. try
  73. {
  74. return JsonUtility.FromJson<StorySystemModule>(data);
  75. }
  76. catch (System.Exception e)
  77. {
  78. Debug.LogError($"Could not deserialize StorySystemModule: \n{e}");
  79. return new StorySystemModule(null);
  80. }
  81. }
  82. public override void OnBeforeSerialize(ObjectSerializer serializer)
  83. {
  84. if(inkJSONAsset != null) serializer.AddUnityObject("inkJSONAsset", inkJSONAsset);
  85. if(storyObjectsContainer != null) serializer.AddGameObject("storyObjectsContainer", storyObjectsContainer.gameObject);
  86. if(storyController != null) serializer.AddObject("storyController", storyController);
  87. for (int i = 0; i < storyObjects.Count; i++)
  88. {
  89. serializer.AddGameObject($"storyObjects_{i}_gameObject", storyObjects[i].prefab);
  90. }
  91. }
  92. public override void OnBeforeDeserialize(ObjectSerializer serializer)
  93. {
  94. // inkJSONAsset
  95. inkJSONAsset = (TextAsset)serializer.GetUnityObject("inkJSONAsset");
  96. // storyObjectsContainer
  97. GameObject gameObject = serializer.GetGameObject("storyObjectsContainer");
  98. if(gameObject != null) storyObjectsContainer = gameObject.transform;
  99. // storyController
  100. storyController = (StoryController)serializer.GetObject("storyController");
  101. // storyObjects
  102. if(storyObjects == null) storyObjects = new List<StoryObject>();
  103. for (int i = 0; i < storyObjects.Count; i++)
  104. {
  105. var obj = storyObjects[i];
  106. if(obj == null) return;
  107. obj.prefab = serializer.GetGameObject($"storyObjects_{i}_gameObject");
  108. }
  109. }
  110. }
  111. }