LoadingBackgroundSpermsManager.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using UnityEngine;
  2. using System.Collections;
  3. /// <summary>
  4. /// This component is responsible for spawning the loading background sperms.
  5. /// They are spawned according to the <code>spawnRatePerSecond</code>.
  6. /// To add an element of randomness we also have <code>timeVariancePercentage</code>
  7. /// which slightly randomizes the <code>_averageTimePerSpawn</code>.
  8. /// The sperms are spawned betwee <code>minY</code> and <code>maxY</code>
  9. /// </summary>
  10. public class LoadingBackgroundSpermsManager : MonoBehaviour
  11. {
  12. public PoolObject backgroundSpermPrefab;
  13. public Transform anchor;
  14. public float spawnRatePerSecond = 1.5f;
  15. public float timeVariancePercentage = 0.15f;
  16. public float minY, maxY; //should be between -0.5 and 0.5f;
  17. public const float MIN_SCALE = 0.15f, MAX_SCALE = 0.5f;
  18. float _averageTimePerSpawn;
  19. float _nextSpawnTime;
  20. Pool _pool;
  21. void Awake()
  22. {
  23. _averageTimePerSpawn = 1/spawnRatePerSecond;
  24. _pool = Pool.Create(backgroundSpermPrefab, 50);
  25. Spawn();
  26. }
  27. void Update()
  28. {
  29. if (Time.time >= _nextSpawnTime)
  30. {
  31. Spawn();
  32. }
  33. }
  34. void Spawn()
  35. {
  36. Transform sperm = _pool.Spawn().transform;
  37. sperm.parent = anchor;
  38. sperm.localScale = Vector3.one * Random.Range(MIN_SCALE, MAX_SCALE);
  39. sperm.localPosition = new Vector3(-400, Random.Range(minY * GameConstants.GAME_HEIGHT, maxY * GameConstants.GAME_HEIGHT));
  40. _nextSpawnTime = Time.time + _averageTimePerSpawn + Random.Range(-_averageTimePerSpawn*timeVariancePercentage, _averageTimePerSpawn*timeVariancePercentage);
  41. }
  42. }