Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PoolUtil.Target"/> struct.
        /// </summary>
        /// <param name='transform'>
        /// Transform that has a Targetable component
        /// </param>
        /// <param name='targetTracker'>
        /// Target tracker that detected this Target
        /// </param>
        public Target(Transform transform, TargetTracker targetTracker)
        {
            // Subtle but important difference with this constructure overload is
            //	it allows targetable to be 'null' which is used to avoid missing
            //	component exceptions in Area.
            this.gameObject = transform.gameObject;
            this.transform  = transform;

            this.targetable = transform.GetComponent <Targetable>();

            this.targetTracker = targetTracker;

            // The targetTracker arg could also be a derived type. If it is. populate more.
            // Also handle colliders to make the struct easier to use when trying to figure
            //	 out what collider triggered the OnHit event.
            this.eventTrigger = null;
            this.collider     = null;
            this.collider2D   = null;
            this.eventTrigger = targetTracker as EventTrigger;
            if (this.eventTrigger != null)
            {
                this.collider   = this.eventTrigger.coll;
                this.collider2D = this.eventTrigger.coll2D;
            }

            this.fireController = null;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Triggered when the Area detects a target. If false is returned, the
        /// target will be ignored unless detected again. Since an EventTrigger is like
        /// an expanding shockwave, if a target is fast enough to get hit, leave and then
        /// re-enter it makes sense to process it again.
        /// </summary>
        /// <param name='targetTracker'>
        /// The TargetTracker whose Area triggered the event. Since this EventTrigger
        /// IS the target tracker. This is ignored. It is for event recievers that use
        /// more than one TargetTracker.
        /// </param>
        /// <param name='target'>
        /// The target Detected
        /// </param>
        protected bool OnNewTargetHandler(TargetTracker targetTracker, Target target)
        {
            // Null detection is for direct calls rather than called-as-delegate
            if (target == Target.Null)
            {
                return(false);
            }

#if UNITY_EDITOR
            if (this.debugLevel > DEBUG_LEVELS.Normal)
            {
                string msg = string.Format("Processing detected Target: {0}", target.transform.name);
                Debug.Log(string.Format("{0}: {1}", this, msg));
            }
#endif
            var updatedTarget = new Target(target.targetable, this);

            this.detectedTargetsCache.Add(updatedTarget);

            if (!this.blockNotifications)
            {
                this.HandleNotifyTarget(target);
            }

            return(false);             // Force ignore
        }
Exemplo n.º 3
0
        protected void FilterTrackerTargetList(TargetTracker source, TargetList targets)
        {
            if (this.filterType == FILTER_TYPE.IgnoreTargetTracking)
            {
                this.FilterTargetList(targets);
            }

            // Else do nothing.
        }
Exemplo n.º 4
0
        /// <summary>
        /// Provide the same event interface as other TargetTrackers. This one is triggerd
        /// when any of its trackers are triggered.
        /// </summary>
        /// <param name='source'>
        /// The TargetTracker that triggered the current call.
        /// </param>
        /// <param name='target'>
        /// The Target that was detected and triggered the current call.
        /// </param>
        protected bool OnTrackersNew(TargetTracker source, Target target)
        {
            if (this.onNewDetectedDelegates == null)
            {
                return(true);
            }

            // If keeping the target, the post sort delegate will trigger this.dirty = true;
            return(this.onNewDetectedDelegates(this, target));
        }
Exemplo n.º 5
0
        // Init by copy
        public Target(Target otherTarget)
        {
            this.gameObject = otherTarget.gameObject;
            this.transform  = otherTarget.transform;
            this.targetable = otherTarget.targetable;

            this.targetTracker  = otherTarget.targetTracker;
            this.fireController = otherTarget.fireController;
            this.eventTrigger   = otherTarget.eventTrigger;

            this.collider   = otherTarget.collider;
            this.collider2D = otherTarget.collider2D;
        }
Exemplo n.º 6
0
        /// <summary>
        /// Triggered when a target is first found by an Area
        /// </summary>
        /// <param name="source">The TargetTracker which triggered this event</param>
        internal void OnNotDetected(TargetTracker source)
        {
#if UNITY_EDITOR
            // Higest level debug
            if (this.debugLevel > DEBUG_LEVELS.Normal)
            {
                string msg = "No longer detected by " + source.name;
                Debug.Log(string.Format("Targetable ({0}): {1}", this.name, msg));
            }
#endif
            this.trackers.Remove(source);

            if (this.onNotDetectedDelegates != null)
            {
                this.onNotDetectedDelegates(source);
            }
        }
Exemplo n.º 7
0
        protected void FilterTrackerTargetList(TargetTracker source, TargetList targets)
        {
            // Quit if the mask is set to nothing == OFF
            if (this.targetTrackerLayerMask.value == 0)
            {
                return;
            }

            Vector3 fromPos;

            if (this.tracker.area != null)
            {
                fromPos = this.tracker.area.transform.position;
            }
            else
            {
                fromPos = this.tracker.transform.position;
            }

            LayerMask mask = this.targetTrackerLayerMask;

            this.FilterTargetList(targets, mask, fromPos, Color.red);
        }
Exemplo n.º 8
0
        protected bool OnNewDetected(TargetTracker targetTracker, Target target)
        {
#if UNITY_EDITOR
            if (this.debugLevel > DEBUG_LEVELS.Normal)
            {
                string msg = string.Format
                             (
                    "Testing target '{0}' with tag '{1}' against ignore tags: '{2}'",
                    target.gameObject.name,
                    target.gameObject.tag,
                    string.Join("', '", this.ignoreList.ToArray())
                             );
                Debug.Log(string.Format("IgnoreTagModifier ({0}): {1}", this.name, msg));
            }
#endif
            for (int i = 0; i < this.ignoreList.Count; i++)
            {
                this.currentTag = this.ignoreList[i];
                if (target.gameObject.tag == this.currentTag)
                {
#if UNITY_EDITOR
                    if (this.debugLevel > DEBUG_LEVELS.Off)
                    {
                        string msg = string.Format(
                            "Ignoring target '{0}' due to tag: '{1}'",
                            target.gameObject.name,
                            this.currentTag
                            );
                        Debug.Log(string.Format("IgnoreTagModifier ({0}): {1}", this.name, msg));
                    }
#endif
                    return(false);                     // Stop looking and ignore
                }
            }

            return(true);
        }
Exemplo n.º 9
0
        protected List <Target> iterTargets = new List <Target>(); // Loop cache

        protected void Awake()
        {
            this.fireCtrl = this.GetComponent <EventFireController>();

            // If a fireController was found it is cheaper to use its cached reference to get the
            //	 TargetTracker
            if (this.fireCtrl != null)
            {
                this.tracker = this.fireCtrl.targetTracker;
            }
            else
            {
                this.tracker = this.GetComponent <TargetTracker>();
            }

            // Just in case
            if (this.fireCtrl == null && this.tracker == null)
            {
                throw new MissingComponentException
                      (
                          "Must have at least a TargetTracker or EventFireController"
                      );
            }

            // If only 1 is set, force the filterType
            if (this.fireCtrl == null || this.tracker == null)
            {
                if (this.fireCtrl != null)
                {
                    this.filterType = FILTER_TYPE.WaitToFireEvent;
                }
                else
                {
                    this.filterType = FILTER_TYPE.IgnoreTargetTracking;
                }
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PoolUtil.Target"/> struct.
        /// This is the most efficient constructor because it just stores references to
        /// caches that the Targetable already holds.
        /// </summary>
        /// <param name='targetable'>
        /// Targetable.
        /// </param>
        /// <param name='targetTracker'>
        /// Target tracker that detected the targetable.
        /// </param>
        public Target(Targetable targetable, TargetTracker targetTracker)
        {
            this.gameObject = targetable.go;
            this.transform  = targetable.transform;

            this.targetable = targetable;

            this.targetTracker = targetTracker;

            // The targetTracker arg could also be serived type. If it is. populate more.
            // Also handle colliders to make the struct easier to use when trying to figure
            //	 out what collider triggered the OnHit event.
            this.eventTrigger = null;
            this.collider     = null;
            this.collider2D   = null;
            this.eventTrigger = targetTracker as EventTrigger;
            if (this.eventTrigger != null)
            {
                this.collider   = this.eventTrigger.coll;
                this.collider2D = this.eventTrigger.coll2D;
            }

            this.fireController = null;
        }
Exemplo n.º 11
0
        protected bool OnNewDetected(TargetTracker targetTracker, Target target)
        {
#if UNITY_EDITOR
            // Print the target.gameObject's name name and list of ignore targetables
            if (this.debugLevel > DEBUG_LEVELS.Normal)
            {
                string[] names = new string[this._ignoreList.Count];
                for (int i = 0; i < this._ignoreList.Count; i++)
                {
                    names[i] = this._ignoreList[i].name;
                }

                string msg = string.Format
                             (
                    "Testing target '{0}' against ignore list: '{1}'",
                    target.gameObject.name,
                    string.Join("', '", names)
                             );
                Debug.Log(string.Format("IgnoreModifier ({0}): {1}", this.name, msg));
            }
#endif

            if (this._ignoreList.Contains(target.targetable))
            {
#if UNITY_EDITOR
                if (this.debugLevel > DEBUG_LEVELS.Off)
                {
                    string msg = string.Format("Ignoring target '{0}'", target.gameObject.name);
                    Debug.Log(string.Format("IgnoreModifier ({0}): {1}", this.name, msg));
                }
#endif
                return(false);                 // Stop looking and ignore
            }

            return(true);
        }
Exemplo n.º 12
0
        protected string currentTag;                 // Loop Cache

        protected void Awake()
        {
            this.tracker = this.GetComponent <TargetTracker>();
        }
Exemplo n.º 13
0
 protected void Awake()
 {
     this.tracker  = this.GetComponent <TargetTracker>();
     this.fireCtrl = this.GetComponent <EventFireController>();
 }
Exemplo n.º 14
0
        /// <summary>
        /// Handles all firing events including target aquisition and firing.
        /// Events are:
        ///     OnStart() :
        ///         Runs once when the firing system first becomes active
        ///     OnUpdate() :
        ///         Runs each frame while the firing system is active
        ///     OnTargetUpdate() :
        ///         Runs each frame while tracking a target (there is at least one target.)
        ///     OnIdleUpdate() :
        ///         Runs each frame while the firing system is idle (no targets)
        ///     OnFire() :
        ///         Runs when it is time to fire.
        ///
        /// Counter Behavior Notes:
        ///   * If there are no targets. the counter will keep running up.
        ///     This means the next target to enter will be fired upon
        ///     immediatly.
        ///
        ///   * The counter is always active so if the last target exits, then a
        ///     new target enters right after that, there may still be a wait.
        /// </summary>
        protected IEnumerator FiringSystem()
        {
            if (this.targetTracker == null)
            {
                this.targetTracker = this.GetComponent <TargetTracker>();
                if (this.targetTracker == null)
                {
                    // Give it a frame to see if this.targetTracker is being set by code.
                    yield return(null);

                    if (this.targetTracker == null)
                    {
                        throw new MissingComponentException
                              (
                                  "FireControllers must be on the same GameObject as a TargetTracker " +
                                  "or have it's targetTracker property set by code or drag-and-drop " +
                                  "in the inspector."
                              );
                    }
                }
            }

            // While (true) because of the timer, we want this to run all the time, not
            //   start and stop based on targets in range
            if (this.initIntervalCountdownAtZero)
            {
                this.fireIntervalCounter = 0;
            }
            else
            {
                this.fireIntervalCounter = this.interval;
            }

            this.targets.Clear();
            this.OnStart();         // EVENT TRIGGER

            this.keepFiring = true; // Can be turned off elsewhere to kill the firing system

            while (this.keepFiring)
            {
                // if there is no target, counter++, handle idle behavior, and
                //   try next frame.
                // Will init this.targets for child classes as well.
                this.targets = new TargetList(this.targetTracker.targets);

                if (this.targets.Count != 0)
                {
                    if (this.fireIntervalCounter <= 0)
                    {
                        // Let the delegate filter a copy of the list just for the OnFire
                        //   Test. We still want this.targets to remain as is.
                        //   Do this in here to still trigger OnTargetUpdate
                        this.targetsCopy.Clear();
                        this.targetsCopy.AddRange(this.targets);

                        if (targetsCopy.Count != 0 && this.onPreFireDelegates != null)
                        {
                            this.onPreFireDelegates(this.targetsCopy);
                        }

                        // If all is right, fire
                        // Check targetsCopy incase of pre-fire delegate changes
                        if (targetsCopy.Count != 0)
                        {
                            this.Fire();
                            this.fireIntervalCounter = this.interval;      // Reset
                        }
                    }

                    // Update event while tracking a target
                    this.OnTargetUpdate(this.targets);   // EVENT TRIGGER
                }
                else
                {
                    // Update event while NOT tracking a target
                    this.OnIdleUpdate();   // EVENT TRIGGER
                }

                this.fireIntervalCounter -= Time.deltaTime;

                // Update event no matter what
                this.OnUpdate();   // EVENT TRIGGER

                // Stager calls to get Target (the whole system actually)
                yield return(null);
            }

            // Wipe out the target list when stopped
            this.targets.Clear();
        }
Exemplo n.º 15
0
 /// <summary>
 /// Causes this CompoundTargetTracker to refresh its targets, sorting, event
 /// memberships, etc. when any of its trackers trigger this delegate
 /// </summary>
 protected void OnTargetsChanged(TargetTracker source)
 {
     this.batchDirty = true;
 }