Ejemplo n.º 1
0
        public bool FindPath(Vector3 startPos, Vector3 targetPos, out List <Tile> path)
        {
            Tile startTile  = MapUtility.TileFromWorldPosition(startPos);
            Tile finishTile = MapUtility.TileFromWorldPosition(targetPos);

            return(this.FindPath_Internal(startTile, finishTile, out path));
        }
 protected bool OnInput()
 {
     if (this.controller.RightClick() == true)
     {
         var pos  = CursorToWorldPos();
         var tile = MapUtility.TileFromWorldPosition(pos);
         tile.ToggleBlock(tile.traversable);
         return(true);
     }
     else if (this.controller.LeftClick() == true)
     {
         var pos = CursorToWorldPos();
         this.player.currentHero.movement.SetDestination(MapUtility.TileFromWorldPosition(pos));
         return(true);
     }
     else if (this.controller.Space() == true)
     {
         this.player.currentHero.currentTile.interaction.Invoke(this.player.currentHero.hero);
     }
     else if (this.controller.Validate() == true)
     {
         this.gm.currentMap.ShowTiles(new TilePositon(x, y), 5);
     }
     return(false);
 }
Ejemplo n.º 3
0
        private bool FindPath_Internal(Tile startTile, Tile finishTile, out List <Tile> path)
        {
            this.openSet.Clear();
            this.closeSet.Clear();

            this.openSet.Add(startTile);

            while (this.openSet.Count > 0)
            {
                Tile currentTile = this.openSet.RemoveFirst();
                this.closeSet.Add(currentTile);

                if (currentTile == finishTile)
                {
                    RetracePath(startTile, finishTile, out path);
                    return(true);
                }

                foreach (Tile neighbour in MapUtility.GetNeighbours(currentTile))
                {
                    if (neighbour.visible == false || neighbour.traversable == false && neighbour.interactable == false || closeSet.Contains(neighbour) == true)
                    {
                        continue;
                    }

                    int costToNeighbor = currentTile.gCost + GetDistance(currentTile, neighbour);                     // + movementPenalty
                    if (costToNeighbor < neighbour.gCost || !openSet.Contains(neighbour))
                    {
                        neighbour.gCost  = costToNeighbor;
                        neighbour.hCost  = GetDistance(neighbour, finishTile);
                        neighbour.parent = currentTile;

                        if (this.openSet.Contains(neighbour) == false)
                        {
                            this.openSet.Add(neighbour);
                        }
                        else
                        {
                            this.openSet.UpdateItem(neighbour);
                        }
                    }
                }
            }
            path = new List <Tile>();
            return(false);
        }