【A星算法】--第一篇(Node节点)

        学习完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;
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值