Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class EnemySpawn : MonoBehaviour {
- protected GameObject[] spawnObject = new GameObject[5];
- //Vars for enemy position
- protected int numberOfCols;
- protected int numberOfRows;
- protected float spawnAreaDepth;
- protected float spawnAreaWidth;
- protected int accenAmt;
- protected int sagittAmt;
- protected int legionAmt;
- protected int sagittHorseAmt;
- protected int centAmt;
- private int numberOfTroopTypes = 4;
- private float spawnAreaHeight = 2.5f;
- Vector3 spawnPosition;
- void Awake () {
- spawnObject [0] = (GameObject)(Resources.Load("Models/SM_Enemy_Accensus"));
- spawnObject [1] = (GameObject)(Resources.Load("Models/SM_Enemy_Sagittarii"));
- spawnObject [2] = (GameObject)(Resources.Load("Models/SM_Enemy_Legionary"));
- spawnObject [3] = (GameObject)(Resources.Load("Models/SM_Enemy_Sagittarii_Horse"));
- spawnObject [4] = (GameObject)(Resources.Load ("Models/SM_Enemy_Centurion"));
- }
- void Start()
- {
- }
- // Update is called once per frame
- void Update () {
- }
- void SetStartingValues(Dictionary<string,int> startingValues)
- {
- //Tries to run the code to see if there is a problem.
- try
- {
- numberOfCols = startingValues ["Number of Cols"];
- numberOfRows = startingValues ["Number of Rows"];
- spawnAreaDepth = startingValues ["Depth"];
- spawnAreaWidth = startingValues ["Width"];
- accenAmt = startingValues ["Accen Amt"];
- sagittAmt = startingValues ["Sagitt Amt"];
- legionAmt = startingValues ["Legionary Amt"];
- sagittHorseAmt = startingValues ["SagittHorse Amt"];
- centAmt = startingValues ["Cent Amt"];
- }
- //If a value was wrong then it will throw an exception and we print it.
- catch(KeyNotFoundException)
- {
- Debug.Log ("KeyNotFoundInDictionary ");
- }
- //Takes the system time and date and stores it in currentTime.
- System.DateTime currentTime = System.DateTime.Now;
- //APlaces the milliseconds 0-999 into the random seed. This means that it will generate the enemies far more randomly.
- Random.seed = currentTime.Millisecond;
- //Temp variables so that we can compared (Used to set enemy limits)
- int accenSpawned = 0;
- int sagittSpawned = 0;
- int legionSpawned = 0;
- int sagittHorseSpawned = 0;
- int centSpawned = 0;
- 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.
- 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.
- spawnPosition = transform.position; //Stores the objects position into the Vector 3.
- spawnPosition.z += i * (spawnAreaDepth/numberOfRows);
- spawnPosition.x += j * (spawnAreaWidth/numberOfCols);
- spawnPosition.y = spawnAreaHeight;
- //Sets a random range between 0,2 however 2 in exclusive so it's between 0 and 1.
- int rand = Random.Range (0,numberOfTroopTypes);
- //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.
- if (accenSpawned == accenAmt)
- {
- if (sagittSpawned == sagittAmt)
- {
- rand = Random.Range(2, numberOfTroopTypes);
- }
- else if (legionSpawned == legionAmt)
- {
- int[] intRange = {1,3,4};
- int rangeFound = Random.Range (0,3);
- rand = intRange[rangeFound];
- }
- else if (sagittHorseSpawned == sagittHorseAmt)
- {
- int[] intRange = {1,2,4};
- int rangeFound = Random.Range (0,3);
- rand = intRange[rangeFound];
- }
- else if (centSpawned == centAmt)
- {
- rand = Random.Range (1,4);
- }
- else
- {
- rand = Random.Range (1,numberOfTroopTypes);
- }
- }
- if (sagittSpawned == sagittAmt)
- {
- if (accenSpawned == accenAmt)
- {
- rand = Random.Range(2, numberOfTroopTypes);
- }
- else if (legionSpawned == legionAmt)
- {
- int[] intRange = {0,3,4};
- int rangeFound = Random.Range (0,3);
- rand = intRange[rangeFound];
- }
- else if (sagittHorseSpawned == sagittHorseAmt)
- {
- int[] intRange = {0,2,4};
- int rangeFound = Random.Range (0,3);
- rand = intRange[rangeFound];
- }
- else if (centSpawned == centAmt)
- {
- int [] intRange = {0,2,3};
- int rangeFound = Random.Range (0,3);
- rand = intRange[rangeFound];
- }
- else
- {
- int [] intRange = {0,2,3,4};
- int rangeFound = Random.Range (0,numberOfTroopTypes);
- rand = intRange[rangeFound];
- }
- }
- if (legionSpawned == legionAmt)
- {
- if (accenSpawned == accenAmt)
- {
- int[] intRange = {1,3,4};
- int rangeFound = Random.Range (0,3);
- rand = intRange[rangeFound];
- }
- else if (sagittSpawned == sagittAmt)
- {
- int[] intRange = {0,3,4};
- int rangeFound = Random.Range (0,3);
- rand = intRange[rangeFound];
- }
- else if (sagittHorseSpawned == sagittHorseAmt)
- {
- int[] intRange = {0,1,4};
- int rangeFound = Random.Range (0,3);
- rand = intRange[rangeFound];
- }
- else if (centSpawned == centAmt)
- {
- int [] intRange = {0,1,3};
- int rangeFound = Random.Range (0,3);
- rand = intRange[rangeFound];
- }
- else
- {
- int [] intRange = {0,1,3,4};
- int rangeFound = Random.Range (0,numberOfTroopTypes);
- rand = intRange[rangeFound];;
- }
- }
- if (sagittHorseSpawned == sagittHorseAmt)
- {
- if (accenSpawned == accenAmt)
- {
- int[] intRange = {1,2,4};
- int rangeFound = Random.Range (0,3);
- rand = intRange[rangeFound];
- }
- else if (sagittSpawned == sagittAmt)
- {
- int[] intRange = {0,2,4};
- int rangeFound = Random.Range (0,3);
- rand = intRange[rangeFound];
- }
- else if (legionSpawned == legionAmt)
- {
- int[] intRange = {0,1,4};
- int rangeFound = Random.Range (0,3);
- rand = intRange[rangeFound];
- }
- else if (centSpawned == centAmt)
- {
- int [] intRange = {0,1,2};
- int rangeFound = Random.Range (0,3);
- rand = intRange[rangeFound];
- }
- else
- {
- int [] intRange = {0,1,2,4};
- int rangeFound = Random.Range (0,numberOfTroopTypes);
- rand = intRange[rangeFound];;
- }
- }
- if (centSpawned == centAmt)
- {
- if (accenSpawned == accenAmt)
- {
- int[] intRange = {1,2,3};
- int rangeFound = Random.Range (0,3);
- rand = intRange[rangeFound];
- }
- else if (sagittSpawned == sagittAmt)
- {
- int[] intRange = {0,2,3};
- int rangeFound = Random.Range (0,3);
- rand = intRange[rangeFound];
- }
- else if (legionSpawned == legionAmt)
- {
- int[] intRange = {0,1,3};
- int rangeFound = Random.Range (0,3);
- rand = intRange[rangeFound];
- }
- else if (sagittHorseSpawned == sagittHorseAmt)
- {
- int [] intRange = {0,1,2};
- int rangeFound = Random.Range (0,3);
- rand = intRange[rangeFound];
- }
- else
- {
- int [] intRange = {0,1,2,3};
- int rangeFound = Random.Range (0,numberOfTroopTypes);
- rand = intRange[rangeFound];;
- }
- }
- switch (rand)
- {
- case 0:
- accenSpawned++;
- break;
- case 1:
- sagittSpawned++;
- break;
- case 2:
- legionSpawned++;
- break;
- case 3:
- sagittHorseSpawned++;
- break;
- case 4:
- centSpawned++;
- break;
- }
- //Creates a new gameobject from the rand number in the spawnObjects array.
- GameObject newObject = Instantiate(spawnObject[rand],spawnPosition,spawnObject[rand].transform.rotation)as GameObject; //Takes the gameobject selected rotation so it's on the right rotation.
- newObject.transform.parent = transform; //Stores the gameobjects transform into the parents transform (Enemy Manager) Useful for neatness.
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement