首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有负索引图这样的东西?索引超出了数组的界限。

有没有负索引图这样的东西?索引超出了数组的界限。
EN

Stack Overflow用户
提问于 2022-11-21 20:55:47
回答 1查看 25关注 0票数 0

我有一个像这样的图表来建立我的统一项目:

代码语言:javascript
复制
public class Point
{
    public int X { get; set; }
    public int Y { get; set; }

    public Point(int x, int y)
    {
        this.X = x;
        this.Y = y;
    }

    public override bool Equals(object obj)
    {
        if (obj == null)
        {
            return false;
        }
        if (obj is Point)
        {
            Point p = obj as Point;
            return this.X == p.X && this.Y == p.Y;
        }
        return false;
    }

    public override int GetHashCode()
    {
        unchecked
        {
            int hash = 6949;
            hash = hash * 7907 + X.GetHashCode();
            hash = hash * 7907 + Y.GetHashCode();
            return hash;
        }
    }

    public override string ToString()
    {
        return "P(" + this.X + ", " + this.Y + ")";
    }
}

public enum CellType
{
    Empty,
    Road,
    Structure,
    SpecialStructure,
    None
}

public class Grid
{
    public CellType[,] _grid;
    private int _width;
    public int Width { get { return _width; } }
    private int _height;
    public int Height { get { return _height; } }

    public List<Point> _roadList = new List<Point>();
    public List<Point> _specialStructure = new List<Point>();
    public List<Point> _houseStructure = new List<Point>();

    public Grid(int width, int height)
    {
        _width = width;
        _height = height;
        _grid = new CellType[width, height];
    }

  

    // Adding index operator to our Grid class so that we can use grid[][] to access specific cell from our grid. 
    public CellType this[int i, int j]
    {
        get
        {
            return _grid[i, j];
        }
        set
        {
            if (value == CellType.Road)
            {
                _roadList.Add(new Point(i, j));
            }
            if (value == CellType.SpecialStructure)
            {
                _specialStructure.Add(new Point(i, j));
            }
            if (value == CellType.Structure)
            {
                _houseStructure.Add(new Point(i, j));
            }
            _grid[i, j] = value;
        }
    }

我正在使用L系统算法实例化一系列游戏对象,并将它们的X和Z位置添加到图中,如下所示:

代码语言:javascript
复制
public Grid AddToGrid(Vector3Int position, string type)
    {
        //CellType parsed_enum = (CellType)System.Enum.Parse(typeof(CellType), type);
        switch (type)
        {
            case "Structure":
                placementGrid._houseStructure.Add(new Point((int)position.x, (int)position.z));
                break;
            case "SpecialStructure":
                placementGrid._specialStructure.Add(new Point((int)position.x, (int)position.z));
                break;
            case "Road":
                placementGrid._roadList.Add(new Point((int)position.x, (int)position.z));
                break;
        }

        return placementGrid;
    }

然后在我的一个脚本中,我调用了另一个函数:

代码语言:javascript
复制
 public List<Point> GetWakableAdjacentCells(int x, int y, bool isAgent)
    {
        List<Point> adjacentCells = GetAllAdjacentCells(x, y);
        
        Debug.Log("Adjacent Cells"+ adjacentCells.Count);//3
        Debug.Log("X"+x); //-1
        Debug.Log("Y"+y);//1
        for (int i = adjacentCells.Count - 1; i >= 0; i--)
        {
            Debug.Log(adjacentCells[i].X); //-1
            Debug.Log(adjacentCells[i].Y);//2
            if (IsCellWakable(_grid[adjacentCells[i].X, adjacentCells[i].Y], isAgent) == false)
            {
                adjacentCells.RemoveAt(i);
            }
        }
        return adjacentCells;
    }

但这表示在if条件下,"index超出了数组的界限“。为了方便起见,我对变量旁边的每个相关值进行了注释。

代码语言:javascript
复制
The function this condition is checking is this:

     public static bool IsCellWakable(CellType cellType, bool aiAgent = false)
        {
            Debug.Log("boo");
            if (aiAgent)
            {
                return cellType == CellType.Road;
            }
            return cellType == CellType.Empty || cellType == CellType.Road;
        }

我做错了什么?

是否有一种实现网格的方式可以访问减值?

EN

回答 1

Stack Overflow用户

发布于 2022-11-22 10:43:25

好吧,你说自己

// -1

-> -1不能成为c#中的有效索引。

如果你想绕一圈,你很可能可以。

代码语言:javascript
复制
public CellType this[int i, int j]
{
    get
    {
        i = (i % _width) + _width) % _width;
        j = (j % _height) + _height) % _height;
        return _grid[i, j];
    }
    set
    {
        i = (i % _width) + _width) % _width;
        j = (j % _height) + _height) % _height;

        switch(value)
        {
            case CellType.Road:
                _roadList.Add(new Point(i, j));
                break;

            case CellType.SpecialStructure:
                _specialStructure.Add(new Point(i, j));
                break;
        
            case CellType.Structure:
                _houseStructure.Add(new Point(i, j));
                break;
        }
        _grid[i, j] = value;
    }
}

当然,这意味着你的网格基本上是“无限”的,跨越顶板,你将再次处于底部等等。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74524789

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档