【问题标题】:Manipulating index values of array property get/set操作数组属性获取/设置的索引值
【发布时间】:2015-12-09 19:27:14
【问题描述】:

在我正在构建的游戏引擎中(为了好玩),开发人员可以创建地牢,其中包含地牢楼层的 1 维数组,而地牢楼层又包含房间的 2 维数组。

每个楼层都可以从其他楼层偏移,(例如,让楼层居中),我想修改 get() 数组的 rooms 以便垂直对齐的楼层将具有相同的(每floor) 坐标,与两个楼层的大小和偏移量无关。

例如,想象一个 5 * 5 大小的地牢地板。它上面的楼层是 3 * 3。二楼偏移 (1, 1),这意味着如果我调用 dungeon.floors[0].rooms[2, 2]dungeon.floors[1].rooms[2,2],我应该检索两个直接在彼此上方/下方的房间。

floor 1  floor 0  floor 1 with offset
 ■ ■ ■  ■ ■ ■ ■ ■  X X X X
 ■ O ■  ■ ■ ■ ■ ■  X ■ ■ ■
 ■ ■ ■  ■ ■ O ■ ■  X ■ O ■
        ■ ■ ■ ■ ■  X ■ ■ ■
        ■ ■ ■ ■ ■
Drawn diagram of the above example, the circle represents the room that should be selected.
The Xs in the last plan show how the offset should be 'visualised'.
Note the selected rooms overlap should floor 0 and floor 1 with offset be overlaid.

下面是工作代码,省略了方法和不相关的细节。 我可以通过所描述的属性访问器来完成它,还是必须使用类索引器?

struct Offset
{
    int x, y;
}

class Dungeon
{
    public DungeonFloor[] floors { get; private set; }
    public int Height { get; private set; }
}

class DungeonFloor
{
    public int Width { get; private set; }
    public int Height { get; private set; }
    public Offset offset {get; private set;}
    public Room[,] rooms {
        get
        {
            //What do I put here??
            //Is it even possible to access the index values in this context?
        }
        private set; 
    }
}

class Room { }

(我知道我可能会用对数组实际尺寸大小的调用来替换 Width/Height)

【问题讨论】:

    标签: c# arrays properties game-engine


    【解决方案1】:

    不确定我是否完全理解这个问题,但我认为您想要的是索引器,请参阅此处的文档:https://msdn.microsoft.com/en-us/library/6x16t2tx.aspx

    一个简单的例子:

    class DongeonFloor
    {
        private room[,] room=new room[rows,columns];
        public Room this[int i,int j] {
        get
        {
            return room[i+1,j+1]; //modify for whatever the offset is 
        }
        private set{
            room[i-1,j-1]=value;
        }
    
    
    }
    

    【讨论】:

    • 那么索引器是最好的选择吗?
    猜你喜欢
    • 1970-01-01
    • 2013-11-04
    • 2021-12-29
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 2012-11-06
    • 2012-09-15
    相关资源
    最近更新 更多