source: 2015/24/ohjaajat/Dungeon/Dungeon/Dungeon/Pathfinding/Node.cs @ 10335

Revision 5967, 3.1 KB checked in by sieerinn, 4 years ago (diff)
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using Point = Microsoft.Xna.Framework.Point;
7
8namespace AStar
9{
10    /// <summary>
11    /// Represents a single node on a grid that is being searched for a path between two points
12    /// </summary>
13    public class Node
14    {
15        private Node parentNode;
16
17        /// <summary>
18        /// The node's location in the grid
19        /// </summary>
20        public Point Location { get; private set; }
21
22        /// <summary>
23        /// True when the node may be traversed, otherwise false
24        /// </summary>
25        public bool IsWalkable { get; set; }
26       
27        /// <summary>
28        /// Cost from start to here
29        /// </summary>
30        public float G { get; private set; }
31
32        /// <summary>
33        /// Estimated cost from here to end
34        /// </summary>
35        public float H { get; private set; }
36
37        /// <summary>
38        /// Flags whether the node is open, closed or untested by the PathFinder
39        /// </summary>
40        public NodeState State { get; set; }
41
42        /// <summary>
43        /// Estimated total cost (F = G + H)
44        /// </summary>
45        public float F
46        {
47            get { return this.G + this.H; }
48        }
49
50        /// <summary>
51        /// Gets or sets the parent node. The start node's parent is always null.
52        /// </summary>
53        public Node ParentNode
54        {
55            get { return this.parentNode; }
56            set
57            {
58                // When setting the parent, also calculate the traversal cost from the start node to here (the 'G' value)
59                this.parentNode = value;
60                this.G = this.parentNode.G + GetTraversalCost(this.Location, this.parentNode.Location);
61            }
62        }
63
64        /// <summary>
65        /// Creates a new instance of Node.
66        /// </summary>
67        /// <param name="x">The node's location along the X axis</param>
68        /// <param name="y">The node's location along the Y axis</param>
69        /// <param name="isWalkable">True if the node can be traversed, false if the node is a wall</param>
70        /// <param name="endLocation">The location of the destination node</param>
71        public Node(int x, int y, bool isWalkable, Point endLocation)
72        {
73            this.Location = new Point(x, y);
74            this.State = NodeState.Untested;
75            this.IsWalkable = isWalkable;
76            this.H = GetTraversalCost(this.Location, endLocation);
77            this.G = 0;
78        }
79
80        public override string ToString()
81        {
82            return string.Format("{0}, {1}: {2}", this.Location.X, this.Location.Y, this.State);
83        }
84
85        /// <summary>
86        /// Gets the distance between two points
87        /// </summary>
88        internal static float GetTraversalCost(Point location, Point otherLocation)
89        {
90            float deltaX = otherLocation.X - location.X;
91            float deltaY = otherLocation.Y - location.Y;
92            return (float)Math.Sqrt(deltaX * deltaX + deltaY * deltaY);
93        }
94    }
95}
Note: See TracBrowser for help on using the repository browser.