Question by
Luxgile · Jun 05, 2017 at 01:42 AM ·
listcontainscustom-class
Overriding List.Contains Equals() for a custom class
I'm making a A* pathfinding so i made a Node class and i have a list with all the Nodes but i need to use the function .Constains and for that i have to override Equals() in the Node class and i have been looking everywhere but everyone makes it different so i'm really confused.
Here is what i've done, but it doesn't work so i really need someone that tells me what's wrong please :)
using System.Collections;
using UnityEngine;
using System.Collections.Generic;
public class Node : object {
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((object)other == null)
return false;
return this.nodePosition == other.nodePosition;
}
public override bool Equals(System.Object other){
if(other == null)
return false;
Node node = other as Node;
if((System.Object)node == null)
return false;
return this.nodePosition == node.nodePosition;
}
public override int GetHashCode(){
return base.GetHashCode();
}
}
Comment