hex grid A * pathfinding ...类型参数错误

时间:2012-05-31 09:02:32

标签: c# path-finding a-star generic-type-argument hexagonal-tiles

我是一位正在崭露头角的编程爱好者和游戏设计师,正在学习完成我的学位,因此在编程领域仍然是一个新生事物。我已经完成了相当数量的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;
}
}

对于如何克服这个错误的任何帮助或见解将不胜感激,过去几天我一直在努力使这个脚本工作,并且不能继续学习这个教程(和我的项目) )有错误。

先谢谢大家!

亚伦:)

1 个答案:

答案 0 :(得分:1)

问题是IHasNeighbours不是通用接口,所以你不能像传递Tile类那样将类传递给它。

您需要修改IHasNeighbours接口以使其成为通用接口,或者您需要在其后取出对Tile类的引用。解决方案取决于您需要代码执行的操作。 :)