123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- using UnityEngine;
- using System.Collections;
- using UnityEngine.UI;
- using DG.Tweening;
- public class StarTarget : MonoBehaviour
- {
- public SoundEvent starReached;
- public Image _spriteBack;
- Image _sprite;
-
- int _scoreToReach;
- bool _isReached;
- bool _alreadyPlayedSoundForReachingStar;
- Vector3 _originalScale;
- const string STAR_FULL = "StarFull";
- const string STAR_EMPTY = "StarEmpty";
- void Awake()
- {
- _sprite = GetComponent<Image>();
- _originalScale = transform.localScale;
- }
- void OnEnable()
- {
- NotificationCenter.AddListener(OnScoreChanged, NotificationType.UpdateScoreLabel);
- transform.localScale = _originalScale;
- _isReached = false;
- _alreadyPlayedSoundForReachingStar = false;
- }
- void OnDisable()
- {
- NotificationCenter.RemoveListener(OnScoreChanged, NotificationType.UpdateScoreLabel);
- }
- public void SetPosition(int scoreToReach, int maxScore, float maxWidth)
- {
- _scoreToReach = scoreToReach;
- Vector3 pos = transform.localPosition;
- pos.x = maxWidth * (scoreToReach/(float)maxScore) - maxWidth/2;
- transform.localPosition = pos;
- if(_spriteBack!= null)
- {
- _spriteBack.transform.localPosition = pos;
- }
- }
- void OnScoreChanged(Notification note)
- {
- int score = GameDataManager.Instance.Score;
- if (!_isReached &&
- score >= _scoreToReach)
- {
- _isReached = true;
- AnimateReachingStar();
- } else
- if (_isReached &&
- score < _scoreToReach)
- {
- _isReached = false;
- AnimateLoosingStar();
- }
- }
- void AnimateReachingStar()
- {
- //So as to avoid lots of replaying the same sound, due to negative marking when shooting
- if (!_alreadyPlayedSoundForReachingStar)
- {
- _alreadyPlayedSoundForReachingStar = true;
- SoundManager.Play(starReached);
- }
- _sprite.StopAllCoroutines();
- //_sprite.color = new Color(_sprite.color.r, _sprite.color.g, _sprite.color.b, 0); ;
- //_sprite.DOFade(0, 0f);
- //_sprite.DOFade(1, 0.5f);
-
- _sprite.transform.DOScale(Vector3.one,0.3f);
- }
- void AnimateLoosingStar()
- {
- this.StopAllCoroutines();
- _sprite.SetNativeSize();
- }
- }
|