Exemple #1
0
    public void GenerateNewTree()
    {
        //Stop the camera movement if it's still moving
        if (cameraMovement != null)
        {
            StopCoroutine(cameraMovement);
        }

        //Check if we finished the current round
        if (treeCountInRound <= 0)
        {
            roundFinished?.Invoke(this, EventArgs.Empty);
            StartNewRound();
            return;
        }

        //Get a new tree from the pool
        Tree tree = treePool.GetPooledObject <Tree>();

        //Get the position of the previous tree and do any necessary cleanup to it
        Vector3 previousTreePosition = Vector3.zero;

        if (currentTree != null)
        {
            previousTreePosition       = currentTree.transform.position;
            currentTree.treeDestroyed -= TreeDestroyedEventHandler;
            StartCoroutine(ReturnTreeToPoolRoutine(currentTree)); //Just so when we click the "new tree" button the current doesn't suddenly disappear, we return it to the pool after the camera already moved
        }

        //Position the new tree behind the current tree and to a random side
        Vector3 treeOffset = currentTree == null ? Vector3.zero : new Vector3((UnityEngine.Random.Range(0, 2) * 2) - 1, 0, 1) * newTreeDistance;

        tree.transform.position = previousTreePosition + treeOffset;
        tree.treeDestroyed     += TreeDestroyedEventHandler;
        currentTree             = tree;
        currentTree.GenerateTree();

        //Move the camera
        cameraMovement = StartCoroutine(MoveCameraToTree(tree)); //We keep a reference of this coroutine so we stop it in case this method is called again before the camera movement ends

        treeCountInRound--;
    }
Exemple #2
0
    private void GenerateTrunk(int size)
    {
        //Clear the list of pieces if any still exists
        while (piecesCollection.Count > 0)
        {
            TreePiece piece = piecesCollection.Dequeue();
            piece.ReturnToPool();
        }

        treeSize = size;
        trunkParent.localPosition = Vector3.zero;

        //Create new pieces based on the size
        for (int i = 0; i < size; i++)
        {
            TreePiece piece = treePiecesPool.GetPooledObject <TreePiece>();
            piece.transform.SetParent(trunkParent);
            piece.transform.SetAsFirstSibling();
            piece.transform.localPosition = Vector3.up * i;
            piece.transform.localScale    = Vector3.one;
            piece.transform.localRotation = Quaternion.AngleAxis(UnityEngine.Random.Range(0, 4) * 90, Vector3.up);
            piecesCollection.Enqueue(piece);
        }
    }