MissionsControllerCS.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * FUNCTION:
  3. * - This script stores all the missions.
  4. * - It keeps record of the currently active missions. Three missions
  5. * are active at a single time.
  6. * - It checks if a currently active mission has been completed.
  7. * - On completion of one mission, it is automatically replaced by
  8. * the next one. Once all missions have been completed, the missions
  9. * are restarted from the first three.
  10. */
  11. using UnityEngine;
  12. using System.Collections;
  13. public class MissionsControllerCS : MonoBehaviour {
  14. //the different types of missions
  15. public enum MissionTypes
  16. {
  17. Score,
  18. Distance,
  19. Powerups,
  20. Jump,
  21. Duck,
  22. Currency,
  23. StartGame
  24. }
  25. //detail of a particular mission
  26. public class MissionDetail
  27. {
  28. public string missionDescription;
  29. public int missionCount;
  30. public MissionTypes missionType;
  31. }
  32. //script references
  33. private MenuScriptCS hMenuScriptCS;
  34. private NGUIMenuScript hNGUIMenuScript;
  35. private InGameScriptCS hInGameScriptCS;
  36. private HUDControllerCS hHUDControllerCS;
  37. private NGUIHUDScript hNGUIHUDScript;
  38. //keeps record of all the missions
  39. private MissionDetail[] missions;//details of all missions
  40. private int iActiveMissionCount = 3;//number of missions active at a time (3 by default)
  41. private int[] iActiveMissions;//keeps record of currently active missions
  42. private int iNextMission = 0;//index of the next mission if a current one is completed
  43. private int iTotalMissionCount;//the total number available missions
  44. //store the current progress of missions
  45. private int[] missionsProgress;
  46. void Start ()
  47. {
  48. //PlayerPrefs.DeleteAll();
  49. //PlayerPrefs.Save();
  50. iActiveMissionCount = 3;//three missions active at a time by deafult
  51. missionsProgress = new int[System.Enum.GetValues(typeof(MissionTypes)).Length];
  52. //set the next mission index
  53. if (PlayerPrefs.HasKey("NextMissionIndex"))
  54. {
  55. iNextMission = PlayerPrefs.GetInt("NextMissionIndex");
  56. }
  57. else
  58. {
  59. iNextMission = 0;
  60. PlayerPrefs.SetInt("NextMissionIndex", iNextMission);
  61. }
  62. hInGameScriptCS = (InGameScriptCS)this.GetComponent(typeof(InGameScriptCS));
  63. if (hInGameScriptCS.isCustomMenuEnabled())
  64. {
  65. hMenuScriptCS = (MenuScriptCS)GameObject.Find("MenuGroup").GetComponent(typeof(MenuScriptCS));
  66. hHUDControllerCS = (HUDControllerCS)GameObject.Find("HUDMainGroup").GetComponent(typeof(HUDControllerCS));
  67. }
  68. else
  69. {
  70. hNGUIMenuScript = (NGUIMenuScript)GameObject.Find("UI Root (2D)").GetComponent(typeof(NGUIMenuScript));
  71. hNGUIHUDScript = hNGUIMenuScript.getNGUIHUDScriptReference();
  72. }
  73. //get the MissionList file from the resources folder
  74. TextAsset taFile = (TextAsset)Resources.Load("MissionsList");
  75. string[] lines = taFile.text.Split('\n');
  76. if (lines.Length == 0)//if the file was empty
  77. {
  78. Debug.Log("No missions found in file");
  79. this.enabled = false;
  80. }
  81. else//read file and extract mission detail
  82. {
  83. int lineIndex=0;
  84. int arrayIndex=0;
  85. iTotalMissionCount = lines.Length/3;
  86. missions = new MissionDetail[iTotalMissionCount];//allocate memory according to the number of missions
  87. for (int i=0; i<iTotalMissionCount; i++)
  88. missions[i] = new MissionDetail();
  89. while (lineIndex < lines.Length)//store the file content in mission array
  90. {
  91. missions[arrayIndex].missionDescription = lines[lineIndex++];
  92. missions[arrayIndex].missionCount = int.Parse(lines[lineIndex++]);
  93. missions[arrayIndex].missionType = (MissionTypes)System.Enum.Parse(typeof(MissionTypes), lines[lineIndex++]);
  94. arrayIndex++;
  95. }//end of while
  96. iActiveMissions = new int[iActiveMissionCount];
  97. for (int i=0; i<iActiveMissionCount; i++)//set the currently active missions
  98. {
  99. if (PlayerPrefs.HasKey("ActiveMission_"+i.ToString()))
  100. iActiveMissions[i] = PlayerPrefs.GetInt("ActiveMission_"+i.ToString());
  101. else
  102. {
  103. iActiveMissions[i] = getNextMission();
  104. PlayerPrefs.SetInt("ActiveMission_"+i.ToString(), iActiveMissions[i]);
  105. }
  106. }//end of for
  107. updateMenuDescriptions();
  108. }//end of else
  109. PlayerPrefs.Save();
  110. }
  111. /*
  112. * FUNCTION: Increment mission counter by 1.
  113. */
  114. public void incrementMissionCount(MissionTypes type)
  115. {
  116. missionsProgress[(int)type]++;
  117. checkCompletion();
  118. }
  119. /*
  120. * FUNCTION: Increment mission counter by required value.
  121. */
  122. public void incrementMissionCount(MissionTypes type, int iVal)
  123. {
  124. missionsProgress[(int)type] += iVal;
  125. checkCompletion();
  126. }
  127. private void checkCompletion()
  128. {
  129. for (int i = 0; i<iActiveMissionCount; i++)//check if an active misson has been completed
  130. {
  131. if (missionsProgress[ (int)missions[iActiveMissions[i]].missionType ] >= missions[ iActiveMissions[i] ].missionCount)
  132. markMissionComplete(i);
  133. }//end of for
  134. updateMenuDescriptions();
  135. }
  136. /*
  137. * FUNCTION: Compiles all the missions' descriptions and
  138. * tells the MenuScript to display it on Pause Menu
  139. * and the Missions Menu
  140. */
  141. public void updateMenuDescriptions()
  142. {
  143. string combinedText = string.Empty;
  144. //combine all the description text in one string
  145. for (int i=0; i<iActiveMissionCount; i++)
  146. {
  147. combinedText += (i+1).ToString() +". " + missions[ iActiveMissions[i] ].missionDescription
  148. + "\n (" + missionsProgress[ (int)missions[iActiveMissions[i]].missionType ] + "/"
  149. + missions[ iActiveMissions[i] ].missionCount + ")\n\n";
  150. }
  151. //tell the MenuScript.js to update the missions description on Pause Menu
  152. if (hInGameScriptCS.isCustomMenuEnabled())
  153. {
  154. hMenuScriptCS.updatePauseMenuMissions(combinedText);
  155. hMenuScriptCS.updateMissionsMenuMissions(combinedText);
  156. }
  157. else//if the NGUI menus are in use
  158. {
  159. hNGUIMenuScript.updatePauseMenuMissions(combinedText);
  160. hNGUIMenuScript.updateMissionsMenuMissions(combinedText);
  161. }
  162. }
  163. /*
  164. * FUNCTION: Mark the currently active mission complete, announce on HUD
  165. * about the completed mission and load the next one.
  166. * PARAMETER 1: The index of the completed mission.
  167. */
  168. private void markMissionComplete(int missionIndex)
  169. {
  170. //announce mission completion on HUD
  171. if (hInGameScriptCS.isCustomMenuEnabled())
  172. StartCoroutine(hHUDControllerCS.displayMissionDescriptionDropDown("DONE!\n" + missions[ iActiveMissions[missionIndex] ].missionDescription));
  173. else
  174. StartCoroutine(hNGUIHUDScript.displayMissionDescriptionDropDown("DONE!\n" + missions[ iActiveMissions[missionIndex] ].missionDescription));
  175. //replace the completed mission with a new one
  176. iActiveMissions[missionIndex] = getNextMission();
  177. //reset the new active mission count
  178. missionsProgress[ (int)missions[iActiveMissions[missionIndex]].missionType ] = 0;
  179. //permenantly save the new active mission
  180. PlayerPrefs.SetInt("ActiveMission_"+missionIndex, iActiveMissions[missionIndex]);
  181. //update the mission decription on the pause menu and missions menu
  182. updateMenuDescriptions();
  183. }
  184. /*
  185. * FUNCTION: Check the next mission int the list. Start from the beginning
  186. * if all missions have been completed.
  187. */
  188. private int getNextMission()
  189. {
  190. int tempNext = iNextMission;
  191. if ( (iNextMission+1) == iTotalMissionCount)//if all missions completed, restart mission list
  192. {
  193. iNextMission = 0;
  194. PlayerPrefs.SetInt("NextMissionIndex", iNextMission);
  195. }
  196. else//return next mission's index located in the 'missions' array
  197. {
  198. iNextMission++;
  199. PlayerPrefs.SetInt("NextMissionIndex", iNextMission);
  200. }
  201. PlayerPrefs.Save();
  202. return tempNext;
  203. }
  204. }