以下是我如何实现 MazeMap 的关键部分。它是为六角网格设计的,因此连接性与正交网格略有不同。
public sealed class MazeMap : MapDisplay {
protected override string[] Board { get { return _board; } }
string[] _board = new string[] {
".............|.........|.......|.........|.............",
/* many similar lines omitted */
".............................|.......|.....|..........."
};
public override bool IsPassable(ICoordsUser coords) {
return IsOnBoard(coords) && this[coords].Elevation == 0;
}
public override IMapGridHex this[ICoordsCanon coords] {
get {return this[coords.User];}
}
public override IMapGridHex this[ICoordsUser coords] { get {
return new GridHex(Board[coords.Y][coords.X], coords);
} }
public struct GridHex : IMapGridHex {
internal static MapDisplay MyBoard { get; set; }
public GridHex(char value, ICoordsUser coords) : this() { Value = value; Coords = coords; }
IBoard<IGridHex> IGridHex.Board { get { return MyBoard; } }
public IBoard<IMapGridHex> Board { get { return MyBoard; } }
public ICoordsUser Coords { get; private set; }
public int Elevation { get { return Value == '.' ? 0 : 1; } }
public int ElevationASL { get { return Elevation * 10; } }
public int HeightObserver { get { return ElevationASL + 1; } }
public int HeightTarget { get { return ElevationASL + 1; } }
public int HeightTerrain { get { return ElevationASL + (Value == '.' ? 0 : 10); } }
public char Value { get; private set; }
public IEnumerable<NeighbourHex> GetNeighbours() {
var @this = this;
return NeighbourHex.GetNeighbours(@this).Where(n=>@this.Board.IsOnBoard(n.Hex.Coords));
}
}
}
注意this
大约一半的定义。这允许像结构数组一样访问 MaxeMap 的实例GridHex
。
ICoordsUser 和 ICoordsCanon 接口分别支持矩形或斜角(即 120 度的轴)的六角网格操作,并自动从一个转换到另一个;Point
这在正交网格上是不必要的,在其中传递一个实例就足够了。