PlaySoundtrackGameAction.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System.Collections;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using Sirenix.OdinInspector;
  6. using KairoEngine.Core;
  7. using KairoEngine.Core.GameActions;
  8. namespace KairoEngine.SoundtrackSystem.GameActions
  9. {
  10. [System.Serializable, HideReferenceObjectPicker]
  11. public class PlaySoundtrackGameAction : GameActionBase
  12. {
  13. public override string name
  14. {
  15. get
  16. {
  17. return $"Play Soundtrack \"{GetTargetSongTitle()}\"";
  18. }
  19. }
  20. public override GameActionsController controller {
  21. get => _controller;
  22. set
  23. {
  24. _controller = value;
  25. typeName = "PlaySoundtrackGameAction";
  26. className = this.GetType().AssemblyQualifiedName;
  27. GetCompatibleVariablenames();
  28. }
  29. }
  30. public override string GetTypeName() => "PlaySoundtrackGameAction";
  31. public override string GetActionName() => "Soundtrack/Play Soundtrack";
  32. [FoldoutGroup("@name")]
  33. public GameActionStringType actionType = GameActionStringType.String;
  34. [FoldoutGroup("@name"), ShowIf("@actionType == GameActionStringType.String")]
  35. public string songTitle = "Song Title";
  36. [FoldoutGroup("@name"), ShowIf("@actionType == GameActionStringType.Variable")]
  37. [ValueDropdown("possibleVariables", IsUniqueList = false)]
  38. public string songTitleVariable;
  39. private IEnumerable possibleVariables = new ValueDropdownList<string>();
  40. public PlaySoundtrackGameAction(GameActionsController controller) : base(controller)
  41. {
  42. this.controller = controller;
  43. className = this.GetType().AssemblyQualifiedName;
  44. }
  45. public override void Start()
  46. {
  47. SoundtrackManager.PlayTrack(GetTargetSongTitle());
  48. _done = true;
  49. _started = true;
  50. }
  51. public override void Update() { }
  52. public override void Restart()
  53. {
  54. _done = false;
  55. _started = false;
  56. }
  57. private string GetTargetSongTitle()
  58. {
  59. string targetSong = "";
  60. if(actionType == GameActionStringType.String) targetSong = songTitle;
  61. else if(actionType == GameActionStringType.Variable) targetSong = GetVariable(songTitleVariable);
  62. return targetSong;
  63. }
  64. private void GetCompatibleVariablenames()
  65. {
  66. if(_controller == null) return;
  67. if(_controller.context == null) return;
  68. possibleVariables = _controller.context.variables
  69. //.Where(x => x.data is GameActionContextString)
  70. .Select(x => new ValueDropdownItem(x.name, x.name));
  71. }
  72. public static PlaySoundtrackGameAction JSONToPlaySoundtrackGameAction(string data)
  73. {
  74. return JsonUtility.FromJson<PlaySoundtrackGameAction>(data);
  75. }
  76. private PlaySoundtrackGameAction Duplicate(GameActionsController controller = null)
  77. {
  78. PlaySoundtrackGameAction action = new PlaySoundtrackGameAction(controller == null ? this.controller : controller);
  79. action.controller = controller;
  80. action.actionType = actionType;
  81. action.songTitle = songTitle;
  82. action.songTitleVariable = songTitleVariable;
  83. return action;
  84. }
  85. private string GetVariable(string title)
  86. {
  87. if(controller == null) return "";
  88. if(controller.context == null) return "";
  89. for (int i = 0; i < controller.context.variables.Count; i++)
  90. {
  91. if(controller.context.variables[i].name == title)
  92. {
  93. string value = controller.context.variables[i].GetValue<string>("");
  94. if(value != "") return value;
  95. }
  96. }
  97. return "";
  98. }
  99. }
  100. }