using System; 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 } [Serializable, HideReferenceObjectPicker] public class StoryObject : System.Object { [HorizontalGroup("line", 0.3f), HideLabel] public string name; [HorizontalGroup("line", 0.7f), HideLabel, NonSerialized, ShowInInspector] public GameObject prefab; [HideInInspector, NonSerialized] public GameObject instance; } [Serializable, HideReferenceObjectPicker] public class StorySystemModule : GameModuleBase { public override string name => "Story Module"; [ShowInInspector, NonSerialized] 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 StorySystemModule(GameConfig config) : base(config) { this.gameConfig = config; this.className = this.GetType().AssemblyQualifiedName; this.typeName = "StorySystemModule"; } public override void Load(Transform parent) { if(storyObjects.Count > 0) storyObjectsContainer = new GameObject("StoryObjects").transform; if(storyObjectsContainer != null) storyObjectsContainer.transform.parent = parent; } public override void Start() { 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 override void Reset() { showStoryLog = false; inkJSONAsset = null; storyController = null; } public override void Destroy() { if(storyController != null) storyController.Stop(); if(startType == StoryInitType.OnEvent) GenericEvents.StopListening(eventStartName, () => LoadStory(storyObjectsContainer)); } public static StorySystemModule JSONToStorySystemModule(string data) { try { return JsonUtility.FromJson(data); } catch (System.Exception e) { Debug.LogError($"Could not deserialize StorySystemModule: \n{e}"); return new StorySystemModule(null); } } public override void OnBeforeSerialize(ObjectSerializer serializer) { if(inkJSONAsset != null) serializer.AddUnityObject("inkJSONAsset", inkJSONAsset); if(storyObjectsContainer != null) serializer.AddGameObject("storyObjectsContainer", storyObjectsContainer.gameObject); if(storyController != null) serializer.AddObject("storyController", storyController); for (int i = 0; i < storyObjects.Count; i++) { serializer.AddGameObject($"storyObjects_{i}_gameObject", storyObjects[i].prefab); } } public override void OnBeforeDeserialize(ObjectSerializer serializer) { // inkJSONAsset inkJSONAsset = (TextAsset)serializer.GetUnityObject("inkJSONAsset"); // storyObjectsContainer GameObject gameObject = serializer.GetGameObject("storyObjectsContainer"); if(gameObject != null) storyObjectsContainer = gameObject.transform; // storyController storyController = (StoryController)serializer.GetObject("storyController"); // storyObjects if(storyObjects == null) storyObjects = new List(); for (int i = 0; i < storyObjects.Count; i++) { var obj = storyObjects[i]; if(obj == null) return; obj.prefab = serializer.GetGameObject($"storyObjects_{i}_gameObject"); } } } }