Example #1
0
        /// <summary>
        /// True if the two points are in range of each other. (Based on the current goal type.)
        /// </summary>
        /// <param name="u">Point u.</param>
        /// <param name="v">Point v.</param>
        /// <returns>True if the two points are in range of each other. </returns>
        public bool IsInRange(Vector3 u, Vector3 v)
        {
            switch (data.goalType)
            {
            case RangeType.At:
                return(Vector3Util.IsInRange(u, v, data.radiusAt, data.heightTolerence));

            case RangeType.Near:
                return(Vector3Util.IsInRange(u, v, data.radiusNear, data.heightTolerence));

            default:
                // Assume most restrictive.
                return(Vector3Util.SloppyEquals(u, v, ExactTolerance));
            }
        }
Example #2
0
        //管理目标位置,有比较的话需要对这个目标进行变更
        private bool HandleNormalPlanning(bool hasNewGoal)
        {
            if (hasNewGoal || !mGoalInRange && theAgent.IsNear(theAgent.desiredPosition.point, mTarget.point))
            {
                //在不变更移动计划的时候,看看能不能走
                if (!SetLocalTarget())
                {
                    if (theAgent.PlanPath(theAgent.desiredPosition, theAgent.plannerGoal) <= 0)
                    {
                        Debug.LogError(string.Format("{0}: Path replan failed. Position: {1}, Goal: {2}"
                                                     , theAgent.transform.name, theAgent.desiredPosition.ToString(), theAgent.plannerGoal.ToString()));
                        return(false);
                    }

                    return(SetLocalTarget());
                }
            }
            else if (mGoalInRange)
            {
                //这个方法根据CritterAI原有的架构来做的,由于这个部分已经被魔改,所以此处可能需要很慎重处理
                if (mAtGoalState == StateNormal)
                {
                    if (Vector3Util.IsInRange(theAgent.desiredPosition.point
                                              , theAgent.plannerGoal.point, 0.12f, theAgent.heightTolerence))
                    {
                        mAtGoalState = StateInRange;
                        theAgent.RemoveFromCrowd();

                        CrowdAgentParams config = theAgent.crowdConfig;
                        config.maxSpeed = 0;

                        theAgent.crowdAgent = theAgent.navGroup.crowd.AddAgent(theAgent.plannerGoal.point, config);

                        theAgent.flags &= ~NavFlag.CrowdConfigUpdated;
                    }
                }
            }
            return(true);
        }
Example #3
0
 /// <summary>
 /// True if the two points are near each other. (Less accurate check.)
 /// </summary>
 /// <param name="u">Point u.</param>
 /// <param name="v">Point v.</param>
 /// <returns>True if the two points are near each other. </returns>
 public bool IsNear(Vector3 u, Vector3 v)
 {
     return(Vector3Util.IsInRange(u, v, data.radiusNear, data.heightTolerence));
 }
Example #4
0
 //判断两个点是否相近,调用底层来做的
 public bool IsNear(Vector3 PointA, Vector3 pointB)
 {
     return(Vector3Util.IsInRange(PointA, pointB, radiusNear, heightTolerence));
 }
Example #5
0
        /// <summary>
        /// Manages the crowd agent target, replanning the long distance path if needed.
        /// </summary>
        /// <returns>False on critical failure.</returns>
        private bool HandleNormalPlanning(bool hasNewGoal)
        {
            // Re-targetting will only occur if there is a new goal or getting close to an
            // intermediate target.

            if (hasNewGoal ||
                !mGoalInRange && agent.IsNear(agent.data.desiredPosition.point, mTarget.point))
            {
                // First try to set the target without replanning.
                if (!SetLocalTarget())
                {
                    // This can happen when approaching the target from
                    // outside the planned path, which can happen when
                    // the crowd manager takes a different path.
                    // (Or a new goal was set.)

                    if (agent.PlanPath(agent.data.desiredPosition, agent.data.plannerGoal) <= 0)
                    {
                        // Critical failure.
                        Debug.LogError(string.Format(
                                           "{0}: Path replan failed. Position: {1}, Goal: {2}"
                                           , agent.transform.name
                                           , agent.data.desiredPosition.ToString()
                                           , agent.data.plannerGoal.ToString()));
                        return(false);
                    }

                    // Next call should not not fail since the path is known to be good.
                    return(SetLocalTarget());
                }
            }
            else if (mGoalInRange)
            {
                /*
                 * Need to monitor to see if the goal has been reached.  This can be tricky.
                 * The optimal 'at' and 'near' range ranges are usually too restrictive and too
                 * large respectively. So a fixed value, based on experience, is used instead.
                 * (OK to make this value configurable.)
                 *
                 * Assumption: Other parts of the code detect if a re-evaluation of position
                 * is needed.
                 */
                if (mAtGoalState == StateNormal)
                {
                    if (Vector3Util.IsInRange(agent.data.desiredPosition.point
                                              , agent.data.plannerGoal.point
                                              , 0.12f, agent.data.heightTolerence))
                    {
                        // Remove from the crowd and add back, exactly at the goal with a zero max
                        // speed.  Basically, puts a static obstacle into the crowd.

                        mAtGoalState = StateInRange;
                        agent.RemoveFromCrowd();

                        CrowdAgentParams config = agent.crowdConfig;
                        config.maxSpeed = 0;

                        agent.crowdAgent =
                            agent.navGroup.crowd.AddAgent(agent.data.plannerGoal.point, config);

                        agent.data.flags &= ~NavFlag.CrowdConfigUpdated;

                        // Debug.Log("Transition to 'at goal' task.");
                    }
                }
            }
            return(true);
        }