学习完AStar,在这里给自己一个简单的总结。
将地图分为一个网格。而其中每一个网格都是一个Node节点,合格Node节点最好包括一个角色的大小。
首先记住公式:
//成本,成本计算公式 C = S + H
// estimatedCost = nodeTotalCost + estimatedCost
其中CompareTo是用于sort()排序是排序方法
/****************************************************
文件:Node.cs
作者:Jim
邮箱: 425636780@qq.com
日期:#CreateTime#
功能:A星算法,node类
*****************************************************/
using UnityEngine;
using System.Collections;
using System;
public class Node : IComparable
{
//成本,成本计算公式 C = S + H
// a = b + a
// estimatedCost = nodeTotalCost + estimatedCost
public float nodeTotalCost; //H 总成本
public float estimatedCost; //S 估算成本
public bool bObstacle; //是否为障碍物
public Node parent; //父节点
public Vector3 postion; //节点位置
public Node()
{
this.estimatedCost = 0.0f;
this.nodeTotalCost = 1.0f;
this.bObstacle = false;
this.parent = null;
}
public Node(Vector3 pos)
{
this.estimatedCost = 0.0f;
this.nodeTotalCost = 1.0f;
this.bObstacle = false;
this.parent = null;
this.postion = pos;
}
//把该节点变为障碍物
public void MarkAsObstacle()
{
this.bObstacle = true;
}
//比较节点的大小
public int CompareTo(object obj)
{
Node node = (Node)obj;
if (this.estimatedCost < node.estimatedCost)
{
return -1;
}
if (this.estimatedCost > node.estimatedCost)
{
return 1;
}
return 0;
}
}