Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
- using PaintIn3D;
- using PaintIn3D.Examples;
- using UnityEngine.SceneManagement;
- using TMPro;
- using GameAnalyticsSDK;
- using PathCreation;
- namespace newScripts
- {
- public class GameManager : MonoBehaviour
- {
- public static int levelIndex = 0;
- private static int diamonds;
- private static int coins;
- private static bool allLevelsCleared = false;
- private static int levelNumber = 1;
- public static bool isLevelCompleted = false;
- public GameObject[] colorButtons;
- public List<Items> paintableItem = new List<Items>();
- public Image progressBar;
- public Image matchPanelprogressBar;
- public float matchPanelprogressBarFillSpeed = 0.001f;
- public P3dHitBetween p3DHitBetween;
- public ParticleSystem paintParticles;
- public GameObject confetti;
- //public GameObject doneButton;
- public GameObject MatchPanel;
- public GameObject levelCountBG;
- public GameObject brushTouchPoint;
- public GameObject brushChildPrefab;
- public GameObject onMouseDragGameObject; //disable this gameobject to disable brush movement
- private P3dColor p3DColor;
- [SerializeField]
- private GameObject colorPanel;
- private Material brushTipMaterial;
- private Material[] childBrushTipMaterial;
- private float targetProgress = 0;
- public GameObject[] star;
- public GameObject[] MatchPanelstar;
- public RawImage matchSneakerImage;
- [SerializeField]
- private RawImage targetSneakerImage;
- public Image bgPanelImage;
- public Sprite[] bgImage;
- public TextMeshProUGUI levelCountText;
- public TextMeshProUGUI matchPercentageText;
- public TextMeshProUGUI coinsText;
- public TextMeshProUGUI diamondsText;
- public static Transform activeObjecttransform;
- public Vector4 RemainingAlphaChannelValue;
- public Animator animator;
- private Animator sneakerAnimator;
- private float time = 0;
- private ButtonClicked selectedButton;
- bool isButtonClicked = false;
- bool brushColorSelected = false;
- bool isAlphaChannelCalculated = false;
- float totalAlphaChannelCounted;
- private Color buttonColor;
- [Header("OWL GAME STUDIO")]
- public PathCreator currentPath = null;
- public Transform brush = null;
- public EndOfPathInstruction endOfPathInstruction;
- public float speed = 5;
- float distanceTravelled;
- private int currentColor = 0;
- private int maxColor = 0;
- public List<Animator> colorAnimators = new List<Animator>();
- public GameObject m_textHold = null;
- public GameObject m_textRelease = null;
- private void Awake()
- {
- // ClearPlayerPrefs();
- brushTipMaterial = brushTouchPoint.GetComponent<MeshRenderer>().materials[1];
- // brushTipMaterial.color = Color.red;
- childBrushTipMaterial = brushChildPrefab.GetComponent<MeshRenderer>().materials;
- }
- private void Start()
- {
- QualitySettings.vSyncCount = 0;
- Application.targetFrameRate = 60;
- LoadData();
- //playerPrefsManager.LoadData(levelNumber, levelIndex, coins, diamonds);
- star[0].SetActive(true);
- star[1].SetActive(true);
- star[2].SetActive(true);
- levelCountBG.SetActive(true);
- isLevelCompleted = false;
- for (int i = 0; i < colorButtons.Length; i++)
- {
- for (int j = 0; j < paintableItem[levelIndex].paintableColors.Count; j++)
- {
- if (colorButtons[i].name.ToString() == paintableItem[levelIndex].paintableColors[j].ToString())
- {
- colorButtons[i].SetActive(true);
- colorAnimators.Add(colorButtons[i].GetComponent<Animator>());
- // Color buttonColor = colorButtons[i].GetComponent<Image>().color;
- //if (brushColorSelected == false)
- //{
- // paintableItem[levelIndex].paintableParts[j].GetComponent<P3dPaintable>().enabled = true;
- // brushColorSelected = true;
- // Color buttonColor = colorButtons[i].GetComponent<Button>().colors.pressedColor;
- // Debug.Log("Selected color is: " + colorButtons[i]);
- // colorButtons[i].GetComponent<Animator>().SetBool("selected", true);
- // ChangeBrushColor(buttonColor);
- //}
- //else
- //{
- // paintableItem[levelIndex].paintableParts[j].GetComponent<P3dPaintable>().enabled = false;
- //}
- paintableItem[levelIndex].paintableParts[j].GetComponent<P3dPaintable>().enabled = false;
- }
- }
- }
- paintableItem[levelIndex].paintableParts[currentColor].GetComponent<P3dPaintable>().enabled = true;
- Color buttonColor = colorAnimators[currentColor].GetComponent<Button>().colors.pressedColor;
- colorAnimators[currentColor].GetComponent<Animator>().SetBool("selected", true);
- ChangeBrushColor(buttonColor);
- m_textHold.SetActive(true);
- m_textRelease.SetActive(false);
- paintableItem[levelIndex].itemPrefab.gameObject.SetActive(true);
- currentPath = paintableItem[levelIndex].paintablePaths[currentColor];
- maxColor = paintableItem[levelIndex].paintableColors.Count;
- SDKManager.instance.LevelStarted(levelNumber, levelNumber);
- Debug.Log("Level Started");
- GameAnalytics.NewProgressionEvent(GAProgressionStatus.Start, Application.version, levelNumber.ToString());
- brushChildPrefab.gameObject.SetActive(true);
- brushTouchPoint.SetActive(true);
- //This code below works prfect than the below but is complex
- activeObjecttransform = paintableItem[levelIndex].itemPrefab.transform;
- p3DColor = paintableItem[levelIndex].itemPrefab.gameObject.GetComponent<P3dColor>();
- onMouseDragGameObject.SetActive(true);
- SelectBG();
- matchSneakerImage.texture = paintableItem[levelIndex].matchImage.texture;
- sneakerAnimator = paintableItem[levelIndex].itemPrefab.gameObject.GetComponent<Animator>();
- progressBar.gameObject.SetActive(true);
- RemainingAlphaChannelValue = P3dChannelCounter.GetRatioRGBA();
- diamondsText.text = diamonds.ToString(); //display the total gems
- coinsText.text = coins.ToString(); //display the coins total
- }
- private void Update()
- {
- UpdateProgressBar();
- if(matchPanelprogressBar.fillAmount<targetProgress && isLevelCompleted == true)
- {
- matchPanelprogressBar.fillAmount += matchPanelprogressBarFillSpeed * Time.deltaTime;
- }
- DisplaymatchPanelStars();
- matchPercentageText.text = "Match " + (Mathf.Round(matchPanelprogressBar.fillAmount * 100) +"%");
- levelCountText.text = "LEVEL " + (levelNumber);
- }
- private void UpdateMatchPanelProgressBar(float progress)
- {
- targetProgress = matchPanelprogressBar.fillAmount + progress;
- }
- void DisplaymatchPanelStars()
- {
- if (matchPanelprogressBar.fillAmount >= 0.33)
- {
- MatchPanelstar[0].GetComponent<Image>().color = Color.yellow;
- }
- if (matchPanelprogressBar.fillAmount >= 0.65 && matchPanelprogressBar.fillAmount <= 0.80)
- {
- MatchPanelstar[1].GetComponent<Image>().color = Color.yellow;
- }
- if (matchPanelprogressBar.fillAmount >= 0.90f && matchPanelprogressBar.fillAmount <= 0.98f)
- {
- MatchPanelstar[2].GetComponent<Image>().color = Color.yellow;
- }
- }
- void UpdateProgressBar()
- {
- CheckForInput();
- if (RemainingAlphaChannelValue.x == 0 & RemainingAlphaChannelValue.y == 0 & RemainingAlphaChannelValue.z == 0)
- {
- progressBar.fillAmount = 0;
- }
- else
- {
- if(!isAlphaChannelCalculated)
- {
- isAlphaChannelCalculated = true;
- totalAlphaChannelCounted = RemainingAlphaChannelValue.w;
- }
- progressBar.fillAmount = 1 - (RemainingAlphaChannelValue.w/ totalAlphaChannelCounted );
- }
- if (progressBar.fillAmount >= 0.3)
- {
- star[0].GetComponent<Image>().color = Color.yellow;
- }
- if (progressBar.fillAmount >= 0.6 && progressBar.fillAmount <= 0.75)
- {
- star[1].GetComponent<Image>().color = Color.yellow;
- }
- if(progressBar.fillAmount > 0.95f && isLevelCompleted == false)
- {
- star[2].GetComponent<Image>().color = Color.yellow;
- }
- if (progressBar.fillAmount >= 0.999f && isLevelCompleted == false)
- {
- //isLevelCompleted = true;
- confetti.SetActive(true);
- Invoke("FinishPainting", 0.5f);
- }
- }
- void SelectBG()
- {
- //bgPanelImage.sprite = paintableItem[levelIndex].bgImage;
- bgPanelImage.sprite = bgImage[Random.Range(0,bgImage.Length)];
- }
- public void LoadNextLevel()
- {
- paintableItem[levelIndex].itemPrefab.gameObject.SetActive(false);
- if (levelIndex < paintableItem.Count-1 && allLevelsCleared == false)
- {
- if(levelIndex == paintableItem.Count - 1)
- {
- allLevelsCleared = true;
- }
- // Debug.Log("Control is inside if statement");
- levelIndex++;
- }
- else
- {
- allLevelsCleared = true;
- levelIndex = Random.Range(0, paintableItem.Count - 1);
- // Debug.Log("Control is inside else statement");
- }
- // Debug.Log("Loading level: " + levelIndex);
- //playerPrefsManager.SaveData(levelNumber, levelIndex, coins, diamonds);
- SaveData(levelNumber, levelIndex, coins, diamonds);
- SceneManager.LoadScene(0);
- }
- public void ReloadScene()
- {
- SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
- }
- void CheckForInput()
- {
- if (Input.GetMouseButton(0))
- {
- p3DHitBetween.enabled = true;
- if (currentPath != null)
- {
- distanceTravelled += speed * Time.deltaTime;
- brush.position = currentPath.path.GetPointAtDistance(distanceTravelled, endOfPathInstruction);
- if (distanceTravelled >= (currentPath.path.length * 0.95f))
- {
- m_textHold.SetActive(false);
- m_textRelease.SetActive(true);
- }
- }
- }
- else if (Input.GetMouseButtonUp(0))
- {
- p3DHitBetween.enabled = false;
- distanceTravelled = 0;
- currentColor++;
- if (currentColor < maxColor)
- {
- m_textHold.SetActive(true);
- m_textRelease.SetActive(false);
- currentPath = paintableItem[levelIndex].paintablePaths[currentColor];
- NextColor(currentColor);
- }
- else
- {
- Invoke("FinishPainting", 1f);
- }
- }
- }
- private void NextColor(int i)
- {
- for (int c = 0; c < colorAnimators.Count; c++)
- {
- colorAnimators[c].SetBool("selected", false);
- }
- colorAnimators[currentColor].SetBool("selected", true);
- for (int j = 0; j < paintableItem[levelIndex].paintableParts.Length; j++)
- {
- paintableItem[levelIndex].paintableParts[j].GetComponent<P3dPaintable>().enabled = false;
- }
- paintableItem[levelIndex].paintableParts[i].GetComponent<P3dPaintable>().enabled = true;
- AnimateParts(paintableItem[levelIndex].paintableParts[i]);
- switch (paintableItem[levelIndex].paintableColors[i])
- {
- case Items.selectColors.Red:
- ChangeBrushColor(Color.red);
- break;
- case Items.selectColors.Black:
- ChangeBrushColor(Color.black);
- break;
- case Items.selectColors.Green:
- ChangeBrushColor(Color.green);
- break;
- case Items.selectColors.Yellow:
- ChangeBrushColor(Color.yellow);
- break;
- case Items.selectColors.White:
- ChangeBrushColor(Color.white);
- break;
- case Items.selectColors.Gray:
- ChangeBrushColor(Color.grey);
- break;
- case Items.selectColors.Blue:
- ChangeBrushColor(Color.black);
- break;
- case Items.selectColors.Pink:
- ChangeBrushColor(new Color(1, 0.6462264f, 0.9492261f));
- break;
- case Items.selectColors.LightBlue:
- ChangeBrushColor(new Color(0, 0.8662651f, 1));
- break;
- case Items.selectColors.Purple:
- ChangeBrushColor(new Color(0.7773685f, 1, 1));
- break;
- case Items.selectColors.Orange:
- ChangeBrushColor(new Color(0, 0.561501f, 0));
- break;
- case Items.selectColors.Cream:
- ChangeBrushColor(new Color(0.9647059f, 0.8509805f, 0.4745098f));
- break;
- case Items.selectColors.LightRed:
- ChangeBrushColor(new Color(0.8274511f, 0.4509804f, 0.3254902f));
- break;
- }
- }
- public void FinishPainting()
- {
- if (isLevelCompleted == false)
- {
- m_textHold.SetActive(false);
- m_textRelease.SetActive(false);
- star[0].SetActive(false);
- star[1].SetActive(false);
- star[2].SetActive(false);
- MatchPanel.SetActive(true);
- //levelCountBG.SetActive(false); //Disable dispalying level count text parent gameobject
- onMouseDragGameObject.SetActive(false);
- confetti.SetActive(true);
- targetSneakerImage.GetComponent<RawImage>().texture = paintableItem[levelIndex].matchImage.texture;
- brushChildPrefab.gameObject.SetActive(false);
- brushTouchPoint.SetActive(false);
- for (int i = 0; i < colorButtons.Length; i++)
- {
- colorButtons[i].SetActive(false);
- }
- colorPanel.SetActive(false);
- isLevelCompleted = true;
- progressBar.gameObject.SetActive(false);
- UpdateMatchPanelProgressBar(progressBar.fillAmount);
- GameAnalytics.NewProgressionEvent(GAProgressionStatus.Complete, Application.version, levelNumber.ToString());
- SDKManager.instance.LevelFinished(levelNumber, levelNumber);
- Debug.Log("Level Finished: "+levelNumber );
- diamonds++;
- coins += 50;
- levelNumber++;
- // SaveData(levelNumber, levelIndex, coins, diamonds);
- }
- }
- void AnimateParts(GameObject obj)
- {
- obj.GetComponent<Animator>().Play("FlickAlphaValue");
- }
- public void SwitchParts()
- {
- selectedButton = EventSystem.current.currentSelectedGameObject.GetComponent<ButtonClicked>();
- for (int i = 0; i < paintableItem[levelIndex].paintableParts.Length; i++)
- {
- if (paintableItem[levelIndex].paintableColors[i].ToString() == EventSystem.current.currentSelectedGameObject.name)
- {
- //Debug.Log("Paint part no: " + paintableItem[levelIndex].paintableParts[i]);
- paintableItem[levelIndex].paintableParts[i].GetComponent<P3dPaintable>().enabled = true;
- if(selectedButton.buttonClicked == false)
- {
- selectedButton.buttonClicked = true;
- AnimateParts(paintableItem[levelIndex].paintableParts[i]);
- }
- }
- else
- {
- paintableItem[levelIndex].paintableParts[i].GetComponent<P3dPaintable>().enabled = false;
- }
- }
- }
- public void ChangeBrushColor(Color color)
- {
- // Debug.Log("Processed brush color function");
- brushTipMaterial.color = color;
- paintParticles.GetComponent<ParticleSystemRenderer>().material.color = color;
- for (int i = 5; i < 10; i++)
- {
- childBrushTipMaterial[i].color = color;
- }
- }
- private void LateUpdate()
- {
- RemainingAlphaChannelValue = P3dChannelCounter.GetRatioRGBA();
- }
- public void SaveData(int levelNumberCount, int levelIndexCount, int coinsCount, int diamondsCount)
- {
- PlayerPrefs.SetInt("LevelNumber", levelNumberCount);
- PlayerPrefs.SetInt("LevelCount", levelIndexCount);
- PlayerPrefs.SetInt("Coins", coinsCount);
- PlayerPrefs.SetInt("Diamonds", diamondsCount);
- PlayerPrefs.Save();
- // Debug.Log("Data saved");
- }
- public void LoadData()
- {
- if (PlayerPrefs.HasKey("LevelCount"))
- {
- levelNumber = PlayerPrefs.GetInt("LevelNumber");
- levelIndex = PlayerPrefs.GetInt("LevelCount");
- coins = PlayerPrefs.GetInt("Coins");
- diamonds = PlayerPrefs.GetInt("Diamonds");
- // Debug.Log("Previously saved level number is: " + PlayerPrefs.GetInt("LevelNumber"));
- //Debug.Log("Data loaded");
- }
- else
- {
- Debug.Log("No save data is available");
- }
- }
- public void ClearPlayerPrefs()
- {
- PlayerPrefs.DeleteAll();
- Debug.Log("Stats Data cleared");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement