ScoreManager.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using UnityEngine;
  2. using System.Collections;
  3. using TMPro;
  4. public class ScoreManager : MonoBehaviour
  5. {
  6. public TextMeshProUGUI label;
  7. public Transform bonusesAnchor;
  8. public PoolObject hitPointsBonusPrefab;
  9. Pool _hitPointsBonusPool;
  10. void Awake()
  11. {
  12. _hitPointsBonusPool = Pool.Get(hitPointsBonusPrefab);
  13. NotificationCenter.AddListener(OnGameStart, NotificationType.GameStart);
  14. NotificationCenter.AddListener(OnAddToScore, NotificationType.AddToScore);
  15. NotificationCenter.AddListener(OnUpdateScoreLabel, NotificationType.UpdateScoreLabel);
  16. }
  17. void OnDestroy()
  18. {
  19. NotificationCenter.RemoveListener(OnGameStart, NotificationType.GameStart);
  20. NotificationCenter.RemoveListener(OnAddToScore, NotificationType.AddToScore);
  21. NotificationCenter.RemoveListener(OnUpdateScoreLabel, NotificationType.UpdateScoreLabel);
  22. }
  23. void OnGameStart(Notification note)
  24. {
  25. UpdateLabel();
  26. }
  27. void OnAddToScore(Notification note)
  28. {
  29. ScoreChangeData data = (ScoreChangeData)note.data;
  30. if (data.source == ScoreChangeData.Source.Enemy)
  31. {
  32. if(_hitPointsBonusPool!= null)
  33. {
  34. TextAnnouncement hitPointsBonus = _hitPointsBonusPool.Spawn().GetComponent<TextAnnouncement>();
  35. Transform hitPointsBonusTransform = hitPointsBonus.transform;
  36. hitPointsBonusTransform.parent = bonusesAnchor;
  37. hitPointsBonus.SetText(data.scoreDelta.ToString());
  38. hitPointsBonusTransform.position = data.position;
  39. //reset the local z
  40. Vector3 localPos = hitPointsBonusTransform.localPosition;
  41. localPos.z = 0;
  42. hitPointsBonusTransform.localPosition = localPos;
  43. }
  44. }
  45. }
  46. void OnUpdateScoreLabel(Notification note)
  47. {
  48. UpdateLabel();
  49. }
  50. void UpdateLabel()
  51. {
  52. label.text = GameDataManager.Instance.Score.ToString("00000");
  53. }
  54. }