Example #1
0
        private IEntity FindBestTarget()
        {
            // "best" target is the closest one with LOS

            var ents        = _entMan.GetEntitiesInRange(SelfEntity, VisionRadius);
            var myTransform = SelfEntity.GetComponent <IServerTransformComponent>();
            var maxRayLen   = VisionRadius * 2.5f; // circle inscribed in square, square diagonal = 2*r*sqrt(2)

            _workList.Clear();
            foreach (var entity in ents)
            {
                // filter to "people" entities (entities with controllers)
                if (!entity.HasComponent <IMoverComponent>())
                {
                    continue;
                }

                // build the ray
                var dir = entity.GetComponent <TransformComponent>().WorldPosition - myTransform.WorldPosition;
                var ray = new Ray(myTransform.WorldPosition, dir.Normalized);

                // cast the ray
                var result = _physMan.IntersectRay(ray, maxRayLen);

                // add to visible list
                if (result.HitEntity == entity)
                {
                    _workList.Add(entity);
                }
            }

            // get closest entity in list
            var closestEnt = GetClosest(myTransform.WorldPosition, _workList);

            // return closest
            return(closestEnt);
        }