Exemple #1
0
        private void DelegateVisualEvent(int entityId, GameEvent e)
        {
            // Get entity object.
            GameObject entityObject;

            this.TryGetEntityObject(entityId, out entityObject);
            if (entityObject == null)
            {
                Debug.LogError(
                    string.Format(
                        "Received event {0} for visual behaviour, but no entity object with id {1} was found.",
                        e.EventType,
                        entityId));
                return;
            }

            // Check which method to call.
            LogicToVisualDelegate logicToVisualDelegate =
                this.logicToVisualDelegates.FirstOrDefault(
                    existingDelegate => existingDelegate.Event.Equals(e.EventType));

            if (logicToVisualDelegate != null)
            {
                foreach (string callbackName in logicToVisualDelegate.CallbackNames)
                {
                    entityObject.SendMessage(callbackName, e, SendMessageOptions.DontRequireReceiver);
                }
            }
        }
Exemple #2
0
        private void Start()
        {
            // Search all visual behaviour which map to logic components.
            ReflectionUtils.HandleTypesWithAttribute <LogicToVisualMappingAttribute>(
                (visualType, mappingAttribute) =>
                this.logicVisualMappings.Add(LogicToVisualMapping.create(mappingAttribute.LogicType, visualType)));

            // Get delegates of visual behaviours.
            foreach (LogicToVisualMapping logicToVisualMapping in this.logicVisualMappings)
            {
                LogicToVisualDelegateAttribute[] delegateAttributes =
                    (LogicToVisualDelegateAttribute[])
                    ReflectionUtils.GetAttributes(
                        logicToVisualMapping.VisualType,
                        typeof(LogicToVisualDelegateAttribute),
                        true);
                if (delegateAttributes.Length == 0)
                {
                    continue;
                }

                foreach (LogicToVisualDelegateAttribute delegateAttribute in delegateAttributes)
                {
                    // Make sure that the same event with the same callback name is not already registered, otherwise
                    // the method would be called multiple times when the event occurs.
                    LogicToVisualDelegate logicToVisualDelegate =
                        this.logicToVisualDelegates.FirstOrDefault(
                            existingDelegate => existingDelegate.Event.Equals(delegateAttribute.Event));
                    if (logicToVisualDelegate == null)
                    {
                        logicToVisualDelegate = LogicToVisualDelegate.create(delegateAttribute);
                        this.logicToVisualDelegates.Add(logicToVisualDelegate);
                    }
                    else
                    {
                        logicToVisualDelegate.CallbackNames.Add(delegateAttribute.CallbackName);
                    }
                }
            }
        }