【发布时间】:2018-01-21 11:28:47
【问题描述】:
我可以为我的播放器设置四个不同的移动方向
- Vector2.up => (0,1)
- Vector2.down => (0,-1)
- Vector2.left => (-1,0)
- Vector2.right => (1,0)
我有一个包含Cell 对象的二维数组
public class Cell
{
public Cell(GameObject cellObject, bool isObstacle)
{
CellObject = cellObject;
IsObstacle = isObstacle;
}
public GameObject CellObject { get; set; }
public bool IsObstacle { get; set; }
}
我的数组是根据地图的大小来初始化的。
private const int MAP_SIZE = 10;
private Cell[,] mapCells = new Cell[MAP_SIZE, MAP_SIZE];
我使用两个循环填充这个数组,这将给我 100 个单元格。
for (int x = 0; x < MAP_SIZE; x++)
{
for (int y = 0; y < MAP_SIZE; y++)
{
Vector3 newCellPosition = new Vector3(x, 0, y);
GameObject newCellObject = Instantiate(cell, newCellPosition, cell.transform.rotation);
bool isObstacle = false; // TEST
Cell newCell = new Cell(newCellObject, isObstacle);
mapCells[x, y] = newCell;
}
}
移动玩家时,我想返回他必须移动到的Cell。 moveDirection 参数将设置要搜索的行和列。
如果有障碍物,玩家应该移动到这个障碍物并停下来。
public Cell GetTargetCell(Vector2 movementDirection)
{
Cell targetCell = null;
// get the path
// get the closest obstacle
return targetCell;
}
有没有一种优雅的方法可以通过二维方向计算正确的目标单元格?
【问题讨论】: