Advertisement
Guest User

Untitled

a guest
Jan 27th, 2015
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.99 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class EnemySpawn : MonoBehaviour {
  6.  
  7. protected GameObject[] spawnObject = new GameObject[5];
  8.  
  9.  
  10. //Vars for enemy position
  11. protected int numberOfCols;
  12. protected int numberOfRows;
  13. protected float spawnAreaDepth;
  14. protected float spawnAreaWidth;
  15.  
  16. protected int accenAmt;
  17. protected int sagittAmt;
  18. protected int legionAmt;
  19. protected int sagittHorseAmt;
  20. protected int centAmt;
  21.  
  22. private int numberOfTroopTypes = 4;
  23. private float spawnAreaHeight = 2.5f;
  24. Vector3 spawnPosition;
  25.  
  26. void Awake () {
  27.  
  28. spawnObject [0] = (GameObject)(Resources.Load("Models/SM_Enemy_Accensus"));
  29. spawnObject [1] = (GameObject)(Resources.Load("Models/SM_Enemy_Sagittarii"));
  30. spawnObject [2] = (GameObject)(Resources.Load("Models/SM_Enemy_Legionary"));
  31. spawnObject [3] = (GameObject)(Resources.Load("Models/SM_Enemy_Sagittarii_Horse"));
  32. spawnObject [4] = (GameObject)(Resources.Load ("Models/SM_Enemy_Centurion"));
  33.  
  34. }
  35.  
  36. void Start()
  37. {
  38.  
  39. }
  40.  
  41. // Update is called once per frame
  42. void Update () {
  43.  
  44. }
  45.  
  46. void SetStartingValues(Dictionary<string,int> startingValues)
  47. {
  48. //Tries to run the code to see if there is a problem.
  49. try
  50. {
  51. numberOfCols = startingValues ["Number of Cols"];
  52. numberOfRows = startingValues ["Number of Rows"];
  53. spawnAreaDepth = startingValues ["Depth"];
  54. spawnAreaWidth = startingValues ["Width"];
  55.  
  56. accenAmt = startingValues ["Accen Amt"];
  57. sagittAmt = startingValues ["Sagitt Amt"];
  58. legionAmt = startingValues ["Legionary Amt"];
  59. sagittHorseAmt = startingValues ["SagittHorse Amt"];
  60. centAmt = startingValues ["Cent Amt"];
  61. }
  62. //If a value was wrong then it will throw an exception and we print it.
  63. catch(KeyNotFoundException)
  64. {
  65. Debug.Log ("KeyNotFoundInDictionary ");
  66. }
  67.  
  68. //Takes the system time and date and stores it in currentTime.
  69. System.DateTime currentTime = System.DateTime.Now;
  70. //APlaces the milliseconds 0-999 into the random seed. This means that it will generate the enemies far more randomly.
  71. Random.seed = currentTime.Millisecond;
  72.  
  73. //Temp variables so that we can compared (Used to set enemy limits)
  74. int accenSpawned = 0;
  75. int sagittSpawned = 0;
  76. int legionSpawned = 0;
  77. int sagittHorseSpawned = 0;
  78. int centSpawned = 0;
  79.  
  80. for (int i = 0; i <numberOfRows; i++) { //Cycles from 0 to the number of enemies var until it's full. Int I is creating a variable to use.
  81.  
  82. for(int j = 0; j < numberOfCols; j++){ //J is used this time as it's next in the alphabet. //This is nested in another if statement.
  83.  
  84. spawnPosition = transform.position; //Stores the objects position into the Vector 3.
  85.  
  86. spawnPosition.z += i * (spawnAreaDepth/numberOfRows);
  87. spawnPosition.x += j * (spawnAreaWidth/numberOfCols);
  88. spawnPosition.y = spawnAreaHeight;
  89.  
  90. //Sets a random range between 0,2 however 2 in exclusive so it's between 0 and 1.
  91. int rand = Random.Range (0,numberOfTroopTypes);
  92.  
  93. //Asks the value of ceturions spawned == to 9 which is the cent amt. IF TRUE then it keeps doing the random range until it gets a 1 for the pilus unit.
  94. if (accenSpawned == accenAmt)
  95. {
  96. if (sagittSpawned == sagittAmt)
  97. {
  98. rand = Random.Range(2, numberOfTroopTypes);
  99. }
  100. else if (legionSpawned == legionAmt)
  101. {
  102. int[] intRange = {1,3,4};
  103. int rangeFound = Random.Range (0,3);
  104. rand = intRange[rangeFound];
  105. }
  106. else if (sagittHorseSpawned == sagittHorseAmt)
  107. {
  108. int[] intRange = {1,2,4};
  109. int rangeFound = Random.Range (0,3);
  110. rand = intRange[rangeFound];
  111. }
  112. else if (centSpawned == centAmt)
  113. {
  114. rand = Random.Range (1,4);
  115. }
  116. else
  117. {
  118. rand = Random.Range (1,numberOfTroopTypes);
  119. }
  120. }
  121.  
  122. if (sagittSpawned == sagittAmt)
  123. {
  124. if (accenSpawned == accenAmt)
  125. {
  126. rand = Random.Range(2, numberOfTroopTypes);
  127. }
  128. else if (legionSpawned == legionAmt)
  129. {
  130. int[] intRange = {0,3,4};
  131. int rangeFound = Random.Range (0,3);
  132. rand = intRange[rangeFound];
  133. }
  134. else if (sagittHorseSpawned == sagittHorseAmt)
  135. {
  136. int[] intRange = {0,2,4};
  137. int rangeFound = Random.Range (0,3);
  138. rand = intRange[rangeFound];
  139. }
  140. else if (centSpawned == centAmt)
  141. {
  142. int [] intRange = {0,2,3};
  143. int rangeFound = Random.Range (0,3);
  144. rand = intRange[rangeFound];
  145. }
  146. else
  147. {
  148. int [] intRange = {0,2,3,4};
  149. int rangeFound = Random.Range (0,numberOfTroopTypes);
  150. rand = intRange[rangeFound];
  151. }
  152. }
  153.  
  154. if (legionSpawned == legionAmt)
  155. {
  156. if (accenSpawned == accenAmt)
  157. {
  158. int[] intRange = {1,3,4};
  159. int rangeFound = Random.Range (0,3);
  160. rand = intRange[rangeFound];
  161. }
  162. else if (sagittSpawned == sagittAmt)
  163. {
  164. int[] intRange = {0,3,4};
  165. int rangeFound = Random.Range (0,3);
  166. rand = intRange[rangeFound];
  167. }
  168. else if (sagittHorseSpawned == sagittHorseAmt)
  169. {
  170. int[] intRange = {0,1,4};
  171. int rangeFound = Random.Range (0,3);
  172. rand = intRange[rangeFound];
  173. }
  174. else if (centSpawned == centAmt)
  175. {
  176. int [] intRange = {0,1,3};
  177. int rangeFound = Random.Range (0,3);
  178. rand = intRange[rangeFound];
  179. }
  180. else
  181. {
  182. int [] intRange = {0,1,3,4};
  183. int rangeFound = Random.Range (0,numberOfTroopTypes);
  184. rand = intRange[rangeFound];;
  185. }
  186. }
  187.  
  188. if (sagittHorseSpawned == sagittHorseAmt)
  189. {
  190. if (accenSpawned == accenAmt)
  191. {
  192. int[] intRange = {1,2,4};
  193. int rangeFound = Random.Range (0,3);
  194. rand = intRange[rangeFound];
  195. }
  196. else if (sagittSpawned == sagittAmt)
  197. {
  198. int[] intRange = {0,2,4};
  199. int rangeFound = Random.Range (0,3);
  200. rand = intRange[rangeFound];
  201. }
  202. else if (legionSpawned == legionAmt)
  203. {
  204. int[] intRange = {0,1,4};
  205. int rangeFound = Random.Range (0,3);
  206. rand = intRange[rangeFound];
  207. }
  208. else if (centSpawned == centAmt)
  209. {
  210. int [] intRange = {0,1,2};
  211. int rangeFound = Random.Range (0,3);
  212. rand = intRange[rangeFound];
  213. }
  214. else
  215. {
  216. int [] intRange = {0,1,2,4};
  217. int rangeFound = Random.Range (0,numberOfTroopTypes);
  218. rand = intRange[rangeFound];;
  219. }
  220. }
  221.  
  222. if (centSpawned == centAmt)
  223. {
  224. if (accenSpawned == accenAmt)
  225. {
  226. int[] intRange = {1,2,3};
  227. int rangeFound = Random.Range (0,3);
  228. rand = intRange[rangeFound];
  229. }
  230. else if (sagittSpawned == sagittAmt)
  231. {
  232. int[] intRange = {0,2,3};
  233. int rangeFound = Random.Range (0,3);
  234. rand = intRange[rangeFound];
  235. }
  236. else if (legionSpawned == legionAmt)
  237. {
  238. int[] intRange = {0,1,3};
  239. int rangeFound = Random.Range (0,3);
  240. rand = intRange[rangeFound];
  241. }
  242. else if (sagittHorseSpawned == sagittHorseAmt)
  243. {
  244. int [] intRange = {0,1,2};
  245. int rangeFound = Random.Range (0,3);
  246. rand = intRange[rangeFound];
  247. }
  248. else
  249. {
  250. int [] intRange = {0,1,2,3};
  251. int rangeFound = Random.Range (0,numberOfTroopTypes);
  252. rand = intRange[rangeFound];;
  253. }
  254. }
  255.  
  256. switch (rand)
  257. {
  258. case 0:
  259. accenSpawned++;
  260. break;
  261. case 1:
  262. sagittSpawned++;
  263. break;
  264. case 2:
  265. legionSpawned++;
  266. break;
  267. case 3:
  268. sagittHorseSpawned++;
  269. break;
  270. case 4:
  271. centSpawned++;
  272. break;
  273. }
  274.  
  275. //Creates a new gameobject from the rand number in the spawnObjects array.
  276. GameObject newObject = Instantiate(spawnObject[rand],spawnPosition,spawnObject[rand].transform.rotation)as GameObject; //Takes the gameobject selected rotation so it's on the right rotation.
  277. newObject.transform.parent = transform; //Stores the gameobjects transform into the parents transform (Enemy Manager) Useful for neatness.
  278.  
  279. }
  280. }
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement