Exemple #1
0
        /// <summary>
        ///   Convenience method for retrieving a component from two possible entities.
        /// </summary>
        /// <typeparam name="TComponent">Type of the component to get.</typeparam>
        /// <param name="data">Data for the event that affected two entities.</param>
        /// <param name="entityId">Id of the entity having the component attached.</param>
        /// <param name="component">Component.</param>
        /// <returns>
        ///   True if one of the entities has a <typeparamref name="TComponent" />
        ///   attached; otherwise, false.
        /// </returns>
        public bool GetEntityComponent <TComponent>(Entity2Data data, out int entityId, out TComponent component)
            where TComponent : class, IEntityComponent
        {
            int entityIdA = data.First;
            int entityIdB = data.Second;

            TComponent componentA = this.GetComponent <TComponent>(entityIdA);

            if (componentA != null)
            {
                entityId  = entityIdA;
                component = componentA;
                return(true);
            }

            TComponent componentB = this.GetComponent <TComponent>(entityIdB);

            if (componentB != null)
            {
                entityId  = entityIdB;
                component = componentB;
                return(true);
            }

            entityId  = 0;
            component = null;
            return(false);
        }
Exemple #2
0
        /// <summary>
        ///   Convenience method for retrieving components from two entities
        ///   in case the order of the entities is unknown.
        /// </summary>
        /// <typeparam name="TComponentTypeA">Type of the first component to get.</typeparam>
        /// <typeparam name="TComponentTypeB">Type of the second component to get.</typeparam>
        /// <param name="data">Data for the event that affected two entities.</param>
        /// <param name="entityIdA">Id of the entity having the first component attached.</param>
        /// <param name="entityIdB">Id of the entity having the second component attached.</param>
        /// <param name="componentA">First component.</param>
        /// <param name="componentB">Second component.</param>
        /// <returns>
        ///   <c>true</c>, if one of the entities has a <typeparamref name="TComponentTypeA" />
        ///   and the other one a <typeparamref name="TComponentTypeB" /> attached,
        ///   and <c>false</c> otherwise.
        /// </returns>
        public bool GetEntityComponents <TComponentTypeA, TComponentTypeB>(
            Entity2Data data,
            out int entityIdA,
            out int entityIdB,
            out TComponentTypeA componentA,
            out TComponentTypeB componentB) where TComponentTypeA : class, IEntityComponent
            where TComponentTypeB : class, IEntityComponent
        {
            entityIdA = data.First;
            entityIdB = data.Second;

            componentA = this.GetComponent <TComponentTypeA>(entityIdA);
            componentB = this.GetComponent <TComponentTypeB>(entityIdB);

            if (componentA == null || componentB == null)
            {
                // Check other way round.
                entityIdA = data.Second;
                entityIdB = data.First;

                componentA = this.GetComponent <TComponentTypeA>(entityIdA);
                componentB = this.GetComponent <TComponentTypeB>(entityIdB);

                return(componentA != null && componentB != null);
            }

            return(true);
        }
Exemple #3
0
        private void OnVisualEvent(GameEvent e)
        {
            // Check for which entity the event is for.
            EntityEventData entityEventData = e.EventData as EntityEventData;

            if (entityEventData != null)
            {
                this.DelegateVisualEvent(entityEventData.EntityId, e);
                return;
            }

            Entity2Data entity2EventData = e.EventData as Entity2Data;

            if (entity2EventData != null)
            {
                this.DelegateVisualEvent(entity2EventData.First, e);
                this.DelegateVisualEvent(entity2EventData.Second, e);
                return;
            }

            Debug.LogError(
                string.Format(
                    "Received event {0} for visual behaviour, but event data {1} wasn't derived from EntityEventData or Entity2Data to get entity id(s).",
                    e.EventType,
                    e.EventData.GetType()));
        }