Пример #1
0
    private void UpdateWaypoint()
    {
        PathNode targetNode = path[path.Count - 1];

        float distance = Vector3.Distance(unit.transform.position, PathfinderData.Position(targetNode));

        if (distance < (path.Count > 1 ? CompletionDist : finalCompletionDist))   // Unit arrived at the next path node
        {
            path.RemoveAt(path.Count - 1);
            if (!HasDestination())   // Unit arrived at the destination
            {
                waypoint = NoPosition;
                return;
            }
            else
            {
                previousNode = targetNode;
                targetNode   = path[path.Count - 1];
            }
        }

        Vector3 targetPosition = PathfinderData.Position(targetNode);

        //Debug.Log(targetNode.isRoad + (previousNode != null ? " "+previousNode.isRoad : ""));
        if (targetNode.isRoad && previousNode != null && previousNode.isRoad)
        {
            targetPosition = GetRoadIntersection(PathfinderData.Position(targetNode), PathfinderData.Position(previousNode), unit.transform.position);
        }

        Vector3 newWaypoint = TakeStep(
            data, unit.transform.position, targetPosition, unit.Data.mobility, unit.Data.radius);

        if (newWaypoint != NoPosition)
        {
            waypoint = straightStep ? targetPosition : newWaypoint;
        }
        else
        {
            // The unit has gotten stuck when following the previously computed path.
            // Now recompute a new path to the destination using the global graph, this time using finite radius
            float pathTime = data.FindPath(path, unit.transform.position, PathfinderData.Position(path[0]), unit.Data.mobility, unit.Data.radius, command);
            //float pathTime = SetPath (path[0].position, command);

            if (pathTime == Forever)    // The unit has somehow gotten itself trapped
            {
                Debug.Log("I am stuck!!!");
                waypoint = NoPosition;
            }
            else
            {
                // If this is an intermediate step of the path, then the pre-computed global graph might
                // be broken and the corresponding arc should be recomputed to avoid having units cycle forever
                if (previousNode != null && path.Count > 1)
                {
                    data.RemoveArc(previousNode, targetNode);
                    data.AddArc(previousNode, targetNode);
                }
            }
        }
    }
Пример #2
0
    // Build a path in an approximately straight line from start to destination by stringing together steps
    // This is NOT guaranteed to not get stuck in a local terrain feature
    // Returns the total normalized path time, or 'forever' if stuck
    public static float FindLocalPath(
        PathfinderData data,
        Vector3 start, Vector3 destination,
        MobilityType mobility,
        float radius)
    {
        float   distance = (destination - start).magnitude;
        Vector3 waypoint = start;
        float   time     = 0f;

        while (distance > CompletionDist)
        {
            Vector3 previous = waypoint;
            waypoint = TakeStep(data, waypoint, destination, mobility, radius);
            if (waypoint == NoPosition)
            {
                return(Forever);
            }
            time    += StepSize / mobility.GetUnitSpeed(data.terrain, data.map, waypoint, radius, (waypoint - previous).normalized);
            distance = (destination - waypoint).magnitude;
        }
        time += distance / mobility.GetUnitSpeed(data.terrain, data.map, waypoint, radius, (destination - waypoint).normalized);

        return(time);
    }
Пример #3
0
        public void Awake()
        {
            var blueTeam = GameObject.Find("Team_Blue").GetComponent <Team>();
            var redTeam  = GameObject.Find("Team_Red").GetComponent <Team>();

            blueTeam.AddPlayer(this);
            redTeam.AddPlayer(this);

            Teams.Add(blueTeam);
            Teams.Add(redTeam);

            LocalPlayer      = gameObject.AddComponent <PlayerBehaviour>();
            LocalPlayer.Data = redTeam.Players[0];

            GameObject.Find("Managers").GetComponent <DeploymentMenu>().LocalPlayer = LocalPlayer;

            _inputManager = FindObjectOfType <InputManager>() ??
                            gameObject.AddComponent <InputManager>();

            _visibilityManager = FindObjectOfType <VisibilityManager>() ??
                                 gameObject.AddComponent <VisibilityManager>();

            _inputManager.Session        = _inputManager.Session ?? this;
            _visibilityManager.LocalTeam = _visibilityManager.LocalTeam ?? LocalPlayer.Data.Team;

            // TODO: Pass terrain from future location of starting matches (no Find)
            PathData = new PathfinderData(GameObject.Find("Terrain").GetComponent <Terrain>());
            Factory  = new UnitFactory(this);
            Settings = new Settings();
        }
Пример #4
0
 public Pathfinder(UnitBehaviour unit, PathfinderData data)
 {
     this.unit           = unit;
     this.data           = data;
     path                = new List <PathNode>();
     finalCompletionDist = 5f * TerrainConstants.MAP_SCALE + unit.Data.minTurnRadius;
     nextUpdateTime      = 0f;
 }
Пример #5
0
        //private Loading _loader;

        // Start is called before the first frame update
        private void Start()
        {
            DontDestroyOnLoad(this.gameObject);

            Armory = ConfigReader.ParseArmory();

            Terrain[] terrains = GameObject.FindObjectsOfType <Terrain>();

            TerrainData = new TerrainMap(terrains, SceneBuildId);

            PathFinderData = new PathfinderData(
                TerrainData, Armory.UniqueMobilityTypes, SceneBuildId);
        }
Пример #6
0
        //private Loading _loader;

        // Start is called before the first frame update
        private void Start()
        {
            DontDestroyOnLoad(this.gameObject);
            SceneManager.sceneLoaded += OnSceneLoaded;

            Terrain[] terrains = GameObject.FindObjectsOfType <Terrain>();

            TerrainData = new TerrainMap(terrains, SceneBuildId);

            PathFinderData = new PathfinderData(
                TerrainData,
                GameSession.Singleton.Armory.UniqueMobilityTypes,
                SceneBuildId);
        }
Пример #7
0
        //private Loading _loader;

        // Start is called before the first frame update
        private void Start()
        {
            DontDestroyOnLoad(this.gameObject);

            Terrain[] terrains = GameObject.FindObjectsOfType <Terrain>();

            foreach (Terrain terrain in terrains)
            {
                MicroSplatTerrain msTerrain = terrain.GetComponent <MicroSplatTerrain>();
                msTerrain.Sync();
            }

            terrainData = new TerrainMap(terrains, scene);

            pathFinderData = new PathfinderData(terrainData);
        }
Пример #8
0
    // Finds an intermediate step along the way from start to destination
    // Returns 'NoPosition' if stuck
    private static Vector3 TakeStep(
        PathfinderData data,
        Vector3 start, Vector3 destination,
        MobilityType mobility,
        float radius)
    {
        Vector3 straight = (destination - start).normalized;

        straightStep = false;

        // Fan out in a two-point horizontal pattern to find a way forward
        for (float ang1 = 0f; ang1 <= MaxAngle; ang1 += AngSearchInc)
        {
            for (int direction = -1; direction <= 1; direction += 2)
            {
                Vector3 direction1 = ang1 > 0f ? Quaternion.AngleAxis(ang1 * direction, Vector3.up) * straight : straight;
                Vector3 midpoint   = start + direction1 * StepSize;
                float   midspeed   = mobility.GetUnitSpeed(data.terrain, data.map, midpoint, radius, direction1);

                if (midspeed > 0f)
                {
                    for (float ang2 = 0f; ang2 <= ang1; ang2 += AngSearchInc)
                    {
                        Vector3 direction2 = ang2 > 0f ? Quaternion.AngleAxis(ang2 * direction, Vector3.up) * straight : straight;
                        Vector3 endpoint   = midpoint + straight * StepSize;
                        float   endspeed   = mobility.GetUnitSpeed(data.terrain, data.map, endpoint, radius, direction2);

                        if (endspeed > 0f)
                        {
                            straightStep = ang1 == 0f && ang2 == 0f;
                            return(straightStep ? endpoint : midpoint);
                        }
                    }
                }
            }
        }

        // No step was found
        return(NoPosition);
    }
Пример #9
0
 public Vector3 GetDestination()
 {
     return(path.Count > 0 ? PathfinderData.Position(path[0]) : NoPosition);
 }