GlobalAchievementControllerCS.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * FUNCTION:
  3. * - This script keeps track of the global/ game center achievements.
  4. * - All of the achievements if not completed are kept active.
  5. * - Counters funcitons are called in every relevant script to track the
  6. * progress of the achievements.
  7. */
  8. using UnityEngine;
  9. using System.Collections;
  10. public class GlobalAchievementControllerCS : MonoBehaviour {
  11. //the types of Global Achievements
  12. public enum GlobalAchievementTypes
  13. {
  14. Score,
  15. Distance,
  16. Powerups,
  17. Jump,
  18. Duck,
  19. Currency,
  20. StartGame
  21. }
  22. public struct GlobalAchievementDetail
  23. {
  24. public string achievementDescription;//description of the achievement
  25. public int achievementCount; //number of actions to perform to complete the achievement
  26. public GlobalAchievementTypes achievementType;//what actions to perform (jump, collect currency etc.)
  27. public bool achievementComplete; //if the achievement has been completed
  28. }
  29. //script references
  30. private MenuScriptCS hMenuScriptCS;
  31. private NGUIMenuScript hNGUIMenuScript;
  32. private InGameScriptCS hInGameScriptCS;
  33. private int iTotalAchievementsCount;//amount of available achievements
  34. private GlobalAchievementDetail[] achievements;//information of all the achievements
  35. private int[] achievementsProgress;//store the current progress of achievements
  36. void Start ()
  37. {
  38. achievementsProgress = new int[System.Enum.GetValues(typeof(GlobalAchievementTypes)).Length];
  39. hInGameScriptCS = (InGameScriptCS)this.GetComponent(typeof(InGameScriptCS));
  40. if (hInGameScriptCS.isCustomMenuEnabled())
  41. hMenuScriptCS = (MenuScriptCS)GameObject.Find("MenuGroup").GetComponent(typeof(MenuScriptCS));
  42. else
  43. hNGUIMenuScript = (NGUIMenuScript)GameObject.Find("UI Root (2D)").GetComponent(typeof(NGUIMenuScript));
  44. //get the GlobalAchievementsList file from the resources folder
  45. TextAsset taFile = (TextAsset)Resources.Load("GlobalAchievementsList");
  46. string[] lines = taFile.text.Split('\n');
  47. if (lines.Length == 0)//if the file was empty
  48. {
  49. Debug.Log("No achievements found in file");
  50. this.enabled = false;
  51. }
  52. else//read file and extract achievements detail
  53. {
  54. int lineIndex=0;
  55. int arrayIndex=0;
  56. iTotalAchievementsCount = lines.Length/3;//get the total number of achievements in the file
  57. achievements = new GlobalAchievementDetail[iTotalAchievementsCount];//allocate memory according to the number of achievement
  58. /*for (var i=0; i<iTotalAchievementsCount; i++)
  59. achievements[i] = new GlobalAchievementDetail();*/
  60. while (lineIndex < lines.Length)//store the file content in achievement array
  61. {
  62. achievements[arrayIndex].achievementDescription = lines[lineIndex++].ToString();
  63. achievements[arrayIndex].achievementCount = int.Parse(lines[lineIndex++].ToString());
  64. achievements[arrayIndex].achievementType = (GlobalAchievementTypes)System.Enum.Parse(typeof(GlobalAchievementTypes), lines[lineIndex++].ToString());
  65. achievements[arrayIndex].achievementComplete = false; //mark achievement incomplete by default
  66. if (PlayerPrefs.HasKey("GlobalAchievement_"+arrayIndex))//check achievement progress
  67. {
  68. achievementsProgress[ (int)achievements[arrayIndex].achievementType ] = PlayerPrefs.GetInt("GlobalAchievement_"+arrayIndex);
  69. //check if the achievement has been completed
  70. if (achievementsProgress[ (int)achievements[arrayIndex].achievementType ] >= achievements[arrayIndex].achievementCount)
  71. achievements[arrayIndex].achievementComplete = true;
  72. }
  73. else//if this is the first game launch
  74. {
  75. PlayerPrefs.SetInt("GlobalAchievement_"+arrayIndex, 0);
  76. achievementsProgress[ (int)achievements[arrayIndex].achievementType ] = 0;
  77. }
  78. arrayIndex++;
  79. }//end of while
  80. updateMenuDescription();
  81. }//end of else
  82. PlayerPrefs.Save();
  83. }
  84. /*
  85. * FUNCTION: Increment achievement counter by 1.
  86. */
  87. public void incrementAchievementCount(GlobalAchievementTypes type)
  88. {
  89. achievementsProgress[(int)type]++;
  90. checkCompletion(type);
  91. }
  92. /*
  93. * FUNCTION: Increment achievement counter by required value.
  94. */
  95. public void incrementAchievementCount(GlobalAchievementTypes type, int iVal)
  96. {
  97. achievementsProgress[(int)type]+= iVal;
  98. checkCompletion(type);
  99. }
  100. /*
  101. * FUNCTION: Check if an achievement has been completed. Save the progress if
  102. * changes have been made in the achievement progress.
  103. * CALLED BY: incrementAchievementCount(...)
  104. */
  105. private void checkCompletion(GlobalAchievementTypes achievementType)
  106. {
  107. for (int i = 0; i<iTotalAchievementsCount; i++)
  108. {
  109. if (achievements[i].achievementType == achievementType //is the updated counter of the current achievement
  110. && achievements[i].achievementComplete == false) //has the achievement been completed previously
  111. {
  112. if (achievementsProgress[(int)achievementType] >= achievements[i].achievementCount)//has the achievement been completed
  113. achievements[i].achievementComplete = true; //mark achievement as complete
  114. PlayerPrefs.SetInt("GlobalAchievement_"+i, achievementsProgress[(int)achievementType]);//save progress permanently
  115. }//end of outer if
  116. }//end of for
  117. }//end of check completion function
  118. /*
  119. * FUNCTION: Compiles all the achievements' descriptions and
  120. * tells the MenuScript to display it on Pause Menu
  121. * and the Missions Menu
  122. */
  123. private void updateMenuDescription()
  124. {
  125. string combinedText = string.Empty;
  126. //traverse through the achievements and compile a list to display on achievements menu
  127. for (int i = 0; i<iTotalAchievementsCount; i++)
  128. {
  129. if (achievements[i].achievementComplete == true)//is the achievement completed
  130. combinedText += (i+1).ToString() + ". " + achievements[i].achievementDescription + " (Done) \n";
  131. else
  132. combinedText += (i+1).ToString() + ". " + achievements[i].achievementDescription + " (" +
  133. achievementsProgress[ (int)achievements[i].achievementType ].ToString() + "/" + achievements[i].achievementCount + ") \n";
  134. }//end of for
  135. /* if (!combinedText.Contains("Jump") && !combinedText.Contains("Slide") && !combinedText.Contains("Gold"))
  136. {
  137. return;
  138. }
  139. */
  140. if (hInGameScriptCS.isCustomMenuEnabled())//if the custom menu is in use
  141. hMenuScriptCS.updateAchievementsMenuDescription(combinedText);
  142. else//if the NGUI menu is in use
  143. hNGUIMenuScript.updateAchievementsMenuDescription(combinedText);
  144. }//end of update menu description function
  145. }