[練習]Flash亂數迷宮

之前有在研究地下城類型的遊戲,所以做了一個亂數迷宮的產生器,算法是使用單次深入的概念,也就是迷宮會一直往下尋路,直到不能走為止,不能走後會在往回推一格繼續尋路,最後檢查完所有節點都不能走時,迷宮就完成了,用說的有點模糊,先看範例吧

maze.png


觀看範例

主要的運算寫在generateMaze,可設定牆的厚度,有興趣的人可以參考

private static function generateMaze(map:Map, startX:int, startY:int, wallWeight:int):void
{
        var nextNodeDistance:int = wallWeight + 1;
        
        //追蹤路徑,用來倒回用
        var tracePaths:Vector.<Node> = new Vector.<Node>();
        
        //更新起始點
        map.updateStartNode(startX, startY);
        
        var node:Node = map.startNode;
        node.startNodeDistance = 0;
        
        //終點
        var endNode:Node = node;
        do 
        {
                node.isWalkable = true;
                
                //可能的下個節點列表
                var posableNextNodes:Vector.<Node> = new Vector.<Node>();
                
                var checkNode:Node;
                
                //四方向
                for (var i:int = -nextNodeDistance; i <= nextNodeDistance; i += nextNodeDistance * 2)
                {
                        checkNode = map.getNode(node.x + i, node.y);
                        if (checkNode && !checkNode.isWalkable)
                                posableNextNodes.push(checkNode);
                        
                        checkNode = map.getNode(node.x, node.y + i);
                        if (checkNode && !checkNode.isWalkable)
                                posableNextNodes.push(checkNode);
                }
                
                var posableNodeCount:int = posableNextNodes.length;
                if (posableNodeCount > 0)
                {
                        //亂數決定前進方向
                        var index:int = int(Math.random() * posableNodeCount);
                        var nextNode:Node = posableNextNodes[index];
                        
                        nextNode.isWalkable = true;
                        nextNode.startNodeDistance = node.startNodeDistance + nextNodeDistance;
                        
                        var offsetX:int = (nextNode.x - node.x) / nextNodeDistance;
                        var offsetY:int = (nextNode.y - node.y) / nextNodeDistance;
                        
                        var pathX:int = node.x;
                        var pathY:int = node.y;
                        
                        var pathDistance:int = node.startNodeDistance;
                        
                        //打通牆壁
                        while (pathX != nextNode.x || pathY != nextNode.y)
                        {
                                pathX += offsetX;
                                pathY += offsetY;
                                
                                var pathNode:Node = map.getNode(pathX, pathY);
                                
                                if (pathNode)
                                {
                                        pathDistance++;
                                        pathNode.startNodeDistance = pathDistance;
                                        pathNode.isWalkable = true;
                                }
                        }
                        
                        tracePaths.push(nextNode);
                        node = nextNode;
                }
                else
                {
                        node = tracePaths.pop();
                        
                        if (node.startNodeDistance > endNode.startNodeDistance)
                                endNode = node;
                }
        }
        while (tracePaths.length > 0)
        
        map.updateEndNode(endNode.x, endNode.y);
}


原始檔下載

0 意見 :: [練習]Flash亂數迷宮