private void StartGame() { // Load level LoadLevel(); // Calculate the fill amount float fillAmount = 1f / cirlceColors; float rotation = 360f / cirlceColors; // Set the main circle's properties mainCircle.color = currentColors[0]; mainCircle.fillAmount = fillAmount; // Loop through the circles for (int i = 1; i < cirlceColors; i++) { // Instantiate a new circle Image newCircle = Instantiate(mainCircle, canvas.transform); newCircle.color = currentColors[i]; // Set it's fill amount newCircle.fillAmount = fillAmount; newCircle.rectTransform.Rotate(0f, 0f, rotation * i); // Add it to the circles array circles.Add(newCircle); } // Add circle collider to the main circle CircleCollider2D collider = mainCircle.gameObject.AddComponent <CircleCollider2D>(); collider.radius = 270f; // Update the level's text levelText.text = "Level\n" + GamePersistence.gameData.level; // Increase the games played GamePersistence.gameData.gamesPlayed++; GamePersistence.Save(); // Hide second chance UI secondChanceLayer.SetActive(false); }
void OnSelectLevel(int level) { // Set the selected level GamePersistence.gameData.level = level; GamePersistence.Save(); // Load the game scene SceneManager.LoadScene("Game"); }
void Update() { // Did we finish the rewarded ad video ? if (AdsController.IsRewardedVideoAdFinished()) { // Increase highest level GamePersistence.gameData.highestLevel++; GamePersistence.gameData.level = GamePersistence.gameData.highestLevel; // Save GamePersistence.Save(); // Start the game scene SceneManager.LoadScene("Game"); } }
void WinGame() { // Set game over flags gameOver = true; won = true; hasWon = true; // Increase the rotation speed rotationSpeed = WIN_ROTATION_SPEED * (rotationSpeed < 0 ? -1 : 1); rotationVector.z = rotationSpeed; // Connectors on game over foreach (Image connector in connectors) { connector.GetComponent <Connector>().SetRotationSpeed(rotationSpeed); } // Static connectors on game over foreach (Image connector in staticConnectors) { connector.GetComponent <Connector>().SetRotationSpeed(rotationSpeed); } // Change connectors count text connectorsCountText.text = "Level Completed!"; connectorsCountText.color = WIN_COLOR; // Hide color and speed ui speedProgressBar.gameObject.SetActive(false); speedText.gameObject.SetActive(false); colorProgressBar.gameObject.SetActive(false); colorText.gameObject.SetActive(false); connectorsLeftText.gameObject.SetActive(false); // Increase the current level GamePersistence.gameData.level++; if (GamePersistence.gameData.level > GamePersistence.gameData.highestLevel) { GamePersistence.gameData.highestLevel = GamePersistence.gameData.level; } // If we finished the game then reset the current level if (GamePersistence.gameData.level > LevelManager.levels.Length) { GamePersistence.gameData.level = 1; } GamePersistence.Save(); // Reset level attempts levelAttempts = 0; }
public void OnRestartButton() { // Reset elements data GamePersistence.gameData.elementsData = null; GamePersistence.Save(); // Destroy all elements foreach (GameObject currentElement in elements) { Destroy(currentElement); } elements.Clear(); // Start a new game NewGame(true); // Toggle pause off TogglePause(false); }
void Start() { // Update the best score if necessary if (GameController.score > GamePersistence.gameData.bestScore) { GamePersistence.gameData.bestScore = GameController.score; } // Update played games GamePersistence.gameData.gamesPlayed++; // Reset elements data GamePersistence.gameData.elementsData = null; // Save GamePersistence.Save(); // Are we signed in to GPGS ? if (GPGSController.IsSignedIn()) { // Upload score GPGSController.PostScore(GamePersistence.gameData.bestScore); // Unlock achievements GPGSController.UnlockAchievement(GPGSResources.achievement_the_beginning); if (GameController.score >= 100) { GPGSController.UnlockAchievement(GPGSResources.achievement_just_warming_up); } if (GameController.score >= 1000) { GPGSController.UnlockAchievement(GPGSResources.achievement_getting_used); } if (GameController.score >= 20000) { GPGSController.UnlockAchievement(GPGSResources.achievement_impressive); } if (GameController.score >= 50000) { GPGSController.UnlockAchievement(GPGSResources.achievement_50k); } if (GameController.score >= 500000) { GPGSController.UnlockAchievement(GPGSResources.achievement_500k); } if (GameController.score >= 1000000) { GPGSController.UnlockAchievement(GPGSResources.achievement_millions); } if (GameController.score >= 50000000) { GPGSController.UnlockAchievement(GPGSResources.achievement_freakish); } if (GameController.score >= 1000000000) { GPGSController.UnlockAchievement(GPGSResources.achievement_just_quit); } } // Set the score text scoreText.text = "" + GameController.score; // Update best score text bestScoreText.text = "" + GamePersistence.gameData.bestScore; // Show video ad AdsController.ShowRewardedVideoAd(); // Change the sound button image accordingly soundButtonImage.sprite = GamePersistence.gameData.muted ? soundOffSprite : soundOnSprite; }
/* * This will regulary retrieve and handle the user's input * 1 - If we're not already combining elements, this will detect the first element touched * 2 - If we are combining elements, this will handle the dragging and combination of elements */ private void UpdateInput() { // Did we first touch the screen if (inputController.IsTouched()) { // Ignore hint button touch Vector3 touchScreenPosition = Camera.main.WorldToScreenPoint(inputController.GetTouchingPosition()); Vector3 buttonPosition = hintButton.transform.position; Vector3 buttonSize = new Vector3(hintButtonRect.rect.width, hintButtonRect.rect.height); if (GameMath.IsInRange(touchScreenPosition, buttonPosition, buttonSize, 0.5f)) { return; } // Get the touch position Vector2 touchingPosition = inputController.GetTouchingPosition(); Vector3 touchPosition = new Vector3(touchingPosition.x, touchingPosition.y, 0f); // Loop through the objects Vector3 position = new Vector3(); Vector3 bounds = elementSpriteRenderer.bounds.size; foreach (GameObject currentElement in elements) { // Set the element's position position = currentElement.transform.position; // Did we touch it ? if (GameMath.IsInRange(touchPosition, position, bounds, 0.8f)) { // Stop playing hint animations if available if (playingHint) { // Stop hint animations on hint elements firstHintElement.GetComponent <Animation>().Stop("ElementHinted"); secondHintElement.GetComponent <Animation>().Stop("ElementHinted"); // Restore to original size firstHintElement.transform.localScale = new Vector3(0.25f, 0.25f, 1f); secondHintElement.transform.localScale = new Vector3(0.25f, 0.25f, 1f); // Reset playing hint flag playingHint = false; } // Set it as the first touched element combinedElements.Add(currentElement); // Set the last touched element lastTouchedElement = currentElement; // Get the current element's color Color color = currentElement.GetComponent <SpriteRenderer>().color; // Create a new line CreateLine(color); // Play the element touched animation currentElement.GetComponent <Animation>().Play("ElementTouched", PlayMode.StopAll); // Get the element's number Element elementScript = currentElement.GetComponent <Element>(); long number = elementScript.GetNumber(); currentPower = elementScript.GetPower() + 1; combinedSum += number; lastNumber = number; // Play touched audio audioSource.PlayOneShot(elementSelectionAudio); break; } } } // Are we dragging ? else if (inputController.IsDragging()) { // Ensure we have a first element if (combinedElements.Count == 0) { return; } // Get the touch position Vector2 touchingPosition = inputController.GetTouchingPosition(); Vector3 touchPosition = new Vector3(touchingPosition.x, touchingPosition.y, 0f); // Change the line's position currentLineRenderer.SetPosition(0, lastTouchedElement.transform.position); currentLineRenderer.SetPosition(1, touchPosition); // Loop through the objects Vector3 position = new Vector3(); Vector3 bounds = elementSpriteRenderer.bounds.size; bounds.x /= 2f; bounds.y /= 2f; foreach (GameObject currentElement in elements) { // Is it one of the surrounding elements ? if (GameMath.GetDistanceBetweenVectors(currentElement.transform.position, lastTouchedElement.transform.position) > elementSpriteRenderer.bounds.size.x + DISTANCE_BETWEEN_ELEMENTS * 3f) { continue; } // Skip already touched elements if (combinedElements.Contains(currentElement) && currentElement != connectedElement) { // If its the last touched element then remove it from the list continue; } // Set the element's position position = currentElement.transform.position; // Did we touch it ? if (GameMath.IsInRange(touchPosition, position, bounds, 1.15f)) { // Is it the connected element ? if (currentElement == connectedElement) { // Remove the current line from the lines array lines.Remove(currentLine); // Destroy the current line Destroy(currentLine); // Set the current line to the one projected from the connected element currentLine = (GameObject)lines[lines.Count - 1]; currentLineRenderer = currentLine.GetComponent <LineRenderer>(); // Remove the current element from the combined elements list combinedElements.Remove(lastTouchedElement); // Point the last touched element to this one element lastTouchedElement = connectedElement; // Get element script Element elementScript = lastTouchedElement.GetComponent <Element>(); // Restore the power currentPower = elementScript.GetPower() + 1; // Remove its points long number = elementScript.GetNumber(); combinedSum -= number; lastNumber = number; // Set the connected element if (combinedElements.Count > 1) { connectedElement = (GameObject)combinedElements[combinedElements.Count - 2]; connectedElementNumber = connectedElement.GetComponent <Element>().GetNumber(); } else { connectedElement = null; connectedElementNumber = 0; } } else { // Can we combine it ? long number = currentElement.GetComponent <Element>().GetNumber(); if (number != lastNumber && number != connectedElementNumber + lastNumber) { continue; } // Increase power if necessary if (number != lastNumber) { currentPower++; } // Change the line's position to hit the center of this element currentLineRenderer.SetPosition(0, lastTouchedElement.transform.position); currentLineRenderer.SetPosition(1, currentElement.transform.position); // Set it as the first touched element combinedElements.Add(currentElement); // Set the connected element connectedElement = lastTouchedElement; // Set the connected element's number connectedElementNumber = lastNumber; // Update the last number combinedSum += number; lastNumber = number; // Set the last touched element lastTouchedElement = currentElement; // Get the current element's color Color color = currentElement.GetComponent <SpriteRenderer>().color; // Create a new line CreateLine(color); // Play the element touched animation currentElement.GetComponent <Animation>().Play("ElementTouched", PlayMode.StopAll); // Play touched audio audioSource.PlayOneShot(elementSelectionAudio); } break; } } } // Clear all elements after dragging else if (combinedElements.Count != 0) { // If its a single element then clear the combined elements only if (combinedElements.Count == 1) { combinedElements.Clear(); // Destroy all lines foreach (GameObject line in lines) { // Destroy line Destroy(line); } // Clear lines lines.Clear(); // Reset values lastNumber = 0; combinedSum = 0; connectedElementNumber = 0; currentPower = 0; lastTouchedElement = null; currentLine = null; currentLineRenderer = null; connectedElement = null; return; } // Loop through the combined elements foreach (GameObject currentElement in combinedElements) { // If its the last one then update its number if (currentElement == lastTouchedElement) { // Update its number currentElement.GetComponent <Element>().UpdateNumber(currentPower); } else { // Translate it to the last touched element's position currentElement.GetComponent <Element>().TranslateToObjectAndDestroy(lastTouchedElement); // Remove it from the elements array elements.Remove(currentElement); } } // Get combined elements number int combinedElementsCount = combinedElements.Count; // Clear combined elements combinedElements.Clear(); // Destroy all lines foreach (GameObject line in lines) { // Destroy line Destroy(line); } // Clear lines lines.Clear(); // Update score score += combinedSum; // Update the score UI UpdateScoreUI(); // Increase highest power if necessary if (currentPower > highestPower + 1) { highestPower = currentPower - 1; } // Calculate the number int numberScored = (int)Mathf.Pow(2, currentPower); // Reset values lastNumber = 0; combinedSum = 0; connectedElementNumber = 0; currentPower = 0; lastTouchedElement = null; currentLine = null; currentLineRenderer = null; connectedElement = null; // Play combination audio audioSource.PlayOneShot(combinationAudio); // Re arrange elements ReArrangeElements(); // Look for hints LookForHints(); // If the game is not over yet then save the elements data if (!gameOver) { GamePersistence.SetElementsData(elements); GamePersistence.gameData.lastScore = score; GamePersistence.gameData.lastTimeSinceStart = timeSinceStart; GamePersistence.gameData.lastHighestPower = highestPower; GamePersistence.Save(); // Are we signed in to GPGS ? if (GPGSController.IsSignedIn()) { // Unlock achievements GPGSController.UnlockAchievement(GPGSResources.achievement_combined); if (combinedElementsCount >= 5) { GPGSController.UnlockAchievement(GPGSResources.achievement_tiled); } if (combinedElementsCount >= 10) { GPGSController.UnlockAchievement(GPGSResources.achievement_really_tiled); } if (numberScored >= 128) { GPGSController.UnlockAchievement(GPGSResources.achievement_hundreads); } if (numberScored >= 1000) { GPGSController.UnlockAchievement(GPGSResources.achievement_thousands); } if (numberScored >= 4000) { GPGSController.UnlockAchievement(GPGSResources.achievement_4k); } if (numberScored >= 16000) { GPGSController.UnlockAchievement(GPGSResources.achievement_tens_of_thousands); } if (numberScored >= 128000) { GPGSController.UnlockAchievement(GPGSResources.achievement_hundreads_of_thousands); } if (numberScored >= 1000000) { GPGSController.UnlockAchievement(GPGSResources.achievement_millionaire); } } // Do we need to display an ad ? combinationsSinceLastAd++; if (combinationsSinceLastAd >= AD_COMBINATIONS_NUMBER) { // Show interstitial AdsController.ShowInterstitialAd(); hasJustShowenbAd = true; // Reset combinations since last ad combinationsSinceLastAd = 0; } } } }