namespace Zinnia.Tracking.Modification { using UnityEngine; using Zinnia.Data.Collection.List; using Zinnia.Extension; /// /// Sets the state of the current target to the specified active state. /// public class GameObjectStateSwitcher : MonoBehaviour { [Tooltip("A collection of targets to set the state on when it is the active index.")] [SerializeField] private GameObjectObservableList targets; /// /// A collection of targets to set the state on when it is the active index. /// public GameObjectObservableList Targets { get { return targets; } set { targets = value; } } [Tooltip("The state to set the active index target. All other targets will be set to the opposite state.")] [SerializeField] private bool targetState = true; /// /// The state to set the active index target. All other targets will be set to the opposite state. /// public bool TargetState { get { return targetState; } set { targetState = value; } } /// /// Switches to the next target in the collection and sets to the appropriate state. /// public virtual void SwitchNext() { if (!this.IsValidState()) { return; } Targets.CurrentIndex++; if (Targets.CurrentIndex >= Targets.NonSubscribableElements.Count) { Targets.CurrentIndex = 0; } Switch(); } /// /// Switches to the previous target in the collection and sets to the appropriate state. /// public virtual void SwitchPrevious() { if (!this.IsValidState()) { return; } Targets.CurrentIndex--; if (Targets.CurrentIndex < 0) { Targets.CurrentIndex = Targets.NonSubscribableElements.Count - 1; } Switch(); } /// /// Switches to the a specific target in the collection and sets to the appropriate state. /// /// The index of the collection to switch to. public virtual void SwitchTo(int index) { if (!this.IsValidState()) { return; } Targets.CurrentIndex = Mathf.Clamp(index, 0, Targets.NonSubscribableElements.Count - 1); Switch(); } /// /// Switches to the target at the in the collection and sets to the appropriate state. /// public virtual void SwitchToCurrentIndex() { if (!this.IsValidState()) { return; } SwitchTo(Targets.CurrentIndex); } /// /// Switches the current active target state. /// protected virtual void Switch() { for (int index = 0; index < Targets.NonSubscribableElements.Count; index++) { Targets.NonSubscribableElements[index].SetActive(index == Targets.CurrentIndex ? TargetState : !TargetState); } } } }