Newer
Older
TheVengeance-Project-IADE-Unity2D / Assets / Scripts / NPC / Pathfinding / Node.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MyCollections.Generic;

namespace MyCollections.Generic.Heap
{
    //DEFINITION: G Cost - MOVEMENT COST
    //The G Cost represents the actual cost to move from the starting node to the current node.
    //This is typically calculated based on the movement distance and any penalties for traversing certain types of terrain.

    // If moving from the starting point to the current node involves traveling over several grid cells,
    // the G Cost might sum the distances or costs associated with each cell.
    // For example, moving to a neighboring cell might have a base cost of 1,
    // but if that cell has a movement penalty (like difficult terrain), the G Cost would increase accordingly.

    //DEFINITION: H Cost(Heuristic Cost)
    //The H Cost is an estimate of the cost to move from the current node to the target node.
    //This cost is calculated using a heuristic function that provides a distance or cost to reach the destination
    //For this scenario it will be used the Euclidian Distance aka Pythagorean theorem

    //EUCLIDIAN DISTANCE - Pythagorean theorem
    // H = Sqrt((x1 - x2)^2 + (y1 - y2)^2)

    public class Node : IheapItem<Node>
    {
        //Variables
        public Node parent; //Parent Node
        public bool walkable; //Indicates whether the node can be traversed
        public Vector2 worldPos; //Position
        public int movementPenalty; //Movement Penalty

        public int gridX, gridY; //Node size

        public int gCost; //movement cost
        public int hCost; //heuristic cost

        private int heapIndex; //Index in the heap

        //Properties
        public int FCost => gCost + hCost; //Calculate total cost

        public int HeapIndex
        {
            get => heapIndex;

            set => heapIndex = value;
        }

        // This method compares two nodes based on their FCost.
        // If the costs are equal, it compares their hCost.
        // The return value is negated to prioritize lower costs in the heap (which usually means smaller FCost should come first).
        public int CompareTo(Node nodeToCompare)
        {
            int compare = FCost.CompareTo(nodeToCompare.FCost); //Compare the total cost
            if (compare == 0)
            {
                compare = hCost.CompareTo(nodeToCompare.hCost); //Compare the heuristic cost
            }

            return -compare; //negates the comparison result to create a max-heap instead of a min-heap
        }

        //Constructor
        //Initializes the properties of a node.
        //It determines if a node is walkable, sets its position, grid coordinates,
        //and any movement penalty associated with it
        public Node(bool walkable, Vector2 worldPos, int gridX, int gridY, int movementPenalty)
        {
            this.walkable = walkable;
            this.worldPos = worldPos;
            this.gridX = gridX;
            this.gridY = gridY;
            this.movementPenalty = movementPenalty;
        }
    }
}