BackgroundManager.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System;
  4. /// <summary>
  5. /// This component is responsible for creating a seamless background
  6. /// </summary>
  7. public class BackgroundManager : MonoBehaviour
  8. {
  9. public static BackgroundManager Instance;
  10. public Transform backgroundPrefab;
  11. public Texture[] backgroundTextures;
  12. public Transform anchor;
  13. public float speed = -100;
  14. public bool shuffle = false;
  15. const int NUM_OF_BACKGROUNDS = 3;
  16. float _backgroundWidth;
  17. List<Transform> _backgrounds;
  18. int _nextTextureIndex;
  19. float _originalSpeed;
  20. public bool _isPlaying;
  21. void Awake()
  22. {
  23. Instance = this;
  24. _originalSpeed = speed;
  25. NotificationCenter.AddListener(GameStart, NotificationType.GameStart);
  26. NotificationCenter.AddListener(OnChangeScrollingSpeed, NotificationType.ChangeScrollingSpeed);
  27. NotificationCenter.AddListener(OnApplySkinBackground, NotificationType.ApplySkinBackgrounds);
  28. NotificationCenter.AddListener(OnEndLife, NotificationType.EndLife);
  29. NotificationCenter.AddListener(OnGetLife, NotificationType.GetLife);
  30. }
  31. private void OnGetLife(Notification note)
  32. {
  33. //_isPlaying = true;
  34. }
  35. private void GameStart(Notification note)
  36. {
  37. _isPlaying = true;
  38. }
  39. void OnDestroy()
  40. {
  41. NotificationCenter.RemoveListener(GameStart, NotificationType.GameStart);
  42. NotificationCenter.RemoveListener(OnChangeScrollingSpeed, NotificationType.ChangeScrollingSpeed);
  43. NotificationCenter.RemoveListener(OnApplySkinBackground, NotificationType.ApplySkinBackgrounds);
  44. NotificationCenter.RemoveListener(OnEndLife, NotificationType.EndLife);
  45. NotificationCenter.RemoveListener(OnGetLife, NotificationType.GetLife);
  46. }
  47. private void OnEndLife(Notification note)
  48. {
  49. _isPlaying = false;
  50. }
  51. void Start()
  52. {
  53. _backgrounds = new List<Transform>(NUM_OF_BACKGROUNDS);
  54. //We need to calculate the background width, taking into consideration the scale change done to fix the aspect ratio problem (See AspectRatioFixer)
  55. //This will make sure that the whole background will be visible on any device
  56. _backgroundWidth = backgroundPrefab.transform.localScale.x * AspectRatioFixer.ScaleChange;
  57. int nextZ = (int)backgroundPrefab.localPosition.z;
  58. for (int i = NUM_OF_BACKGROUNDS-1; i >= 0; i--)
  59. {
  60. Transform background = (Transform)Instantiate(backgroundPrefab);
  61. UITexture uiTexture = background.GetComponentInChildren<UITexture>();
  62. //make a clone of the material, otherwise the background instances would be pointing to the same material
  63. Material materialClone = new Material(uiTexture.material);
  64. uiTexture.material = materialClone;
  65. background.parent = anchor;
  66. background.localScale = new Vector3(_backgroundWidth, _backgroundWidth, background.localScale.z);
  67. background.localPosition = new Vector3(_backgroundWidth * (i-1), 0, nextZ++); //-1 so we have one on the left, one in the middle and one on the right
  68. _backgrounds.Add(background);
  69. SetNextTextureForBackground(background);
  70. }
  71. }
  72. void Update()
  73. {
  74. if (!_isPlaying)
  75. {
  76. return;
  77. }
  78. bool shouldUpdateZPositions = false;
  79. foreach (Transform b in _backgrounds)
  80. {
  81. //move background
  82. b.localPosition += new Vector3(speed*Time.deltaTime, 0, 0);
  83. //check if needs to be wrapped
  84. if (b.localPosition.x - _backgroundWidth/2 > GameConstants.GAME_WIDTH/2)
  85. {
  86. b.localPosition -= new Vector3(_backgroundWidth * NUM_OF_BACKGROUNDS, 0);
  87. shouldUpdateZPositions = true;
  88. SetNextTextureForBackground(b);
  89. }
  90. }
  91. if (shouldUpdateZPositions)
  92. {
  93. UpdateZPositions();
  94. }
  95. }
  96. void OnChangeScrollingSpeed(Notification note)
  97. {
  98. float scrollingSpeedFactor = (float)note.data;
  99. speed = _originalSpeed * scrollingSpeedFactor;
  100. }
  101. /// <summary>
  102. /// Updates the Z positions of the backgrounds, so that the first background on the right will be closer to the camera.
  103. /// The prefabs are made in such a way so that there is a bit of clamping bleeding on the right of the plane.
  104. /// This helps avoid flickering of the edges by overlapping the planes. The order of rendering is enforced through
  105. /// setting the Z positions accordingly. Without this fix of using overlapping planes, the flickering would occur due to
  106. /// moving the backgrounds at non-pixel perfect updates per frame.
  107. /// </summary>
  108. void UpdateZPositions()
  109. {
  110. Transform pushedToBack = _backgrounds[0];
  111. _backgrounds.RemoveAt(0);
  112. _backgrounds.Add(pushedToBack);
  113. int nextZ = (int)backgroundPrefab.localPosition.z;
  114. for (int i = _backgrounds.Count-1; i >= 0; i--)
  115. {
  116. Transform b = _backgrounds[i];
  117. Vector3 newPos = b.localPosition;
  118. newPos.z = nextZ--;
  119. b.localPosition = newPos;
  120. }
  121. }
  122. void SetNextTextureForBackground(Transform background)
  123. {
  124. //choose a random texture
  125. UITexture uiTexture = background.GetComponentInChildren<UITexture>();
  126. if (shuffle)
  127. {
  128. _nextTextureIndex = UnityEngine.Random.Range(0, backgroundTextures.Length);
  129. } else
  130. {
  131. --_nextTextureIndex;
  132. if (_nextTextureIndex < 0)
  133. {
  134. _nextTextureIndex = backgroundTextures.Length-1;
  135. }
  136. }
  137. uiTexture.mainTexture = backgroundTextures[_nextTextureIndex];
  138. }
  139. void OnApplySkinBackground(Notification note)
  140. {
  141. Texture2D[] textures = (Texture2D[])note.data;
  142. backgroundTextures = textures;
  143. foreach (Transform background in _backgrounds)
  144. {
  145. SetNextTextureForBackground(background);
  146. }
  147. }
  148. }