Exemplo n.º 1
0
    private WorldData.RoomCell BuildRoomsRecurrent(ref WorldData world, Room.MapPos pos, int length)
    {
        var cell = world.RoomMap[pos.x][pos.y];

        if (cell.IsEmpty())
        {
            RoomInfo roomInfo = Info.RoomsLeft != world.RoomCount ? RandomizePrefab(ref world)
                : new RoomInfo()
            {
                Prefab = FirstRoom, Type = Room.RoomType.Normal
            };

            var go   = Instantiate(roomInfo.Prefab, WorldParent) as GameObject;
            var room = go.GetComponent <Room>();
            room.MapPosition = pos;
            room.gameObject.SetActive(false);

            cell = new WorldData.RoomCell(roomInfo.Type, room);

            world.RoomMap[pos.x][pos.y] = cell;
            world.AllRooms.Add(room);
        }

        if (Info.RoomsLeft > 0 && cell.Type != Room.RoomType.Boss)
        {
            BuildChildRooms(ref world, cell, length);
        }

        return(cell);
    }
Exemplo n.º 2
0
    private void BuildChildRooms(ref WorldData world, WorldData.RoomCell cell, int length)
    {
        var room = cell.Reference;

        room.IsMainRoom = !Info.HasBoss;

        length = room.IsMainRoom ? CorridorLength : length - 1;

        int possibleCount;
        List <Room.DoorDirection> directions = CheckFreeConnections(ref world, out possibleCount, cell);
        int roomCount = Mathf.Min(possibleCount, Info.RoomsLeft);

        while (roomCount > 0 && Info.RoomsLeft > 0 && directions.Any() && length > 0)
        {
            int index = Random.Range(0, directions.Count);
            Room.DoorDirection dir = directions[index];

            roomCount--;
            Info.RoomsLeft--;

            directions.RemoveAt(index);

            var newRoom = BuildRoomsRecurrent(ref world, room.MapPosition + Room.RoomOffset[dir], length);
            room.SetConnection(dir, newRoom, cell);

            if (!allowLoops)
            {
                directions = CheckFreeConnections(ref world, out possibleCount, cell);
                roomCount  = Mathf.Min(possibleCount, Info.RoomsLeft);
            }
        }
    }
Exemplo n.º 3
0
    public void SetConnection(DoorDirection direction, WorldData.RoomCell cell, WorldData.RoomCell thisCell)
    {
        if (cell.Type == RoomType.Empty || cell.Reference == null)
        {
            return;
        }

        Connections[direction] = cell;
        cell.Reference.Connections[InverseDirection[direction]] = thisCell;
    }
Exemplo n.º 4
0
    public WorldData.RoomCell GetConnectedRoomCell(DoorDirection direction)
    {
        WorldData.RoomCell result = new WorldData.RoomCell();

        if (Connections.ContainsKey(direction))
        {
            result = Connections[direction];
        }

        return(result);
    }
Exemplo n.º 5
0
    public WorldData.RoomCell GetGlobalRoomCell(WorldData data, Room.DoorDirection direction)
    {
        var offset = RoomOffset[direction];
        var pos    = MapPosition + offset;

        if (HasEdge(data, direction))
        {
            return(new WorldData.RoomCell());
        }

        WorldData.RoomCell result = data.RoomMap[pos.x][pos.y];

        return(result);
    }
Exemplo n.º 6
0
    public IList <Room> GetAdjacent(Room room)
    {
        IList <Room> result = new List <Room>();

        foreach (Room.DoorDirection dir in Room.AllDirs)
        {
            WorldData.RoomCell cell = room.GetGlobalRoomCell(Data, dir);
            if (cell.Type != Room.RoomType.Empty)
            {
                result.Add(cell.Reference);
            }
        }

        return(result);
    }
Exemplo n.º 7
0
    private FindResult FindFurthest(WorldData.RoomCell cell, ref List <Room> processed, int distance = 0)
    {
        var thisDistance = distance + 1;
        var result       = new FindResult()
        {
            Room = cell.Reference, Distance = thisDistance
        };

        processed.Add(cell.Reference);

        foreach (var connection in cell.Reference.Connections)
        {
            if (!processed.Contains(connection.Value.Reference) && connection.Value.Reference.KeyLock == null)
            {
                var next = FindFurthest(connection.Value, ref processed, thisDistance);
                if (next.Distance > result.Distance)
                {
                    result = next;
                }
            }
        }

        return(result);
    }
Exemplo n.º 8
0
 public void Init(WorldData.RoomCell room)
 {
     Cell = room;
 }
Exemplo n.º 9
0
 public bool HasRoom(WorldData data, Room.DoorDirection direction)
 {
     WorldData.RoomCell cell = GetGlobalRoomCell(data, direction);
     return(cell.Type != RoomType.Empty);
 }
Exemplo n.º 10
0
    private List <Room.DoorDirection> CheckFreeConnections(ref WorldData world, out int count, WorldData.RoomCell cell)
    {
        List <Room.DoorDirection> result = new List <Room.DoorDirection>();

        count = 0;

        foreach (Room.DoorDirection dir in Room.AllDirs)
        {
            var room = cell.Reference.GetGlobalRoomCell(world, dir);

            if (room.Type == Room.RoomType.Empty &&
                !cell.Reference.HasEdge(world, dir))
            {
                result.Add(dir);
                count++;
            }
        }

        return(result);
    }
Exemplo n.º 11
0
    private Room.DoorDirection CheckFreeConnections(ref WorldData world, out int count, WorldData.RoomCell cell)
    {
        Room.DoorDirection result = Room.DoorDirection.None;
        count = 0;

        foreach (Room.DoorDirection dir in Room.AllDirs)
        {
            var room = cell.Reference.GetGlobalRoomCell(world, dir);

            if (room.Type == Room.RoomType.Empty &&
                !cell.Reference.HasEdge(world, dir))
            {
                result |= dir;
                count++;
            }
        }

        return(result);
    }
Exemplo n.º 12
0
    private WorldData.RoomCell BuildRoomsRecurrent(ref WorldData world, Room.MapPos pos, int length)
    {
        RoomInfo roomInfo = Info.RoomsLeft != world.RoomCount ? RandomizePrefab(ref world)
            : new RoomInfo()
        {
            Prefab = FirstRoom, Type = Room.RoomType.Normal
        };

        var go   = Instantiate(roomInfo.Prefab, WorldParent) as GameObject;
        var room = go.GetComponent <Room>();

        room.MapPosition = pos;
        room.gameObject.SetActive(false);

        var cell = new WorldData.RoomCell(roomInfo.Type, room);

        /*if (roomInfo.Type == Room.RoomType.Boss)
         * {
         *  room.KeyLock = new Door.Key() {color = Color.red};
         *  Info.Keys.Push(new KeyInfo() { Key = room.KeyLock, HasLock = true});
         *
         *  for (int i = 0; i < Info.KeysCount; i++)
         *  {
         *      var key = Keys[i];
         *
         *      Info.Keys.Push(new KeyInfo() { Key = key });
         *  }
         * }*/

        world.RoomMap[pos.x][pos.y] = cell;
        world.AllRooms.Add(room);

        if (Info.RoomsLeft > 0 && roomInfo.Type != Room.RoomType.Boss)
        {
            ProcessRooms(ref world, cell, length);
        }

        /*if (Info.HasBoss
         * && roomInfo.Type != Room.RoomType.Boss
         * && room != world.FirstRoom
         * && Info.Keys.Any())
         * {
         *  var keyInfo = Info.Keys.Peek();
         *  if (!keyInfo.HasPickup)
         *  {
         *      keyInfo.HasPickup = true;
         *      room.KeyPickup = keyInfo.Key;
         *  }
         *
         *  if (!keyInfo.HasLock && !room.IsMainRoom)
         *  {
         *      keyInfo.HasLock = true;
         *      room.KeyLock = keyInfo.Key;
         *  }
         *
         *  if (keyInfo.HasLock && keyInfo.HasPickup)
         *  {
         *      Info.Keys.Pop();
         *  }
         * }*/

        return(cell);
    }
Exemplo n.º 13
0
    private void ProcessRooms(ref WorldData world, WorldData.RoomCell cell, int length)
    {
        var room = cell.Reference;

        room.IsMainRoom = !Info.HasBoss;

        length = room.IsMainRoom ? CorridorLength : length - 1;

        int possibleCount;
        var potentialDoors = CheckFreeConnections(ref world, out possibleCount, cell);
        int roomCount      = Mathf.Min(possibleCount, Info.RoomsLeft);

        IList <int> directionList = new List <int>()
        {
            0, 1, 2, 3
        };

        while (roomCount > 0 && Info.RoomsLeft > 0 && potentialDoors != Room.DoorDirection.None && length > 0)
        {
            int index = Random.Range(0, directionList.Count);
            int dir   = directionList[index];

            switch (dir)
            {
            case 0:
                if ((potentialDoors & Room.DoorDirection.Left) != 0)
                {
                    potentialDoors &= ~Room.DoorDirection.Left;

                    roomCount--;
                    Info.RoomsLeft--;
                    directionList.RemoveAt(index);

                    var newRoom = BuildRoomsRecurrent(ref world, room.MapPosition + new Room.MapPos(-1, 0), length);
                    //newRoom.Reference.SetConnection(Room.InverseDirection[Room.DoorDirection.Left], cell);
                    room.SetConnection(Room.DoorDirection.Left, newRoom, cell);
                }
                break;

            case 1:
                if ((potentialDoors & Room.DoorDirection.Right) != 0)
                {
                    potentialDoors &= ~Room.DoorDirection.Right;

                    roomCount--;
                    Info.RoomsLeft--;
                    directionList.RemoveAt(index);

                    var newRoom = BuildRoomsRecurrent(ref world, room.MapPosition + new Room.MapPos(1, 0), length);
                    //newRoom.Reference.SetConnection(Room.InverseDirection[Room.DoorDirection.Right], cell);
                    room.SetConnection(Room.DoorDirection.Right, newRoom, cell);
                }
                break;

            case 2:
                if ((potentialDoors & Room.DoorDirection.Down) != 0)
                {
                    potentialDoors &= ~Room.DoorDirection.Down;

                    roomCount--;
                    Info.RoomsLeft--;
                    directionList.RemoveAt(index);

                    var newRoom = BuildRoomsRecurrent(ref world, room.MapPosition + new Room.MapPos(0, -1), length);
                    //newRoom.Reference.SetConnection(Room.InverseDirection[Room.DoorDirection.Down], cell);
                    room.SetConnection(Room.DoorDirection.Down, newRoom, cell);
                }
                break;

            case 3:
                if ((potentialDoors & Room.DoorDirection.Up) != 0)
                {
                    potentialDoors &= ~Room.DoorDirection.Up;

                    roomCount--;
                    Info.RoomsLeft--;
                    directionList.RemoveAt(index);

                    var newRoom = BuildRoomsRecurrent(ref world, room.MapPosition + new Room.MapPos(0, 1), length);
                    //newRoom.Reference.SetConnection(Room.InverseDirection[Room.DoorDirection.Up], cell);
                    room.SetConnection(Room.DoorDirection.Up, newRoom, cell);
                }
                break;
            }
        }
    }