123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- 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<StoryObject> storyObjects = new List<StoryObject>();
- 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<StorySystemModule>(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<StoryObject>();
- for (int i = 0; i < storyObjects.Count; i++)
- {
- var obj = storyObjects[i];
- if(obj == null) return;
- obj.prefab = serializer.GetGameObject($"storyObjects_{i}_gameObject");
- }
- }
- }
- }
|