using System;
using UnityEngine;
///
/// A* 算法专用节点
///
public class AStarNode : IComparable
{
///
/// 节点的x轴坐标
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:命名样式", Justification = "<挂起>")]
public float x
{
get { return position.x; }
set { position = new Vector2(value, y); }
}
///
/// 节点的y轴坐标
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:命名样式", Justification = "<挂起>")]
public float y
{
get { return position.y; }
set { position = new Vector2(x, value); }
}
private Vector2 m_Position;
///
/// 获得节点的二维坐标
///
/// 节点的二维坐标
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:命名样式", Justification = "<挂起>")]
public Vector2 position
{
get { return m_Position; }
set { m_Position = value; }
}
///
/// 节点的权值
///
public float f;
///
/// 起点达到目前遍历节点的距离
///
public float g;
///
/// 目前遍历的节点到达终点的距离
///
public float h;
///
/// 节点状态
///
public GlobalEnum.NodeStatu nodeStatu;
///
/// 该节点的父节点
///
public AStarNode father;
///
/// 初始化节点
///
/// x轴坐标
/// y轴坐标
/// 节点状态
public AStarNode(float x, float y, GlobalEnum.NodeStatu nodeStatu)
{
m_Position = new Vector2(x, y);
this.nodeStatu = nodeStatu;
}
///
/// 自定义升序排列
///
/// 作对比的节点
/// 节点的权值谁更大, -1 则 other 更大, 1 则当前节点更大
public int CompareTo(AStarNode other)
{
// 返回值>0时,当前节点排在other节点右边
// 返回值<0时,当前节点排在other节点左边
// 可以理解为other节点处于0位置
// 按F值排序(F值小的优先级高)
return f.CompareTo(other.f);
// int fComparison = f.CompareTo(other.f);
// if (fComparison != 0) return fComparison;
// // F值相同的情况下,可以按H值排序(H值小的优先级高)
// // 这有助于在总代价相同时优先选择更接近目标的节点
// return h.CompareTo(other.h);
}
public override bool Equals(object obj)
{
return obj is AStarNode other && position.Equals(other.position);
}
public override int GetHashCode()
{
return position.GetHashCode();
}
public static bool operator ==(AStarNode left, AStarNode right)
{
return left?.position.Equals(right?.position) ?? right is null;
}
public static bool operator !=(AStarNode left, AStarNode right)
{
return !(left == right);
}
}