//================================================================
//
// Copyright (C) 2017 Team Saluka
// All Rights Reserved
//
// Author:小妖精Balous
//
//Summary:这里一共列出3种启发因子,启发因子决定搜索精确度与CPU开销
//
//================================================================
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Saruka
{
/// <summary>
/// 启发因子
/// </summary>
public abstract class Heuristics
{
public abstract float GetHeuristics(NavNode startNode, NavNode targetNode);
}
/// <summary>
/// 曼哈顿距离
/// </summary>
public class ManhattanDistance : Heuristics
{
public override float GetHeuristics(NavNode startNode, NavNode targetNode)
{
return Mathf.Abs(startNode.gridX - targetNode.gridX) + Mathf.Abs(startNode.gridY - targetNode.gridY);
}
}
/// <summary>
/// 欧几里得距离
/// </summary>
public class EuclideanDistance : Heuristics
{
public override float GetHeuristics(NavNode startNode, NavNode targetNode)
{
int disX = startNode.gridX - targetNode.gridX;
int disY = startNode.gridY - targetNode.gridY;
return Mathf.Sqrt(disX * disX + disY * disY);
}
}
/// <summary>
/// 八方向距离
/// </summary>
public class EightDirectionsDistance : Heuristics
{
public override float GetHeuristics(NavNode startNode, NavNode targetNode)
{
int disX = Mathf.Abs(startNode.gridX - targetNode.gridX);
int disY = Mathf.Abs(startNode.gridY - targetNode.gridY);
if (disX > disY) return 1.414213562f * disY + (disX - disY);
return 1.414213562f * disX + (disY - disX);
}
}
}