Example #1
0
    /// <summary>
    /// Take a map of a level and a current room, then, recursivly spawns the level
    /// </summary>
    /// <param name="map">Dictionary of every room on the map</param>
    /// <param name="room">The current room to spawn</param>
    /// <param name="spawned">A list of already spawned rooms</param>
    private int spawnLevel(Dictionary<Vector2, Room> map, Room room, List<Room> spawned, int keyCountDown)
    {
        
        spawned.Add(room);
        //Create the room
        Instantiate(rooms[room.getPrefabIndex()], room.position, Quaternion.identity);
        if (!room.position.Equals(Vector2.zero))
        {
            generateEnemies(room);
        }
        else
        {


            Instantiate(playerPrefab, new Vector2(room.position.x + 3.0f, room.position.y - 3.0f), Quaternion.identity);
            Instantiate(elevatorPrefab, new Vector2(room.position.x + 2.0f, room.position.y - 2.0f), Quaternion.identity);
        }

        if (keyCountDown == 0)
        {
            //This room has the elevator's keycard
            Instantiate(keycardPrefab, new Vector2(room.position.x + 3.0f, room.position.y - 3.0f), Quaternion.identity);
        }

        --keyCountDown;

        //Go through each door for the room
        for (int e = 0; e < 4; ++e)
        {
            //If there is a door in that direction and the corresponding room hasn't already been spawned
            if (room.doors[e])
            {
                Room nextRoom;
                if (map.TryGetValue(dirToPos(room.position, e), out nextRoom) && !spawned.Contains(nextRoom))
                {
                    //Spawn corresponding room
                    keyCountDown = spawnLevel(map, nextRoom, spawned, keyCountDown);

                }
            }
        }
        return keyCountDown;
    }