2015-12-09 23 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