Pergunta

this question is probably going to be a bit confusing, as I'm just starting c# and object-oriented coding in general, but I'll do my best to explain things clearly! For context, I'm trying to make a random dungeon generator for a game project.

I have the jagged array "Dungeon," which I've declared like this:

public Level[][,] Dungeon;

To clarify, Dungeon is a 1D array of "Levels." A level is its own object, and it has a few unique properties that I defined in its constructor.

Each level is a 2D array of "mazePieces," which are also objects.

I've figured out how to refer to the entire Dungeon array to do things such as see how many levels are in the dungeon:

Dungeon[x].Length

I also can refer to individual mazePieces in a given level:

Dungeon[i][x,y].mobAmount

However, I can't figure out how to refer to the properties of an entire Level. If I type in

Dungeon[i].numberOfRooms

"numberOfRooms" is not recognized as a property of the level. However, I've found that it will be recognized if I type

Dungeon[i][,].numberOfRooms

The issue I'm running into is that the second set of brackets are marked with a syntax error, saying that a value is expected.

What can I put into the second set of brackets so I can refer to the entire level rather than just a specific part of the level?

Hopefully this was at least somewhat understandable, please let me know and I'll do my best to clarify! Thanks!

Foi útil?

Solução 2

I would say that pieces of maze should be a part of Level class,

So you would have

public Levels[] Dungeon.

Also your Level class would have indexer:

public SomeReturnType this[int x, int y]
{
   get
   {
      // return maze piece
   }
}

In this case you would be able to access to any level property via Dungeon[i].numberOfRooms. And you still would be able to access to maze pieces like you want Dungeon[i][x,y].mobAmount

Outras dicas

You could use a little more composition in your design. Maybe something like this:

class Dungeon {
    public Level[] Levels { get; private set; }

class Level {
    public int NumberOfRooms
        { get { return Maze.GetUpperBound(0) * Maze.GetUpperBound(1); }
    public MazePiece[,] Maze { get; private set; }
}

class MazePiece {
    private List<Mob> _mobs = new List<Mob>();
    public IEnumerable<Mob> Mobs { get { return _mobs; } }
    public int MobCount { get { return _mobs.Count; } }
}

class Mob {
    public string Name { get; private set; }
}

Then you can more naturally refer to things:

var mobsAtTenTenOfLevelThree = dungeon.Levels[2].Maze[9, 9].Mobs;

Assuming numberOfRooms is a property of MazePiece, if you want to find a total for the dungeon you can use SelectMany:

int totalRooms = dungeon.SelectMany(p=>p.numberOfRooms).Sum();
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top