小妖精的完美游戏教室——人工智能,A*算法,导航网络篇

//================================================================
//
// Copyright (C) 2017 Team Saluka
// All Rights Reserved
//
// Author:小妖精Balous
//
//================================================================

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Saruka
{
/// <summary>
/// 导航网格
/// </summary>
public class NavGrid : MonoBehaviour
{
/// <summary>
/// 景物层级,用来建立导航网格
/// </summary>
public LayerMask scapeMask;
/// <summary>
/// 导航网格大小
/// </summary>
public Vector2 navGridSize;
/// <summary>
/// 单个网格半径
/// </summary>
public float gridRadius;
/// <summary>
/// 单个网格直径
/// </summary>
float gridDiameter;
/// <summary>
/// 网格结点
/// </summary>
public NavNode[,] grids
{
private set;
get;
}
/// <summary>
/// X轴方向网格数量
/// </summary>
public int gridCountX
{
private set;
get;
}
/// <summary>
/// Y轴方向网格数量
/// </summary>
public int gridCountY
{
private set;
get;
}

/// <summary>
/// 创建导航网格
/// </summary>
void CreateNavGrid()
{
gridDiameter = gridRadius * 2;
gridCountX = Mathf.RoundToInt(navGridSize.x / gridDiameter);
gridCountY = Mathf.RoundToInt(navGridSize.y / gridDiameter);

grids = new NavNode[gridCountX, gridCountY];

//导航网格左下角,平面直角坐标系
Vector3 navGridBottomLeft = transform.position - Vector3.right * navGridSize.x / 2 - Vector3.up * navGridSize.y / 2;

//创建网格
for (int x = 0; x < gridCountX; x++)
for (int y = 0; y < gridCountY; y++)
{
//网格中心坐标
Vector3 gridCenter = navGridBottomLeft + Vector3.right * (x * gridDiameter + gridRadius) + Vector3.up * (y * gridDiameter + gridRadius);
bool isWalkable = !(Physics2D.CircleCast(new Vector2(gridCenter.x, gridCenter.y), gridRadius, Vector2.zero, 0.0f, scapeMask));
grids[x, y] = new NavNode(gridCenter, isWalkable, x, y);
}
}

/// <summary>
/// 获得世界坐标所在的结点
/// </summary>
/// <param name="worldPosition">世界坐标</param>
/// <returns>结点</returns>
public NavNode NavNodeFromWorldPosition(Vector3 worldPosition)
{
worldPosition -= transform.position;
float percentX = Mathf.Clamp01((worldPosition.x + navGridSize.x / 2) / navGridSize.x);
float percentY = Mathf.Clamp01((worldPosition.y + navGridSize.y / 2) / navGridSize.y);
int x = Mathf.RoundToInt((gridCountX - 1) * percentX);
int y = Mathf.RoundToInt((gridCountY - 1) * percentY);
return grids[x, y];
}

public List<NavNode> GetNeighborNodes(NavNode navNode)
{
List<NavNode> neighborNodes = new List<NavNode>();

for (int x = -1; x <= 1; x++)
{
for (int y = -1; y <= 1; y++)
{
if (x == 0 && y == 0) continue;

int checkX = navNode.gridX + x;
int checkY = navNode.gridY + y;
if (checkX >= 0 && checkX < gridCountX && checkY >= 0 && checkY < gridCountY) neighborNodes.Add(grids[checkX, checkY]);
}
}
return neighborNodes;
}

void OnDrawGizmos()
{
//导航网格边框
Gizmos.DrawWireCube(transform.position, new Vector3(navGridSize.x, navGridSize.y, 1));

if(grids != null)
{
foreach(NavNode node in grids)
{
Gizmos.color = (node.isWalkable) ? new Color(1, 1, 1, 0.4f) : new Color(1, 0, 0, 0.4f);
Gizmos.DrawCube(node.worldPosition, Vector3.one * (gridDiameter - 0.03f));
}
}
}

// Use this for initialization
void Start()
{
CreateNavGrid();
}
}
}

时间: 2024-10-05 04:58:37

小妖精的完美游戏教室——人工智能,A*算法,导航网络篇的相关文章

小妖精的完美游戏教室——人工智能,状态机理论篇

今天也要直播魔法,求科学的! 欢迎来到小妖精Balous的完美游戏教室! 每一款好玩的游戏里面肯定有很多人工智能,例如一些帮助玩家的NPC,一些被玩家调教小怪物,更有输掉后会卖萌的Boss(小妖精真的没有玩过东方project,真的没有,小妖精甚至连这个游戏都没听说过!).那么,米娜桑想不想知道这些人工智能是如何实现的呢?那就跟着小妖精的步伐看下去吧~~但是小妖精现在的实力不是很强耶,目前还达不到妖精姐的水平,所以小妖精也只是教大家一些简单的基础.不要见怪哟. 人工智能简单来讲,是由状态跟转移条

小妖精的完美游戏教室——人工智能,A*算法,实现篇

//================================================================//// Copyright (C) 2017 Team Saluka// All Rights Reserved//// Author:小妖精Balous////================================================================ using System.Collections;using System

小妖精的完美游戏教室——人工智能,A*算法,结点篇

//================================================================//// Copyright (C) 2017 Team Saluka// All Rights Reserved//// Author小妖精Balous////================================================================ using System.Collections;using System.

小妖精的完美游戏教室——人工智能,A*算法,启发因子篇

//================================================================//// Copyright (C) 2017 Team Saluka// All Rights Reserved//// Author:小妖精Balous // //Summary:这里一共列出3种启发因子,启发因子决定搜索精确度与CPU开销////==========================================================

小妖精的完美游戏教室——魔方塔防01,路径

今天也要直播魔法,求科学的! 欢迎来到小妖精Balous的完美游戏教室! 现在呢,小妖精要教大家如何制作一个游戏,是不是很激动呢?小妖精现在也是很兴奋呀.废话不多说,我们直接进入正题.作为一个游戏,必须得有策划案,所以呢,小妖精就先简单叙述下这个游戏的策划吧~ 这是个传统的塔防游戏,玩家通过建造防御塔,升级防御塔来抵御怪物的进攻.咦?这个策划看上去很low啊.但是要做一个大型游戏,得先有好的技术对不对?我们就拿这个很low的游戏入手吧.因为小妖精是美术白痴啦,所以这个游戏的防御塔都是立方体,怪物

小妖精的完美游戏教室——东方PROJECT,同人,墙

//================================================================//// Copyright (C) 东方同人社// All Rights Reserved//// Author:小妖精Balous// //Summary:墙是很重要的,除非你想让玩家穿模.因为墙只有一个,所以就用静态的Singleton实现了 ////=======================================================

小妖精的完美游戏教室——东方PROJECT,同人,th12灵梦A

╮(╯▽╰)╭没办法,小妖精Balous也很讨厌学院化的教育呀,一点意义都没有. 这次就上传东方地灵殿灵梦A逻辑部分的核心代码吧,估计连老师都看不懂.动画部分的代码就不放上来了. //================================================================//// Copyright (C)// All Rights Reserved//// Author:小妖精Balous////=============================

小妖精的完美游戏教室——东方PROJECT,同人,符卡系统

//================================================================//// Copyright (C) 东方同人社// All Rights Reserved//// Author:小妖精Balous // //Summary:这次是符卡系统,这个系统能完成绝大多数符卡,算是比较通用的了////================================================================ usin

小妖精的完美游戏教室——技能系统

//================================================================//// Copyright (C) 2017 Team Saluka// All Rights Reserved//// Author:小妖精Balous////================================================================ using System.Collections;using System