Beispiel #1
0
    /// <summary>
    /// This method adds a bouncy stage and obstacle objects to the game world.
    /// </summary>
    /// <param name="index">The index of the bouncy stage, of type int</param>
    private void AddBouncyStage(int index)
    {
        // Condition for getting new stage distance value for z-axis
        if (_isOffsetStageZAxis)
        {
            CalculateStageOffset(ref _offsetZAxis);
        }
        // Condition for getting new stage distance value for x-axis
        else
        {
            CalculateStageOffset(ref _offsetXAxis);
        }

        _stagePosition = Vector3.zero; // Resetting the value
                                       // to get accurate
                                       // calculation

        // Condition to change the x-axis position randomly
        if (_isOffsetStageZAxis)
        {
            CalculateStageSideOffset(ref _offsetXAxis,
                                     Random.Range(
                                         -OffsetSide,
                                         OffsetSide));
        }
        // Condition to change the z-axis position randomly
        else
        {
            CalculateStageSideOffset(ref _offsetZAxis,
                                     Random.Range(
                                         -OffsetSide,
                                         OffsetSide));
        }

        // Setting up the new stage object position
        _stagePosition.Set(_offsetXAxis,
                           _offsetYAxis,
                           _offsetZAxis);

        _currentBouncyStage = null; // Removing the previous stage

        // Storing the newly added bouncy stage
        _currentBouncyStage = BouncyStagesAvailable.GetChild(index)
                              .GetChild(0).GetComponent <BouncyStage>();

        // Setting the stage object position
        _currentBouncyStage.transform.parent.position = _stagePosition;

        // Showing the stage object
        _currentBouncyStage.transform.parent.gameObject.SetActive(true);

        // Condition to check if the stage being added is BouncyStageBreakable
        if (_currentBouncyStage.GetComponent <BouncyStageBreakable>() != null)
        {
            // Enabling the script and showing the model
            _currentBouncyStage.gameObject.SetActive(true);

            // Resetting bouncy breakable stage
            _currentBouncyStage.GetComponent <BouncyStageBreakable>().ResetStage();
        }

        // Removing the stage object from the available list
        _currentBouncyStage.transform.parent.SetParent(StageObjectsUsed);

        // Linking the current stage with the previous stage
        _currentBouncyStage.GetComponent <BouncyStage>().LinkedStage =
            StageObjectsUsed.GetChild(StageObjectsUsed.childCount - 2)
            .GetChild(0)
            .GetComponent <BouncyStage>();

        // Setting the the stage number of the stage
        _currentBouncyStage.GetComponent <BouncyStage>().StageNumber
            = _stageNumberCounter++;

        // Calculating the new position of the current stage, needed for
        // calculating the direction
        _stagePosCurrent.Set(_currentBouncyStage.transform.parent.position.x,
                             0,
                             _currentBouncyStage.transform.parent.position.z);

        // Calculating the new position of the previous stage, needed for
        // calculating the direction
        _stagePosPrevious.Set(StageObjectsUsed.GetChild(StageObjectsUsed.childCount - 2)
                              .transform.position.x,
                              0,
                              StageObjectsUsed.GetChild(StageObjectsUsed.childCount - 2)
                              .transform.position.z);

        // Rotating the current stage to face the previous stage
        _currentBouncyStage.transform.parent.rotation = Quaternion.LookRotation(
            _stagePosPrevious - _stagePosCurrent);

        // Adding the self and average points
        AddLinkPoint(_currentBouncyStage);

        _stageGeneratedCounter++; // stage object added
    }