using MoreMountains.Tools; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; namespace MoreMountains.Feedbacks { public class MMFeedbackBaseTarget { /// the receiver to write the level to public MMPropertyReceiver Target; /// the curve to tween the intensity on public MMTweenType LevelCurve = new MMTweenType(new AnimationCurve(new Keyframe(0, 0), new Keyframe(0.3f, 1f), new Keyframe(1, 0))); /// the value to remap the intensity curve's 0 to public float RemapLevelZero = 0f; /// the value to remap the intensity curve's 1 to public float RemapLevelOne = 1f; /// the value to move the intensity to in instant mode public float InstantLevel; /// the initial value for this level public float InitialLevel; } public abstract class MMFeedbackBase : MMFeedback { /// the possible modes for this feedback public enum Modes { OverTime, Instant } [Header("Mode")] /// whether the feedback should affect the target property instantly or over a period of time [Tooltip("whether the feedback should affect the target property instantly or over a period of time")] public Modes Mode = Modes.OverTime; /// how long the target property should change over time [Tooltip("how long the target property should change over time")] [MMFEnumCondition("Mode", (int)Modes.OverTime)] public float Duration = 0.2f; /// whether or not that target property should be turned off on start [Tooltip("whether or not that target property should be turned off on start")] public bool StartsOff = false; /// whether or not the values should be relative or not [Tooltip("whether or not the values should be relative or not")] public bool RelativeValues = true; /// if this is true, calling that feedback will trigger it, even if it's in progress. If it's false, it'll prevent any new Play until the current one is over [Tooltip("if this is true, calling that feedback will trigger it, even if it's in progress. If it's false, it'll prevent any new Play until the current one is over")] public bool AllowAdditivePlays = false; /// if this is true, the target object will be disabled on stop [Tooltip("if this is true, the target object will be disabled on stop")] public bool DisableOnStop = false; /// the duration of this feedback is the duration of the target property, or 0 if instant public override float FeedbackDuration { get { return (Mode == Modes.Instant) ? 0f : ApplyTimeMultiplier(Duration); } set { if (Mode != Modes.Instant) { Duration = value; } } } protected List _targets; protected Coroutine _coroutine = null; /// /// On init we turn the target property off if needed /// /// protected override void CustomInitialization(GameObject owner) { base.CustomInitialization(owner); PrepareTargets(); if (Active) { if (StartsOff) { Turn(false); } } } /// /// Creates a new list, fills the targets, and initializes them /// protected virtual void PrepareTargets() { _targets = new List(); FillTargets(); InitializeTargets(); } /// /// On validate (if a value has changed in the inspector), we reinitialize what needs to be /// protected virtual void OnValidate() { PrepareTargets(); } /// /// Fills our list of targets, meant to be extended /// protected abstract void FillTargets(); /// /// Initializes each target in the list /// protected virtual void InitializeTargets() { if (_targets.Count == 0) { return; } foreach(MMFeedbackBaseTarget target in _targets) { target.Target.Initialization(this.gameObject); target.InitialLevel = target.Target.Level; } } /// /// On Play we turn our target property on and start an over time coroutine if needed /// /// /// protected override void CustomPlayFeedback(Vector3 position, float feedbacksIntensity = 1.0f) { if (Active) { Turn(true); switch (Mode) { case Modes.Instant: Instant(); break; case Modes.OverTime: if (!AllowAdditivePlays && (_coroutine != null)) { return; } _coroutine = StartCoroutine(UpdateValueSequence(feedbacksIntensity)); break; } } } /// /// Plays an instant feedback /// protected virtual void Instant() { if (_targets.Count == 0) { return; } foreach (MMFeedbackBaseTarget target in _targets) { target.Target.SetLevel(target.InstantLevel); } } /// /// This coroutine will modify the values on the target property /// /// protected virtual IEnumerator UpdateValueSequence(float feedbacksIntensity) { float journey = NormalPlayDirection ? 0f : FeedbackDuration; while ((journey >= 0) && (journey <= FeedbackDuration) && (FeedbackDuration > 0)) { float remappedTime = MMFeedbacksHelpers.Remap(journey, 0f, FeedbackDuration, 0f, 1f); SetValues(remappedTime, feedbacksIntensity); journey += NormalPlayDirection ? FeedbackDeltaTime : -FeedbackDeltaTime; yield return null; } SetValues(FinalNormalizedTime, feedbacksIntensity); if (StartsOff) { Turn(false); } _coroutine = null; yield return null; } /// /// Sets the various values on the target property on a specified time (between 0 and 1) /// /// protected virtual void SetValues(float time, float feedbacksIntensity) { if (_targets.Count == 0) { return; } float intensityMultiplier = Timing.ConstantIntensity ? 1f : feedbacksIntensity; foreach (MMFeedbackBaseTarget target in _targets) { float intensity = MMTween.Tween(time, 0f, 1f, target.RemapLevelZero, target.RemapLevelOne, target.LevelCurve); if (RelativeValues) { intensity += target.InitialLevel; } target.Target.SetLevel(intensity * intensityMultiplier); } } /// /// Turns the target property object off on stop if needed /// /// /// protected override void CustomStopFeedback(Vector3 position, float feedbacksIntensity = 1) { base.CustomStopFeedback(position, feedbacksIntensity); if (Active) { if (_coroutine != null) { StopCoroutine(_coroutine); _coroutine = null; } if (DisableOnStop) { Turn(false); } } } /// /// Turns the target object on or off /// /// protected virtual void Turn(bool status) { if (_targets.Count == 0) { return; } foreach (MMFeedbackBaseTarget target in _targets) { if (target.Target.TargetComponent.gameObject != null) { target.Target.TargetComponent.gameObject.SetActive(status); } } } } }