2015-12-09 25 views
0

在我建造的游戏引擎(为了好玩)中,开发人员可以创建地下城,其中包含地牢地板的一维数组,其中又包含2维的房间阵列。操纵数组属性的索引值get/set

每个楼层可从其他人所抵消,(例如,以允许中心层),我想修改rooms阵列,使得地板是垂直对齐将具有相同(每一层)坐标的get() ,而不管这两层的大小和偏移。

例如,想象一个尺寸为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 { } 

(我知道我大概可以通过调用数组实际尺寸大小更换宽/高)

回答

2

不知道我理解的问题完全是,但我想你想要的是一个索引,见文档浏览: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; 
    } 


} 
+0

所以索引仅仅是去那么最好的方法是什么? – LeftRight92