Exemplo n.º 1
0
 public ASTile(Point Postion, ASTile Parent, Point Destination)
 {
     this.Position = Postion;
     this.H        = Math.Abs(Destination.X - Postion.X) + Math.Abs(Destination.Y - Postion.Y);
     if (Parent == null)
     {
         G = 0;
     }
     else
     {
         this.Parent = Parent;
         this.G      = Parent.G + 1;
     }
 }
        public static List <Point> Find(Point Start, Point End, Character character)
        {
            Dictionary <Point, ASTile> openTiles  = new Dictionary <Point, ASTile>();
            Dictionary <Point, ASTile> closeTiles = new Dictionary <Point, ASTile>();
            ASTile curTile = new ASTile(Start, null, End);

            closeTiles.Add(curTile.Position, curTile);

            Point tmpTile = new Point();

            while (curTile.H != 0)
            {
                foreach (Point dir in Directions)
                {
                    tmpTile.X = curTile.Position.X + dir.X;
                    tmpTile.Y = curTile.Position.Y + dir.Y;
                    if (closeTiles.ContainsKey(tmpTile) == false)
                    {
                        if (openTiles.ContainsKey(tmpTile))
                        {
                            if (curTile.G + 1 < openTiles[tmpTile].G)
                            {
                                openTiles[tmpTile].ChangeParent(curTile);
                            }
                        }
                        else
                        {
                            if (Collided(tmpTile, character))
                            {
                                closeTiles.Add(tmpTile, null);
                            }
                            else
                            {
                                openTiles.Add(tmpTile, new ASTile(tmpTile, curTile, End));
                            }
                        }
                    }
                }

                if (openTiles.Count > 0)
                {
                    curTile = openTiles.First().Value;
                    foreach (ASTile at in openTiles.Values)
                    {
                        if (at.F < curTile.F || (at.F == curTile.F && at.H <= curTile.H))
                        {
                            curTile = at;
                        }
                    }
                    openTiles.Remove(curTile.Position);
                    closeTiles.Add(curTile.Position, curTile);
                }
                else
                {
                    return(null);
                }
            }

            List <Point> path = new List <Point>();

            while (curTile.G != 0)
            {
                path.Add(curTile.Position);
                curTile = curTile.Parent;
            }

            if (path.Count < 2)
            {
                return(null);
            }
            path.RemoveAt(path.Count - 1);
            path.Reverse();

            return(path);
        }
Exemplo n.º 3
0
 public void ChangeParent(ASTile newParent)
 {
     this.Parent = newParent;
     this.G      = newParent.G + 1;
 }