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 storyObjects = new List(); 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)); } } }