}//Update public ObjectPool SpawnByChance() { List <MeteorPool> qualifiedToSpawn = new List <MeteorPool>(); ObjectPool spawnCandidate = null; float highestChance = 0f; foreach (MeteorPool pool in allPools) { EMeteor meteorCmp = pool.GetMeteorCmp(); if (meteorCmp == null || meteorCmp.Chances == null) { #if UNITY_EDITOR Debug.LogWarning("MeteorSpawn failed to get meteor component or meteor chances!!" + "\n Cmp: " + meteorCmp + "; Chances: " + meteorCmp.Chances); #endif meteorCmp.Start(); } float randVal = Random.Range(0, 100); qualifiedToSpawn.Add(pool); if (randVal < highestChance) { continue; } highestChance = randVal; spawnCandidate = pool; }//foreach if (spawnCandidate == null) { if (qualifiedToSpawn.Count > 0) { spawnCandidate = qualifiedToSpawn[Random.Range(0, qualifiedToSpawn.Count - 1)]; } else //Paranoia! This should never happened! //spawnCandidate = _meteorPools[0]; // Pick first pool to be "safe" to spawn at least something. { #if UNITY_EDITOR Debug.LogWarning("MeteorSpawn couldnt pick pool to spawn from!!"); #endif } } //StartCoroutine(Spawn(spawnCandidate)); return(spawnCandidate); } //SpawnByChance
/// <summary> /// Spawn an enemy object inside the bounds of the Collider that is /// attached to This gameobject. /// </summary> /// <param name="toSpawn"> Override Wave's pool pick with user's prefered pool. </param> public virtual IEnumerator Spawn(ObjectPool toSpawn = null, int numberOfSpawns = -1) { toSpawn = (toSpawn == null) ? ActiveWave.PickRandomPool() : toSpawn; numberOfSpawns = (numberOfSpawns == -1) ? ActiveWave.ObjectsPerSpawn : numberOfSpawns; for (int i = 0; i < numberOfSpawns; i++) { var spawned = toSpawn.SpawnAt(GetRandomPosition(_boxCollider.bounds)); if(spawned == null) { //happenes when there are no more available objects in to spawn in the pool continue; } EMeteor meteor = spawned.GetComponent<EMeteor>(); if (meteor) { meteor.SetDirection(GetPositionToMoveTowards()); } float waitTime = Random.Range(2, 6) / 10.0f; if (numberOfSpawns == 1) //Don't waste cicles on 1 object per spawn. waitTime = 0.0f; yield return new WaitForSeconds(waitTime); }//for }//Spawn
public override void Start() { base.Start(); _meteor = GetComponent <EMeteor>(); }//Start