Пример #1
0
    /// <summary>
    /// 递归创建随机地图
    /// </summary>
    /// <param name="previousX"></param>
    /// <param name="previousY"></param>
    /// <returns></returns>
    private bool CreateMap(int previousX, int previousY)
    {
        for (int i = 0; i < 30; i++)
        {
            if (CheckCreateMapFinished())
            {
                return(true);
            }
            CommonDefine.MoveDirection dir = (CommonDefine.MoveDirection)(Random.Range(0, 100) % 4);
            int tmpX = previousX;
            int tmpY = previousY;
            switch (dir)
            {
            case CommonDefine.MoveDirection.East:
                tmpX += 1;
                break;

            case CommonDefine.MoveDirection.South:
                tmpY -= 1;
                break;

            case CommonDefine.MoveDirection.West:
                tmpX -= 1;
                break;

            case CommonDefine.MoveDirection.North:
                tmpY += 1;
                break;
            }

            if (TryMakeRoom(tmpX, tmpY))
            {
                RoomInfo prevRoom = GetRoomByXY(previousX, previousY);
                RoadInfo road     = MakeRoad(previousX, previousY, dir);
                prevRoom.AddNeibourRoads(road);
                road.AddNeibourRoom(prevRoom);
                RoomInfo curRoom = MakeRoom(tmpX, tmpY);
                curRoom.AddNeibourRoads(road);
                road.AddNeibourRoom(curRoom);
                CreateMap(tmpX, tmpY);
            }
        }

        return(false);
    }