예제 #1
0
        protected void FilterTargetList(TargetList targets)
        {
#if UNITY_EDITOR
            var debugRemoveNames = new List <string>();
#endif
            this.iterTargets.Clear();
            this.iterTargets.AddRange(targets);
            for (int i = 0; i < iterTargets.Count; i++)
            {
                this.currentTarget = iterTargets[i];

                if (this.IsInAngle(this.currentTarget))
                {
                    continue;  // This target is good. Don't ignore it. Continue.
                }
#if UNITY_EDITOR
                debugRemoveNames.Add(this.currentTarget.targetable.name);
#endif
                targets.Remove(this.currentTarget);
            }

#if UNITY_EDITOR
            if (this.debugLevel == DEBUG_LEVELS.High && debugRemoveNames.Count > 0)
            {
                string msg = string.Format
                             (
                    "Holding fire due to misalignment: {0}",
                    string.Join(", ", debugRemoveNames.ToArray())
                             );

                Debug.Log(string.Format("{0}: {1}", this, msg));
            }
#endif
        }
예제 #2
0
        protected void FilterTargetList(TargetList targets, LayerMask mask, Vector3 fromPos,
                                        Color debugLineColor)
        {
#if UNITY_EDITOR
            var debugRemoveNames = new List <string>();
#endif

            Vector3 toPos;
            bool    isNotLOS;
            var     iterTargets = new List <Target>(targets);

            Collider2D targetColl2D;
            Collider   targetColl;
            Vector3    ext;
            bool       use2d;
            foreach (Target target in iterTargets)
            {
                use2d    = target.targetable.coll2D != null;
                isNotLOS = false;

                if (this.testMode == TEST_MODE.BoundingBox)
                {
                    if (use2d)
                    {
                        targetColl2D = target.targetable.coll2D;
                        ext          = targetColl2D.bounds.extents * 0.5f;
                    }
                    else
                    {
                        targetColl = target.targetable.coll;
                        ext        = targetColl.bounds.extents * 0.5f;
                    }

                    // This solution works with rotation pretty well
                    Matrix4x4 mtx = target.targetable.transform.localToWorldMatrix;

                    var bboxPnts = new Vector3[8];
                    bboxPnts[0] = mtx.MultiplyPoint3x4(ext);
                    bboxPnts[1] = mtx.MultiplyPoint3x4(new Vector3(-ext.x, ext.y, ext.z));
                    bboxPnts[2] = mtx.MultiplyPoint3x4(new Vector3(ext.x, ext.y, -ext.z));
                    bboxPnts[3] = mtx.MultiplyPoint3x4(new Vector3(-ext.x, ext.y, -ext.z));
                    bboxPnts[4] = mtx.MultiplyPoint3x4(new Vector3(ext.x, -ext.y, ext.z));
                    bboxPnts[5] = mtx.MultiplyPoint3x4(new Vector3(-ext.x, -ext.y, ext.z));
                    bboxPnts[6] = mtx.MultiplyPoint3x4(new Vector3(ext.x, -ext.y, -ext.z));
                    bboxPnts[7] = mtx.MultiplyPoint3x4(-ext);

                    for (int i = 0; i < bboxPnts.Length; i++)
                    {
                        if (use2d)
                        {
                            isNotLOS = Physics2D.Linecast(fromPos, bboxPnts[i], mask);
                        }
                        else
                        {
                            isNotLOS = Physics.Linecast(fromPos, bboxPnts[i], mask);
                        }

                        // Quit loop at first positive test
                        if (isNotLOS)
                        {
#if UNITY_EDITOR
                            if (this.debugLevel > DEBUG_LEVELS.Off)
                            {
                                Debug.DrawLine(fromPos, bboxPnts[i], debugLineColor, 0.01f);
                            }
#endif
                            continue;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                else
                {
                    toPos = target.targetable.transform.position;
                    if (use2d)
                    {
                        isNotLOS = Physics2D.Linecast(fromPos, toPos, mask);
                    }
                    else
                    {
                        isNotLOS = Physics.Linecast(fromPos, toPos, mask);
                    }

#if UNITY_EDITOR
                    if (isNotLOS && this.debugLevel > DEBUG_LEVELS.Off)
                    {
                        Debug.DrawLine(fromPos, toPos, debugLineColor, 0.01f);
                    }
#endif
                }

                if (isNotLOS)
                {
                    targets.Remove(target);

#if UNITY_EDITOR
                    debugRemoveNames.Add(target.targetable.name);
#endif
                }
            }

#if UNITY_EDITOR
            if (this.debugLevel == DEBUG_LEVELS.High && debugRemoveNames.Count > 0)
            {
                Debug.Log("Holding fire for LOS: " +
                          string.Join(",", debugRemoveNames.ToArray()));
            }
#endif
        }
예제 #3
0
        protected void FilterFireTargetList(TargetList targets)
        {
            // Get the position expected to be used to fire from.
            Vector3 fromPos = this.origin.position;

#if UNITY_EDITOR
            var debugRemoveNames = new List <string>();
#endif
            this.iterTargets.Clear();
            this.iterTargets.AddRange(targets);
            float dist;
            for (int i = 0; i < iterTargets.Count; i++)
            {
                this.currentTarget = iterTargets[i];
                dist = this.currentTarget.targetable.GetDistToPos(fromPos);

                // Skip if the target is in the distance range
                if (dist > this.minDistance && dist < this.maxDistance)
                {
#if UNITY_EDITOR
                    if (this.debugLevel > DEBUG_LEVELS.Off)
                    {
                        Debug.DrawLine
                        (
                            fromPos,
                            this.currentTarget.targetable.transform.position,
                            Color.green,
                            0.01f
                        );
                    }
#endif
                    continue;
                }

#if UNITY_EDITOR
                if (this.debugLevel > DEBUG_LEVELS.Off)
                {
                    Debug.DrawLine
                    (
                        fromPos,
                        this.currentTarget.targetable.transform.position,
                        Color.red,
                        0.01f
                    );
                }
#endif
                targets.Remove(this.currentTarget);

#if UNITY_EDITOR
                debugRemoveNames.Add(this.currentTarget.targetable.name);
#endif
            }

#if UNITY_EDITOR
            if (this.debugLevel == DEBUG_LEVELS.High && debugRemoveNames.Count > 0)
            {
                string msg = string.Format
                             (
                    "Holding fire due to distance: {0}",
                    string.Join(", ", debugRemoveNames.ToArray())
                             );

                Debug.Log(string.Format("{0}: {1}", this, msg));
            }
#endif
        }