小妖精的完美游戏教室——人工智能,A*算法,实现篇
//================================================================
//
// Copyright (C) 2017 Team Saluka
// All Rights Reserved
//
// Author:小妖精Balous
//
//================================================================
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Saruka
{
/// <summary>
/// A*算法
/// </summary>
public class AStar
{
private AStar() { }
/// <summary>
/// A*搜索算法
/// </summary>
/// <param name="navGrid">导航网格</param>
/// <param name="startPosition">起点坐标</param>
/// <param name="targetPosition">目标点坐标</param>
/// <param name="heuristics">启发因子</param>
/// <returns>路径</returns>
public static List<NavNode> SearchPath(NavGrid navGrid, Vector3 startPosition, Vector3 targetPosition, Heuristics heuristics)
{
if (navGrid == null)
{
Debug.LogError("你正在使用A*算法,但是没有提供导航网格!");
return null;
}
NavNode startNode = navGrid.NavNodeFromWorldPosition(startPosition);
NavNode targetNode = navGrid.NavNodeFromWorldPosition(targetPosition);
if (!targetNode.isWalkable) return null;
List<NavNode> queueNodes = new List<NavNode>();
HashSet<NavNode> evaluatedNodes = new HashSet<NavNode>();
queueNodes.Add(startNode);
while(queueNodes.Count > 0)
{
NavNode currentNode = queueNodes[0];
for (int i = 1; i < queueNodes.Count; i++)
if (queueNodes[i].fCost < currentNode.fCost || queueNodes[i].fCost == currentNode.fCost && queueNodes[i].hCost < currentNode.hCost)
currentNode = queueNodes[i];
queueNodes.Remove(currentNode);
evaluatedNodes.Add(currentNode);
//找到路径,返回路径
if (currentNode == targetNode)
{
List<NavNode> path = new List<NavNode>();
NavNode node = targetNode;
while(node != startNode)
{
path.Add(node);
node = node.parent;
}
path.Reverse();
return path;
}
foreach (NavNode neighborNode in navGrid.GetNeighborNodes(currentNode))
{
if (!neighborNode.isWalkable || evaluatedNodes.Contains(neighborNode)) continue;
float newGCostToNeighborNode = currentNode.gCost + heuristics.GetHeuristics(currentNode, neighborNode);
if (!queueNodes.Contains(neighborNode) || newGCostToNeighborNode < neighborNode.gCost)
{
if (!queueNodes.Contains(neighborNode)) queueNodes.Add(neighborNode);
neighborNode.gCost = newGCostToNeighborNode;
neighborNode.hCost = heuristics.GetHeuristics(neighborNode, targetNode);
neighborNode.parent = currentNode;
}
}
}
//找不到路径,返回null
return null;
}
}
}
小妖精的完美游戏教室——人工智能,A*算法,实现篇的更多相关文章
- 小妖精的完美游戏教室——人工智能,A*算法,引言
今天也要直播魔法,求科学的! 欢迎来到小妖精Balous的完美游戏教室! 经过前两周的学习,相信米娜桑已经对状态机有所了解了呢~虽然状态机能够实现几乎所有的人工智能,但是,在实践中,你们有没有发现,自 ...
- 小妖精的完美游戏教室——人工智能,A*算法,启发因子篇
//================================================================//// Copyright (C) 2017 Team Saluk ...
- 小妖精的完美游戏教室——人工智能,A*算法,导航网络篇
//================================================================//// Copyright (C) 2017 Team Saluk ...
- 小妖精的完美游戏教室——人工智能,A*算法,结点篇
//================================================================//// Copyright (C) 2017 Team Saluk ...
- 小妖精的完美游戏教室——buff系统
作者:小妖精Balous,未经作者允许,任何个人与单位不得将此源代码用于商业化项目 #region buff /// <summary> /// 是否魔法免疫,魔法免疫的生物不会受到除自己 ...
- 小妖精的完美游戏教室——东方PROJECT,同人,墙
//================================================================//// Copyright (C) 东方同人社// All Rig ...
- 小妖精的完美游戏教室——东方PROJECT,同人,符卡系统
//================================================================//// Copyright (C) 东方同人社// All Rig ...
- 小妖精的完美游戏教室——东方PROJECT,同人,th12灵梦A
╮(╯▽╰)╭没办法,小妖精Balous也很讨厌学院化的教育呀,一点意义都没有. 这次就上传东方地灵殿灵梦A逻辑部分的核心代码吧,估计连老师都看不懂.动画部分的代码就不放上来了. //======== ...
- 小妖精的完美游戏教室——东方PROJECT,同人,子机
//================================================================//// Copyright (C)// All Rights Re ...
随机推荐
- 给出一百分制成绩,要求输出成绩等级’A’、’B’、’C’、’D’、’E’。
import java.util.Scanner; public class test5 { public static void main(String[] args) { // TODO Auto ...
- UVa LA 4094 WonderTeam 构造 难度: 1
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- 汽车行业解决方案_K2助力车企实现费控/生产“端到端流程”
如今汽车行业正面对一轮全球范围内新变革周期,这种“变革”一方面来源于在新能源技术.人工智能.信息技术.物联网技术等高新科技地猛烈敲击,另一方面源于全球的经济政策变幻莫测,贸易保护时代地到来,车企深陷发 ...
- 页面显示LCD液晶字体或者其他特殊字体
如果web项目中需要用到LCD液晶字体显示数值(如下图所示)该怎么办? 在这就需要用到@font-face(具体看一下语法) /* 定义 */ @font-face { font-family: 'M ...
- C#中d的??和?
在C#中??和?分别是什么意思? 1. 可空类型修饰符(?):引用类型可以使用空引用表示一个不存在的值,而值类型通常不能表示为空.例如:string str=null; 是正确的,int i=nu ...
- tpot ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
机器学习训练的时候报出这个问题 是因为dataframe中的数据类型有一个是‘object’,把它转成int,或float 就行,如下 df['A'] = df['A‘].astype(int) 参考 ...
- 递归实现列出当前工程下所有.Java文件
package com.lanxi.demo2_3; import java.io.File; import java.util.ArrayList; import java.util.List; / ...
- java + tomcat cookie 异常
Cookie cookie = new Cookie(username,value); cookie.setMaxAge(60*60*24*7,cookie); ...
- SoapUI并发模式
soapUI支持test suite, test case级别的并发,合理使用这个功能,可以让自动化脚本短时间内跑完,为release省下时间. 1. 如何开启并发模式 图示,click projec ...
- python的迭代器
迭代器 我们已经知道,可以直接作用于for循环的数据类型有以下几种: 一类是集合数据类型,如list.tuple.dict.set.str等: 一类是generator,包括生成器和带yield的ge ...