Exemplo n.º 1
0
 private void deleteComponentRemovedHandler(ListenerKey key, ComponentRemovedHandler handler)
 {
     if (componentRemovedHandlers.ContainsKey(key))
     {
         componentRemovedHandlers[key].Remove(handler);
         if (componentRemovedHandlers[key].Count == 0)
         {
             componentRemovedHandlers.Remove(key);
         }
     }
 }
        /// <summary>
        /// Attempts activate this SnapZone with the given <see cref="GameObject"/>.
        /// </summary>
        /// <param name="activator">The colliding interactable.</param>
        /// <param name="activatingZone">The SnapZone that was previously being activated by the colliding interactable.</param>
        protected virtual void AttemptReactivation(GameObject activator, SnapZoneActivator activatingZone)
        {
            activator.TryGetComponent <InteractableFacade>(true, true).ActiveCollisions.Remove(activatingZone.Facade.Configuration.ActivationArea.gameObject);
            ListenerKey listenerKey = new ListenerKey(activator, activatingZone);

            activatingZoneListeners.TryGetValue(listenerKey, out UnityAction <GameObject> activatingZoneListener);
            if (activatingZoneListener != null)
            {
                activatingZone.Facade.Exited.RemoveListener(activatingZoneListener);
            }
            activatingZoneListeners.Remove(listenerKey);
            Activate(activator);
        }
Exemplo n.º 3
0
        private ComponentRemovedHandler createComponentRemovedHandler <T>(ListenerKey key, Action <T> onRemoved) where T : BaseData
        {
            if (!componentRemovedHandlers.ContainsKey(key))
            {
                componentRemovedHandlers.Add(key, new List <ComponentRemovedHandler>());
            }
            ComponentRemovedHandler <T> componentRemovedHandler = new ComponentRemovedHandler <T>();

            componentRemovedHandler.Removed = onRemoved;
            ComponentRemovedHandler <T> componentRemovedHandler2 = componentRemovedHandler;

            componentRemovedHandlers[key].Add(componentRemovedHandler2);
            return(componentRemovedHandler2);
        }
Exemplo n.º 4
0
        private ComponentAddedHandler createComponentAddedHandler <T>(ListenerKey key, Action <T> onAdded, bool onlyOnce) where T : BaseData
        {
            if (!componentAddedHandlers.ContainsKey(key))
            {
                componentAddedHandlers.Add(key, new List <ComponentAddedHandler>());
            }
            ComponentAddedHandler <T> componentAddedHandler = new ComponentAddedHandler <T>();

            componentAddedHandler.Added    = onAdded;
            componentAddedHandler.onlyOnce = onlyOnce;
            ComponentAddedHandler <T> componentAddedHandler2 = componentAddedHandler;

            componentAddedHandlers[key].Add(componentAddedHandler2);
            return(componentAddedHandler2);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Cancels the attempt to activate the SnapZone upon the previous activating SnapZone becoming deactivated.
        /// </summary>
        /// <param name="activator">The colliding interactable.</param>
        /// <param name="activatingZone">The SnapZone that was previously being activated by the colliding interactable.</param>
        protected virtual void CancelAttemptReactivation(GameObject activator, SnapZoneActivator activatingZone)
        {
            ListenerKey listenerKey = new ListenerKey(activator, activatingZone);

            activatingZoneListeners.TryGetValue(listenerKey, out UnityAction <GameObject> onExitActivatingZoneListener);
            if (onExitActivatingZoneListener != null)
            {
                activatingZone.Facade.Exited.RemoveListener(onExitActivatingZoneListener);
                activatingZoneListeners.Remove(listenerKey);
            }

            currentZoneListeners.TryGetValue(listenerKey, out UnityAction <GameObject> onExitCurrentZoneListener);
            if (onExitCurrentZoneListener != null)
            {
                Facade.Exited.RemoveListener(onExitCurrentZoneListener);
                currentZoneListeners.Remove(listenerKey);
            }
        }
Exemplo n.º 6
0
        public void ComponentRemoved <T>(string entityName, T component) where T : BaseData
        {
            ListenerKey listenerKey = default(ListenerKey);

            listenerKey.EntityName = entityName;
            listenerKey.Type       = typeof(T);
            ListenerKey key = listenerKey;

            if (componentRemovedHandlers.ContainsKey(key))
            {
                List <ComponentRemovedHandler> list = componentRemovedHandlers[key];
                int count = list.Count;
                for (int i = 0; i < count; i++)
                {
                    ComponentRemovedHandler <T> componentRemovedHandler = (ComponentRemovedHandler <T>)list[i];
                    componentRemovedHandler.Removed.InvokeSafe(component);
                }
            }
        }
Exemplo n.º 7
0
        internal DataEventListener Whenever <T>(string entityName, Action <T> onAdded, Action <T> onRemoved) where T : BaseData
        {
            DataEventListenerImpl dataEventListenerImpl = (DataEventListenerImpl)When(entityName, onAdded);
            ListenerKey           listenerKey           = default(ListenerKey);

            listenerKey.EntityName = entityName;
            listenerKey.Type       = typeof(T);
            ListenerKey key = listenerKey;

            if (dataEventListenerImpl.AddedHandler == null)
            {
                dataEventListenerImpl.AddedHandler = createComponentAddedHandler(key, onAdded, onlyOnce: false);
            }
            else
            {
                dataEventListenerImpl.AddedHandler.onlyOnce = false;
            }
            dataEventListenerImpl.RemovedHandler = createComponentRemovedHandler(key, onRemoved);
            return(dataEventListenerImpl);
        }
Exemplo n.º 8
0
        internal void ComponentAdded <T>(string entityName, T component) where T : BaseData
        {
            ListenerKey listenerKey = default(ListenerKey);

            listenerKey.EntityName = entityName;
            listenerKey.Type       = typeof(T);
            ListenerKey key = listenerKey;

            if (!componentAddedHandlers.ContainsKey(key))
            {
                return;
            }
            List <ComponentAddedHandler> list = componentAddedHandlers[key];
            Stack <int> stack = new Stack <int>();
            int         count = list.Count;

            for (int i = 0; i < count; i++)
            {
                ComponentAddedHandler <T> componentAddedHandler = (ComponentAddedHandler <T>)list[i];
                componentAddedHandler.Added.InvokeSafe(component);
                if (componentAddedHandler.onlyOnce)
                {
                    stack.Push(i);
                }
            }
            if (stack.Count <= 0)
            {
                return;
            }
            foreach (int item in stack)
            {
                list.RemoveAt(item);
            }
            if (list.Count == 0)
            {
                componentAddedHandlers.Remove(key);
            }
        }
Exemplo n.º 9
0
        internal DataEventListener When <T>(string entityName, Action <T> onAdded) where T : BaseData
        {
            DataEventListenerImpl dataEventListenerImpl = new DataEventListenerImpl();

            dataEventListenerImpl.ListenerCollection = this;
            dataEventListenerImpl.Key.EntityName     = entityName;
            dataEventListenerImpl.Key.Type           = typeof(T);
            DataEventListenerImpl dataEventListenerImpl2 = dataEventListenerImpl;
            DataEntityHandle      dataEntityHandle       = dataEntityCollection.FindEntityByName(entityName);

            if (!dataEntityHandle.IsNull && dataEntityCollection.HasComponent <T>(dataEntityHandle))
            {
                onAdded(dataEntityCollection.GetComponent <T>(dataEntityHandle));
            }
            else
            {
                ListenerKey listenerKey = default(ListenerKey);
                listenerKey.EntityName = entityName;
                listenerKey.Type       = typeof(T);
                ListenerKey key = listenerKey;
                dataEventListenerImpl2.AddedHandler = createComponentAddedHandler(key, onAdded, onlyOnce: true);
            }
            return(dataEventListenerImpl2);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Attempts to activate the SnapZone if the colliding <see cref="GameObject"/> is not already activating another SnapZone.
        /// </summary>
        /// <param name="activator">The colliding interactable.</param>
        public virtual void Activate(GameObject activator)
        {
            IsActivated = false;
            TrySetInteractableFacade(activator);
            if (currentActivatingInteractable == null || Facade.Configuration.ActivationArea == null || !Facade.Configuration.CollidingObjectsList.Contains(currentActivatingInteractable.gameObject))
            {
                return;
            }

            currentActivatingInteractable.Configuration.ActiveCollisions.AddUnique(Facade.Configuration.ActivationArea.gameObject);
            invalidSnapZoneCollisions.Clear();

            foreach (GameObject collidingObject in currentActivatingInteractable.Configuration.ActiveCollisions.SubscribableElements)
            {
                if (collidingObject == null)
                {
                    continue;
                }

                SnapZoneActivator activatingZone = collidingObject.GetComponent <SnapZoneActivator>();
                if (activatingZone == null)
                {
                    continue;
                }

                if (!activatingZone.Facade.Configuration.CollidingObjectsList.Contains(currentActivatingInteractable.gameObject))
                {
                    invalidSnapZoneCollisions.Add(activatingZone.Facade.Configuration.ActivationArea.gameObject);
                    continue;
                }

                if (activatingZone == Facade.Configuration.ActivationArea)
                {
                    if (!IsActivated)
                    {
                        Facade.Configuration.EmitActivated(activator);
                    }
                    IsActivated = true;
                    Validated?.Invoke(activator);
                    ClearInvalidSnapZoneCollisions(currentActivatingInteractable, ref invalidSnapZoneCollisions);
                    break;
                }
                else
                {
                    ListenerKey listenerKey = new ListenerKey(activator, activatingZone);

                    if (!activatingZoneListeners.ContainsKey(listenerKey))
                    {
                        UnityAction <GameObject> onExitActivatingZoneListener = activatingInteractable => AttemptReactivation(activatingInteractable, activatingZone);
                        activatingZone.Facade.Exited.AddListener(onExitActivatingZoneListener);
                        activatingZoneListeners.Add(listenerKey, onExitActivatingZoneListener);
                    }

                    if (!currentZoneListeners.ContainsKey(listenerKey))
                    {
                        UnityAction <GameObject> onExitCurrentZoneListener = activatingInteractable => CancelAttemptReactivation(activatingInteractable, activatingZone);
                        Facade.Exited.AddListener(onExitCurrentZoneListener);
                        currentZoneListeners.Add(listenerKey, onExitCurrentZoneListener);
                    }
                    ClearInvalidSnapZoneCollisions(currentActivatingInteractable, ref invalidSnapZoneCollisions);
                    break;
                }
            }

            ClearInvalidSnapZoneCollisions(currentActivatingInteractable, ref invalidSnapZoneCollisions);
        }