Advertisement
Guest User

Untitled

a guest
Mar 5th, 2015
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. private Transform myTransform;
  2.  
  3. // Use this for initialization
  4. void Start ()
  5. {
  6. targets = new List<Transform> ();
  7. selectedTarget = null;
  8. myTransform = transform;
  9.  
  10. AddAllEnemies();
  11.  
  12. }
  13.  
  14.  
  15. public void AddAllEnemies()
  16. {
  17. GameObject[] go = GameObject.FindGameObjectsWithTag ("Enemy");
  18.  
  19. foreach (GameObject enemy in go)
  20. {
  21.  
  22. Debug.Log ("Adding enemies to list....");
  23. AddTarget (enemy.transform);
  24. }
  25.  
  26. }
  27.  
  28. public void AddTarget(Transform enemy)
  29. {
  30. Debug.Log ("Added enemy to the list");
  31. targets.Add (enemy);
  32. }
  33.  
  34. private void SortTargetByDistance()
  35. {
  36. targets.Sort (delegate(Transform t1, Transform t2)
  37. { return Vector3.Distance (t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position));
  38. });
  39. }
  40.  
  41.  
  42. private void TargetEnemy()
  43. {
  44. if(selectedTarget == null)
  45. {
  46. SortTargetByDistance();
  47. selectedTarget = targets[0]; //so the list is empty? well at first it is thats why its null, so it isnt adding them to list? doesn't seem like that, how can you output stuff? is there a comma
  48. }
  49. else
  50. {
  51. int index = targets.IndexOf (selectedTarget);
  52.  
  53. if(index < targets.Count - 1)
  54. {
  55. index++;
  56. }
  57. else
  58. {
  59. index = 0;
  60. }
  61. DeselectTarget();
  62. selectedTarget = targets[index];
  63.  
  64. }
  65. SelectTarget();
  66. }
  67.  
  68. private void SelectTarget()
  69. {
  70. Transform name = selectedTarget.FindChild ("Name");
  71.  
  72. if(name == null)
  73. {
  74. Debug.LogError("Could not find the Name on " + selectedTarget.name);
  75. return;
  76. }
  77.  
  78. name.GetComponent<TextMesh> ().text = selectedTarget.GetComponent<Mob>().Name;
  79. name.GetComponent<MeshRenderer> ().enabled = true;
  80. selectedTarget.GetComponent<Mob> ().DisplayHealth();
  81.  
  82. Messenger.Broadcast<bool> ("show mob vitalbars", true);
  83.  
  84. }
  85.  
  86. private void DeselectTarget()
  87. {
  88. selectedTarget.FindChild("Name").GetComponent<MeshRenderer>().enabled = false;
  89.  
  90. selectedTarget = null;
  91. Messenger.Broadcast<bool> ("show mob vitalbars", false);
  92. }
  93.  
  94. // Update is called once per frame
  95. void Update ()
  96. {
  97. if(Input.GetKeyDown (KeyCode.Tab))
  98. {
  99. TargetEnemy();
  100. }
  101. }
  102.  
  103. public enum State
  104. {
  105. Idle,
  106. Initialize,
  107. Setup,
  108. SpawnMob
  109.  
  110.  
  111. }
  112.  
  113. public GameObject[] mobPrefabs; // an array to hold all of the prefabs of mobs we want to spawn
  114. public GameObject[] spawnPoints; // an array will hold a reference to all of the spawn points in game
  115.  
  116. public State state; // our local variable that holds our current state
  117.  
  118. void Awake()
  119. {
  120. state = MobGenerator.State.Initialize;
  121.  
  122.  
  123. }
  124.  
  125. // USE THIS FOR INITIALIZATION
  126. IEnumerator Start()
  127. {
  128. while(true)
  129. {
  130. switch(state)
  131. {
  132. case State.Initialize:
  133. Initialize();
  134. break;
  135.  
  136. case State.Setup:
  137. Setup();
  138. break;
  139.  
  140. case State.SpawnMob:
  141. SpawnMob();
  142. break;
  143.  
  144. }
  145. yield return 0;
  146. }
  147. }
  148.  
  149. // make sure that eeverything is initialized before we go on to the next step
  150. private void Initialize()
  151. {
  152. Debug.Log("*** We are in the initialize function ***");
  153.  
  154. if (!CheckForMobPrefabs())
  155. return;
  156.  
  157. if (!CheckForSpawnPoints ())
  158. return;
  159.  
  160. state = MobGenerator.State.Setup;
  161. }
  162.  
  163. // make sure that everything is setup before we continue
  164. private void Setup()
  165. {
  166. Debug.Log("*** We are in the setup function ***");
  167.  
  168. state = MobGenerator.State.SpawnMob;
  169.  
  170. }
  171.  
  172. // spawn a mob if we have an open spawn point
  173. private void SpawnMob()
  174. {
  175. Debug.Log ("*** Spawn Mob ***");
  176.  
  177. GameObject[] gos = AvailableSpawnPoints ();
  178.  
  179. for (int cnt = 0; cnt < gos.Length; cnt++)
  180. {
  181. GameObject go = Instantiate (mobPrefabs [Random.Range (0, mobPrefabs.Length)],
  182. gos [cnt].transform.position,
  183. Quaternion.identity) as GameObject;
  184.  
  185. go.transform.parent = gos [cnt].transform;
  186. Debug.Log ("there are " + go.transform.childCount.ToString () + " children");
  187.  
  188. state = MobGenerator.State.Idle;
  189. }
  190. }
  191.  
  192. // check to see that we have at least one mob prefab to spawn
  193. private bool CheckForMobPrefabs()
  194. {
  195. if (mobPrefabs.Length > 0)
  196. return true;
  197. else
  198. return false;
  199. }
  200.  
  201. // check to see if we have at least one spawnpoint to spawn mobs at
  202. private bool CheckForSpawnPoints()
  203. {
  204. if (spawnPoints.Length > 0)
  205. return true;
  206. else
  207. return false;
  208.  
  209. }
  210.  
  211. // generate a list of available spawn points that do not have any mobs childed to it
  212. private GameObject[] AvailableSpawnPoints()
  213. {
  214. List<GameObject> gos = new List<GameObject> ();
  215.  
  216. for(int cnt = 0; cnt < spawnPoints.Length; cnt++)
  217. {
  218. if(spawnPoints[cnt].transform.childCount == 0)
  219. {
  220.  
  221. Debug.Log ("*** Spawn point available ***");
  222. gos.Add (spawnPoints[cnt]);
  223.  
  224. }
  225. }
  226. return gos.ToArray ();
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement