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(); _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(); } }