12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Sirenix.OdinInspector;
- using KairoEngine.Core;
- using KairoEngine.Core.ModuleSystem;
- using Ink.Runtime;
- namespace KairoEngine.StorySystem
- {
- public enum StoryInitType
- {
- OnLoad,
- OnEvent,
- None
- }
- [System.Serializable]
- public class StoryObject
- {
- public string name;
- public GameObject prefab;
- [HideInInspector] public GameObject instance;
- }
- public class StorySystemModule : IGameModule
- {
- private string _name = "Story Module";
- public string name { get => _name; set => _name = value; }
- public TextAsset inkJSONAsset = null;
- public string storyName = "Storyline";
- public bool showStoryLog = false;
- public int lineInverval = 250;
- [LabelText("Start")] public StoryInitType startType;
- [ShowIf("@startType == StoryInitType.OnEvent")] public string eventStartName = "StartStory";
- [Space] public List<StoryObject> storyObjects = new List<StoryObject>();
- private StoryController storyController;
- private Transform storyObjectsContainer = null;
- public void Load(Transform parent)
- {
- if(storyObjects.Count > 0) storyObjectsContainer = new GameObject("StoryObjects").transform;
- if(storyObjectsContainer != null) storyObjectsContainer.transform.parent = parent;
- if(startType == StoryInitType.OnLoad) LoadStory(storyObjectsContainer);
- else if(startType == StoryInitType.OnEvent) GenericEvents.StartListening(eventStartName, () => LoadStory(storyObjectsContainer));
- }
- private void LoadStory(Transform parent)
- {
- if(inkJSONAsset != null) storyController = new StoryController(inkJSONAsset, storyName, storyObjects, parent, showStoryLog, lineInverval);
- else Debug.LogError($"Missing Ink Story asset in StorySystemModule");
- if(storyController != null) storyController.Start();
- }
- public void Reset()
- {
- showStoryLog = false;
- inkJSONAsset = null;
- storyController = null;
- }
- public void Destroy()
- {
- if(storyController != null) storyController.Stop();
- if(startType == StoryInitType.OnEvent) GenericEvents.StopListening(eventStartName, () => LoadStory(storyObjectsContainer));
- }
- }
- }
|