namespace Zinnia.Association { using UnityEngine; using Zinnia.Association.Collection; using Zinnia.Extension; using Zinnia.Process; /// /// (De)activates s. /// public class GameObjectsAssociationActivator : MonoBehaviour, IProcessable { [Tooltip("The associations in order they will be activated if they match the currently expected state.")] [SerializeField] private GameObjectsAssociationObservableList associations; /// /// The associations in order they will be activated if they match the currently expected state. /// public GameObjectsAssociationObservableList Associations { get { return associations; } set { if (this.IsMemberChangeAllowed()) { OnBeforeAssociationsChange(); } associations = value; if (this.IsMemberChangeAllowed()) { OnAfterAssociationsChange(); } } } /// /// The currently activated association, or if no association is activated. /// public GameObjectsAssociation CurrentAssociation { get; private set; } /// /// Activates the s that are part of the association if the association matches the currently expected state. /// public virtual void Activate() { if (Associations == null) { return; } GameObjectsAssociation desiredAssociation = null; foreach (GameObjectsAssociation association in Associations.NonSubscribableElements) { if (association == null) { continue; } if (association.ShouldBeActive()) { desiredAssociation = association; break; } } if (desiredAssociation == null || CurrentAssociation == desiredAssociation) { return; } CurrentAssociation = desiredAssociation; foreach (GameObjectsAssociation association in Associations.NonSubscribableElements) { if (association == desiredAssociation) { continue; } foreach (GameObject associatedObject in association.GameObjects.NonSubscribableElements) { if (associatedObject != null) { associatedObject.SetActive(false); } } } foreach (GameObject associatedObject in desiredAssociation.GameObjects.NonSubscribableElements) { if (associatedObject != null) { associatedObject.SetActive(true); } } } /// /// Deactivates the association that is currently activated and all other known associations. /// public virtual void Deactivate() { Deactivate(Associations); } /// /// Calls on the specified moment. /// public void Process() { Activate(); } protected virtual void Awake() { if (Associations == null) { return; } foreach (GameObjectsAssociation association in Associations.NonSubscribableElements) { if (association == null) { continue; } foreach (GameObject associatedObject in association.GameObjects.NonSubscribableElements) { if (associatedObject.activeInHierarchy) { Debug.LogWarning($"At least one association object is active in the scene on {nameof(Awake)} of this {GetType().Name}. Having multiple association objects active at the same time will most likely lead to issues. Make sure to deactivate them all before you play or create a build."); return; } } } } protected virtual void OnEnable() { Activate(); } protected virtual void OnDisable() { Deactivate(); } /// /// Deactivates the association that is currently activated and all other known associations. /// /// The associations to deactivate. protected virtual void Deactivate(GameObjectsAssociationObservableList associations) { if (associations == null) { return; } foreach (GameObjectsAssociation association in associations.NonSubscribableElements) { if (association == null) { continue; } foreach (GameObject associatedObject in association.GameObjects.NonSubscribableElements) { if (associatedObject != null) { associatedObject.SetActive(false); } } } if (CurrentAssociation != null) { foreach (GameObject associatedObject in CurrentAssociation.GameObjects.NonSubscribableElements) { if (associatedObject != null) { associatedObject.SetActive(false); } } CurrentAssociation = null; } } /// /// Called before has been changed. /// protected virtual void OnBeforeAssociationsChange() { Deactivate(Associations); } /// /// Called after has been changed. /// protected virtual void OnAfterAssociationsChange() { Activate(); } } }