コード例 #1
0
  public boolean addHallway(DungeonDir dir) {
    int amount = elementArray[x][y] == DungeonElementType.ROOM ? 2 : 1;
    int prevX = x, prevY = y;

    if (move(dir, amount)) {
      if (elementArray[x][y] == DungeonElementType.EMPTY) {
        elementArray[x][y] = DungeonElementType.HALLWAY;
        elementList.add(
            new DungeonElement(x, y, DungeonElementType.HALLWAY).connect(dir.reversed()));
        if (elementList.getAt(prevX, prevY) != null) elementList.getAt(prevX, prevY).connect(dir);
        return true;
      } else move(dir.reversed(), amount);
    }

    return false;
  }
コード例 #2
0
  public void createDescend(int x, int y) {
    if (elementList.getTypeAt(x, y) != DungeonElementType.ROOM)
      throw new IllegalStateException("Cannot create descend, invalid dungeon element!");

    DungeonElement descend;
    elementArray[x][y] = DungeonElementType.DESCEND;
    elementList.add(descend = new DungeonElement(x, y, DungeonElementType.DESCEND));

    for (DungeonDir dir : DungeonDir.values) {
      DungeonElement element = elementList.getAt(x + dir.addX * 2, y + dir.addY * 2);

      if (element != null) {
        descend.connect(dir);
        element.connect(dir.reversed());
      }
    }
  }
コード例 #3
0
  public int addRoom(DungeonDir dir, Random rand) {
    if (!canGenerateRoom(dir)) return 0;

    int prevX = x, prevY = y;

    if (move(dir, 2)) {
      addRoomAt(x, y);
      elementList.getAt(x, y).connect(dir.reversed());
      DungeonElement prevElement = elementList.getAt(prevX, prevY);
      if (prevElement != null) prevElement.connect(dir);

      RoomCombo shape = DungeonElementType.RoomCombo.random(rand);

      if (shape != RoomCombo.SINGLE) {
        List<byte[]> rooms = new ArrayList<>(4);
        byte origX = x, origY = y;

        switch (shape) {
          case LONG:
            switch (rand.nextInt(3)) {
              case 0:
                dir = dir.rotatedLeft();
                break;
              case 1:
                dir = dir.rotatedRight();
                break;
              default:
            }

            move(dir, 1);
            if (canGenerateRoom(dir)) {
              move(dir, 2);
              rooms.add(new byte[] {x, y});
            }

            break;

          case TURN:
          case BLOCK:
            boolean turnWay = rand.nextBoolean();

            for (int a = 0; a < (shape == RoomCombo.TURN ? 2 : 3); a++) {
              move(dir, 1);
              if (!canGenerateRoom(dir)) {
                rooms.clear();
                break;
              }

              move(dir, 2);
              rooms.add(new byte[] {x, y});
              dir = turnWay ? dir.rotatedLeft() : dir.rotatedRight();
            }

            break;

          default:
        }

        for (byte[] loc : rooms) addRoomAt(loc[0], loc[1]);

        rooms.add(new byte[] {origX, origY});

        if (rooms.size() > 1) {
          int xx, yy;
          boolean found;

          for (byte[] loc : rooms) {
            for (DungeonDir testDir : DungeonDir.values) {
              xx = loc[0] + testDir.addX * 3;
              yy = loc[1] + testDir.addY * 3;

              if (elementList.getTypeAt(xx, yy) == DungeonElementType.ROOM) {
                found = false;

                for (byte[] testLoc : rooms) {
                  if (testLoc[0] == xx && testLoc[1] == yy) {
                    found = true;
                    break;
                  }
                }

                if (found) elementList.getAt(loc[0], loc[1]).connect(testDir);
              }
            }
          }
        }

        byte[] moveTo = rooms.get(rand.nextInt(rooms.size()));
        moveToElement(elementList.getAt(moveTo[0], moveTo[1]));

        return rooms.size();
      }

      return 1;
    } else return 0;
  }