using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using KairoEngine.Core; using KairoEngine.Core.ModuleSystem; using Sirenix.OdinInspector; using QFSW.MOP2; namespace KairoEngine.SFX { [Serializable, HideReferenceObjectPicker] public class SFXModule : GameModuleBase { public override string name => "SFX Module"; [Tooltip("Use a custom sound emitter prefab or load the default prefab")] [HorizontalGroup("@name/emitter", 0.015f), LabelText("Custom Sound Emitter")] public bool useCustomSoundEmitter; [HorizontalGroup("@name/emitter", 0.985f), HideLabel, ShowIf("@useCustomSoundEmitter")] public GameObject soundEmitterPrefab; [FoldoutGroup("@name"), Tooltip("Use the ObjectPool system so that sound emitters can be reused. This results in better overral performance. Requires the Object Pool Module to be installed.")] public bool useObjectPooling = false; [HorizontalGroup("@name/pool", 0.015f), LabelText("Custom Object Pool"), ShowIf("@useObjectPooling")] public bool useCustomPool; [HorizontalGroup("@name/pool", 0.985f), HideLabel, ShowIf("@useCustomPool"), NonSerialized, ShowInInspector] public ObjectPool soundEmitterPool; [FoldoutGroup("@name"), Tooltip("Optional sound library asset")] public SFXLibrary soundLibrary; private SoundController soundController; public SFXModule(GameConfig config) : base(config) { this.gameConfig = config; this.className = this.GetType().AssemblyQualifiedName; this.typeName = "SFXModule"; } public override void Load(Transform parent) { if(!useCustomSoundEmitter) soundEmitterPrefab = Resources.Load("SoundEmmiterPrefab") as GameObject; GameObject soundControllerPrefab = Resources.Load("SoundControllerPrefab") as GameObject; GameObject soundControllerObj = GameObject.Instantiate(soundControllerPrefab, parent); soundController = soundControllerObj.GetComponent(); if(soundController == null) { Debug.LogError($"SoundControllerPrefab has no SoundController component", soundControllerObj); return; } soundController.soundEmitterPrefab = soundEmitterPrefab; if(soundLibrary != null) soundController.soundLibrary = soundLibrary; soundControllerObj.name = "SoundController"; } public override void Start() { if(!useObjectPooling) { soundController._soundEmitterPool = ""; SoundController.soundEmitterPool = ""; soundEmitterPrefab.GetComponent().soundEmitterPrefabPool = ""; return; } if(soundController == null) return; soundController.soundEmitterPrefab = null; if(soundEmitterPool == null) soundEmitterPool = Resources.Load("SoundEmitterPool") as ObjectPool; if(soundEmitterPool == null) { Debug.LogError("Missing soundEmitterPool in SFX Module"); return; } if(MasterObjectPooler.Instance == null) { Debug.LogError("The MasterObjectPooler component is not loaded. Add the Object Pool module in the game config to load it."); return; } soundEmitterPrefab.GetComponent().soundEmitterPrefabPool = soundEmitterPool.PoolName; MasterObjectPooler.Instance.AddPool(soundEmitterPool.PoolName, soundEmitterPool); soundController._soundEmitterPool = soundEmitterPool.PoolName; SoundController.soundEmitterPool = soundEmitterPool.PoolName; var pool = MasterObjectPooler.Instance.GetPool(soundEmitterPool.PoolName); if(pool == null) Debug.LogError("Error configuring SFX pool."); else { soundController._soundsParent = pool.ObjectParent.gameObject; SoundController.soundsParent = pool.ObjectParent.gameObject; } } public override void Reset() { } public override void Destroy() { } public static SFXModule JSONToSFXModule(string data) { try { return JsonUtility.FromJson(data); } catch (System.Exception e) { Debug.LogError($"Could not deserialize SFXModule: \n{e}"); return new SFXModule(null); } } public override void OnBeforeSerialize(ObjectSerializer serializer) { if(soundEmitterPrefab != null) serializer.AddGameObject("SFXModule_soundEmitterPrefab", soundEmitterPrefab); if(soundEmitterPool != null) serializer.AddObject("SFXModule_soundEmitterPool", soundEmitterPool); } public override void OnBeforeDeserialize(ObjectSerializer serializer) { soundEmitterPrefab = serializer.GetGameObject("SFXModule_soundEmitterPrefab"); soundEmitterPool = (ObjectPool)serializer.GetObject("SFXModule_soundEmitterPool"); } } }