Advertisement
Guest User

GameManager

a guest
Apr 8th, 2020
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 19.47 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6. using PaintIn3D;
  7. using PaintIn3D.Examples;
  8. using UnityEngine.SceneManagement;
  9. using TMPro;
  10.  
  11. using GameAnalyticsSDK;
  12. using PathCreation;
  13.  
  14. namespace newScripts
  15. {
  16.     public class GameManager : MonoBehaviour
  17.     {
  18.         public static int levelIndex = 0;
  19.         private static int diamonds;
  20.         private static int coins;
  21.         private static bool allLevelsCleared = false;
  22.         private static int levelNumber = 1;
  23.         public static bool isLevelCompleted = false;
  24.  
  25.         public GameObject[] colorButtons;
  26.         public List<Items> paintableItem = new List<Items>();
  27.         public Image progressBar;
  28.         public Image matchPanelprogressBar;
  29.         public float matchPanelprogressBarFillSpeed = 0.001f;
  30.         public P3dHitBetween p3DHitBetween;
  31.         public ParticleSystem paintParticles;
  32.         public GameObject confetti;
  33.         //public GameObject doneButton;
  34.         public GameObject MatchPanel;
  35.         public GameObject levelCountBG;
  36.         public GameObject brushTouchPoint;
  37.         public GameObject brushChildPrefab;
  38.         public GameObject onMouseDragGameObject; //disable this gameobject to disable brush movement
  39.         private P3dColor p3DColor;
  40.         [SerializeField]
  41.         private GameObject colorPanel;
  42.         private Material brushTipMaterial;
  43.         private Material[] childBrushTipMaterial;
  44.         private float targetProgress = 0;
  45.         public GameObject[] star;
  46.         public GameObject[] MatchPanelstar;
  47.         public RawImage matchSneakerImage;
  48.         [SerializeField]
  49.         private RawImage targetSneakerImage;
  50.         public Image bgPanelImage;
  51.         public Sprite[] bgImage;
  52.         public TextMeshProUGUI levelCountText;
  53.         public TextMeshProUGUI matchPercentageText;
  54.         public TextMeshProUGUI coinsText;
  55.         public TextMeshProUGUI diamondsText;
  56.         public static Transform activeObjecttransform;
  57.         public Vector4 RemainingAlphaChannelValue;
  58.         public Animator animator;
  59.         private Animator sneakerAnimator;
  60.         private float time = 0;
  61.        
  62.         private ButtonClicked selectedButton;
  63.         bool isButtonClicked = false;
  64.         bool brushColorSelected = false;
  65.         bool isAlphaChannelCalculated = false;
  66.         float totalAlphaChannelCounted;
  67.  
  68.         private Color buttonColor;
  69.  
  70.         [Header("OWL GAME STUDIO")]
  71.         public PathCreator currentPath = null;
  72.         public Transform brush = null;
  73.         public EndOfPathInstruction endOfPathInstruction;
  74.         public float speed = 5;
  75.         float distanceTravelled;
  76.         private int currentColor = 0;
  77.         private int maxColor = 0;
  78.         public List<Animator> colorAnimators = new List<Animator>();
  79.         public GameObject m_textHold = null;
  80.         public GameObject m_textRelease = null;
  81.  
  82.         private void Awake()
  83.         {
  84.            // ClearPlayerPrefs();
  85.             brushTipMaterial = brushTouchPoint.GetComponent<MeshRenderer>().materials[1];
  86.            // brushTipMaterial.color = Color.red;
  87.             childBrushTipMaterial = brushChildPrefab.GetComponent<MeshRenderer>().materials;
  88.         }
  89.  
  90.         private void Start()
  91.         {
  92.             QualitySettings.vSyncCount = 0;
  93.             Application.targetFrameRate = 60;
  94.             LoadData();
  95.             //playerPrefsManager.LoadData(levelNumber, levelIndex, coins, diamonds);
  96.             star[0].SetActive(true);
  97.             star[1].SetActive(true);
  98.             star[2].SetActive(true);
  99.             levelCountBG.SetActive(true);
  100.             isLevelCompleted = false;
  101.  
  102.             for (int i = 0; i < colorButtons.Length; i++)
  103.             {
  104.                 for (int j = 0; j < paintableItem[levelIndex].paintableColors.Count; j++)
  105.                 {
  106.                     if (colorButtons[i].name.ToString() == paintableItem[levelIndex].paintableColors[j].ToString())
  107.                     {
  108.                         colorButtons[i].SetActive(true);
  109.  
  110.                         colorAnimators.Add(colorButtons[i].GetComponent<Animator>());
  111.                         //  Color buttonColor = colorButtons[i].GetComponent<Image>().color;
  112.                         //if (brushColorSelected == false)
  113.                         //{
  114.                         //    paintableItem[levelIndex].paintableParts[j].GetComponent<P3dPaintable>().enabled = true;
  115.                         //    brushColorSelected = true;
  116.                         //    Color buttonColor = colorButtons[i].GetComponent<Button>().colors.pressedColor;
  117.                         //    Debug.Log("Selected color is: " + colorButtons[i]);
  118.                         //    colorButtons[i].GetComponent<Animator>().SetBool("selected", true);
  119.                         //    ChangeBrushColor(buttonColor);
  120.                         //}
  121.                         //else
  122.                         //{
  123.                         //    paintableItem[levelIndex].paintableParts[j].GetComponent<P3dPaintable>().enabled = false;
  124.                         //}
  125.                         paintableItem[levelIndex].paintableParts[j].GetComponent<P3dPaintable>().enabled = false;
  126.                     }
  127.                 }
  128.             }
  129.  
  130.             paintableItem[levelIndex].paintableParts[currentColor].GetComponent<P3dPaintable>().enabled = true;
  131.             Color buttonColor = colorAnimators[currentColor].GetComponent<Button>().colors.pressedColor;
  132.             colorAnimators[currentColor].GetComponent<Animator>().SetBool("selected", true);
  133.             ChangeBrushColor(buttonColor);
  134.  
  135.             m_textHold.SetActive(true);
  136.             m_textRelease.SetActive(false);
  137.  
  138.             paintableItem[levelIndex].itemPrefab.gameObject.SetActive(true);
  139.  
  140.             currentPath = paintableItem[levelIndex].paintablePaths[currentColor];
  141.             maxColor = paintableItem[levelIndex].paintableColors.Count;
  142.  
  143.             SDKManager.instance.LevelStarted(levelNumber, levelNumber);
  144.             Debug.Log("Level Started");
  145.             GameAnalytics.NewProgressionEvent(GAProgressionStatus.Start, Application.version, levelNumber.ToString());
  146.             brushChildPrefab.gameObject.SetActive(true);
  147.             brushTouchPoint.SetActive(true);
  148.             //This code below works prfect than the below but is complex
  149.            
  150.             activeObjecttransform = paintableItem[levelIndex].itemPrefab.transform;
  151.             p3DColor = paintableItem[levelIndex].itemPrefab.gameObject.GetComponent<P3dColor>();
  152.             onMouseDragGameObject.SetActive(true);
  153.             SelectBG();
  154.            
  155.             matchSneakerImage.texture = paintableItem[levelIndex].matchImage.texture;
  156.             sneakerAnimator = paintableItem[levelIndex].itemPrefab.gameObject.GetComponent<Animator>();
  157.             progressBar.gameObject.SetActive(true);
  158.  
  159.             RemainingAlphaChannelValue = P3dChannelCounter.GetRatioRGBA();
  160.  
  161.             diamondsText.text = diamonds.ToString(); //display the total gems
  162.             coinsText.text = coins.ToString(); //display the coins total
  163.         }
  164.  
  165.         private void Update()
  166.         {
  167.             UpdateProgressBar();
  168.             if(matchPanelprogressBar.fillAmount<targetProgress && isLevelCompleted == true)
  169.             {
  170.                 matchPanelprogressBar.fillAmount += matchPanelprogressBarFillSpeed * Time.deltaTime;
  171.             }
  172.             DisplaymatchPanelStars();
  173.             matchPercentageText.text = "Match " + (Mathf.Round(matchPanelprogressBar.fillAmount * 100) +"%");
  174.             levelCountText.text = "LEVEL  " + (levelNumber);
  175.         }
  176.  
  177.         private void UpdateMatchPanelProgressBar(float progress)
  178.         {
  179.             targetProgress = matchPanelprogressBar.fillAmount + progress;
  180.         }
  181.  
  182.         void DisplaymatchPanelStars()
  183.         {
  184.             if (matchPanelprogressBar.fillAmount >= 0.33)
  185.             {
  186.                 MatchPanelstar[0].GetComponent<Image>().color = Color.yellow;
  187.             }
  188.  
  189.             if (matchPanelprogressBar.fillAmount >= 0.65 && matchPanelprogressBar.fillAmount <= 0.80)
  190.             {
  191.                 MatchPanelstar[1].GetComponent<Image>().color = Color.yellow;
  192.             }
  193.  
  194.             if (matchPanelprogressBar.fillAmount >= 0.90f && matchPanelprogressBar.fillAmount <= 0.98f)
  195.             {
  196.                 MatchPanelstar[2].GetComponent<Image>().color = Color.yellow;
  197.             }
  198.         }
  199.  
  200.         void UpdateProgressBar()
  201.         {
  202.             CheckForInput();
  203.  
  204.             if (RemainingAlphaChannelValue.x == 0 & RemainingAlphaChannelValue.y == 0 & RemainingAlphaChannelValue.z == 0)
  205.             {
  206.                 progressBar.fillAmount = 0;
  207.             }
  208.  
  209.             else
  210.             {
  211.                 if(!isAlphaChannelCalculated)
  212.                 {
  213.                     isAlphaChannelCalculated = true;
  214.                     totalAlphaChannelCounted = RemainingAlphaChannelValue.w;
  215.  
  216.                 }
  217.                 progressBar.fillAmount = 1 - (RemainingAlphaChannelValue.w/ totalAlphaChannelCounted );
  218.             }
  219.  
  220.             if (progressBar.fillAmount >= 0.3)
  221.             {
  222.                 star[0].GetComponent<Image>().color = Color.yellow;
  223.             }
  224.             if (progressBar.fillAmount >= 0.6 && progressBar.fillAmount <= 0.75)
  225.             {
  226.                 star[1].GetComponent<Image>().color = Color.yellow;
  227.             }
  228.             if(progressBar.fillAmount > 0.95f && isLevelCompleted == false)
  229.             {
  230.                 star[2].GetComponent<Image>().color = Color.yellow;
  231.             }
  232.  
  233.             if (progressBar.fillAmount >= 0.999f && isLevelCompleted == false)
  234.             {
  235.                 //isLevelCompleted = true;
  236.                 confetti.SetActive(true);
  237.                 Invoke("FinishPainting", 0.5f);
  238.             }
  239.  
  240.         }
  241.         void SelectBG()
  242.         {
  243.             //bgPanelImage.sprite = paintableItem[levelIndex].bgImage;
  244.             bgPanelImage.sprite = bgImage[Random.Range(0,bgImage.Length)];
  245.         }
  246.  
  247.         public void LoadNextLevel()
  248.         {
  249.             paintableItem[levelIndex].itemPrefab.gameObject.SetActive(false);
  250.  
  251.             if (levelIndex < paintableItem.Count-1 && allLevelsCleared == false)
  252.             {
  253.                 if(levelIndex == paintableItem.Count - 1)
  254.                 {
  255.                     allLevelsCleared = true;
  256.                 }
  257.  
  258.                // Debug.Log("Control is inside if statement");
  259.                 levelIndex++;
  260.                
  261.             }
  262.             else
  263.             {
  264.                 allLevelsCleared = true;
  265.                 levelIndex = Random.Range(0, paintableItem.Count - 1);
  266.                
  267.                // Debug.Log("Control is inside else statement");
  268.             }
  269.             // Debug.Log("Loading level: " + levelIndex);
  270.  
  271.             //playerPrefsManager.SaveData(levelNumber, levelIndex, coins, diamonds);
  272.             SaveData(levelNumber, levelIndex, coins, diamonds);
  273.             SceneManager.LoadScene(0);
  274.         }
  275.  
  276.         public void ReloadScene()
  277.         {
  278.             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
  279.         }
  280.  
  281.         void CheckForInput()
  282.         {
  283.             if (Input.GetMouseButton(0))
  284.             {
  285.                 p3DHitBetween.enabled = true;
  286.                
  287.                 if (currentPath != null)
  288.                 {
  289.                     distanceTravelled += speed * Time.deltaTime;
  290.                     brush.position = currentPath.path.GetPointAtDistance(distanceTravelled, endOfPathInstruction);
  291.  
  292.                     if (distanceTravelled >= (currentPath.path.length * 0.95f))
  293.                     {
  294.                         m_textHold.SetActive(false);
  295.                         m_textRelease.SetActive(true);
  296.                     }
  297.                 }
  298.             }
  299.             else if (Input.GetMouseButtonUp(0))
  300.             {
  301.                 p3DHitBetween.enabled = false;
  302.  
  303.                 distanceTravelled = 0;
  304.                 currentColor++;
  305.  
  306.                 if (currentColor < maxColor)
  307.                 {
  308.                     m_textHold.SetActive(true);
  309.                     m_textRelease.SetActive(false);
  310.  
  311.                     currentPath = paintableItem[levelIndex].paintablePaths[currentColor];
  312.                     NextColor(currentColor);
  313.                 }
  314.                 else
  315.                 {
  316.                     Invoke("FinishPainting", 1f);
  317.                 }
  318.             }
  319.         }
  320.  
  321.         private void NextColor(int i)
  322.         {
  323.             for (int c = 0; c < colorAnimators.Count; c++)
  324.             {
  325.                 colorAnimators[c].SetBool("selected", false);
  326.             }
  327.  
  328.             colorAnimators[currentColor].SetBool("selected", true);
  329.  
  330.             for (int j = 0; j < paintableItem[levelIndex].paintableParts.Length; j++)
  331.             {
  332.                 paintableItem[levelIndex].paintableParts[j].GetComponent<P3dPaintable>().enabled = false;
  333.  
  334.             }
  335.  
  336.             paintableItem[levelIndex].paintableParts[i].GetComponent<P3dPaintable>().enabled = true;
  337.             AnimateParts(paintableItem[levelIndex].paintableParts[i]);
  338.             switch (paintableItem[levelIndex].paintableColors[i])
  339.             {
  340.                 case Items.selectColors.Red:
  341.                     ChangeBrushColor(Color.red);
  342.                     break;
  343.                 case Items.selectColors.Black:
  344.                     ChangeBrushColor(Color.black);
  345.                     break;
  346.                 case Items.selectColors.Green:
  347.                     ChangeBrushColor(Color.green);
  348.                     break;
  349.                 case Items.selectColors.Yellow:
  350.                     ChangeBrushColor(Color.yellow);
  351.                     break;
  352.                 case Items.selectColors.White:
  353.                     ChangeBrushColor(Color.white);
  354.                     break;
  355.                 case Items.selectColors.Gray:
  356.                     ChangeBrushColor(Color.grey);
  357.                     break;
  358.                 case Items.selectColors.Blue:
  359.                     ChangeBrushColor(Color.black);
  360.                     break;
  361.                 case Items.selectColors.Pink:
  362.                     ChangeBrushColor(new Color(1, 0.6462264f, 0.9492261f));
  363.                     break;
  364.                 case Items.selectColors.LightBlue:
  365.                     ChangeBrushColor(new Color(0, 0.8662651f, 1));
  366.                     break;
  367.                 case Items.selectColors.Purple:
  368.                     ChangeBrushColor(new Color(0.7773685f, 1, 1));
  369.                     break;
  370.                 case Items.selectColors.Orange:
  371.                     ChangeBrushColor(new Color(0, 0.561501f, 0));
  372.                     break;
  373.                 case Items.selectColors.Cream:
  374.                     ChangeBrushColor(new Color(0.9647059f, 0.8509805f, 0.4745098f));
  375.                     break;
  376.                 case Items.selectColors.LightRed:
  377.                     ChangeBrushColor(new Color(0.8274511f, 0.4509804f, 0.3254902f));
  378.                     break;
  379.             }
  380.         }
  381.  
  382.         public void FinishPainting()
  383.         {
  384.             if (isLevelCompleted == false)
  385.             {
  386.                 m_textHold.SetActive(false);
  387.                 m_textRelease.SetActive(false);
  388.  
  389.                 star[0].SetActive(false);
  390.                 star[1].SetActive(false);
  391.                 star[2].SetActive(false);
  392.                 MatchPanel.SetActive(true);
  393.                 //levelCountBG.SetActive(false); //Disable dispalying level count text parent gameobject
  394.                 onMouseDragGameObject.SetActive(false);
  395.                 confetti.SetActive(true);
  396.                 targetSneakerImage.GetComponent<RawImage>().texture = paintableItem[levelIndex].matchImage.texture;
  397.                 brushChildPrefab.gameObject.SetActive(false);
  398.                 brushTouchPoint.SetActive(false);
  399.                 for (int i = 0; i < colorButtons.Length; i++)
  400.                 {
  401.                     colorButtons[i].SetActive(false);
  402.                 }
  403.                 colorPanel.SetActive(false);
  404.                 isLevelCompleted = true;
  405.                 progressBar.gameObject.SetActive(false);
  406.                 UpdateMatchPanelProgressBar(progressBar.fillAmount);
  407.                 GameAnalytics.NewProgressionEvent(GAProgressionStatus.Complete, Application.version, levelNumber.ToString());
  408.                 SDKManager.instance.LevelFinished(levelNumber, levelNumber);
  409.                 Debug.Log("Level Finished: "+levelNumber );
  410.                 diamonds++;
  411.                 coins += 50;
  412.                 levelNumber++;
  413.                // SaveData(levelNumber, levelIndex, coins, diamonds);
  414.             }
  415.         }
  416.  
  417.         void AnimateParts(GameObject obj)
  418.         {
  419.             obj.GetComponent<Animator>().Play("FlickAlphaValue");
  420.         }
  421.  
  422.         public void SwitchParts()
  423.         {
  424.             selectedButton = EventSystem.current.currentSelectedGameObject.GetComponent<ButtonClicked>();
  425.            
  426.             for (int i = 0; i < paintableItem[levelIndex].paintableParts.Length; i++)
  427.             {
  428.                 if (paintableItem[levelIndex].paintableColors[i].ToString() == EventSystem.current.currentSelectedGameObject.name)
  429.                 {
  430.                     //Debug.Log("Paint part no: " + paintableItem[levelIndex].paintableParts[i]);
  431.                     paintableItem[levelIndex].paintableParts[i].GetComponent<P3dPaintable>().enabled = true;
  432.                     if(selectedButton.buttonClicked == false)
  433.                     {
  434.                         selectedButton.buttonClicked = true;
  435.                         AnimateParts(paintableItem[levelIndex].paintableParts[i]);
  436.                     }
  437.                 }
  438.                 else
  439.                 {
  440.                     paintableItem[levelIndex].paintableParts[i].GetComponent<P3dPaintable>().enabled = false;
  441.                 }
  442.             }
  443.         }
  444.  
  445.         public void ChangeBrushColor(Color color)
  446.         {
  447.            // Debug.Log("Processed brush color function");
  448.             brushTipMaterial.color = color;
  449.             paintParticles.GetComponent<ParticleSystemRenderer>().material.color = color;
  450.             for (int i = 5; i < 10; i++)
  451.             {
  452.                 childBrushTipMaterial[i].color = color;
  453.             }
  454.         }
  455.  
  456.         private void LateUpdate()
  457.         {
  458.             RemainingAlphaChannelValue = P3dChannelCounter.GetRatioRGBA();
  459.         }
  460.  
  461.         public void SaveData(int levelNumberCount, int levelIndexCount, int coinsCount, int diamondsCount)
  462.         {
  463.             PlayerPrefs.SetInt("LevelNumber", levelNumberCount);
  464.             PlayerPrefs.SetInt("LevelCount", levelIndexCount);
  465.             PlayerPrefs.SetInt("Coins", coinsCount);
  466.             PlayerPrefs.SetInt("Diamonds", diamondsCount);
  467.             PlayerPrefs.Save();
  468.            // Debug.Log("Data saved");
  469.         }
  470.  
  471.         public void LoadData()
  472.         {
  473.             if (PlayerPrefs.HasKey("LevelCount"))
  474.             {
  475.                 levelNumber = PlayerPrefs.GetInt("LevelNumber");
  476.                 levelIndex = PlayerPrefs.GetInt("LevelCount");
  477.                 coins = PlayerPrefs.GetInt("Coins");
  478.                 diamonds = PlayerPrefs.GetInt("Diamonds");
  479.                // Debug.Log("Previously saved level number is: " + PlayerPrefs.GetInt("LevelNumber"));
  480.                 //Debug.Log("Data loaded");
  481.             }
  482.  
  483.             else
  484.             {
  485.                 Debug.Log("No save data is available");
  486.             }
  487.         }
  488.  
  489.         public void ClearPlayerPrefs()
  490.         {
  491.             PlayerPrefs.DeleteAll();
  492.             Debug.Log("Stats Data cleared");
  493.         }
  494.     }
  495.  
  496. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement