【发布时间】:2017-11-05 15:56:54
【问题描述】:
我正在尝试使用 A* 进行寻路,并创建了一个“节点”类。
关键是我有一个节点列表,我需要知道一个节点是否已经在列表中,但是 List.Contains 不起作用。
顺便说一句,我需要比较 Node 内变量中的世界位置,但我不知道如何覆盖 List.Contains 以与该变量进行比较。
编辑:添加了尝试实现 Equals 和 HashSet 的 Node 类
using System.Collections;
using UnityEngine;
using System.Collections.Generic;
public class Node {
public Node parentNode;
public int nodeX;
public int nodeY;
public Vector2 nodePosition;
public bool state;
public int gCost;
public int hCost;
public int fCost;
public Node(int x, int y){
nodeX = Mathf.RoundToInt(x);
nodeY = Mathf.RoundToInt(y);
nodePosition = new Vector2(nodeX, nodeY);
}
public void SetCost(Node end){
if(parentNode != null){
gCost =(int) parentNode.gCost + 10;
}
else{
gCost = 10;
}
hCost =(int) (Mathf.Abs(nodeX - end.nodeX) + Mathf.Abs(nodeY-end.nodeY));
fCost =(int) gCost + hCost;
}
public bool Equals (Node other){
if(other == null){return false;}
if(this.nodePosition == other.nodePosition){return true;}
else{return false;}
}
public override bool Equals(Object other){
if(other == null){return false;}
if(other is Node){
return this.Equals(other);
}
else{
return false;
}
}
public override int GetHashCode(){
return nodeX ^ nodeY;
}
【问题讨论】:
标签: list contains path-finding