Ejemplo n.º 1
0
 public void StartPathSearch(Ants.Pathfinding.Grid targetGrid, Ants.Pathfinding.GridNode startNode, Ants.Pathfinding.GridNode endNode)
 {
     this.targetGrid = targetGrid;
     this.pathFinder.StartPathSearch(targetGrid, startNode, endNode);
     this.pathlineRenderer.numPositions = 0;
 }
Ejemplo n.º 2
0
        public void Update()
        {
            //Find path finding grid
            var targetGrid = World.TerrainActor.Terrain.pathfindingGrid;

            if (targetGrid == null)
            {
                return;
            }

            //Find node pointed at by mouse
            var worldPos = UnityEngine.Camera.main.ScreenToWorldPoint(UnityEngine.Input.mousePosition);

            mousedOverNode = targetGrid.WorldToTile(worldPos);

            //If left click
            if (UnityEngine.Input.GetMouseButtonDown(0))
            {
                //Add node to list as origin, if passable
                if (this.leftClickedNodes.IndexOf(mousedOverNode) == -1)
                {
                    if (mousedOverNode.Passable)
                    {
                        this.leftClickedNodes.Add(mousedOverNode);
                    }
                    else
                    {
                        UnityEngine.Debug.Log("Cannot start path on impassable terrain.");
                    }
                }
            }
            //IF right click, start pathing
            else if (UnityEngine.Input.GetMouseButtonDown(1))
            {
                //Clear out old path finders
                foreach (var pathFinder in this.targetPathfinders)
                {
                    UnityEngine.GameObject.Destroy(pathFinder.gameObject);
                }
                targetPathfinders.Clear();

                //Start path from all origins to right click position
                if (leftClickedNodes.Count > 0)
                {
                    foreach (var node in this.leftClickedNodes)
                    {
                        var newPathFinderObj = new UnityEngine.GameObject("Path Finder");
                        newPathFinderObj.transform.parent = this.transform;
                        var newPathFinder = newPathFinderObj.AddComponent <Ants.Pathfinding.PathFinderActor>();
                        newPathFinder.StartPathSearch(targetGrid, node, mousedOverNode);

                        targetPathfinders.Add(newPathFinder);
                    }
                }

                this.leftClickedNodes.Clear();
            }
            //If path finders exist, update them
            else if (this.targetPathfinders != null)
            {
                foreach (var pathFinder in this.targetPathfinders)
                {
                    pathFinder.UpdateSearch(20);
                }
            }
        }