Beispiel #1
0
        private void SetNextTarget()
        {
            Vector3D?prevTarget = TargetWorld;

            if (m_path == null || !m_path.IsValid)
            {
                UnsetTarget();
                return;
            }

            IMyDestinationShape dest         = m_path.Destination;
            Vector3D            closestPoint = dest.GetClosestPoint(CapsuleCenter());
            double distSq = TargetDistanceSq(ref closestPoint);

            // TODO: Get rid of the ad-hoc numbers and use goal shapes

            if (distSq > END_RADIUS * END_RADIUS)
            {
                float    radius;
                Vector3D targetWorld;
                MyEntity relativeEntity;

                Vector3D currentParentPosition = Parent.PositionAndOrientation.Translation;
                if (m_path.PathCompleted)
                {
                    if (distSq < DISTANCE_FOR_FINAL_APPROACH * DISTANCE_FOR_FINAL_APPROACH)
                    {
                        var endEntity = m_path.EndEntity;
                        UnsetPath();
                        SetTarget(closestPoint, END_RADIUS, endEntity, m_weight);
                        return;
                    }
                    else
                    {
                        if (prevTarget.HasValue)
                        {
                            m_path.Reinit(prevTarget.Value);
                        }
                        else
                        {
                            m_path.Reinit(currentParentPosition);
                        }
                    }
                }

                ProfilerShort.Begin("MySmartPath.GetNextTarget");
                MyPathfindingStopwatch.Start();
                bool result = m_path.GetNextTarget(Parent.PositionAndOrientation.Translation, out targetWorld, out radius, out relativeEntity);
                MyPathfindingStopwatch.Stop();
                ProfilerShort.End();

                if (result)
                {
                    SetTarget(targetWorld, radius, relativeEntity, m_weight);
                    return;
                }
            }

            UnsetPath();
        }