123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- using System;
- using System.Collections;
- using System.Linq;
- using System.Collections.Generic;
- using UnityEngine;
- using Sirenix.OdinInspector;
- using KairoEngine.Core;
- using KairoEngine.Core.GameActions;
- namespace KairoEngine.SFX.GameActions
- {
- [System.Serializable, HideReferenceObjectPicker]
- public class PlaySFXClipGameAction : GameActionBase
- {
- public override string name
- {
- get
- {
- return $"Play {(sfxClip != null ? sfxClip.name : "SFX Clip")}";
- }
- }
- public override GameActionsController controller {
- get => _controller;
- set
- {
- _controller = value;
- typeName = "PlaySFXClipGameAction";
- GetCompatibleVariablenames();
- }
- }
- public override string GetTypeName() => "PlaySFXClipGameAction";
- public override string GetActionName() => "Play SFX Clip";
- [FoldoutGroup("@name")]
- public SFXClip sfxClip;
- [FoldoutGroup("@name"), NonSerialized, ShowInInspector, ValueDropdown("possibleVariables", IsUniqueList = false)] public string parent;
- private IEnumerable possibleVariables = new ValueDropdownList<string>();
- public PlaySFXClipGameAction(GameActionsController controller) : base(controller)
- {
- this.controller = controller;
- className = this.GetType().AssemblyQualifiedName;
- }
- public override void Initialize(GameActionObjectSerializer objectSerializer)
- {
- var obj = objectSerializer.DeserializeScriptableObject($"{sfxClipName}-sfxClip");
- if(obj != null && sfxClip == null) sfxClip = (SFXClip)obj;
- }
- public override void Start()
- {
- GameObject parentObj = GetVariable<GameObject>(parent, null);
- Vector3 pos = new Vector3();
- if(parentObj != null) pos = parentObj.transform.position;
- SoundController.EmmitSound(sfxClip, pos);
- _done = true;
- _started = true;
- }
- public override void Update() { }
- public override void Restart()
- {
- _done = false;
- _started = false;
- }
- public static PlaySFXClipGameAction JSONToPlaySFXClipGameAction(string data)
- {
- return JsonUtility.FromJson<PlaySFXClipGameAction>(data);
- }
- private PlaySFXClipGameAction Duplicate(GameActionsController controller)
- {
- PlaySFXClipGameAction action = new PlaySFXClipGameAction(controller == null ? this.controller : controller);
- action.controller = controller;
- return action;
- }
- private T GetVariable<T>(string title, T defaultValue)
- {
- for (int i = 0; i < controller.context.variables.Count; i++)
- {
- if(controller.context.variables[i].name == title)
- {
- return controller.context.variables[i].GetValue<T>(defaultValue);
- }
- }
- return defaultValue;
- }
- [HideInInspector] public string sfxClipName;
- public override void OnBeforeSerialize(GameActionObjectSerializer serializer, int n, int depth)
- {
- if(sfxClip != null)
- {
- sfxClipName = sfxClip.name;
- serializer.SerializeScriptableObject($"{sfxClipName}-sfxClip", sfxClip);
- }
- }
- public override void OnBeforeDeserialize(GameActionObjectSerializer serializer, int n, int depth)
- {
-
- }
- 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));
- }
- }
- }
|