Ejemplo n.º 1
0
    public void GenerateFloor()
    {
        FloorManager new_floor = new FloorManager();

        new_floor.max_dist  += floors.Count;
        new_floor.num_rooms += floors.Count;
        Room current_room = new_floor.rooms[0];

        if (floors.Count > 0)
        {
            new_floor.height = floors[floors.Count - 1].height - 5;
        }
        while (new_floor.rooms.Count < new_floor.num_rooms)
        {
            int north = 0;
            int east  = 0;
            List <Vector2Int> directions = new List <Vector2Int>();
            if (new_floor.entrance != FloorManager.EntranceDirection.North)
            {
                directions.Add(new Vector2Int(0, 1));
            }
            if (new_floor.entrance != FloorManager.EntranceDirection.South)
            {
                directions.Add(new Vector2Int(0, -1));
            }
            if (new_floor.entrance != FloorManager.EntranceDirection.East)
            {
                directions.Add(new Vector2Int(1, 0));
            }
            if (new_floor.entrance != FloorManager.EntranceDirection.West)
            {
                directions.Add(new Vector2Int(-1, 0));
            }

            int dir_id = Random.Range(0, directions.Count);

            Room new_room = new Room(current_room.grid_coords + directions[dir_id]);

            north = directions[dir_id].y;
            east  = directions[dir_id].x;


            if (Vector2Int.Distance(new_floor.rooms[0].grid_coords, new_room.grid_coords) > new_floor.max_dist)
            {
                current_room = new_floor.rooms[0];
                continue;
            }

            if (east == 1)
            {
                current_room.east_open = true;
            }
            else if (east == -1)
            {
                current_room.west_open = true;
            }
            else if (north == 1)
            {
                current_room.north_open = true;
            }
            else if (north == -1)
            {
                current_room.south_open = true;
            }

            if (new_floor.GetRoom(new_room.grid_coords) == null)
            {
                new_floor.rooms.Add(new_room);
                current_room = new_room;
            }
            else
            {
                current_room = new_floor.GetRoom(new_room.grid_coords);
            }

            if (east == 1)
            {
                current_room.west_open = true;
            }
            else if (east == -1)
            {
                current_room.east_open = true;
            }
            else if (north == 1)
            {
                current_room.south_open = true;
            }
            else if (north == -1)
            {
                current_room.north_open = true;
            }
        }
        Room furthest_room = new_floor.rooms[0];

        foreach (var r in new_floor.rooms)
        {
            if (r.HasClosedDoor() && Vector2Int.Distance(r.grid_coords, new_floor.rooms[0].grid_coords) > Vector2Int.Distance(furthest_room.grid_coords, new_floor.rooms[0].grid_coords))
            {
                furthest_room = r;
            }
        }


        if (!furthest_room.north_open)
        {
            new_floor.exit           = FloorManager.EntranceDirection.North;
            furthest_room.north_open = true;
        }
        else if (!furthest_room.south_open)
        {
            new_floor.exit           = FloorManager.EntranceDirection.South;
            furthest_room.south_open = true;
        }
        else if (!furthest_room.east_open)
        {
            new_floor.exit          = FloorManager.EntranceDirection.East;
            furthest_room.east_open = true;
        }
        else
        {
            new_floor.exit          = FloorManager.EntranceDirection.West;
            furthest_room.west_open = true;
        }
        new_floor.exit_room = furthest_room;

        Debug.Log($"Floor count is : {floors.Count}");
        floors.Add(new_floor);
        Debug.Log($"Added floor, floor count is : {floors.Count}");
        floor_num++;

        int id = Random.Range(0, enemy_type_lists.Count);

        new_floor.enemy_types = enemy_type_lists[id];

        ServiceLocator.SetCurrentFloor(new_floor);
    }