示例#1
0
        /// <summary>
        /// Searches the NodeOwner object for any methods that have the attribute [CommunicationCallback] and have a function signature that matches the requirements.
        /// </summary>
        protected virtual void RefreshCallbackBindings()
        {
            enabledHandlers.Clear();

            // Iterate over all methods on the owner object.
            var methodInfos = NodeOwner.GetType().GetMethods(bindingFlags);

            foreach (MethodInfo mi in methodInfos)
            {
                // the method has a [CommunicationCallback] attribute
                bool isHandler = mi.GetCustomAttributes(true).OfType <CommunicationCallback>().Any();
                if (!isHandler)
                {
                    continue;
                }

                ParameterInfo[] parameters = mi.GetParameters();
                Type            handlerKey = parameters[0].ParameterType;

                //handlers must take 1 or 2 parameters
                if (parameters.Length > 0 || parameters.Length < 3)
                {
                    CommunicationCallback attribute = mi.GetCustomAttributes(true).OfType <CommunicationCallback>().FirstOrDefault();

                    if (!enabledHandlers.ContainsKey(handlerKey))
                    {
                        enabledHandlers.Add(handlerKey, new Dictionary <string, MethodInfo>());
                    }

                    string[] tags = attribute.tags;
                    if (tags.Length > 0 && attribute.tags.matching == Tags.Matching.Any)
                    {
                        foreach (string s in tags)
                        {
                            enabledHandlers[handlerKey].Add(s, mi);
                        }
                    }
                    else if (tags.Length <= 0 || attribute.tags.matching == Tags.Matching.All)
                    {
                        enabledHandlers[handlerKey].Add(string.Empty, mi);
                    }
                    else
                    {
#if UNITY_5_3_OR_NEWER
                        DebugLogger.LogError("Unknown tag configuration " + attribute.tags.matching + ". Handler " + mi.Name + " not added to subscribed callbacks.");
#else
                        DebugLogger.WriteLine("Unknown tag configuration " + attribute.tags.matching + ". Handler " + mi.Name + " not added to subscribed callbacks.");
#endif
                    }
                }
                else
                {
#if UNITY_5_3_OR_NEWER
                    DebugLogger.LogError(mi.Name + " - Handler callbacks must take 1 or 2 parameters. The first parameter is the data type to be handled, the 2nd is optional and will be the object invoking the handler.");
#else
                    DebugLogger.WriteLine(methodInfo.Name + " - Handler callbacks must take 1 or 2 parameters. The first parameter is the data type to be handled, the 2nd is optional and will be the object invoking the handler.");
#endif
                }
            }
        }
示例#2
0
        public override BehaviourStatus Update()
        {
            if (NodeOwner.IsLookingAround)
            {
                return(BehaviourStatus.Running);
            }

            NodeOwner.LookAround();
            return(BehaviourStatus.Success);
        }
示例#3
0
    private void Awake()
    {
        onMouseOverEvent = new FightNodeEvent();
        currentHealth    = totalHealth;
        currentNodeOwner = startNodeOwner;
        StartCoroutine(RegenerationCoroutine());
        Select(false);
        SetHealthUI();
        SetOwner();

        fightNodes.Add(this);
    }
示例#4
0
        public override BehaviourStatus Update()
        {
            if (NodeOwner.LastKnownSpyPosition == Vector3.zero)
            {
                return(BehaviourStatus.Failure);
            }

            NodeOwner.GoToLastKnownPosition();
            NodeOwner.IsChasing = true;
            InterruptLookingAround();

            return(BehaviourStatus.Success);
        }
示例#5
0
        public override BehaviourStatus Update()
        {
            if (!NodeOwner.GuardSight.CanSeeSpy())
            {
                return(BehaviourStatus.Failure);
            }

            NodeOwner.IsInvestigating = false;
            NodeOwner.StopAllCoroutines();
            NodeOwner.IsChasing = true;
            NodeOwner.WasNextInvestigationPointSet = false;

            return(BehaviourStatus.Success);
        }
示例#6
0
        public override BehaviourStatus Update()
        {
            if (!NodeOwner.GuardHearing.CanHearSpy() && !NodeOwner.WasGuardInformedAboutSpy)
            {
                return(BehaviourStatus.Failure);
            }

            NodeOwner.IsInvestigating = false;
            NodeOwner.StopAllCoroutines();
            NodeOwner.IsChasing = true;
            NodeOwner.WasNextInvestigationPointSet = false;
            NodeOwner.WasGuardInformedAboutSpy     = false;

            return(BehaviourStatus.Success);
        }
示例#7
0
 public void MergeAttackUnits(int damage, NodeOwner damageOwner)
 {
     if (damageOwner == currentNodeOwner)
     {
         currentHealth += damage;
         currentHealth  = Mathf.Clamp(currentHealth, 0, totalHealth);
     }
     else
     {
         currentHealth -= damage;
         if (currentHealth == 0)
         {
             currentNodeOwner = NodeOwner.Nobody;
         }
         else if (currentHealth < 0)
         {
             currentNodeOwner = damageOwner;
             currentHealth    = Mathf.Clamp(currentHealth, 0, totalHealth);
         }
     }
     SetOwner();
     SetHealthUI();
 }
示例#8
0
 public override BehaviourStatus Update()
 {
     NodeOwner.IsChasing = false;
     NodeOwner.Investigate();
     return(BehaviourStatus.Success);
 }
示例#9
0
 private void StopLookingAroundCoroutines()
 {
     NodeOwner.StopCoroutine("LookAroundCoroutine");
     NodeOwner.StopCoroutine("LookForwardAfterReachingPatrolPoint");
 }
示例#10
0
        /// <summary>
        /// Invoke the callback that matches the data type of the data passed to the publish method.
        /// </summary>
        /// <returns>Returns true if this object contained a handler that was invoked.</returns>
        protected virtual bool InvokeMatchingCallback(object publishedData, object publisher, Tags tags)
        {
            bool result = false;

            Type handlerKey = publishedData.GetType();
            Dictionary <string, MethodInfo> matchingHandlers;

            //Does a method handling this data type exist?
            if (!enabledHandlers.TryGetValue(handlerKey, out matchingHandlers))
            {
                return(result);
            }

            MethodInfo method = null;

            //were any tags specified on publish?
            if (tags.Count > 0)
            {
                //if any, check each tag and take the first matching key
                if (tags.matching == Tags.Matching.Any)
                {
                    foreach (var t in tags.tags)
                    {
                        if (matchingHandlers.ContainsKey(t))
                        {
                            method = matchingHandlers[t];
                            break;
                        }
                    }
                }
                //if all, try to match all tags
                else if (tags.matching == Tags.Matching.All)
                {
                    if (!matchingHandlers.TryGetValue(tags.ToString(), out method))
                    {
                        return(result);
                    }
                }
            }
            else
            {
                //none specified? see if there's a default
                if (!matchingHandlers.TryGetValue(string.Empty, out method))
                {
                    return(result);
                }
            }

            if (method == null)
            {
                return(result);
            }

            if (NodeOwner == null)
            {
                return(result);
            }

            try
            {
                //also send out the publisher
                if (method.GetParameters().Length == 2)
                {
                    Type publisherType = (publisher == null ? null : publisher.GetType());

                    //only warn if the reciever method is expecting a specific type for the publisher and it doesn't match
                    if (method.GetParameters()[1].GetType() != typeof(object) && method.GetParameters()[1].GetType() != publisherType)
                    {
                        string publisherTypeName = (publisher == null ? "null" : publisher.GetType().Name);
                        DebugLogger.Log(method.Name + " parameter 2 has type " + publisherTypeName + " which does not match the method's type " + method.GetParameters()[1].GetType().Name);
                    }

                    method.Invoke(NodeOwner, new object[] { publishedData, publisher });
                }
                else
                {
                    //don't send the publisher
                    method.Invoke(NodeOwner, new object[] { publishedData });
                }
                result = true;
            }
            catch (Exception e)
            {
                string monoName = ") ";
                if (NodeOwner as UnityEngine.MonoBehaviour != null)
                {
                    monoName = ", " + (NodeOwner as UnityEngine.MonoBehaviour).gameObject.name + ") ";
                }
                else if (NodeOwner as UnityEngine.Component != null)
                {
                    monoName = ", " + (NodeOwner as UnityEngine.Component).gameObject.name + ") ";
                }
                else if (NodeOwner as UnityEngine.Object != null)
                {
                    monoName = ", " + (NodeOwner as UnityEngine.Object).name + ") ";
                }

                if (tags.Count > 0)
                {
                    monoName  = monoName.TrimEnd(')', ' ');
                    monoName += " with tags " + tags.ToString() + ") ";
                }

                string publisherName = GetObjectName(publisher);

                string errorString = "Unhandled exception caught when invoking matching callback from publisher " + publisherName + " in handler " + method.Name + " on object (" + NodeOwner.GetType().Name + monoName + ": " + e.Message + " -> " + e.InnerException.Message + " at " + e.InnerException.StackTrace;
#if UNITY_5_3_OR_NEWER
                DebugLogger.LogError(errorString);
#else
                DebugLogger.WriteLine(errorString);
#endif
            }
            return(result);
        }