ElementsGeneratorCS.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. /*
  2. * FUNCTION:
  3. * This script generates obstacles, currency units and powerups on every patch.
  4. * The spline is used to ensure obstacles are created after regular distances. The start of
  5. * the spline is 0.0 and the end is 1.0.
  6. *
  7. * USED BY:
  8. * This script is a part of the "Player" prefab.
  9. *
  10. */
  11. using System;
  12. using UnityEngine;
  13. using System.Collections;
  14. using System.Collections.Generic;
  15. using System.Linq;
  16. using Random = UnityEngine.Random;
  17. public class ElementsGeneratorCS : MonoBehaviour {
  18. public enum ElementType { SingleLaneObstacle = 0, MultiLaneObstacle = 1, Currency = 3, Powerup = 4, Null = 5, Destroyable = 6, Prize }
  19. public bool Spawnprize;
  20. //properties of elements (obstacles, powerups, currency)
  21. private struct Element
  22. {
  23. public Transform[] tPrefabHandle;//array of clones of a particular element
  24. public int iFrequency; //frequency of occurance of a particular element
  25. public ElementType elementType;
  26. public int iPrefabHandleIndex;//element clone currently in use
  27. }
  28. public GameObject[] obstaclePrefabs;//prefabs of obstacles
  29. public GameObject[] powerupPrefabs; //prefabs of powerups
  30. public GameObject currencyPrefab; //prefab of currency
  31. public GameObject superPrize;//prefabSuperPrize
  32. public GameObject Letter;//prefabSuperPrize
  33. private Element[] elements; //class array
  34. private Transform tPrefabHandlesParent;//holds all instances of element clones
  35. private int iObstacleCount;//total number obstacles
  36. private int iPowerupCount; //total number of powerups
  37. private int iTotalCount; //total count of all elements handled by this script
  38. public int CounterCurrency;
  39. //script references
  40. private PowerupsMainControllerCS hPowerupsMainControllerCS;
  41. private CheckPointsMainCS hCheckPointsMainCS;
  42. private PatchesRandomizerCS hPatchesRandomizerCS;
  43. public float fDefaultIncrement = 0.02f;//the default gap between obstacles
  44. public int iLastPowerupGenerated = 0; //how many patches have been generated without a powerup
  45. private int iForcePowerupGeneration = 1; //force powerup generation after how many patches
  46. private bool bPowerupPlaced = false; //force only a single powerup at a time
  47. public int LastOrc;
  48. /*
  49. * FUNCTION: Tell the randomiser to generate obstacles on newly created patch
  50. * CALLED BY: PatchesRandomizer.createNewPatch()
  51. */
  52. public bool bGenerateElements; //flag the script to generate elements on next patch
  53. public bool GetGift;
  54. private int _magnetismIndex = 0;
  55. private int _sledgeIndex = 0;
  56. private int _lastPowerUp = 0;
  57. public bool SpawnLetter;
  58. public void generateElements()
  59. {
  60. bGenerateElements = true;
  61. }
  62. private bool FirstPlay
  63. {
  64. get
  65. {
  66. return GameObject.Find("InfinitelyTraining") || InstructionsHomeButtonHandler.FirstPlay;
  67. }
  68. }
  69. public void Start()
  70. {
  71. curSpawnCoupone = Random.Range(150, 160);
  72. iObstacleCount = obstaclePrefabs.Length;
  73. iPowerupCount = powerupPrefabs.Length;
  74. iTotalCount = iObstacleCount + iPowerupCount + 1;//obstacles + powerups + currency
  75. //bGenerateElements = false;
  76. //bPowerupPlaced = true;//do not place powerup on first patch
  77. System.DateTime dt = System.DateTime.Now;
  78. Random.seed = dt.Hour * dt.Minute * dt.Second;
  79. setPrefabHandlers();
  80. //StartCoroutine(SetPrefabHandler());
  81. hPowerupsMainControllerCS = (PowerupsMainControllerCS)this.GetComponent(typeof(PowerupsMainControllerCS));
  82. hCheckPointsMainCS = (CheckPointsMainCS)GameObject.Find("Player").GetComponent(typeof(CheckPointsMainCS));
  83. hPatchesRandomizerCS = (PatchesRandomizerCS)this.GetComponent(typeof(PatchesRandomizerCS));
  84. //generate elements on first patch
  85. //GetGift = true;
  86. //if (!Tutor.TutorPassed)
  87. //{
  88. // Tutor.ActivateColliders();
  89. // //Debug.Log("HERE!!!");
  90. // //generateElements(1, 0.15f, true, 2, 0, 1);
  91. // //generateElements(7, 0.15f, true, 0, 0, 1);
  92. // //generateElements(1, 0.25f, true, 2, 0, 1);
  93. // //generateElements(0, 0.35f, true, 0, 0, 1);
  94. // // generateElements(5, 0.50f, true, 0, 0, 1);
  95. // // generateElements(5, 0.50f, true, 1, 0, 1);
  96. // // generateElements(5, 0.50f, true, 2, 0, 1);
  97. // // generateElements(6, 0.65f, true, 0, 0, 1);
  98. // // generateElements(6, 0.65f, true, 1, 0, 1);
  99. // // generateElements(6, 0.65f, true, 2, 0, 1);
  100. //}
  101. }//end of Start()
  102. private IEnumerator SetPrefabHandler()
  103. {
  104. yield return new WaitForSeconds(2);
  105. setPrefabHandlers();
  106. }
  107. public void ResetElemensForScene()
  108. {
  109. //Destroy(GameObject.Find("PFHandlesGroup").gameObject);
  110. var gos = GameObject.Find("PFHandlesGroup").gameObject.GetComponentsInChildren<Transform>();
  111. //Debug.Log(gos.Count());
  112. for (int i = 0; i < gos.Count(); i++)
  113. {
  114. if (gos[i].name != "PFHandlesGroup")
  115. {
  116. Destroy(gos[i].gameObject);
  117. }
  118. }
  119. curSpawnCoupone = Random.Range(50, 100);
  120. iObstacleCount = obstaclePrefabs.Length;
  121. iPowerupCount = powerupPrefabs.Length;
  122. iTotalCount = iObstacleCount + iPowerupCount + 1;//obstacles + powerups + currency
  123. //bGenerateElements = false;
  124. //bPowerupPlaced = true;//do not place powerup on first patch
  125. System.DateTime dt = System.DateTime.Now;
  126. Random.seed = dt.Hour * dt.Minute * dt.Second;
  127. //Start();
  128. setPrefabHandlers();
  129. //bGenerateElements = true;
  130. //StartGenerateElement();
  131. //bGenerateElements = true;
  132. foreach (var lat in ListLetters)
  133. {
  134. Destroy(lat);
  135. }
  136. ListLetters.Clear();
  137. }
  138. public void StartGenerateElement()
  139. {
  140. //Debug.Log("Generate");
  141. for (int j = 0; j < 2; j++)
  142. {
  143. float i = 0.1f;
  144. while (i < 0.9f)
  145. {
  146. int element = getRandomElement();
  147. //while (true)
  148. //{
  149. // element = getRandomElement();
  150. // if (element == 5)
  151. // {
  152. // continue;
  153. // }
  154. // else
  155. // {
  156. // break;
  157. // }
  158. // if (element == 5)
  159. // {
  160. // if (LastOrc < 10)
  161. // {
  162. // continue;
  163. // }
  164. // LastOrc = 0;
  165. // break;
  166. // }
  167. // LastOrc++;
  168. // /*if (LastOrc > 20)
  169. // {
  170. // element = 5;
  171. // LastOrc = 0;
  172. // }*/
  173. // break;
  174. //}
  175. float incrementValue = 0f;
  176. if (j == 1)
  177. {
  178. incrementValue = generateElements(element, i, false);//get any type of element
  179. }
  180. else
  181. {
  182. incrementValue = generateElements(element, i, true);//get any type of element
  183. }
  184. //Debug.Log(incrementValue);
  185. i += incrementValue;
  186. }//end of while
  187. }
  188. }
  189. public List<GameObject> ListLetters;
  190. void FixedUpdate()
  191. {
  192. if (bGenerateElements)
  193. {
  194. bGenerateElements = false;
  195. bPowerupPlaced = false;//place powerup on current patch if randomised
  196. iLastPowerupGenerated++;//count how many patches have passed without a power-up
  197. StartCoroutine(generateElementsCoroutine());
  198. }//end of if
  199. }//end of Update
  200. public int j=0;
  201. private IEnumerator generateElementsCoroutine()
  202. {
  203. //Debug.LogError("Generate");
  204. float i = 0.16f;
  205. while (i < 0.9)
  206. {
  207. yield return new WaitForFixedUpdate();
  208. if (i >= 0.1f)
  209. {
  210. int element = getRandomElement();
  211. float incrementValue = generateElements(element, i, false); //get any type of element
  212. //Debug.Log("generateElements !!! " + incrementValue);
  213. i += incrementValue;
  214. }
  215. else
  216. {
  217. i += 0.03f;
  218. }
  219. }//end of while
  220. StopCoroutine("generateElementsCoroutine");
  221. }
  222. /*
  223. * FUNCITON: Generate random elements and group of elements.
  224. * PARAMETER 1: Element to generate.
  225. * PARAMETER 2: Where to generate based on the spline.
  226. * PARAMETER 3: If the patch is the starting patch or the next patch generated.
  227. */
  228. public int curSpawnCoupone;
  229. public bool couponeSending;
  230. private float generateElements(int elementNumber, float fLocation, bool bStartPatch, int exactlyPosX = -1, int exactlyPosY = -1, int exactlyCount = -1)
  231. {
  232. Vector3 v3Position = new Vector3(); //position to put the obstacle on
  233. RaycastHit hitInfo; //check y-axis location
  234. float CurrentAngle; //billboard the obstacle towards the player
  235. float fDefaultDisplacementValue = 15;
  236. float fDisplacement = fDefaultDisplacementValue; //horizontal displacement
  237. //if obstacle only covers one lane generate a random number of instances of the
  238. //obstacle on random locations within a particular radius
  239. //Debug.Log(elements[elementNumber].elementType + " bStartPatch " + bStartPatch);
  240. if (elements[elementNumber].elementType == ElementType.SingleLaneObstacle)
  241. {
  242. for (int i = 0; i < (exactlyCount == -1 ? Random.Range(2, 9) : exactlyCount); i++)
  243. {
  244. //pick where to generate obstacle horizontally on path
  245. int iLane = exactlyPosX == -1 ? Random.Range(0, 3) : exactlyPosX;
  246. if (iLane == 0)
  247. fDisplacement = Mathf.Abs(fDefaultDisplacementValue);
  248. else if (iLane == 1)
  249. fDisplacement = -Mathf.Abs(fDefaultDisplacementValue);
  250. else
  251. fDisplacement = 0;
  252. //pick where to generate obstacle vertically on path
  253. int iVerticalPosition = exactlyPosY == -1 ? Random.Range(0, 3) : exactlyPosY;
  254. if (iVerticalPosition == 0)
  255. {
  256. v3Position = getPosition(fLocation+fDefaultIncrement, bStartPatch);
  257. hitInfo = getHitInfo(v3Position);
  258. }
  259. else if (iVerticalPosition == 1)
  260. {
  261. v3Position = getPosition(fLocation+(fDefaultIncrement*2), bStartPatch);
  262. hitInfo = getHitInfo(v3Position);
  263. }
  264. else
  265. {
  266. v3Position = getPosition(fLocation, bStartPatch);
  267. hitInfo = getHitInfo(v3Position);
  268. }
  269. if (fLocation >= 1.0)//dont create obstacles on next patch
  270. continue;
  271. v3Position.z += fDisplacement;
  272. v3Position.y = hitInfo.point.y;
  273. CurrentAngle = -hCheckPointsMainCS.getWaypointAngle();
  274. instantiateElement(elementNumber, v3Position, CurrentAngle, hitInfo.normal);
  275. }
  276. fLocation = fDefaultIncrement*3;
  277. }
  278. //if the randomised element is currency generate three or more currency units together in a particular lane
  279. //also generate single lane obstacles on the lanes without the currency
  280. else if (elements[elementNumber].elementType == ElementType.Currency || elements[elementNumber].elementType == ElementType.Powerup)
  281. {
  282. float[] fObstacleDisplacement = new float[2];//horizontal displacement of obstacles that will be created along currency
  283. //pick the lane where to generate currency
  284. int iCurrencyLane = UnityEngine.Random.Range(0,3);
  285. // Debug.Log(iCurrencyLane);
  286. if (iCurrencyLane == 0)
  287. {
  288. fDisplacement = Mathf.Abs(fDefaultDisplacementValue);
  289. fObstacleDisplacement[0] = 0;
  290. fObstacleDisplacement[1] = -Mathf.Abs(fDefaultDisplacementValue);
  291. }
  292. else
  293. if (iCurrencyLane == 1)
  294. {
  295. fDisplacement = -Mathf.Abs(fDefaultDisplacementValue);
  296. fObstacleDisplacement[0] = Mathf.Abs(fDefaultDisplacementValue);
  297. fObstacleDisplacement[1] = 0;
  298. }
  299. else
  300. {
  301. fDisplacement = 0;
  302. fObstacleDisplacement[0] = -Mathf.Abs(fDefaultDisplacementValue);
  303. fObstacleDisplacement[1] = Mathf.Abs(fDefaultDisplacementValue);
  304. }
  305. int iToGenerate = Random.Range(3,5);//amount of currency units to generate
  306. //Debug.Log(iToGenerate);
  307. for (int i=0; i<iToGenerate; i++)
  308. {
  309. CounterCurrency++;
  310. //Debug.Log(CounterCurrency);
  311. v3Position = getPosition(fLocation, bStartPatch);
  312. v3Position.z += fDisplacement;
  313. hitInfo = getHitInfo(v3Position);
  314. v3Position.y = hitInfo.point.y;
  315. CurrentAngle = -hCheckPointsMainCS.getWaypointAngle();
  316. if (CounterCurrency== curSpawnCoupone && GetGift)
  317. {
  318. if (GetGift && PlayerPrefs.GetInt("GetCoupon") == 0)
  319. {
  320. var p = (GameObject)Instantiate(superPrize, v3Position, Quaternion.identity);
  321. Spawnprize = true;
  322. ListLetters.Add(p);
  323. }
  324. //Debug.LogError("SpawnCoupon");
  325. //p.transform.eulerAngles = CurrentAngle;
  326. instantiateElement(elementNumber, v3Position , CurrentAngle, hitInfo.normal);
  327. }
  328. else if (CounterCurrency > 10 && SpawnLetter && !TooltipLastterController.Instance.WordDone && !LoginManager.Instance.OVERGAME)
  329. {
  330. var p = (GameObject)Instantiate(Letter, v3Position, Quaternion.identity);
  331. SpawnLetter = false;
  332. var pText = p.GetComponentInChildren<VTextInterface>();
  333. ListLetters.Add(p);
  334. pText.RenderText = TooltipLastterController.Instance.GetNextLetter();
  335. }
  336. else
  337. {
  338. //Debug.Log(elementNumber);
  339. instantiateElement(elementNumber, v3Position, CurrentAngle, hitInfo.normal);
  340. //Debug.Log(v3Position);
  341. }
  342. //int parallelElement = getRandomElement();
  343. int parallelElement = 0;
  344. if (elements[elementNumber].elementType == ElementType.Powerup)
  345. {
  346. while (true)
  347. {
  348. parallelElement = getRandomElement();
  349. if (parallelElement != 5)
  350. {
  351. break;
  352. }
  353. else
  354. {
  355. // Debug.Log(elements[parallelElement].tPrefabHandle[0].name);
  356. }
  357. }
  358. if (elements[parallelElement].elementType != ElementType.MultiLaneObstacle)
  359. {
  360. v3Position = getPosition(fLocation, bStartPatch);
  361. v3Position.z += fObstacleDisplacement[Random.Range(0, fObstacleDisplacement.Length)];
  362. hitInfo = getHitInfo(v3Position);
  363. v3Position.y = hitInfo.point.y;
  364. CurrentAngle = -hCheckPointsMainCS.getWaypointAngle();
  365. instantiateElement(parallelElement, v3Position, CurrentAngle, hitInfo.normal);
  366. }
  367. }
  368. fLocation += 0.010f;
  369. if (fLocation >= 1.0f)
  370. break;
  371. }
  372. fLocation = iToGenerate*0.010f;
  373. }
  374. //if the obstacle randomised covers multiple lanes, generate it and move on
  375. else if (elements[elementNumber].elementType == ElementType.MultiLaneObstacle)
  376. {
  377. v3Position = getPosition(fLocation, bStartPatch);
  378. hitInfo = getHitInfo(v3Position);
  379. v3Position.y = hitInfo.point.y;
  380. CurrentAngle = -hCheckPointsMainCS.getWaypointAngle();
  381. instantiateElement(elementNumber, v3Position, CurrentAngle, hitInfo.normal);
  382. fLocation = 0.05f;
  383. }
  384. return fLocation;
  385. }//end of instantiate by number
  386. /*
  387. * FUNCTION: Transfroms the location on spline (0.0 to 1.0) to a Vector3 position
  388. * PARAMETER 1: Position on spline (0.0 to 1.0)
  389. * PARAMETER 2: Is the patch the first or consecutive generated ones
  390. */
  391. private Vector3 getPosition(float fLocation, bool bStartPatch)
  392. {
  393. if (bStartPatch == true)
  394. {
  395. return hCheckPointsMainCS.getCurrentWSPointBasedOnPercent(fLocation);
  396. }
  397. else
  398. {
  399. return hCheckPointsMainCS.getNextWSPointBasedOnPercent(fLocation);
  400. }
  401. }
  402. /*
  403. * FUNCTION: Calculate vertical position to put the element on
  404. * PARAMETER 1: The Vector3 position where the element will be placed.
  405. * RETURNS: The Raycast hit information.
  406. */
  407. private RaycastHit getHitInfo(Vector3 v3Position)
  408. {
  409. //Raycast towards the ground to check if terrain present
  410. bool Groundhit = false;
  411. RaycastHit hitInfo;
  412. Vector3 DownPos = new Vector3(0,-100,0) + v3Position;
  413. var layerMask = 1<<9;
  414. Groundhit = Physics.Linecast(v3Position + new Vector3(0,100,0),DownPos, out hitInfo,layerMask);
  415. return hitInfo;
  416. }
  417. /*
  418. * FUNCTION: Place element on the required position.
  419. * PARAMETER 1: The obstacle, currency unit or powerup to instantiate.
  420. * PARAMETER 2: The position where to place the element.
  421. * PARAMETER 3: The angle of the element which is based on path's curve.
  422. * PATAMETER 4: The Raycast hit information which is based on the element's placement position.
  423. */
  424. private void instantiateElement(int elementNumber, Vector3 v3Position, float CurrentAngle, Vector3 hitInfoNormal)
  425. {
  426. if (elementNumber < 0)
  427. return;
  428. Transform ObjectHandle;
  429. if (elementNumber < iObstacleCount)//obstacles
  430. {
  431. ObjectHandle = elements[elementNumber].tPrefabHandle[elements[elementNumber].iPrefabHandleIndex];
  432. elements[elementNumber].iPrefabHandleIndex++;
  433. if (elements[elementNumber].iPrefabHandleIndex >= elements[elementNumber].tPrefabHandle.Length)
  434. elements[elementNumber].iPrefabHandleIndex = 0;
  435. ObjectHandle.gameObject.SetActive(true);
  436. ObjectHandle.up = hitInfoNormal;
  437. ObjectHandle.position = v3Position;
  438. ObjectHandle.localEulerAngles = new Vector3(0,0,0);
  439. ObjectHandle.Rotate(0,CurrentAngle,0);
  440. }
  441. else if (elementNumber >= iObstacleCount && elementNumber < (iObstacleCount+iPowerupCount))//powerups
  442. {
  443. ObjectHandle = elements[elementNumber].tPrefabHandle[elements[elementNumber].iPrefabHandleIndex];
  444. elements[elementNumber].iPrefabHandleIndex++;
  445. if (elements[elementNumber].iPrefabHandleIndex >= elements[elementNumber].tPrefabHandle.Length)
  446. elements[elementNumber].iPrefabHandleIndex = 0;
  447. ObjectHandle.gameObject.SetActive(true);
  448. ObjectHandle.up = hitInfoNormal;
  449. ObjectHandle.position = v3Position;
  450. ObjectHandle.localEulerAngles = new Vector3(0,0,0);
  451. ObjectHandle.Rotate(0,CurrentAngle,0);
  452. ((PowerupScriptCS)ObjectHandle.GetComponent(typeof(PowerupScriptCS))).initPowerupScript();
  453. }
  454. else if (elementNumber == (iObstacleCount+iPowerupCount))//currency
  455. {
  456. ObjectHandle = elements[elementNumber].tPrefabHandle[elements[elementNumber].iPrefabHandleIndex];
  457. elements[elementNumber].iPrefabHandleIndex++;
  458. if (elements[elementNumber].iPrefabHandleIndex >= elements[elementNumber].tPrefabHandle.Length)
  459. elements[elementNumber].iPrefabHandleIndex = 0;
  460. ObjectHandle.gameObject.SetActive(true);
  461. ObjectHandle.up = hitInfoNormal;
  462. ObjectHandle.position = v3Position;
  463. ObjectHandle.localEulerAngles = new Vector3(0,0,0);
  464. ObjectHandle.Rotate(0,CurrentAngle,0);
  465. ((PowerupScriptCS)ObjectHandle.GetComponent(typeof(PowerupScriptCS))).initPowerupScript();
  466. }
  467. }
  468. /*
  469. * FUNCTION: Randomise an element to generate on the path
  470. * RETURNS: Element number to generate
  471. * CALLED BY: Start()
  472. * Update()
  473. * generateElements()
  474. */
  475. private int getRandomElement()
  476. {
  477. float highestFrequency = 0;
  478. int elementIndex = 0;
  479. int i = 0;
  480. float tempFreq = 0;
  481. // Debug.Log(elements[i].elementType);
  482. if (iLastPowerupGenerated > iForcePowerupGeneration //force powerup generation if not generated for a while
  483. && !hPowerupsMainControllerCS.isPowerupActive() //ensure that a powerup is not currently active
  484. && bPowerupPlaced == false) //do not generate powerup if one is already active
  485. {
  486. // for (i= iObstacleCount; i<(iObstacleCount+iPowerupCount); i++)
  487. //{
  488. // tempFreq = elements[i].iFrequency * Random.value;
  489. // if (highestFrequency < tempFreq)
  490. // {
  491. // highestFrequency = tempFreq;
  492. // elementIndex = i;
  493. // }
  494. //}
  495. if (_lastPowerUp == _magnetismIndex)
  496. {
  497. elementIndex = /*iObstacleCount + */ _sledgeIndex;
  498. _lastPowerUp = _sledgeIndex;
  499. }
  500. else
  501. {
  502. elementIndex = /*iObstacleCount +*/ _magnetismIndex;
  503. _lastPowerUp = _magnetismIndex;
  504. }
  505. //elementIndex = iObstacleCount + Random.Range(0,20) > 10? _magnetismIndex:_sledgeIndex;
  506. //Debug.LogWarning("PLACE POWERUP " + elements[elementIndex].tPrefabHandle[0].name);
  507. //#if UNITY_EDITOR
  508. // UnityEditor.Selection.activeGameObject = elements[elementIndex].tPrefabHandle[0].gameObject;
  509. // UnityEditor.EditorApplication.isPaused = true;
  510. //#endif
  511. iLastPowerupGenerated = 0;//reset variable
  512. }
  513. else//normal case; generate any random element
  514. {
  515. for (i=0; i<iTotalCount; i++)
  516. {
  517. if ( (elements[i].elementType == ElementType.Powerup
  518. && hPowerupsMainControllerCS.isPowerupActive() ) //do not generate powerup if one is already active
  519. || (elements[i].elementType == ElementType.Powerup && bPowerupPlaced == true) ) //do not place powerup on current patch if one has already been placed
  520. continue;
  521. tempFreq = elements[i].iFrequency * Random.value;
  522. if (highestFrequency < tempFreq)
  523. {
  524. highestFrequency = tempFreq;
  525. elementIndex = i;
  526. }
  527. }//end of for
  528. }
  529. //if a powerup has been placed, stop placing more powerups in current patch
  530. if (elements[elementIndex].elementType == ElementType.Powerup)
  531. {
  532. if (elements[elementIndex].tPrefabHandle[0].name.Contains("Mag") || elements[elementIndex].tPrefabHandle[0].name.Contains("Sle"))
  533. {
  534. bPowerupPlaced = true;
  535. }
  536. }
  537. return elementIndex;
  538. }
  539. /*
  540. * FUNCTION: Generate a list of clones of all elements to be used int the game
  541. * CALLED BY: Start()
  542. */
  543. private void setPrefabHandlers()
  544. {
  545. //Debug.LogWarning("PFHandlesGroup");
  546. int i=0;
  547. tPrefabHandlesParent = GameObject.Find("PFHandlesGroup").transform;//get the transfrom of the parent of all clones
  548. elements = new Element[iTotalCount];
  549. //Obstacles
  550. for (i=0; i< iObstacleCount; i++)//traverse through the obstacles
  551. {
  552. elements[i].iFrequency = ((ObstacleScriptCS)obstaclePrefabs[i].GetComponent(typeof(ObstacleScriptCS))).frequency;//get the occurance frequency of the particular obstacle
  553. elements[i].tPrefabHandle = new Transform[elements[i].iFrequency*4 + 1]; //allocate memory to element prefabs handler array
  554. elements[i].iPrefabHandleIndex = 0;
  555. elements[i].elementType = ((ObstacleScriptCS)obstaclePrefabs[i].GetComponent(typeof(ObstacleScriptCS))).obstacleAreaType; //set the element type
  556. //Debug.Log(elements[i].tPrefabHandle.Length);
  557. for (int j=0; j<elements[i].tPrefabHandle.Length; j++)//generate obstacle clones
  558. {
  559. var go = (GameObject) Instantiate(obstaclePrefabs[i], new Vector3(-1000, 0, 0), new Quaternion());
  560. elements[i].tPrefabHandle[j] = go.transform;
  561. //Debug.Log(go.transform.name);
  562. // Debug.Log(tPrefabHandlesParent);
  563. elements[i].tPrefabHandle[j].parent = tPrefabHandlesParent;
  564. }
  565. }
  566. //Powerups
  567. int index = 0;
  568. for (i=iObstacleCount; i<(iPowerupCount+iObstacleCount); i++)//traverse through the power-ups
  569. {
  570. var pvs = powerupPrefabs[index].GetComponent<PowerupScriptCS>();
  571. if (pvs.powerupType == PowerupsMainControllerCS.PowerUps.Magnetism)
  572. {
  573. _magnetismIndex = i;
  574. }
  575. if (pvs.powerupType == PowerupsMainControllerCS.PowerUps.SuperSledge)
  576. {
  577. _sledgeIndex = i;
  578. }
  579. elements[i].iFrequency = pvs.frequency;
  580. elements[i].tPrefabHandle = new Transform[elements[i].iFrequency];
  581. elements[i].iPrefabHandleIndex = 0;
  582. elements[i].elementType = ElementType.Powerup;
  583. for (int j=0; j<elements[i].tPrefabHandle.Length; j++)//generate clones
  584. {
  585. elements[i].tPrefabHandle[j] = ( (GameObject)Instantiate(powerupPrefabs[index], new Vector3(-1000,0,0), new Quaternion()) ).transform;
  586. elements[i].tPrefabHandle[j].parent = tPrefabHandlesParent;
  587. ((PowerupScriptCS)elements[i].tPrefabHandle[j].GetComponent(typeof(PowerupScriptCS))).enabled = true;
  588. }
  589. ++index;
  590. }
  591. //Currency
  592. index = iPowerupCount+iObstacleCount;
  593. elements[index].iFrequency = ((PowerupScriptCS)currencyPrefab.GetComponent(typeof(PowerupScriptCS))).frequency;
  594. elements[index].tPrefabHandle = new Transform[elements[i].iFrequency*11 + 1];
  595. elements[index].iPrefabHandleIndex = 0;
  596. elements[index].elementType = ElementType.Currency;
  597. for (int j=0; j<elements[index].tPrefabHandle.Length; j++)//generate clones
  598. {
  599. elements[index].tPrefabHandle[j] = ( (GameObject)Instantiate(currencyPrefab,new Vector3(-1000,0,0), new Quaternion()) ).transform;
  600. elements[index].tPrefabHandle[j].parent = tPrefabHandlesParent;
  601. ((PowerupScriptCS)elements[index].tPrefabHandle[j].GetComponent(typeof(PowerupScriptCS))).enabled = true;
  602. }
  603. ////SuperPrize
  604. //index = iPowerupCount + iObstacleCount;
  605. //elements[index].iFrequency = ((PowerupScriptCS)superPrize.GetComponent(typeof(PowerupScriptCS))).frequency;
  606. //elements[index].tPrefabHandle = new Transform[elements[i].iFrequency * 11 + 1];
  607. //elements[index].iPrefabHandleIndex = 0;
  608. //elements[index].elementType = ElementType.Prize;
  609. //for (int j = 0; j < 4; j++)//generate clones
  610. //{
  611. // elements[index].tPrefabHandle[j] = ((GameObject)Instantiate(superPrize, new Vector3(-1000, 0, 0), new Quaternion())).transform;
  612. // elements[index].tPrefabHandle[j].parent = tPrefabHandlesParent;
  613. // ((PowerupScriptCS)elements[index].tPrefabHandle[j].GetComponent(typeof(PowerupScriptCS))).enabled = true;
  614. //}
  615. }//end of set prefabs handler function
  616. }