2013-10-05 2 views
-3

Я пытаюсь создать генератор подземелья для проекта, над которым я работал, основываясь на этом algorithm. Я все потерял, но мой массив (рис.1) по-видимому не удерживает данные карты. Я использую три типа данных, чтобы определить, является ли ячейка на карте пустой (0), пробел, на котором может быть символ (1), коридор (2) или стена (3).C# Array не держит данные

Я немного застрял на этой части, поэтому любая помощь приветствуется!

EDIT: проблема заключается в том, что объект карты не хранит данные в цикле, показанном на рисунке 1. Извините за то, что вы так расплывчаты.

(рис. 1)

 for (int i = 0; i < roomList.Count; i++) 
     { 
      for (int x = roomList[i].X; x < (roomList[i].X + roomList[i].W); x++) 
      { 
       for (int y = roomList[i].Y; y < (roomList[i].Y + roomList[i].H); y++) 
       { 
        map[x, y] = 1; 
       } 
      } 
     } 

(Все мои соответствующего кода)

namespace Project 
{ 
    } 
    public class Room 
    { 
     int xValue, yValue, widthValue, heightValue; 

     public int X 
     { 
      get { return xValue; } 
      set { xValue = value; } 
     } 
     public int Y 
     { 
      get { return yValue; } 
      set { yValue = value; } 
     } 
     public int W 
     { 
      get { return widthValue; } 
      set { widthValue = value; } 
     } 
     public int H 
     { 
      get { return heightValue; } 
      set { heightValue = value; } 
     } 
    } 
public class DungeonGenerate 
{ 
    public int baseWidth = 513; 
    public int baseHeight = 513; 
    public int width = 64; 
    public int height = 64; 
    Color[,] arrayColor; 
    Random rand = new Random(); 
    Room room = new Room(); 
    Rectangle[,] rectMap; 

    public void Generate() 
    { 
     rectMap = new Rectangle[baseWidth, baseHeight]; 
     //Creates a 2-D Array/Grid for the Dungeon 
     int[,] map = new int[baseWidth, baseHeight]; 
     //Determines all the cells to be empty until otherwise stated 
     for (int x = 0; x < width; x++) 
     { 
      for (int y = 0; y < height; y++) 
      { 
       map[x, y] = 0; 
      } 
     } 

     //Determines the amount of rooms in the dungeon 
     int minRooms = (width * height)/300; 
     int maxRooms = (width * height)/150; 
     int amountOfRooms = rand.Next(minRooms, maxRooms); 

     //Room dimensions 
     int widthRoot = Convert.ToInt32(Math.Round(Math.Sqrt(width * 2))); 
     int heightRoot = Convert.ToInt32(Math.Round(Math.Sqrt(height * 2))); 
     int minWidth = Convert.ToInt32(Math.Round((width * .5)/widthRoot)); 
     int maxWidth = Convert.ToInt32((width * 2)/widthRoot); 
     int minHeight = Convert.ToInt32(Math.Round(height * .5)/heightRoot); 
     int maxHeight = Convert.ToInt32((height * 2)/heightRoot); 

     //Creates the rooms 
     List<Room> roomList = new List<Room>(amountOfRooms); 

     for (int i = 0; i < amountOfRooms; i++) 
     { 
      bool ok = false; 
      do 
      { 
       room.X = rand.Next(width); 
       room.Y = rand.Next(height); 
       room.W = (rand.Next(maxWidth)) + minWidth; 
       room.H = (rand.Next(maxHeight)) + minHeight; 

       if (room.X + room.W >= width && room.Y + room.H >= height) 
       { 
        continue; 
       } 
       for (int q = 0; q < roomList.Count; q++) 
       { 
        if (room.X > roomList[q].X && room.X < roomList[q].X + room.W && room.Y > roomList[q].Y && room.Y < roomList[q].Y + room.H) 
        { 
         ok = false; 
         break; 
        } 
       } 
       ok = true; 
       roomList.Add(room); 
      } while (!ok); 
     } 
     //This will create hallways that lead to and from the rooms 
     int connectionCount = roomList.Count; 
     List<Point> connectedCells = new List<Point>((width * height)); 
     for (int i = 0; i < connectionCount; i++) 
     { 
      Room roomA = roomList[i]; 
      int roomNum = i; 

      while (roomNum == i) 
      { 
       roomNum = rand.Next(roomList.Count); 
      } 

      Room roomB = roomList[roomNum]; 

      //Increasing this will make the hallway more straight, decreasing it will make the hallway more skewed 
      int sidestepChance = 10; 

      Point pointA = new Point(x: (rand.Next(roomA.W)) + roomA.X, y: (rand.Next(roomA.H)) + roomA.Y); 
      Point pointB = new Point(x: (rand.Next(roomB.W)) + roomB.X, y: (rand.Next(roomB.H)) + roomB.Y); 

      while (pointA != pointB) 
      { 
       int num = rand.Next() * 100; 

       if (num < sidestepChance) 
       { 
        if (pointB.X != pointA.X) 
        { 
         if (pointB.X > pointA.X) 
         { 
          pointB.X--; 
         } 
         else 
         { 
          pointB.X++; 
         } 
        } 
       } 
       else if(pointB.Y != pointA.Y) 
       { 
        if (pointB.Y > pointA.Y) 
        { 
         pointB.Y--; 
        } 
        else 
        { 
         pointB.Y++; 
        } 
       } 
      } 

      if (pointB.X < width && pointB.Y < height) 
      { 
       connectedCells.Add(pointB); 
      } 
     } 

     //Fills the room with data 
     for (int i = 0; i < roomList.Count; i++) 
     { 
      for (int x = roomList[i].X; x < (roomList[i].X + roomList[i].W); x++) 
      { 
       for (int y = roomList[i].Y; y < (roomList[i].Y + roomList[i].H); y++) 
       { 
        map[x, y] = 1; 
       } 
      } 
     } 

     for (int y = 0; y < height; y++) 
     { 
      for (int x = 0; x < width; x++) 
      { 
       if (map[x, y] == 0) 
       { 
        bool wall = false; 
        for (int yy = y - 2; yy < y + 2; yy++) 
        { 
         for (int xx = x - 2; xx < x + 2; xx++) 
         { 
          if (xx > 0 && yy > 0 && xx < width && yy < height) 
          { 
           if (map[xx, yy] == 1 || map[xx, yy] == 2) 
           { 
            map[x, y] = 3; 
            wall = true; 
           } 
          } 
         } 
         if (wall) 
         { 
          break; 
         } 
        } 
       } 
      } 
     } 

     //Rendering the Map and giving it some Color (Sort of)! 
     int scaler = baseWidth/width; 

     for (int x = 0; x < baseWidth; x++) 
     { 
      for (int y = 0; y < baseHeight; y++) 
      { 
       rectMap[x, y] = new Rectangle(x, y, 1, 1); 
       arrayColor = new Color[baseWidth, baseHeight]; 
       switch (map[x, y]) 
       { 
        case 0: 
         arrayColor[x, y] = new Color(0,0,0); 
         break; 
        case 1: 
         arrayColor[x, y] = new Color(0,0,0); 
         break; 
        case 2: 
         arrayColor[x, y] = new Color(0,0,0); 
         break; 
        case 3: 
         arrayColor[x, y] = new Color (0,0,0); 
         break; 
       } 
      } 
     } 
    } 
    public Rectangle[,] GetMap() 
    { 
     return rectMap; 
    } 
    public Color[,] GetColors() 
    { 
     return arrayColor; 
    } 
} 
+1

Что вы имеете в виду, что не имеет значения? Какая часть точно? – Szymon

+2

Можете ли вы попытаться быть более конкретным в том, что проблема? Большой список кода и загадочный вопрос о том, чтобы не держать ценности, на самом деле не помогают нам помочь вам! –

ответ

2

В for -loop где вы заселение roomList, вы не инстанцировании новый Room каждый раз. Вы просто манипулируете одним и тем же объектом Room и повторно добавляете его в список, поэтому roomList будет содержать только много ссылок на тот же объект Room. Попробуйте удалить room поле из вашего DungeonGenerate класса и использовать локальную переменную вместо:

for (int i = 0; i < amountOfRooms; i++) 
{ 
    bool ok = false; 
    do 
    { 
     var room = new Room(); 
     ... 
     roomList.Add(room); 
    } while (!ok); 
} 
+0

Nice catch, +1. – Brian

Смежные вопросы