Esempio n. 1
0
        /// <summary>
        /// 部屋と部屋をつなぐ道を作成する
        /// </summary>
        /// <returns></returns>
        private void CreateRoad(TileDataMap tileDataMap, MapRectData mapRect, int indexA, int indexB)
        {
            var dataA = mapRect[indexA];
            var dataB = mapRect[indexB];

            var rectA = dataA.rect;
            var rectB = dataB.rect;
            var roomA = dataA.room;
            var roomB = dataB.room;

            var roadData = new RoadData();

            System.Predicate <TileData> createRoadData =
                (tileData_) =>
            {
                roadData.Add(tileData_.Pos);
                return(true);
            };

            // 区画は上下左右のどちらでつながっているかで処理を分ける
            if (rectA.yMax == rectB.yMin || rectA.yMin == rectB.yMax)
            {
                var x1 = Utility.Random.MaxIncludedRange(roomA.xMin, roomA.xMax - 1);
                var x2 = Utility.Random.MaxIncludedRange(roomB.xMin, roomB.xMax - 1);

                int y;

                // Aのほうが左
                var isLeftA = x1 < x2;

                var left  = Mathf.Min(x1, x2);
                var right = Mathf.Max(x1, x2) + 1;

                if (rectA.yMin > rectB.yMin)
                {
                    // B
                    // A
                    y = rectA.yMin;

                    if (isLeftA)
                    {
                        // Bと横道を繋ぐ道を作る
                        tileDataMap.FillRectRoad(x2, roomB.yMax, x2 + 1, y, createRoadData);

                        tileDataMap.FillRectRoadReverse(left, y, right, y + 1, createRoadData);

                        // Aと横道を繋ぐ道を作る
                        tileDataMap.FillRectRoad(x1, y + 1, x1 + 1, roomA.yMin, createRoadData);
                    }
                    else
                    {
                        // Bと横道を繋ぐ道を作る
                        tileDataMap.FillRectRoad(x2, roomB.yMax, x2 + 1, y, createRoadData);

                        tileDataMap.FillRectRoad(left, y, right, y + 1, createRoadData);

                        // Aと横道を繋ぐ道を作る
                        tileDataMap.FillRectRoad(x1, y + 1, x1 + 1, roomA.yMin, createRoadData);
                    }
                }
                else
                {
                    // A
                    // B
                    y = rectB.yMin;

                    if (isLeftA)
                    {
                        tileDataMap.FillRectRoad(x1, roomA.yMax, x1 + 1, y, createRoadData);
                        tileDataMap.FillRectRoad(left, y, right, y + 1, createRoadData);
                        tileDataMap.FillRectRoad(x2, y + 1, x2 + 1, roomB.yMin, createRoadData);
                    }
                    else
                    {
                        tileDataMap.FillRectRoad(x1, roomA.yMax, x1 + 1, y, createRoadData);
                        tileDataMap.FillRectRoadReverse(left, y, right, y + 1, createRoadData);
                        tileDataMap.FillRectRoad(x2, y + 1, x2 + 1, roomB.yMin, createRoadData);
                    }
                }

                // AとB両方に道のデータをもたせる
                dataA.roads.Add(roadData);

                return;
            }

            if (rectA.xMax == rectB.xMin || rectA.xMin == rectB.xMax)
            {
                var y1 = Utility.Random.MaxIncludedRange(roomA.yMin, roomA.yMax - 1);
                var y2 = Utility.Random.MaxIncludedRange(roomB.yMin, roomB.yMax - 1);

                int x;

                // Aのほうが上
                var isTopA = y1 < y2;

                var top    = Mathf.Min(y1, y2);
                var bottom = Mathf.Max(y1, y2) + 1;


                if (rectA.xMin > rectB.xMin)
                {
                    // BA
                    x = rectA.xMin;

                    if (isTopA)
                    {
                        tileDataMap.FillRectRoad(roomB.xMax, y2, x, y2 + 1, createRoadData);
                        tileDataMap.FillRectRoadReverse(x, top, x + 1, bottom, createRoadData);
                        tileDataMap.FillRectRoad(x + 1, y1, roomA.xMin, y1 + 1, createRoadData);
                    }
                    else
                    {
                        tileDataMap.FillRectRoad(roomB.xMax, y2, x, y2 + 1, createRoadData);
                        tileDataMap.FillRectRoad(x, top, x + 1, bottom, createRoadData);
                        tileDataMap.FillRectRoad(x + 1, y1, roomA.xMin, y1 + 1, createRoadData);
                    }
                }
                else
                {
                    // AB
                    x = rectB.xMin;

                    if (isTopA)
                    {
                        tileDataMap.FillRectRoad(roomA.xMax, y1, x, y1 + 1, createRoadData);
                        tileDataMap.FillRectRoad(x, top, x + 1, bottom, createRoadData);
                        tileDataMap.FillRectRoad(x + 1, y2, roomB.xMin, y2 + 1, createRoadData);
                    }
                    else
                    {
                        tileDataMap.FillRectRoad(roomA.xMax, y1, x, y1 + 1, createRoadData);
                        tileDataMap.FillRectRoadReverse(x, top, x + 1, bottom, createRoadData);
                        tileDataMap.FillRectRoad(x + 1, y2, roomB.xMin, y2 + 1, createRoadData);
                    }
                }

                // AとB両方に道のデータをもたせる
                dataA.roads.Add(roadData);

                return;
            }
        }