12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- using UnityEngine;
- using System.Collections;
- /// <summary>
- /// This component is responsible for spawning the loading background sperms.
- /// They are spawned according to the <code>spawnRatePerSecond</code>.
- /// To add an element of randomness we also have <code>timeVariancePercentage</code>
- /// which slightly randomizes the <code>_averageTimePerSpawn</code>.
- /// The sperms are spawned betwee <code>minY</code> and <code>maxY</code>
- /// </summary>
- public class LoadingBackgroundSpermsManager : MonoBehaviour
- {
- public PoolObject backgroundSpermPrefab;
- public Transform anchor;
- public float spawnRatePerSecond = 1.5f;
- public float timeVariancePercentage = 0.15f;
- public float minY, maxY; //should be between -0.5 and 0.5f;
- public const float MIN_SCALE = 0.15f, MAX_SCALE = 0.5f;
- float _averageTimePerSpawn;
- float _nextSpawnTime;
- Pool _pool;
- void Awake()
- {
- _averageTimePerSpawn = 1/spawnRatePerSecond;
- _pool = Pool.Create(backgroundSpermPrefab, 50);
- Spawn();
- }
- void Update()
- {
- if (Time.time >= _nextSpawnTime)
- {
- Spawn();
- }
- }
- void Spawn()
- {
- Transform sperm = _pool.Spawn().transform;
- sperm.parent = anchor;
- sperm.localScale = Vector3.one * Random.Range(MIN_SCALE, MAX_SCALE);
- sperm.localPosition = new Vector3(-400, Random.Range(minY * GameConstants.GAME_HEIGHT, maxY * GameConstants.GAME_HEIGHT));
- _nextSpawnTime = Time.time + _averageTimePerSpawn + Random.Range(-_averageTimePerSpawn*timeVariancePercentage, _averageTimePerSpawn*timeVariancePercentage);
- }
- }
|