Exemplo n.º 1
0
        /// <summary>
        /// Direct this entity to move towards another entity using given speed.
        /// </summary>
        /// <param name="goal"></param>
        /// <param name="withSpeed"></param>
        public void VelocityTowardsClosestEntity(IEnumerable <Entity> entities, double?withSpeed = null)
        {
            Entity goal = null;
            var    minDistanceSquared = double.MaxValue;

            foreach (var e in entities)
            {
                var currentDistanceSquared = CollisionCheck.DistanceSquared(e.Xpos, e.Ypos, Xpos, Ypos);
                if (currentDistanceSquared < minDistanceSquared)
                {
                    goal = e;
                    minDistanceSquared = currentDistanceSquared;
                }
            }
            VelocityTowards(goal, withSpeed);
        }