Reputation: 266
In a game engine I am building (for fun), the developer can create dungeons, which contain a 1 dimensional array of dungeon floors, which in turn contain a 2 dimensional array of rooms.
Each floor can be offset from the others, (for example to allow floors to be centred), I would like to modify the get()
of the rooms
array such that floors are vertically aligned will have the same (per floor) coordinates, regardless of size and offset of the two floors.
For example, imagine a dungeon floor which is 5 * 5 in size. The floor above it is 3 * 3. The second floor is offset by (1, 1), which means that should I call dungeon.floors[0].rooms[2, 2]
and dungeon.floors[1].rooms[2,2]
, I should retrieve two rooms that are directly above/below one another.
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.
Below is the working code, with methods and irrelevant details omitted. Would I be able to do it through property accessors as depicted or would I have to make use of a class indexer?
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 { }
(I am aware I could probably replace Width/Height with calls to the arrays actual dimension sizes)
Upvotes: 0
Views: 312
Reputation: 4339
Not sure I understand the question exactly, but I think what you want is an indexer, see documentation here: https://msdn.microsoft.com/en-us/library/6x16t2tx.aspx
A quick example:
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;
}
}
Upvotes: 2