/// <summary> /// Spawns birds at the start of the game /// </summary> public void SpawnBird() { if (crowSpawnMin > currentCrowAmt && currentBirdAmt < birdSpawnMax) { Vector3 temp = new Vector3(Random.Range(-20, 20) + playerPosition.x, 5 + playerPosition.y, Random.Range(-20, 20) + playerPosition.z); crow = BirdObjectPool.Instance.SpawnFromPool("crow"); crow.transform.localScale *= 3; crow.transform.position = temp; currentCrowAmt++; currentBirdAmt++; } if (robinSpawnMin > currentRobinAmt && currentBirdAmt < birdSpawnMax) { Vector3 temp = new Vector3(Random.Range(-20, 20) + playerPosition.x, 5 + playerPosition.y, Random.Range(-20, 20) + playerPosition.z); robin = BirdObjectPool.Instance.SpawnFromPool("robin"); robin.transform.localScale *= 3; robin.transform.position = temp; currentRobinAmt++; currentBirdAmt++; } if (sparrowSpawnMin > currentSparrowAmt && currentBirdAmt < birdSpawnMax) { Vector3 temp = new Vector3(Random.Range(-20, 20) + playerPosition.x, 5 + playerPosition.y, Random.Range(-20, 20) + playerPosition.z); sparrow = BirdObjectPool.Instance.SpawnFromPool("sparrow"); sparrow.transform.localScale *= 3; sparrow.transform.position = temp; currentSparrowAmt++; currentBirdAmt++; } }
/// <summary> /// chooses a random bird type to spawn after a delay /// </summary> public void ChanceToSpawnBird() { if (currentBirdAmt < birdSpawnMax) { int random = Random.Range(0, 3); switch (random) { case 0: { if (currentCrowAmt < BirdObjectPool.Instance.pools[random].size) { Vector3 temp = new Vector3(Random.Range(-10, 10), 35, Random.Range(-10, 10)); crow = BirdObjectPool.Instance.SpawnFromPool("crow"); crow.transform.position = temp; currentCrowAmt++; currentBirdAmt++; } break; } case 1: { if (currentRobinAmt < BirdObjectPool.Instance.pools[random].size) { Vector3 temp = new Vector3(Random.Range(-10, 10), 35, Random.Range(-10, 10)); robin = BirdObjectPool.Instance.SpawnFromPool("robin"); robin.transform.position = temp; currentRobinAmt++; currentBirdAmt++; } break; } case 2: { if (currentSparrowAmt < BirdObjectPool.Instance.pools[random].size) { Vector3 temp = new Vector3(Random.Range(-10, 10), 35, Random.Range(-10, 10)); sparrow = BirdObjectPool.Instance.SpawnFromPool("sparrow"); sparrow.transform.position = temp; currentSparrowAmt++; currentBirdAmt++; } break; } default: break; } } }
/// <summary> /// pulls out the first element in the queue, sets it to active then add it to the end of the queue /// </summary> /// <param name="tag">tag found in objectpool</param> /// <returns></returns> public BirdClass SpawnFromPool(string tag) { if (!poolDictionary.ContainsKey(tag)) { Debug.Log("Pool with tag does not exist"); } BirdClass objectToSpawn = poolDictionary[tag].Dequeue(); objectToSpawn.gameObject.SetActive(true); poolDictionary[tag].Enqueue(objectToSpawn); return(objectToSpawn); }
void Start() { poolDictionary = new Dictionary <string, Queue <BirdClass> >(); foreach (Pool pool in pools) { objectPool = new Queue <BirdClass>(); for (int i = 0; i < pool.size; ++i) { BirdClass obj = Instantiate(pool.Prefab); obj.gameObject.SetActive(false); objectPool.Enqueue(obj);//!< add it to the end of the queue */ } poolDictionary.Add(pool.tag, objectPool); } }