十六进制网格 A* 寻路..类型参数错误

本文关键字:类型参数 错误 寻路 网格 十六进制 | 更新日期: 2023-09-27 18:36:03

我是一个崭露头角的编程爱好者和游戏设计师,正在学习完成我的学位,因此在编程领域仍然很陌生。我已经做了相当多的JavaScript(实际上是UnityScript),现在正在尝试涉足C#。我一直在遵循 hakimio 在 Unity 中制作回合制 RPG 的教程,该教程基于 A* 寻路的六边形网格。(http://tbswithunity3d.wordpress.com/)

我的问题是,我已经按照他的教程逐步完成 A* 寻路脚本和资源,但在 Unity 中遇到了错误:

"错误 CS0308:非泛型类型'IHasNeighbors' 不能与类型参数一起使用"

这是触发错误消息的代码,在第 public class Tile: GridObject, IHasNeighbours<Tile> 行:

using System.Collections.Generic;
using System;
using System.Linq;
using UnityEngine;
public class Tile: GridObject, IHasNeighbours<Tile>
{
public bool Passable;
public Tile(int x, int y)
    : base(x, y)
{
    Passable = true;
}
public IEnumerable AllNeighbours { get; set; }
public IEnumerable Neighbours
{
    get { return AllNeighbours.Where(o => o.Passable); }
}
public static List<Point> NeighbourShift
{
    get
    {
        return new List<Point>
        {
            new Point(0, 1),
            new Point(1, 0),
            new Point(1, -1),
            new Point(0, -1),
            new Point(-1, 0),
            new Point(-1, 1),
        };
    }
}
public void FindNeighbours(Dictionary<Point, Tile> Board, Vector2 BoardSize, bool EqualLineLengths)
{
    List<Tile> neighbours = new List<Tile>();
    foreach (Point point in NeighbourShift)
    {
        int neighbourX = X + point.X;
        int neighbourY = Y + point.Y;
        //x coordinate offset specific to straight axis coordinates
        int xOffset = neighbourY / 2;
        //if every second hexagon row has less hexagons than the first one, just skip the last one when we come to it
        if (neighbourY % 2 != 0 && !EqualLineLengths && neighbourX + xOffset == BoardSize.x - 1)
            continue;
        //check to determine if currently processed coordinate is still inside the board limits
        if (neighbourX >= 0 - xOffset &&
            neighbourX < (int)BoardSize.x - xOffset &&
            neighbourY >= 0 && neighbourY < (int)BoardSize.y)
            neighbours.Add(Board[new Point(neighbourX, neighbourY)]);
    }
    AllNeighbours = neighbours;
}
}

有关如何克服此错误的任何帮助或见解将不胜感激,过去几天我一直在努力尝试使其工作,并且无法继续教程(和我的项目)错误。

提前感谢大家!

亚伦·:)

十六进制网格 A* 寻路..类型参数错误

问题是 IHasNeighbors 不是一个通用接口,因此您无法像传递 Tile 类那样将类传递给它。

要么需要修改 IHasNeighbors 接口以使其通用,要么需要在它之后删除对 Tile 类的引用。解决方案将取决于你需要你的代码做什么。 :)