PlaySFXClipGameAction.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using Sirenix.OdinInspector;
  7. using KairoEngine.Core;
  8. using KairoEngine.Core.GameActions;
  9. namespace KairoEngine.SFX.GameActions
  10. {
  11. [System.Serializable, HideReferenceObjectPicker]
  12. public class PlaySFXClipGameAction : GameActionBase
  13. {
  14. public override string name
  15. {
  16. get
  17. {
  18. return $"Play {(sfxClip != null ? sfxClip.name : "SFX Clip")}";
  19. }
  20. }
  21. public override GameActionsController controller {
  22. get => _controller;
  23. set
  24. {
  25. _controller = value;
  26. typeName = "PlaySFXClipGameAction";
  27. GetCompatibleVariablenames();
  28. }
  29. }
  30. public override string GetTypeName() => "PlaySFXClipGameAction";
  31. public override string GetActionName() => "Play SFX Clip";
  32. [FoldoutGroup("@name"), NonSerialized, ShowInInspector, HideIf("@!string.IsNullOrEmpty(librarySfxClipName)")]
  33. public SFXClip sfxClip;
  34. [FoldoutGroup("@name"), HideIf("@sfxClip != null")]
  35. public string librarySfxClipName = "";
  36. [FoldoutGroup("@name"), ValueDropdown("possibleVariables", IsUniqueList = false)] public string parent;
  37. private IEnumerable possibleVariables = new ValueDropdownList<string>();
  38. public PlaySFXClipGameAction(GameActionsController controller) : base(controller)
  39. {
  40. this.controller = controller;
  41. className = this.GetType().AssemblyQualifiedName;
  42. }
  43. public override void Initialize(GameActionObjectSerializer objectSerializer)
  44. {
  45. var obj = objectSerializer.DeserializeScriptableObject($"{sfxClipName}-sfxClip");
  46. if(obj != null && sfxClip == null) sfxClip = (SFXClip)obj;
  47. }
  48. public override void Start()
  49. {
  50. Debug.Log("Playing sound");
  51. GameObject parentObj = GetVariable<GameObject>(parent, null);
  52. Transform parentTransform = null;
  53. if(parentObj != null) parentTransform = parentObj.transform;
  54. Vector3 pos = new Vector3();
  55. if(parentObj != null) pos = parentObj.transform.position;
  56. if(sfxClip != null) SoundController.EmmitSound(sfxClip, pos, parentTransform);
  57. else if(!string.IsNullOrEmpty(librarySfxClipName)) SoundController.EmmitSound(librarySfxClipName, pos, parentTransform);
  58. _done = true;
  59. _started = true;
  60. }
  61. public override void Update() { }
  62. public override void Restart()
  63. {
  64. _done = false;
  65. _started = false;
  66. }
  67. public static PlaySFXClipGameAction JSONToPlaySFXClipGameAction(string data)
  68. {
  69. return JsonUtility.FromJson<PlaySFXClipGameAction>(data);
  70. }
  71. private PlaySFXClipGameAction Duplicate(GameActionsController controller)
  72. {
  73. PlaySFXClipGameAction action = new PlaySFXClipGameAction(controller == null ? this.controller : controller);
  74. action.controller = controller;
  75. return action;
  76. }
  77. private T GetVariable<T>(string title, T defaultValue)
  78. {
  79. for (int i = 0; i < controller.context.variables.Count; i++)
  80. {
  81. if(controller.context.variables[i].name == title)
  82. {
  83. return controller.context.variables[i].GetValue<T>(defaultValue);
  84. }
  85. }
  86. return defaultValue;
  87. }
  88. [HideInInspector] public string sfxClipName;
  89. public override void OnBeforeSerialize(GameActionObjectSerializer serializer, int n, int depth)
  90. {
  91. if(sfxClip != null)
  92. {
  93. sfxClipName = sfxClip.name;
  94. serializer.SerializeScriptableObject($"{sfxClipName}-sfxClip", sfxClip);
  95. }
  96. }
  97. public override void OnBeforeDeserialize(GameActionObjectSerializer serializer, int n, int depth)
  98. {
  99. sfxClip = (SFXClip)serializer.DeserializeScriptableObject($"{depth}-{n}-Prefab");
  100. }
  101. private void GetCompatibleVariablenames()
  102. {
  103. if(_controller == null) return;
  104. if(_controller.context == null) return;
  105. possibleVariables = _controller.context.variables
  106. //.Where(x => x.data is GameActionContextString)
  107. .Select(x => new ValueDropdownItem(x.name, x.name));
  108. }
  109. }
  110. }