Beispiel #1
0
        /// <summary>
        /// Calculates the distance between each node
        /// </summary>
        /// <param name="from">The node to calculate from</param>
        /// <param name="to">The Node to calculate to </param>
        /// <returns>The distance in number of nodes away times 10</returns>
        /// <example>
        /// From 1,1 to 2,2 Returns 14 as the the hypotenus/distance of 1,1 is 1.4
        /// then we multiply this by 10 
        /// </example>
        public int distanceBetweenNodes(Node from, Node to)
        {
            int horDist = Math.Abs(to.positionX - from.positionX) / 32;
            int vertDist = Math.Abs(to.positionY - from.positionY) / 32;

            return (int)(Math.Sqrt(horDist * horDist + vertDist * vertDist)*10);
        }
Beispiel #2
0
 public Game1()
 {
     graphics = new GraphicsDeviceManager(this);
     Content.RootDirectory = "Content";
     //Create a map of nodes
     map = new Node[Window.ClientBounds.Width/32,Window.ClientBounds.Height/32];
     for (int y = 0; y < map.GetLength(1); y++)
     {
         for (int x = 0; x < map.GetLength(0); x++)
         {
             map[x, y] = new Node(false, x*32, y*32, Color.Gray);
         }
     }
     startNode = map[0, 0];
     endNode = map[1, 1];
     path = new List<Node>();
 }
Beispiel #3
0
        /// <summary>
        /// Generate the heurstic value from node to end
        /// </summary>
        /// <param name="node">The node of which to generate the value</param>
        /// <returns>The heuristic value</returns>
        /// <remarks>Uses eucalean heuristic</remarks>
        public int Heuristic(Node node)
        {
            int horDist = Math.Abs(endNode.positionX - node.positionX) / 32;
            int vertDist = Math.Abs(endNode.positionY - node.positionY) / 32;

            //return (int)Math.Sqrt(horDist * horDist + vertDist * vertDist)*10;
            return ((horDist + vertDist != 0) ? (horDist + vertDist - 1) : 0)*10;
        }
Beispiel #4
0
 /// <summary>
 /// Gets the neighboors of a node
 /// </summary>
 /// <param name="node">The node to find parent of</param>
 /// <returns>An array containing arrays that are adjacent of the node</returns>
 private Node[] getNeighboors(Node node)
 {
     int nodeIndexX = node.positionX/32;
     int nodeIndexY = node.positionY/32;
     List<Node> nodesAsNeighboors = new List<Node>();
     for (int y = -1; y < 2; y++)
     {
         for (int x = -1; x < 2; x++)
         {
             //dont include ourselves
             if (x == 0 && y == 0)
                 continue;
             int xx = nodeIndexX + x;
             int yy = nodeIndexY + y;
             if (xx >= 0 && xx < map.GetLength(0) && yy >= 0 && yy < map.GetLength(1))
             {
                 nodesAsNeighboors.Add(map[xx, yy]);
             }
         }
     }
     return nodesAsNeighboors.ToArray();
 }
Beispiel #5
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            this.gameTime = gameTime;
            currentMouseState = Mouse.GetState();
            currentKeyboard = Keyboard.GetState();
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            //Adds a wall
            if (currentMouseState.LeftButton == ButtonState.Pressed &&
                previousMouseState.LeftButton == ButtonState.Released)
            {
                int gridX = (int)MathHelper.Clamp(currentMouseState.X / 32,0,map.GetLength(0)-1);
                int gridY = (int)MathHelper.Clamp(currentMouseState.Y / 32, 0, map.GetLength(1)-1);

                map[gridX, gridY].color = Color.Black;
                if (map[gridX, gridY].isWall)
                {
                    map[gridX, gridY].isWall = false;
                }
                else
                {
                    map[gridX, gridY].isWall = true;
                }

            }
            //Change start and stop
            if (currentMouseState.RightButton == ButtonState.Pressed &&
                previousMouseState.RightButton == ButtonState.Released)
            {
                int gridX = currentMouseState.X / 32;
                int gridY = currentMouseState.Y / 32;
                startNode = map[gridX, gridY];
            }
            if (currentMouseState.MiddleButton == ButtonState.Pressed &&
                previousMouseState.MiddleButton == ButtonState.Released)
            {
                int gridX = currentMouseState.X / 32;
                int gridY = currentMouseState.Y / 32;
                endNode = map[gridX, gridY];
            }
            //Generate map
            if (currentKeyboard.IsKeyDown(Keys.Space) && previousKeyboard.IsKeyUp(Keys.Space))
            {
                generatePath();
            }
            //Reset the map
            if (currentKeyboard.IsKeyDown(Keys.R) && previousKeyboard.IsKeyUp(Keys.R))
            {
                foreach (Node n in map)
                {
                    n.isWall = false;
                }
                openNodes.Clear();
                closedNodes.Clear();
                path.Clear();
            }

            previousMouseState = Mouse.GetState();
            previousKeyboard = Keyboard.GetState();
            base.Update(gameTime);
        }