/// <summary> /// Responsible for the block cutting , perfect and game over logic when user taps. /// </summary> void Tap() { //Storing positions with other component zeroed out so that the distance can be along one for score/gameover. (can also be checked on XZ plane) //Using some util methods for doing so. Makes the code smaller Vector3 currentBlockPosVector = xOrZ ? VectorHelper.GetXVector(currentBlock.transform.localPosition) : VectorHelper.GetZVector(currentBlock.transform.localPosition); Vector3 baseBlockPosVector = xOrZ ? VectorHelper.GetXVector(baseBlock.transform.localPosition) : VectorHelper.GetZVector(baseBlock.transform.localPosition); float gameOverDist = xOrZ ? currentBlock.transform.lossyScale.x * 0.5f + baseBlock.lossyScale.x * 0.5f : currentBlock.transform.lossyScale.z * 0.5f + baseBlock.lossyScale.z * 0.5f; if (Vector3.Distance(currentBlockPosVector, baseBlockPosVector) > gameOverDist) { //This is game over and the desired behaviour is that the block simply falls. //Instead of attaching a rb to this block, replacing the current block with a leftover block and disabling current block, since leftover Block already has a rigidbody attached. currentBlock.SetActive(false); GameObject leftoverBlockPiece = objectPooler.GetPooledObject(ObjectPoolItems.leftoverBlock, true); VectorHelper.CopyTransformProperties(leftoverBlockPiece.transform, currentBlock.transform); leftoverBlockPiece.GetComponent <MeshRenderer>().material = blockMat; MeshUVAdjuster.CopyUVs(currentBlock.transform, leftoverBlockPiece.transform); SoundManager.Instance.playSound(AudioClips.split); isGameRunning = false; isInputActivated = false; #if ARENABLED //Stack tower should move up in gameover to reveal its actual height. if (isARModeOn) { stackTargetPos = stackStartPos; stackMoveSpeed = moveUpSpeed; } #endif //Don't show Continue game screen if Ad is not enabled #if ADSENABLED //Decide whether to end game or present the continue option gameOverCount++; //Continue if frequency matches and current score is higher than minimum score. Else, end game. if (!isARModeOn && !alreadyContinued && frequency != 0 && gameOverCount % frequency == 0 && ScoreAndCashManager.Instance.currentScore >= minScoreToContinue) { GameManager.Instance.GameContinued(); } else { GameManager.Instance.GameOver(); } #else GameManager.Instance.GameOver(); #endif return; } //If the distance between the blocks is less than the perfetDist, It is a perfect score. YAY!! if (Vector3.Distance(currentBlockPosVector, baseBlockPosVector) < perfectDist) { PerfectScore(); } else { ChopBlock(); } BringNewBlock(); cameraController.UpdatePos(); ScoreAndCashManager.Instance.UpdateScore(); }
/// <summary> /// Responsible for instantiating and setting up next blocks on scoring. /// </summary> void BringNewBlock() { //Inverting the boolean because the blocks come from alternate directions everytime you score // If xOrZ is true the block is coming from x direction, otherwise from z. xOrZ = !xOrZ; //Get a new block from Object pooler GameObject block = objectPooler.GetPooledObject(ObjectPoolItems.Block, true); block.transform.SetParent(stack.transform); //Set the block's scale to the scale of the base block block.transform.localScale = baseBlock.transform.localScale; float x = baseBlock.transform.localPosition.x; float y = baseBlock.transform.localPosition.y + yOffset; float z = baseBlock.transform.localPosition.z; float offset = 0; #if ARENABLED if (isARModeOn) { blockCount++; //If max block height is reached feed in the lower position for the stack to move down. if (blockCount > heightCutOffInBlockCount) { stackTargetPos = stack.position - Vector3.up * yOffset; } } #endif BlockScript blockScript = block.GetComponent <BlockScript>(); //Calculating the ranges inside which the blocks will oscillate. Since the sizes of the blocks change as we progress in the game, the position also changes. // offsetFromCentre_x / offsetFromCentre_z are offsets from the centre. it is taken as 85 % of the start base block's scale towards the respective incoming direction // This offset is then added to half of incoming block's scale. This total offset is calculated so that start and end points of incoming block never overlap with the start base block. if (xOrZ) { offset = startBaseBlock.transform.lossyScale.x * 0.85f + block.transform.localScale.x * 0.5f; blockScript.setPositions( new Vector3(startBaseBlock.transform.localPosition.x + offset, y, z), new Vector3(startBaseBlock.transform.localPosition.x - offset, y, z)); } else { offset = startBaseBlock.transform.lossyScale.z * 0.85f + block.transform.localScale.z * 0.5f; blockScript.setPositions( new Vector3(x, y, startBaseBlock.transform.localPosition.z + offset), new Vector3(x, y, startBaseBlock.transform.localPosition.z - offset)); } //Start position is offset by twice so that the block starts from outside the screen Vector3 offsetVector = xOrZ ? offset * Vector3.right : offset * Vector3.forward; Vector3 yoffset = Vector3.up * yOffset; block.transform.localPosition = baseBlock.transform.localPosition - offsetVector * 2 + yoffset; block.transform.rotation = baseBlock.transform.rotation; //Create a new material for the block and set its color and texture blockMat = new Material(Shader.Find(shaderType)); blockMat.SetColor("_Color", colorController.BlockColor); blockMat.mainTexture = selectedSkinTexture; block.GetComponent <MeshRenderer>().material = blockMat; #if ARENABLED //Setting a higher Render queue for depth mask to work in AR if (isARModeOn) { blockMat.renderQueue = 3000; } #endif //Block speed if (ScoreAndCashManager.Instance.currentScore % blockSpeedResetScore == 0) { blockSpeed = defaultBlockSpeed; } else if (ScoreAndCashManager.Instance.currentScore % blockSpeedIncreaseScore == 0) { float newSpeed = blockSpeed + blockSpeedDelta; blockSpeed = newSpeed <= maxBlockSpeed ? newSpeed : maxBlockSpeed; } blockScript.speed = blockSpeed; //Set currentBlock and enable blockScript since it might be disabled if it is a pooled object. currentBlock = block; blockScript.enabled = true; //Copy the uvs from baseBlock to this newBlock because default UVs will not work as the UVs are adjusted to match a cut. MeshUVAdjuster.CopyUVs(baseBlock, currentBlock.transform); }