BoardSetup.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using Random = UnityEngine.Random;
  7. namespace Receive
  8. {
  9. public class BoardSetup : MonoBehaviour {
  10. public GameObject cubePrefab;
  11. public GameObject redCrossPrefab;
  12. public GameObject blueCrossPrefab;
  13. public Text timerText;
  14. public Text escMessage;
  15. public bool shwoVisualCue = true;
  16. public Vector3 startPosition;
  17. public GameObject rightHandGun;
  18. float rotationSpeed = 5f;
  19. Receive messenger;
  20. public GameObject vmObject;
  21. VirbationManager vm;
  22. float cubeDistance = 2.2f;
  23. float crossDistance = 7.2f;
  24. float lenght = 4f;
  25. float height = 4f;
  26. int maxRed;
  27. int maxBlue;
  28. int foundRed = 0;
  29. int foundBlue = 0;
  30. private List<GameObject> cubeList;
  31. GameObject currentRed;
  32. GameObject currentBlue;
  33. GameObject redCross;
  34. GameObject blueCross;
  35. DirectionIndicator[] redArrows;
  36. DirectionIndicator[] blueArrows;
  37. bool timerActive = true;
  38. // Use this for initialization
  39. void Start () {
  40. maxRed = 16;//(int) Mathf.Floor ((lenght * height) / 2f);
  41. maxBlue = 0;//(int) Mathf.Ceil ((lenght * height) / 2f);
  42. messenger = new Receive {RunLoop = true, protID = 3};
  43. messenger.Setup();
  44. cubeList = new List<GameObject> ();
  45. //Vector3 startPosition = new Vector3(-3.5f,-2.5f,0f);
  46. for (int i = 0; i < lenght; i++) {
  47. for (int j = 0; j < height; j++) {
  48. Vector3 spawnPosition = startPosition + new Vector3(i * cubeDistance,j * cubeDistance,0);
  49. GameObject currentCube = Instantiate (cubePrefab, spawnPosition, Quaternion.identity);
  50. currentCube.transform.RotateAround (currentCube.transform.position, Vector3.down, 180f);
  51. cubeList.Add (currentCube);
  52. }
  53. }
  54. SetRandomRed ();
  55. //SetRandomBlue ();
  56. redCross = Instantiate (redCrossPrefab, startPosition + new Vector3( 2*crossDistance,1,0), Quaternion.identity);
  57. redArrows = redCross.GetComponentsInChildren<DirectionIndicator> ();
  58. //blueCross = Instantiate (blueCrossPrefab, new Vector3(-crossDistance,1,-10), Quaternion.identity);
  59. //blueArrows = blueCross.GetComponentsInChildren<DirectionIndicator> ();
  60. //setCrossVisibility(shwoVisualCue);
  61. vm = vmObject.GetComponent<VirbationManager>();
  62. vm.arrows = redArrows;
  63. vm.messenger = messenger;
  64. SetupArrows();
  65. }
  66. void SetRandomRed(){
  67. if (cubeList.Count > 0 && foundRed < maxRed) {
  68. int randomIndex = Random.Range (0, cubeList.Count);
  69. cubeList [randomIndex].GetComponent<CubeBehaviour> ().ToggleToRed ();
  70. currentRed = cubeList [randomIndex];
  71. cubeList.RemoveAt (randomIndex);
  72. }
  73. }
  74. void SetRandomBlue(){
  75. if (cubeList.Count > 0 && foundBlue < maxBlue) {
  76. int randomIndex = Random.Range (0, cubeList.Count);
  77. cubeList [randomIndex].GetComponent<CubeBehaviour> ().ToggleToBlue ();
  78. currentBlue = cubeList [randomIndex];
  79. cubeList.RemoveAt (randomIndex);
  80. }
  81. }
  82. void SetupArrows(){
  83. for (int i = 0; i < 4; i++) {
  84. if (foundRed == maxRed) {
  85. redArrows [i].currentlyActive = false;
  86. vm.gameEnded = true;
  87. } else {
  88. vm.TargetPostion = currentRed.transform;
  89. vm.pointerPosition = rightHandGun.GetComponent<GunBehaviour>().getAimTransform();
  90. vm.initialized = true;
  91. redArrows [i].target = currentRed;
  92. redArrows[i].messenger = messenger;
  93. redArrows[i].crossCenter = redCross.transform.position;
  94. }
  95. /*
  96. if (foundBlue == maxBlue) {
  97. blueArrows [i].currentlyActive = false;
  98. } else {
  99. blueArrows [i].target = currentBlue;
  100. }*/
  101. redArrows[i].pointerPosition = rightHandGun.GetComponent<GunBehaviour>().getAimPosition();
  102. redArrows[i].GetComponent<MeshRenderer>().enabled = shwoVisualCue;
  103. }
  104. }
  105. void setCrossVisibility(bool b)
  106. {
  107. MeshRenderer[] mr = redCross.GetComponentsInChildren<MeshRenderer>();
  108. for(int i = 0;i<mr.Length;i++)
  109. {
  110. mr[i].enabled = b;
  111. }
  112. }
  113. // Update is called once per frame
  114. void Update () {
  115. SetupArrows();
  116. if (currentRed.GetComponent<CubeBehaviour> ().uncovered && foundRed < maxRed) {
  117. foundRed++;
  118. SetRandomRed ();
  119. SetupArrows ();
  120. }/*
  121. if (currentBlue.GetComponent<CubeBehaviour> ().uncovered && foundBlue < maxBlue) {
  122. foundBlue++;
  123. SetRandomBlue ();
  124. SetupArrows();
  125. }*/
  126. if (timerActive) {
  127. if (!(foundRed == maxRed && foundBlue == maxBlue)) {
  128. float passedTime = Time.time;
  129. int minutes = (int)Mathf.Floor (passedTime / 60);
  130. int seconds = (int)Mathf.Floor (passedTime % 60);
  131. string secondsString = seconds.ToString ();
  132. if (seconds < 10) {
  133. secondsString = "0" + seconds;
  134. }
  135. timerText.text = minutes + ":" + secondsString;
  136. } else {
  137. timerActive = false;
  138. timerText.text = "Final Time: " + timerText.text;
  139. escMessage.text = "Press Esc to quit.";
  140. }
  141. }
  142. if (Input.GetKeyDown(KeyCode.Escape)) {
  143. Application.Quit ();
  144. }
  145. float horizontalInput = Input.GetAxis("Horizontal");
  146. if (horizontalInput != 0 && redArrows[0].target != null)
  147. {
  148. redCross.transform.RotateAround(redCross.transform.position, Vector3.forward, rotationSpeed * -horizontalInput);
  149. }
  150. redCross.transform.rotation = Quaternion.Euler(0,0,rightHandGun.transform.rotation.eulerAngles.z);
  151. vm.rotationAngle = -redCross.transform.rotation.eulerAngles.z;
  152. }
  153. }
  154. }