PlaySFXClipGameAction.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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")]
  33. public SFXClip sfxClip;
  34. [FoldoutGroup("@name"), NonSerialized, ShowInInspector, ValueDropdown("possibleVariables", IsUniqueList = false)] public string parent;
  35. private IEnumerable possibleVariables = new ValueDropdownList<string>();
  36. public PlaySFXClipGameAction(GameActionsController controller) : base(controller)
  37. {
  38. this.controller = controller;
  39. className = this.GetType().AssemblyQualifiedName;
  40. }
  41. public override void Initialize(GameActionObjectSerializer objectSerializer)
  42. {
  43. var obj = objectSerializer.DeserializeScriptableObject($"{sfxClipName}-sfxClip");
  44. if(obj != null && sfxClip == null) sfxClip = (SFXClip)obj;
  45. }
  46. public override void Start()
  47. {
  48. GameObject parentObj = GetVariable<GameObject>(parent, null);
  49. Vector3 pos = new Vector3();
  50. if(parentObj != null) pos = parentObj.transform.position;
  51. SoundController.EmmitSound(sfxClip, pos);
  52. _done = true;
  53. _started = true;
  54. }
  55. public override void Update() { }
  56. public override void Restart()
  57. {
  58. _done = false;
  59. _started = false;
  60. }
  61. public static PlaySFXClipGameAction JSONToPlaySFXClipGameAction(string data)
  62. {
  63. return JsonUtility.FromJson<PlaySFXClipGameAction>(data);
  64. }
  65. private PlaySFXClipGameAction Duplicate(GameActionsController controller)
  66. {
  67. PlaySFXClipGameAction action = new PlaySFXClipGameAction(controller == null ? this.controller : controller);
  68. action.controller = controller;
  69. return action;
  70. }
  71. private T GetVariable<T>(string title, T defaultValue)
  72. {
  73. for (int i = 0; i < controller.context.variables.Count; i++)
  74. {
  75. if(controller.context.variables[i].name == title)
  76. {
  77. return controller.context.variables[i].GetValue<T>(defaultValue);
  78. }
  79. }
  80. return defaultValue;
  81. }
  82. [HideInInspector] public string sfxClipName;
  83. public override void OnBeforeSerialize(GameActionObjectSerializer serializer, int n, int depth)
  84. {
  85. if(sfxClip != null)
  86. {
  87. sfxClipName = sfxClip.name;
  88. serializer.SerializeScriptableObject($"{sfxClipName}-sfxClip", sfxClip);
  89. }
  90. }
  91. public override void OnBeforeDeserialize(GameActionObjectSerializer serializer, int n, int depth)
  92. {
  93. }
  94. private void GetCompatibleVariablenames()
  95. {
  96. if(_controller == null) return;
  97. if(_controller.context == null) return;
  98. possibleVariables = _controller.context.variables
  99. //.Where(x => x.data is GameActionContextString)
  100. .Select(x => new ValueDropdownItem(x.name, x.name));
  101. }
  102. }
  103. }