StorySystemModule.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. using KairoEngine.Core;
  6. using KairoEngine.Core.ModuleSystem;
  7. using Ink.Runtime;
  8. namespace KairoEngine.StorySystem
  9. {
  10. public enum StoryInitType
  11. {
  12. OnLoad,
  13. OnEvent,
  14. None
  15. }
  16. [System.Serializable]
  17. public class StoryObject
  18. {
  19. public string name;
  20. public GameObject prefab;
  21. [HideInInspector] public GameObject instance;
  22. }
  23. public class StorySystemModule : IGameModule
  24. {
  25. private string _name = "Story Module";
  26. public string name { get => _name; set => _name = value; }
  27. public TextAsset inkJSONAsset = null;
  28. public string storyName = "Storyline";
  29. public bool showStoryLog = false;
  30. public int lineInverval = 250;
  31. [LabelText("Start")] public StoryInitType startType;
  32. [ShowIf("@startType == StoryInitType.OnEvent")] public string eventStartName = "StartStory";
  33. [Space] public List<StoryObject> storyObjects = new List<StoryObject>();
  34. private StoryController storyController;
  35. private Transform storyObjectsContainer = null;
  36. public void Load(Transform parent)
  37. {
  38. if(storyObjects.Count > 0) storyObjectsContainer = new GameObject("StoryObjects").transform;
  39. if(storyObjectsContainer != null) storyObjectsContainer.transform.parent = parent;
  40. if(startType == StoryInitType.OnLoad) LoadStory(storyObjectsContainer);
  41. else if(startType == StoryInitType.OnEvent) GenericEvents.StartListening(eventStartName, () => LoadStory(storyObjectsContainer));
  42. }
  43. private void LoadStory(Transform parent)
  44. {
  45. if(inkJSONAsset != null) storyController = new StoryController(inkJSONAsset, storyName, storyObjects, parent, showStoryLog, lineInverval);
  46. else Debug.LogError($"Missing Ink Story asset in StorySystemModule");
  47. if(storyController != null) storyController.Start();
  48. }
  49. public void Reset()
  50. {
  51. showStoryLog = false;
  52. inkJSONAsset = null;
  53. storyController = null;
  54. }
  55. public void Destroy()
  56. {
  57. if(storyController != null) storyController.Stop();
  58. if(startType == StoryInitType.OnEvent) GenericEvents.StopListening(eventStartName, () => LoadStory(storyObjectsContainer));
  59. }
  60. }
  61. }