// Copyright (c) 2015 - 2023 Doozy Entertainment. All Rights Reserved. // This code can only be used under the standard Unity Asset Store End User License Agreement // A Copy of the EULA APPENDIX 1 is available at http://unity3d.com/company/legal/as_terms using System; using Doozy.Runtime.Reactor.Internal; using Doozy.Runtime.Reactor.Reflection; using UnityEngine; namespace Doozy.Runtime.Reactor.Reactions { /// /// Specialized reaction built to work with a ReflectedInt /// Designed to animate a int value over time /// [Serializable] public class ReflectedIntReaction : IntReaction { /// Int value target accessed via reflection public ReflectedInt valueTarget { get; private set; } [SerializeField] private bool Enabled; /// Enable or disable the animation public bool enabled { get => Enabled; set => Enabled = value; } [SerializeField] private int StartValue; /// Start value public int startValue { get => StartValue; set { StartValue = value; if (isActive) UpdateValues(); } } /// Current reflected value (can be different from the animation's currentValue) public int currentReflectedValue { get => valueTarget.value; set => valueTarget.value = value; } [SerializeField] private ReferenceValue FromReferenceValue = ReferenceValue.StartValue; /// Selector for the reference value used to calculate the From value public ReferenceValue fromReferenceValue { get => FromReferenceValue; set => FromReferenceValue = value; } [SerializeField] private ReferenceValue ToReferenceValue = ReferenceValue.StartValue; /// Selector for the reference value used to calculate the To value public ReferenceValue toReferenceValue { get => ToReferenceValue; set => ToReferenceValue = value; } [SerializeField] private int FromCustomValue; /// Custom used to calculate the From value, when the FromReferenceValue is set to CustomValue public int fromCustomValue { get => FromCustomValue; set => FromCustomValue = value; } [SerializeField] private int ToCustomValue; /// Custom used to calculate the To value, when the ToReferenceValue is set to CustomValue public int toCustomValue { get => ToCustomValue; set => ToCustomValue = value; } [SerializeField] private int FromOffset; /// Offset value added to the From value, when the FromReferenceValue is set to either StartValue or CurrentValue public int fromOffset { get => FromOffset; set => FromOffset = value; } [SerializeField] private int ToOffset; /// Offset value added to the To value, when the ToReferenceValue is set to either StartValue or CurrentValue public int toOffset { get => ToOffset; set => ToOffset = value; } public override void Reset() { base.Reset(); valueTarget = null; FromReferenceValue = ReferenceValue.StartValue; FromCustomValue = 0; FromOffset = 0; ToReferenceValue = ReferenceValue.StartValue; ToCustomValue = 100; ToOffset = 0; } /// Set the target reflected value for this reaction /// Reflected Value public ReflectedIntReaction SetTarget(ReflectedInt target) { this.SetTargetObject(target); valueTarget = target; startValue = target.value; getter = () => currentReflectedValue; setter = value => currentReflectedValue = value; return this; } /// Play the reaction /// If TRUE, the reaction will play in reverse public override void Play(bool inReverse = false) { if (!isActive) { UpdateValues(); SetValue(inReverse ? ToValue : FromValue); } base.Play(inReverse); } /// Play the reaction from the given start progress (from) to the current progress /// From (start) progress public override void PlayFromProgress(float fromProgress) { UpdateValues(); base.PlayFromProgress(fromProgress); } /// Set the reaction's progress at the given target progress /// Target progress public override void SetProgressAt(float targetProgress) { UpdateValues(); base.SetProgressAt(targetProgress); } /// Update From and To values by recalculating them public void UpdateValues() { SetFrom(GetValue(FromReferenceValue, FromOffset, FromCustomValue)); SetTo(GetValue(ToReferenceValue, ToOffset, ToCustomValue)); } private int GetValue(ReferenceValue referenceValue, int offset, int customValue) { int value = referenceValue switch { ReferenceValue.StartValue => startValue + offset, ReferenceValue.CurrentValue => currentReflectedValue + offset, ReferenceValue.CustomValue => customValue, _ => throw new ArgumentOutOfRangeException() }; return value; } } }