Here is my source code:
[source lang="csharp"]//MapGraph is a 2D array of booleans, false denotes impassable and true passable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
namespace WindowsGameLibrary1
{
public class AeeStar
{
class PathNode
{
int x, y, moves, dist;
PathNode parent;
public PathNode(int x, int y, int moves, int dist, PathNode parent = null)
{
this.x = x;
this.y = y;
this.moves = moves;
this.dist = dist;
this.parent = parent;
}
public int X
{
get { return x; }
set { x = value; }
}
public int Y
{
get { return y; }
set { y = value; }
}
public int Moves
{
get { return moves; }
set { moves = value; }
}
public int Dist
{
get { return dist; }
set { dist = value; }
}
public int finalCost()
{
return moves + dist;
}
public PathNode Parent
{
get { return parent; }
set { parent = value; }
}
}
int x1, y1, x2, y2, scale;
MapGraph mg;
PriorityQueue
List
List
PathNode current, up, left, right, down;
bool exists;
int max;
public AeeStar(MapGraph mg, int scale = 60, int max = 1000)
{
this.mg = mg;
this.scale = scale;
this.max = max;
open = new PriorityQueue
closed = new List
}
public int calcDist(int x1, int y1, int x2, int y2)
{
return DistanceCalculator.calcDistance(new Vector2(x1, y1), new Vector2(x2, y2));
}
public void setUp()
{
if (current.Y-1 > 0)
{
if (mg.getCell(current.X, current.Y - 1) == false)
{
up = null;
}
else
{
up = new PathNode(current.X, current.Y - 1, current.Moves + 1, calcDist(current.X, current.Y - 1, x2, y2), current);
}
}
}
public void setLeft()
{
if (current.X-1 > 0)
{
if (mg.getCell(current.X - 1, current.Y) == false)
{
left = null;
}
else
{
left = new PathNode(current.X - 1, current.Y, current.Moves + 1, calcDist(current.X - 1, current.Y, x2, y2), current);
}
}
}
public void setRight()
{
if (current.X+1 < mg.Size.X - 1)
{
if (mg.getCell(current.X + 1, current.Y) == false)
{
right = null;
}
else
{
right = new PathNode(current.X + 1, current.Y, current.Moves + 1, calcDist(current.X + 1, current.Y, x2, y2), current);
}
}
}
public void setDown()
{
if (current.Y+1 < mg.Size.Y - 1)
{
if (mg.getCell(current.X, current.Y + 1) == false)
{
down = null;
}
else
{
down = new PathNode(current.X, current.Y + 1, current.Moves + 1, calcDist(current.X, current.Y + 1, x2, y2), current);
}
}
}
public List
{
this.x1 = x1/scale;
this.y1 = y1/scale;
this.x2 = x2/scale;
this.y2 = y2/scale;
exists = false;
open.enqueue((new PathNode(x1 / scale, y1 / scale, 0, calcDist(x1 / scale, y1 / scale, x2 / scale, y2 / scale))), calcDist(x1 / scale, y1 / scale, x2 / scale, y2 / scale));
int searches = 0;
while (open.Count > 0 && searches < max)
{
current = open.dequeue();
setUp();
if (up != null)
{
open.enqueue(up, up.finalCost());
}
setLeft();
if (left != null)
{
open.enqueue(left, left.finalCost());
}
setRight();
if (right != null)
{
open.enqueue(right, right.finalCost());
}
setDown();
if (down != null)
{
open.enqueue(down, down.finalCost());
}
for (int i = 0; i < closed.Count; i++)
{
if (closed.X == current.X && closed.Y == current.Y && closed.finalCost() > current.finalCost())
{
closed = current;
exists = true;
}
}
if (!exists)
{
closed.Add(current);
}
if (current.X == x2 && current.Y == y2)
{
break;
}
searches++;
}
//path = new List
createPath();
return path;
}
public void createPath()
{
path = new List
while (current.Moves != 0)
{
path.Add(new VectorInt(current.X*scale, current.Y*scale));
current = current.Parent;
}
}
}
}[/source]