2012-11-22 35 views
1

我在隨機化遊戲中削減我的牙齒,並試圖找出像遊戲一樣綁定Isacc & Sperlunky使用隨機排列的預製作級別碎片創建功能性地板/水平,沿着這條線:隨機等級佈局(AS3/Flashpunk)

一層將永遠包含播放器,退出和獎金室(黃點)。連接的房間直接相互連接。

我有一個如何去它淡淡的想法:通過創建持有tilemap的&網格數據的多維數組開始:

public static var levelLayout:Array = 
[[tile,grid],[tile,grid],[tile,grid],[tile,grid] 
[tile, grid],[tile,grid],[tile,grid],[tile,grid] etc] 

從那裏,經過每個網格空間,卷看看這個地方是不是什麼東西,如果他們被隔離,也不會讓房間成爲空間,然後開始從包含所有瓷磚地圖/網格的混洗陣列中分配房間。

我在正確的軌道上嗎?我應該如何處理出口?任何幫助,將不勝感激。歡呼聲

回答

0

從一個完美的迷宮開始(一個迷宮,你可以從迷宮中的任何其他地方到達迷宮中的任何一點)。這有助於創建以可勝任/可解決的方式連接的房間網絡。

這裏有很多教程。這一個是有點髒,有一串代碼,你可能並不需要,但它的好,期待通過這樣的一些相關的例子來了解不同的方法: http://www.emanueleferonato.com/2008/11/28/perfect-maze-generation-with-as3/

我有好運氣的遞歸回溯算法: http://en.wikipedia.org/wiki/Maze_generation_algorithm#Recursive_backtracker

考慮迷宮中的每個細胞是一個房間。一旦迷宮建成後,你就知道哪些房間連接到了其他房間,所以讓每個房間都有一種方法可以讓門連接到下一個房間或牆上。你可能會想成爲一個4x4完美的迷宮。

方式來豐富上調

您可以安全地刪除您的迷宮deadends,它仍然會是完美的。一旦迷宮產生,循環通過迷宮中的所有細胞。任何有3面牆的牢房都是死路一條,你可以完全將其關閉。或者把它變成一個祕密的房間或任何東西

向迷宮中添加循環。隨機連接迷宮中以前未連接的2間客房。這仍然保持迷宮的完美,它只是讓它更開放一點。

這是有趣的領地,享受吧:)

0

我實際上一直在尋找到信息像這樣自己最近,由Spelunky和流氓遺產已被啓發。在閱讀時,我發現有很多關於Spelunky中的水平佈局如何生成的信息,我已經設計了一個簡單版本來生成一個多維數組,並且在其中生成了一個多維數組。使用單個解決方案路徑,使用數字0-5來確定每個單元格表示的房間類型(基於上面列出的網站)。請注意,您可以根據自己的需求輕鬆地返回不同類型的結果。此外,我在整個過程中都使用了Math.random(),但是如果需要,您可以用FP.rand()替代。支持任何大小的房間x房間的級別佈局,默認爲4 x 4。我有一些隨機的結果[編輯,以使其可讀性更強]:

4 x 4   6 x 5    7 x 12 
[S][ ][ ][ ] [ ][ ][ ][ ][ ][S] [v][-][-][-][-][-][S] 
[v][ ][ ][ ] [ ][ ][v][-][-][^] [^][-][-][-][-][-][v] 
[^][-][-][v] [ ][v][^][ ][ ][ ] [ ][ ][v][-][-][-][^] 
[E][-][-][^] [v][^][ ][ ][ ][ ] [v][-][^][ ][ ][ ][ ] 
       [^][-][-][-][-][E] [v][ ][ ][ ][ ][ ][ ] 
            [^][-][-][-][-][-][v] 
            [ ][ ][v][-][-][-][^] 
            [v][-][^][ ][ ][ ][ ] 
            [v][ ][ ][ ][ ][ ][ ] 
            [^][-][-][-][-][-][v] 
            [ ][ ][ ][ ][ ][ ][v] 
            [E][-][-][-][-][-][^] 

在任何情況下,我認爲這是更適合Spelunky(哎呀,我不知道爲什麼)的四輪驅動佈局,我敢肯定我正在稍微關閉它,但這裏是我的代碼:

/**Generates a pseudo-random single solution path layout to be used for tile painting 
* @param dimenR Number of rows (Y-Axis) Should correspond with height of level, in rooms 
* @param dimenC Number of columns (X-Axis) Should correspond with width of level, in rooms 
* @return The resulting multi-dimensional array*/ 
private function generateNewRoomLayout(dimenC:int = 4, dimenR:int = 4):Array { 
    // NOTE: 0= non solution, 1= left/right, 2= left/right/down, 3= left/right/up, 4= start, 5= end 
    // Initialize array as all non-solution path rooms 
    var generatedLayout:Array = new Array(dimenR); 
    for (var i:int = 0; i < dimenR; i++) { 
     generatedLayout[i] = new Array(dimenC); 
     for (var j:int = 0; j < dimenC; j++) { generatedLayout[i][j] = 0; } 
    } 
    // Initialize our Start Room location 
    /**Position along X-Axis in the grid*/ 
    var column:int = Math.random() * dimenC; 
    /**Position along Y-Axis in the grid*/ 
    var row:int = 0; 
    generatedLayout[row][column] = 4; 
    /**Chooses the next direction. 0-1= left, 2-3= right, 5= down*/ 
    var chooseDirection:int = Math.random() * 6; 
    /**Direction solution path is currently taking. -1= left, 1= right*/ 
    var currentDirection:int = 0; 
    // Keep running until our position goes beyond the dimension of the grid 
    while (row < dimenR) { 
     // Chosen to go downards 
     if (chooseDirection == 5) { 
      // Reset which way we're going so we can make a random choice later 
      currentDirection = 0; 
      if (generatedLayout[row][column] != 4) { generatedLayout[row][column] = 2; } 
      // Bottom row 
      if (row == dimenR - 1) { 
       generatedLayout[row][column] = 5; // Found our ending point 
       break; 
      } 
      else { 
       row++; 
       generatedLayout[row][column] = 3; 
      } 
     } 
     else { 
      // If we don't have a direction to go left/right 
      if (currentDirection == 0) { 
       if (chooseDirection < 3) { currentDirection = -1; } 
       else { currentDirection = 1; } 
      } 
      // If we're at the edge of the grid and trying to move past it, we move down and flip our direction instead 
      if ((currentDirection < 0 && column == 0) || (currentDirection > 0 && column == dimenC - 1)) { 
       currentDirection = -currentDirection; 
       generatedLayout[row][column] = 2; 
       if (row == dimenR - 1) { 
        generatedLayout[row][column] = 5; 
        break; 
       } 
       else { 
        row++; 
        generatedLayout[row][column] = 3; 
       } 
      } 
      else { 
       column += currentDirection; 
       generatedLayout[row][column] = 1; 
      } 
     } 
     chooseDirection = Math.random() * 6; 
    } 
    return generatedLayout; 
} 
+0

不一定是一個不好的答案,但碰到一個一歲的問題是有點可疑的。 – 2014-07-20 22:58:30

+0

@EsaLakaniemi,在哪個意義上可疑? –

+0

對不起,碰巧遇到這個問題在谷歌搜索,同時查找類似的信息爲我自己。我想我可以下載一小段代碼來幫助別人解決這個問題 – keywi