操作数组 属性 get/set 的索引值
Manipulating index values of array property get/set
在我正在构建的游戏引擎中(为了好玩),开发人员可以创建地牢,其中包含地牢楼层的一维数组,而地牢楼层又包含房间的二维数组。
每个楼层都可以与其他楼层偏移(例如允许楼层居中),我想修改 rooms
数组的 get()
以使楼层垂直对齐将具有相同的(每层)坐标,无论两层的大小和偏移如何。
例如,想象一个 5 * 5 大小的地牢地板。它上面的楼层是3 * 3,二楼偏移了(1, 1),也就是说我应该调用dungeon.floors[0].rooms[2, 2]
和dungeon.floors[1].rooms[2,2]
,我应该检索两个直接是[=28的房间=] 彼此。
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.
下面是工作代码,省略了方法和无关的细节。
我可以通过描述的 属性 访问器来完成,还是必须使用 class 索引器?
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)
不确定我是否完全理解这个问题,但我认为您想要的是索引器,请参阅此处的文档: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;
}
}
在我正在构建的游戏引擎中(为了好玩),开发人员可以创建地牢,其中包含地牢楼层的一维数组,而地牢楼层又包含房间的二维数组。
每个楼层都可以与其他楼层偏移(例如允许楼层居中),我想修改 rooms
数组的 get()
以使楼层垂直对齐将具有相同的(每层)坐标,无论两层的大小和偏移如何。
例如,想象一个 5 * 5 大小的地牢地板。它上面的楼层是3 * 3,二楼偏移了(1, 1),也就是说我应该调用dungeon.floors[0].rooms[2, 2]
和dungeon.floors[1].rooms[2,2]
,我应该检索两个直接是[=28的房间=] 彼此。
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.
下面是工作代码,省略了方法和无关的细节。 我可以通过描述的 属性 访问器来完成,还是必须使用 class 索引器?
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)
不确定我是否完全理解这个问题,但我认为您想要的是索引器,请参阅此处的文档: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;
}
}