Пример #1
0
        public override int SenseAll <T>(ICollection <T> results, System.Func <T, bool> p = null)
        {
            //System.Func<T, bool> p2;
            //if (p == null)
            //    p2 = (a) => this.Visible(a);
            //else
            //    p2 = (a) => this.Visible(a) && p(a);

            //return VisualAspect.Pool.FindAll<T>(lst, p2);

            int cnt    = 0;
            var sphere = this.GetBoundingSphere();

            using (var lst = TempCollection.GetList <IAspect>())
            {
                if (VisualAspect.GetNearby(lst, sphere.position, sphere.radius) > 0)
                {
                    for (int i = 0; i < lst.Count; i++)
                    {
                        if (lst[i] is T && this.Visible(lst[i]))
                        {
                            if (p != null && !p(lst[i] as T))
                            {
                                continue;
                            }
                            results.Add(lst[i] as T);
                            cnt++;
                        }
                    }
                }
            }
            return(cnt);
        }
Пример #2
0
        private bool ConcernedWith(VisualAspect vaspect)
        {
            if (vaspect == null)
            {
                return(false);
            }
            if (!vaspect.isActiveAndEnabled)
            {
                return(false);
            }
            if (_aspectLayerMask != -1 && !vaspect.gameObject.IntersectsLayerMask(_aspectLayerMask))
            {
                return(false);
            }
            if (!_aspectTagMask.Intersects(vaspect))
            {
                return(false);
            }
            if (!_canDetectSelf && vaspect.entityRoot == this.entityRoot)
            {
                return(false);
            }

            return(true);
        }
Пример #3
0
        public override IEnumerable <IAspect> SenseAll(System.Func <IAspect, bool> p = null)
        {
            //p = this.GetPredicate(p);
            //foreach(var a in VisualAspect.Pool)
            //{
            //    if (p(a)) yield return a;
            //}

            var sphere = this.GetBoundingSphere();

            using (var lst = TempCollection.GetList <IAspect>())
            {
                if (VisualAspect.GetNearby(lst, sphere.position, sphere.radius) > 0)
                {
                    p = this.GetPredicate(p);
                    for (int i = 0; i < lst.Count; i++)
                    {
                        if (p(lst[i]))
                        {
                            yield return(lst[i]);
                        }
                    }
                }
            }
        }
Пример #4
0
        public override int SenseAll(ICollection <IAspect> results, System.Func <IAspect, bool> p = null)
        {
            //p = this.GetPredicate(p);
            //return VisualAspect.Pool.FindAll(results, p);

            int cnt    = 0;
            var sphere = this.GetBoundingSphere();

            using (var lst = TempCollection.GetList <IAspect>())
            {
                if (VisualAspect.GetNearby(lst, sphere.position, sphere.radius) > 0)
                {
                    p = this.GetPredicate(p);
                    for (int i = 0; i < lst.Count; i++)
                    {
                        if (p(lst[i]))
                        {
                            results.Add(lst[i]);
                            cnt++;
                        }
                    }
                }
            }
            return(cnt);
        }
Пример #5
0
        protected override bool TestVisibility(VisualAspect aspect)
        {
            if (this.RequiresLineOfSight)
            {
                var v = aspect.transform.position - this.transform.position;
                //RaycastHit[] hits = Physics.RaycastAll(this.transform.position, v, v.magnitude, this.LineOfSightMask);
                //foreach (var hit in hits)
                //{
                //    //we ignore ourself
                //    var r = hit.collider.FindRoot();
                //    if (r != aspect.entityRoot && r != this.entityRoot) return false;
                //}
                using (var lst = com.spacepuppy.Collections.TempCollection.GetList <RaycastHit>())
                {
                    int cnt = PhysicsUtil.RaycastAll(this.transform.position, v, lst, v.magnitude, this.LineOfSightMask);
                    for (int i = 0; i < cnt; i++)
                    {
                        //we ignore ourself
                        var r = lst[i].collider.FindRoot();
                        if (r != aspect.entityRoot && r != this.entityRoot)
                        {
                            return(false);
                        }
                    }
                }
            }

            return(true);
        }
Пример #6
0
        protected override bool TestVisibility(VisualAspect aspect)
        {
            //if not in cylinder, can not see it
            var halfHeight = _height / 2.0f;
            var rod        = this.Rod;
            var center     = this.GetCenterInWorldSpace();
            var otherPos   = aspect.transform.position;

            if (!Cylinder.ContainsPoint(center - (rod * halfHeight),
                                        center + (rod * halfHeight),
                                        _radius,
                                        _innerRadius,
                                        otherPos))
            {
                return(false);
            }

            if (this._angle < 360.0f)
            {
                var v = VectorUtil.SetLengthOnAxis(otherPos - center, rod, 0f);
                var a = Vector3.Angle(this.transform.forward, v);
                if (a > this._angle / 2.0f)
                {
                    return(false);
                }
            }

            if (this.RequiresLineOfSight)
            {
                var v = otherPos - center;
                //RaycastHit[] hits = Physics.RaycastAll(this.transform.position, v, v.magnitude, this.LineOfSightMask);
                //foreach (var hit in hits)
                //{
                //    //we ignore ourself
                //    var r = hit.collider.FindRoot();
                //    if (r != aspect.entityRoot && r != this.entityRoot) return false;
                //}
                using (var lst = com.spacepuppy.Collections.TempCollection.GetList <RaycastHit>())
                {
                    int cnt = PhysicsUtil.RaycastAll(this.transform.position, v, lst, v.magnitude, this.LineOfSightMask);
                    for (int i = 0; i < cnt; i++)
                    {
                        //we ignore ourself
                        var r = lst[i].collider.FindRoot();
                        if (r != aspect.entityRoot && r != this.entityRoot)
                        {
                            return(false);
                        }
                    }
                }
            }

            return(true);
        }
Пример #7
0
        protected override bool TestVisibility(VisualAspect aspect)
        {
            //if not in cylinder, can not see it
            float aspRad = aspect.Radius;

            if (aspRad > MathUtil.EPSILON)
            {
                if (!Cone.ContainsSphere(this.transform.position,
                                         this.transform.position + this.Direction * _range,
                                         _startRadius,
                                         _endRadius,
                                         aspect.transform.position,
                                         aspRad))
                {
                    return(false);
                }
            }
            else
            {
                if (!Cone.ContainsPoint(this.transform.position,
                                        this.transform.position + this.Direction * _range,
                                        _startRadius,
                                        _endRadius,
                                        aspect.transform.position))
                {
                    return(false);
                }
            }

            if (this.RequiresLineOfSight)
            {
                var v = aspect.transform.position - this.transform.position;
                using (var lst = com.spacepuppy.Collections.TempCollection.GetList <RaycastHit>())
                {
                    int cnt = PhysicsUtil.RaycastAll(this.transform.position, v, lst, v.magnitude, this.LineOfSightMask);
                    for (int i = 0; i < cnt; i++)
                    {
                        //we ignore ourself
                        var r = lst[i].collider.FindRoot();
                        if (r != aspect.entityRoot && r != this.entityRoot)
                        {
                            return(false);
                        }
                    }
                }
            }

            return(true);
        }
Пример #8
0
        public override bool SenseAny(System.Func <IAspect, bool> p = null)
        {
            //return VisualAspect.Pool.Any(this.GetPredicate(p));

            var sphere = this.GetBoundingSphere();

            using (var lst = TempCollection.GetList <IAspect>())
            {
                if (VisualAspect.GetNearby(lst, sphere.position, sphere.radius) > 0)
                {
                    p = this.GetPredicate(p);
                    for (int i = 0; i < lst.Count; i++)
                    {
                        if (p(lst[i]))
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
Пример #9
0
 protected abstract bool TestVisibility(VisualAspect aspect);
Пример #10
0
 public override bool SenseAny(System.Func <IAspect, bool> p = null)
 {
     return(VisualAspect.Any(this.GetPredicate(p)));
 }
Пример #11
0
        protected override bool TestVisibility(VisualAspect aspect)
        {
            var sqrRadius = _radius * _radius;
            var v         = aspect.transform.position - this.transform.position;

            if (v.sqrMagnitude > sqrRadius)
            {
                return(false);
            }
            if (this._innerRadius > 0f && v.sqrMagnitude < this._innerRadius * this._innerRadius)
            {
                return(false);
            }

            if (this._horizontalAngle != 360.0f && this._verticalAngle != 360.0f)
            {
                Vector3 directionOfAspectInLocalSpace = this.transform.InverseTransformDirection(v); //Quaternion.Inverse(this.transform.rotation) * v;
                //var lookAtAngles = (VectorUtil.NearZeroVector(directionOfAspectInLocalSpace)) ? Vector3.zero : Quaternion.LookRotation(directionOfAspectInLocalSpace).eulerAngles;
                //if (Mathf.Abs(lookAtAngles.y * 2.0f) > this._horizontalAngle)
                //    return false;
                //else if (Mathf.Abs(lookAtAngles.x * 2.0f) > this._verticalAngle)
                //    return false;

                float a;
                a = VectorUtil.AngleBetween(new Vector2(1f, 0f), new Vector2(directionOfAspectInLocalSpace.z, directionOfAspectInLocalSpace.x));
                if (a > this._horizontalAngle)
                {
                    return(false);
                }
                a = VectorUtil.AngleBetween(new Vector2(1f, 0f), new Vector2(directionOfAspectInLocalSpace.z, directionOfAspectInLocalSpace.y));
                if (a > this._verticalAngle)
                {
                    return(false);
                }
            }

            if (this.RequiresLineOfSight)
            {
                //RaycastHit[] hits = Physics.RaycastAll(this.transform.position, v, v.magnitude, this.LineOfSightMask);
                //foreach(var hit in hits)
                //{
                //    //we ignore ourself
                //    var r = hit.collider.FindRoot();
                //    if (r != aspect.entityRoot && r != this.entityRoot) return false;
                //}
                using (var lst = com.spacepuppy.Collections.TempCollection.GetList <RaycastHit>())
                {
                    int cnt = PhysicsUtil.RaycastAll(this.transform.position, v, lst, v.magnitude, this.LineOfSightMask);
                    for (int i = 0; i < cnt; i++)
                    {
                        //we ignore ourself
                        var r = lst[i].collider.FindRoot();
                        if (r != aspect.entityRoot && r != this.entityRoot)
                        {
                            return(false);
                        }
                    }
                }
            }

            return(true);
        }
        protected override bool TestVisibility(VisualAspect aspect)
        {
            //if not in cylinder, can not see it
            var halfHeight = _height / 2.0f;
            var rod        = this.Rod;
            var center     = this.GetCenterInWorldSpace();
            var otherPos   = aspect.transform.position;

            float aspRad = aspect.Radius;

            if (aspRad > MathUtil.EPSILON)
            {
                if (!Cylinder.ContainsSphere(center - (rod * halfHeight),
                                             center + (rod * halfHeight),
                                             _radius,
                                             _innerRadius,
                                             otherPos,
                                             aspRad))
                {
                    return(false);
                }

                if (this._angle < 360.0f)
                {
                    var   v = VectorUtil.SetLengthOnAxis(otherPos - center, rod, 0f);
                    var   a = Vector3.Angle(this.transform.forward, v);
                    float k = 2f * Mathf.Asin(aspRad / (Mathf.Sqrt(v.sqrMagnitude + (aspRad * aspRad) / 4f))) * Mathf.Rad2Deg;
                    if (a > (_angle / 2.0f) - k)
                    {
                        return(false);
                    }
                }
            }
            else
            {
                if (!Cylinder.ContainsPoint(center - (rod * halfHeight),
                                            center + (rod * halfHeight),
                                            _radius,
                                            _innerRadius,
                                            otherPos))
                {
                    return(false);
                }

                if (this._angle < 360.0f)
                {
                    var v = VectorUtil.SetLengthOnAxis(otherPos - center, rod, 0f);
                    var a = Vector3.Angle(this.transform.forward, v);
                    if (a > this._angle / 2.0f)
                    {
                        return(false);
                    }
                }
            }

            if (this.RequiresLineOfSight)
            {
                var v = otherPos - center;
                using (var lst = com.spacepuppy.Collections.TempCollection.GetList <RaycastHit>())
                {
                    int cnt = PhysicsUtil.RaycastAll(this.transform.position, v, lst, v.magnitude, this.LineOfSightMask);
                    for (int i = 0; i < cnt; i++)
                    {
                        //we ignore ourself
                        var r = lst[i].collider.FindRoot();
                        if (r != aspect.entityRoot && r != this.entityRoot)
                        {
                            return(false);
                        }
                    }
                }
            }

            return(true);
        }
Пример #13
0
        protected override bool TestVisibility(VisualAspect aspect)
        {
            float   aspRad    = aspect.Radius;
            float   sqrRadius = _radius * _radius;
            Vector3 v         = aspect.transform.position - this.transform.position;
            float   sqrDist   = v.sqrMagnitude;

            if (sqrDist - (aspRad * aspRad) > sqrRadius)
            {
                return(false);
            }
            if (this._innerRadius > aspRad && sqrDist < this._innerRadius * this._innerRadius)
            {
                return(false);
            }

            if (this._horizontalAngle < 360.0f && this._verticalAngle < 360.0f)
            {
                Vector3 directionOfAspectInLocalSpace = this.transform.InverseTransformDirection(v); //Quaternion.Inverse(this.transform.rotation) * v;
                float   a;
                if (aspRad > MathUtil.EPSILON)
                {
                    float k = 2f * Mathf.Asin(aspRad / (Mathf.Sqrt(sqrDist + (aspRad * aspRad) / 4f))) * Mathf.Rad2Deg;
                    a = VectorUtil.AngleBetween(new Vector2(1f, 0f), new Vector2(directionOfAspectInLocalSpace.z, directionOfAspectInLocalSpace.x));

                    if (a > (this._horizontalAngle / 2f) - k)
                    {
                        return(false);
                    }
                    a = VectorUtil.AngleBetween(new Vector2(1f, 0f), new Vector2(directionOfAspectInLocalSpace.z, directionOfAspectInLocalSpace.y));
                    if (a > (this._verticalAngle / 2f) - k)
                    {
                        return(false);
                    }
                }
                else
                {
                    a = VectorUtil.AngleBetween(new Vector2(1f, 0f), new Vector2(directionOfAspectInLocalSpace.z, directionOfAspectInLocalSpace.x));
                    if (a > this._horizontalAngle / 2f)
                    {
                        return(false);
                    }
                    a = VectorUtil.AngleBetween(new Vector2(1f, 0f), new Vector2(directionOfAspectInLocalSpace.z, directionOfAspectInLocalSpace.y));
                    if (a > this._verticalAngle / 2f)
                    {
                        return(false);
                    }
                }
            }

            if (this.RequiresLineOfSight)
            {
                using (var lst = com.spacepuppy.Collections.TempCollection.GetList <RaycastHit>())
                {
                    int cnt = PhysicsUtil.RaycastAll(this.transform.position, v, lst, v.magnitude, this.LineOfSightMask);
                    for (int i = 0; i < cnt; i++)
                    {
                        //we ignore ourself
                        var r = lst[i].collider.FindRoot();
                        if (r != aspect.entityRoot && r != this.entityRoot)
                        {
                            return(false);
                        }
                    }
                }
            }

            return(true);
        }