示例#1
0
    public override void Init()
    {
        base.Init();

        LevelConfig config = ConfigModel.Instance.GetConfigForLevel(GameModel.Instance.currLevel);

        width  = config.dungeon.width;
        height = config.dungeon.height;

        minWidth = config.dungeon.minWidth;
        maxWidth = config.dungeon.maxWidth;

        minHeight = config.dungeon.minHeight;
        maxHeight = config.dungeon.maxHeight;

        quadSize = config.dungeon.quadSize;

        numOfPacks = config.dungeon.numPacks;

        wallHeight = 15;
        borderSize = 2;

        //create FOW
        _fog.GenerateFOW(width / quadSize, height / quadSize, quadSize, wallHeight + 3);

        _tree = new BSPTree(0, 0, width, height, minWidth, minHeight, maxWidth, maxHeight, quadSize);

        int       roomsCreated = 0;
        int       count        = 0;
        const int MAX_TRY      = 10;

        while (count < MAX_TRY && roomsCreated != numOfPacks)
        {
            if (_tree.Iterate())
            {
                roomsCreated++;
                count = 0;
            }
            else
            {
                count++;
            }
        }
        _tree.PrintTree();

        CreateRooms(_tree.data);

        //process data
        int runningRegionId = 0;

        foreach (var item in _tree.data)
        {
            if (item.regionId == -1)
            {
                item.regionId = runningRegionId++;
            }
            foreach (var connection in item.connectedNodes)
            {
                connection.regionId = item.regionId;
                connection.color    = item.color;
            }
        }

        //create regions
        regions = new Dictionary <int, List <BSPNode> >();
        foreach (var item in _tree.data)
        {
            if (!regions.ContainsKey(item.regionId))
            {
                regions[item.regionId] = new List <BSPNode>();
            }
            regions[item.regionId].Add(item);
        }

        //clone the regions for later use
        clonedRegions = new Dictionary <int, List <BSPNode> >();
        foreach (KeyValuePair <int, List <BSPNode> > iter in regions)
        {
            if (!clonedRegions.ContainsKey(iter.Key))
            {
                clonedRegions[iter.Key] = new List <BSPNode>();
            }
            foreach (var item in iter.Value)
            {
                clonedRegions[iter.Key].Add(item.Clone());
            }
        }

        //connect regions till no missing regions
        while (regions.Count != 1)
        {
            int key = GetKeyAtIndex(0);
            ConnectRegions(key);
        }

        //find single exit rooms
        Dictionary <int, int> numConnections = new Dictionary <int, int>();

        foreach (var item in _tree.data)
        {
            int roomId = item.id;
            foreach (var room in _tree.data)
            {
                if (room.id != roomId)
                {
                    foreach (var connection in room.connectedNodes)
                    {
                        if (connection.id == roomId)
                        {
                            if (!numConnections.ContainsKey(roomId))
                            {
                                numConnections[roomId] = 0;
                            }
                            numConnections[roomId]++;
                        }
                    }
                }
                else
                {
                    if (!numConnections.ContainsKey(roomId))
                    {
                        numConnections[roomId] = 0;
                    }
                    numConnections[roomId] += room.connectedNodes.Count;
                }
            }
        }

        foreach (KeyValuePair <int, int> numConnect in numConnections)
        {
            if (numConnect.Value == 1)
            {
                BSPNode node = FindNodeWithId(numConnect.Key);
                node.hasSingleExit = true;
            }
        }

        //create exit config
        BSPPathGenerator pathGenerator = new BSPPathGenerator();

        pathGenerator.UpdatePathData(_tree.data);

        //create room mesh
        {
            _rooms   = new List <Room>();
            _roomIds = new List <int>();

            foreach (var pack in _tree.data)
            {
                Room room = new Room(Mathf.CeilToInt(pack.rect.width), Mathf.CeilToInt(pack.rect.height), quadSize, borderSize, wallHeight, pack);
                room.gameObject.transform.SetParent(transform);
                room.generateMesh();
                room.gameObject.transform.position = new Vector3(pack.rect.x, 1, pack.rect.y);
                _rooms.Add(room);
                _roomIds.Add(room.roomData.id);
            }
        }

        //Create doors
        CreateDoors();

        //Update Path Finder
        map = new int[width / quadSize + 1, height / quadSize + 1];

        foreach (Room room in _rooms)
        {
            int[,] roomMap = room.meshData.roomBorderMesh.getMap();
            for (int row = 0; row < roomMap.GetLength(0); row++)
            {
                for (int col = 0; col < roomMap.GetLength(1); col++)
                {
                    map[(int)(room.roomData.rect.x / room.quadSize) + row, (int)(room.roomData.rect.y / room.quadSize) + col] = roomMap[row, col];
                }
            }
        }
        PathFinder.GetInstance().tileSize = quadSize;
        PathFinder.GetInstance().map      = map;
    }
    //void Update()
    //{
    //	if (Input.GetMouseButtonUp(0))
    //	{
    //		if (_rooms != null)
    //		{
    //			foreach (var room in _rooms)
    //			{
    //				Destroy(room.gameObject);
    //			}
    //			_rooms.Clear();
    //		}
    //		Start();
    //	}
    //}

    void Start()
    {
        _tree = new BSPTree(0, 0, width, height, minWidth, minHeight, maxWidth, maxHeight, quadSize);

        int roomsCreated = 0;

        while (roomsCreated != numOfPacks)
        {
            if (_tree.Iterate())
            {
                roomsCreated++;
            }
        }
        _tree.PrintTree();

        CreateRooms(_tree.data);

        //process data
        int runningRegionId = 0;

        foreach (var item in _tree.data)
        {
            if (item.regionId == -1)
            {
                item.regionId = runningRegionId++;
            }
            foreach (var connection in item.connectedNodes)
            {
                connection.regionId = item.regionId;
                connection.color    = item.color;
            }
        }

        //create regions
        regions = new Dictionary <int, List <BSPNode> >();
        foreach (var item in _tree.data)
        {
            if (!regions.ContainsKey(item.regionId))
            {
                regions[item.regionId] = new List <BSPNode>();
            }
            regions[item.regionId].Add(item);
        }

        //connect regions till no missing regions
        while (regions.Count != 1)
        {
            int key = GetKeyAtIndex(0);
            ConnectRegions(key);
        }

        //find single exit rooms
        Dictionary <int, int> numConnections = new Dictionary <int, int>();

        foreach (var item in _tree.data)
        {
            int roomId = item.id;
            foreach (var room in _tree.data)
            {
                if (room.id != roomId)
                {
                    foreach (var connection in room.connectedNodes)
                    {
                        if (connection.id == roomId)
                        {
                            if (!numConnections.ContainsKey(roomId))
                            {
                                numConnections[roomId] = 0;
                            }
                            numConnections[roomId]++;
                        }
                    }
                }
                else
                {
                    if (!numConnections.ContainsKey(roomId))
                    {
                        numConnections[roomId] = 0;
                    }
                    numConnections[roomId] += room.connectedNodes.Count;
                }
            }
        }

        foreach (KeyValuePair <int, int> numConnect in numConnections)
        {
            if (numConnect.Value == 1)
            {
                BSPNode node = FindRoomWithId(numConnect.Key);
                node.hasSingleExit = true;
            }
        }

        //create exit config
        BSPPathGenerator pathGenerator = new BSPPathGenerator();

        pathGenerator.UpdatePathData(_tree.data);

        //create room mesh
        if (generateMesh)
        {
            _rooms = new List <Room>();
            foreach (var pack in _tree.data)
            {
                Room room = new Room("Prefabs/Room", Mathf.CeilToInt(pack.rect.width), Mathf.CeilToInt(pack.rect.height), quadSize, borderSize, wallHeight, pack);
                room.generateMesh();
                room.gameObject.transform.position = new Vector3(pack.rect.x, 1, pack.rect.y);
                _rooms.Add(room);
            }
        }
    }