Example #1
0
    [SerializeField] private float bias; //How far from the centre should the connection point spawn the next room?

    public IEnumerator GenerateRoom(GameObject roomToGenerate, bool isDeadEnd = false)
    {
        Quaternion rotation      = Quaternion.LookRotation(this.transform.forward);
        Vector3    position      = GenerateRelativeVector(rotation.eulerAngles.y, bias);
        GameObject generatedRoom = Instantiate(roomToGenerate, position, rotation);

        yield return(null);

        ConnectionPoint[] connectionPoints = null;
        ConnectionPoint   chosenPoint      = null;

        if (!isDeadEnd)
        {
            connectionPoints = generatedRoom.GetComponentsInChildren <ConnectionPoint>();
            chosenPoint      = connectionPoints[0];
        }
        CheckCollision validatorNewRoom = generatedRoom.GetComponentInChildren <CheckCollision>();

        validatorNewRoom.TurnOnCollider();
        yield return(new WaitForSeconds(0.03f));

        if (validatorNewRoom.GetIsCollided())
        {
            Destroy(generatedRoom);
            yield return(null);
        }
        else
        {
            this.SetConnected();
            if (!isDeadEnd)
            {
                //deactivate Deco before generating NavMesh
                generatedRoom.transform.Find("Environment").Find("Deco").gameObject.SetActive(false);

                MapGenerator.AddGeneratedRoom(generatedRoom);
                chosenPoint.SetConnected();
                MapGenerator.AddConnectionPoints(connectionPoints);
            }
        }
    }