Browse Source

Added PlaySoundtrack GameAction

James Peret 2 years ago
parent
commit
fddb34f408

+ 8 - 0
Runtime/GameActions.meta

@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: 0b025a354ff54304c8897be1435a4318
+folderAsset: yes
+DefaultImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 117 - 0
Runtime/GameActions/PlaySoundtrackGameAction.cs

@@ -0,0 +1,117 @@
+using System.Collections;
+using System.Linq;
+using System.Collections.Generic;
+using UnityEngine;
+using Sirenix.OdinInspector;
+using KairoEngine.Core;
+using KairoEngine.Core.GameActions;
+
+namespace KairoEngine.SoundtrackSystem.GameActions
+{
+
+    [System.Serializable, HideReferenceObjectPicker]
+    public class PlaySoundtrackGameAction : GameActionBase
+    {
+        public override string name 
+        { 
+            get
+            {
+                return $"Play Soundtrack \"{GetTargetSongTitle()}\"";
+            }
+        }
+
+        public override GameActionsController controller { 
+            get => _controller; 
+            set 
+            {
+                _controller = value;
+                typeName = "PlaySoundtrackGameAction";
+                className =  this.GetType().AssemblyQualifiedName;
+                GetCompatibleVariablenames();
+            }
+        }
+        public override string GetTypeName() => "PlaySoundtrackGameAction";
+        public override string GetActionName() => "Soundtrack/Play Soundtrack";
+
+        [FoldoutGroup("@name")] 
+        public GameActionStringType actionType = GameActionStringType.String;
+        
+        [FoldoutGroup("@name"), ShowIf("@actionType == GameActionStringType.String")]
+        public string songTitle = "Song Title";
+
+        [FoldoutGroup("@name"), ShowIf("@actionType == GameActionStringType.Variable")]
+        [ValueDropdown("possibleVariables", IsUniqueList = false)]
+        public string songTitleVariable;
+
+        private IEnumerable possibleVariables = new ValueDropdownList<string>();
+
+        public PlaySoundtrackGameAction(GameActionsController controller) : base(controller)
+        {
+            this.controller = controller;
+            className =  this.GetType().AssemblyQualifiedName;
+        }
+
+        public override void Start()
+        {
+            SoundtrackManager.PlayTrack(GetTargetSongTitle());
+            _done = true;
+            _started = true;
+        }
+
+        public override void Update() { }
+
+        public override void Restart()
+        {
+            _done = false;
+            _started = false;
+        }
+
+        private string GetTargetSongTitle()
+        {
+            string targetSong = "";
+            if(actionType == GameActionStringType.String) targetSong = songTitle;
+            else if(actionType == GameActionStringType.Variable) targetSong = GetVariable(songTitleVariable);
+            return targetSong;
+        }
+
+        private void GetCompatibleVariablenames()
+        {
+            if(_controller == null) return;
+            if(_controller.context == null) return;
+            possibleVariables = _controller.context.variables
+                //.Where(x => x.data is GameActionContextString)
+                .Select(x => new ValueDropdownItem(x.name, x.name)); 
+        }
+
+        public static PlaySoundtrackGameAction JSONToPlaySoundtrackGameAction(string data)
+        {
+            return JsonUtility.FromJson<PlaySoundtrackGameAction>(data);
+        }
+
+        private PlaySoundtrackGameAction Duplicate(GameActionsController controller = null)
+        {
+            PlaySoundtrackGameAction action = new PlaySoundtrackGameAction(controller == null ? this.controller : controller);
+            action.controller = controller;
+            action.actionType = actionType;
+            action.songTitle = songTitle;
+            action.songTitleVariable = songTitleVariable;
+            return action;
+        }
+
+        private string GetVariable(string title)
+        {
+            if(controller == null) return "";
+            if(controller.context == null) return "";
+            for (int i = 0; i < controller.context.variables.Count; i++)
+            {
+                if(controller.context.variables[i].name == title)
+                {
+                    string value = controller.context.variables[i].GetValue<string>("");
+                    if(value != "") return value;
+                }
+            }
+            return "";
+        }
+    }
+}
+

+ 11 - 0
Runtime/GameActions/PlaySoundtrackGameAction.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 8708430627bc8214b8a0ed85c058fb50
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: