我是一名初出茅庐的编程爱好者和游戏设计师,正在学习完成我的学位,因此在编程世界中还是很新的。我已经完成了大量的 JavaScript(实际上是 UnityScript),现在正尝试涉足 C#。我一直在关注 hakimio 的在 Unity 中制作回合制 RPG 的教程,该教程基于使用 A* 寻路的六边形网格。(http://tbswithunity3d.wordpress.com/)
我的问题是,我已经按照他的教程一步一步完成了 A* 寻路脚本和资产,但在 Unity 中遇到了错误:
“错误 CS0308:非泛型类型‘IHasNeighbours’不能与类型参数一起使用”
这是在行上引发错误消息的代码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;
}
}
任何有关如何克服此错误的帮助或见解将不胜感激,过去几天我一直在努力解决这个脚本,试图让它工作,并且无法继续使用教程(和我的项目)错误。
提前谢谢大家!
亚伦:)