Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private Transform myTransform;
- // Use this for initialization
- void Start ()
- {
- targets = new List<Transform> ();
- selectedTarget = null;
- myTransform = transform;
- AddAllEnemies();
- }
- public void AddAllEnemies()
- {
- GameObject[] go = GameObject.FindGameObjectsWithTag ("Enemy");
- foreach (GameObject enemy in go)
- {
- Debug.Log ("Adding enemies to list....");
- AddTarget (enemy.transform);
- }
- }
- public void AddTarget(Transform enemy)
- {
- Debug.Log ("Added enemy to the list");
- targets.Add (enemy);
- }
- private void SortTargetByDistance()
- {
- targets.Sort (delegate(Transform t1, Transform t2)
- { return Vector3.Distance (t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position));
- });
- }
- private void TargetEnemy()
- {
- if(selectedTarget == null)
- {
- SortTargetByDistance();
- 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
- }
- else
- {
- int index = targets.IndexOf (selectedTarget);
- if(index < targets.Count - 1)
- {
- index++;
- }
- else
- {
- index = 0;
- }
- DeselectTarget();
- selectedTarget = targets[index];
- }
- SelectTarget();
- }
- private void SelectTarget()
- {
- Transform name = selectedTarget.FindChild ("Name");
- if(name == null)
- {
- Debug.LogError("Could not find the Name on " + selectedTarget.name);
- return;
- }
- name.GetComponent<TextMesh> ().text = selectedTarget.GetComponent<Mob>().Name;
- name.GetComponent<MeshRenderer> ().enabled = true;
- selectedTarget.GetComponent<Mob> ().DisplayHealth();
- Messenger.Broadcast<bool> ("show mob vitalbars", true);
- }
- private void DeselectTarget()
- {
- selectedTarget.FindChild("Name").GetComponent<MeshRenderer>().enabled = false;
- selectedTarget = null;
- Messenger.Broadcast<bool> ("show mob vitalbars", false);
- }
- // Update is called once per frame
- void Update ()
- {
- if(Input.GetKeyDown (KeyCode.Tab))
- {
- TargetEnemy();
- }
- }
- public enum State
- {
- Idle,
- Initialize,
- Setup,
- SpawnMob
- }
- public GameObject[] mobPrefabs; // an array to hold all of the prefabs of mobs we want to spawn
- public GameObject[] spawnPoints; // an array will hold a reference to all of the spawn points in game
- public State state; // our local variable that holds our current state
- void Awake()
- {
- state = MobGenerator.State.Initialize;
- }
- // USE THIS FOR INITIALIZATION
- IEnumerator Start()
- {
- while(true)
- {
- switch(state)
- {
- case State.Initialize:
- Initialize();
- break;
- case State.Setup:
- Setup();
- break;
- case State.SpawnMob:
- SpawnMob();
- break;
- }
- yield return 0;
- }
- }
- // make sure that eeverything is initialized before we go on to the next step
- private void Initialize()
- {
- Debug.Log("*** We are in the initialize function ***");
- if (!CheckForMobPrefabs())
- return;
- if (!CheckForSpawnPoints ())
- return;
- state = MobGenerator.State.Setup;
- }
- // make sure that everything is setup before we continue
- private void Setup()
- {
- Debug.Log("*** We are in the setup function ***");
- state = MobGenerator.State.SpawnMob;
- }
- // spawn a mob if we have an open spawn point
- private void SpawnMob()
- {
- Debug.Log ("*** Spawn Mob ***");
- GameObject[] gos = AvailableSpawnPoints ();
- for (int cnt = 0; cnt < gos.Length; cnt++)
- {
- GameObject go = Instantiate (mobPrefabs [Random.Range (0, mobPrefabs.Length)],
- gos [cnt].transform.position,
- Quaternion.identity) as GameObject;
- go.transform.parent = gos [cnt].transform;
- Debug.Log ("there are " + go.transform.childCount.ToString () + " children");
- state = MobGenerator.State.Idle;
- }
- }
- // check to see that we have at least one mob prefab to spawn
- private bool CheckForMobPrefabs()
- {
- if (mobPrefabs.Length > 0)
- return true;
- else
- return false;
- }
- // check to see if we have at least one spawnpoint to spawn mobs at
- private bool CheckForSpawnPoints()
- {
- if (spawnPoints.Length > 0)
- return true;
- else
- return false;
- }
- // generate a list of available spawn points that do not have any mobs childed to it
- private GameObject[] AvailableSpawnPoints()
- {
- List<GameObject> gos = new List<GameObject> ();
- for(int cnt = 0; cnt < spawnPoints.Length; cnt++)
- {
- if(spawnPoints[cnt].transform.childCount == 0)
- {
- Debug.Log ("*** Spawn point available ***");
- gos.Add (spawnPoints[cnt]);
- }
- }
- return gos.ToArray ();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement