using UnityEngine; using System.Collections; using System; public class StartCounter : MonoBehaviour { int _count; UILabel _label; const int INITIAL_DELAY_TICKS = 1; const float TIME_PER_NUMBER = 0.5f; const int START_NUMBER = 3; int _nextAnimationTick; public bool HasPlayerGetLife = false; //Due to the processing time to create the sperms, the coming sound is not synched with 3-2-1 go sound //So we introduced this bool so that we play it once the FixedUpdates for start counter start rolling out. public bool HasPlayedStartSound = true; private int _lastRectTickCalculation; Vector3 originalPos; void Awake() { _label = GetComponentInChildren(); originalPos = transform.localPosition; LevelFromDiskGameEntitySpawnManager.Instance.StartCounter = this; NotificationCenter.AddListener(OnCelebrityDisappeared, NotificationType.CelebrityDisappeared); NotificationCenter.AddListener(OnGetLife, NotificationType.GetLife); NotificationCenter.AddListener(OnGameOver, NotificationType.GameOver); } private void OnGameOver(Notification note) { HasPlayerGetLife = false; HasPlayedStartSound = true; } void OnDestroy() { NotificationCenter.RemoveListener(OnCelebrityDisappeared, NotificationType.CelebrityDisappeared); NotificationCenter.RemoveListener(OnGetLife, NotificationType.GetLife); } private void OnGetLife(Notification note) { if (!MenuManager._instance.mainMenuScreen.isActiveAndEnabled) { HasPlayerGetLife = true; _label.enabled = true; _count = 4; iTween.Stop(gameObject); //just in case transform.localPosition = originalPos; _lastRectTickCalculation = RecordingManager.Ticks + 1; //Debug.Log("OnCelebrityDisappeared " + HasPlayedStartSound ); HasPlayedStartSound = false; } } void OnEnable() { _label.enabled = false; } void OnCelebrityDisappeared(Notification note) { _label.enabled = true; _count = START_NUMBER; iTween.Stop(gameObject); //just in case transform.localPosition = originalPos; _nextAnimationTick = RecordingManager.Ticks+1; //Debug.Log("OnCelebrityDisappeared " + HasPlayedStartSound ); HasPlayedStartSound = false; } void FixedUpdate() { if (!HasPlayedStartSound) { HasPlayedStartSound = true; SoundManager.Play(SoundEvent.coming); } if (HasPlayerGetLife) { if (RecordingManager.Ticks == _lastRectTickCalculation) { if (_count > 0) { UpdateLabel(); transform.localScale = Vector3.one * 4; iTween.ScaleTo(gameObject, iTween.Hash( "usefixedupdate", true, "scale", Vector3.one, //"delay", _count == START_NUMBER ? INITIAL_DELAY : 0, "time", TIME_PER_NUMBER, "onupdate", "OniTweenUpdate", "onupdatetarget", gameObject, "oncomplete", "OniTweenScaleComplete", "oncompletetarget", gameObject)); --_count; } else { GameUpdateManager.Instance._isPlaying = true; BackgroundManager.Instance._isPlaying = true; //SceneryGenerator.Instance._isPlaying = true; HasPlayerGetLife = false; iTween.MoveTo(gameObject, iTween.Hash( "islocal", true, "x", GameConstants.GAME_WIDTH, "time", 0.5f, "easetype", iTween.EaseType.easeInBack, "onupdate", "OniTweenUpdate", "onupdatetarget", gameObject)); } } } else { if (RecordingManager.Ticks == _nextAnimationTick) { if (_count > 0) { UpdateLabel(); transform.localScale = Vector3.one * 4; iTween.ScaleTo(gameObject, iTween.Hash( "usefixedupdate", true, "scale", Vector3.one, //"delay", _count == START_NUMBER ? INITIAL_DELAY : 0, "time", TIME_PER_NUMBER, "onupdate", "OniTweenUpdate", "onupdatetarget", gameObject, "oncomplete", "OniTweenScaleComplete", "oncompletetarget", gameObject)); --_count; } else { NotificationCenter.Post(NotificationType.StartSpawningEnemies); iTween.MoveTo(gameObject, iTween.Hash( "islocal", true, "x", GameConstants.GAME_WIDTH, "time", 0.5f, "easetype", iTween.EaseType.easeInBack, "onupdate", "OniTweenUpdate", "onupdatetarget", gameObject)); } } } } //void UpdateTimerOnAddLife() //{ // if (!HasPlayedStartSound) // { // HasPlayedStartSound = true; // SoundManager.Play(SoundEvent.coming); // } // if (RecordingManager.Ticks == _nextAnimationTick) // { // if (_count > 0) // { // UpdateLabel(); // transform.localScale = Vector3.one * 4; // iTween.ScaleTo(gameObject, iTween.Hash( // "usefixedupdate", true, // "scale", Vector3.one, // //"delay", _count == START_NUMBER ? INITIAL_DELAY : 0, // "time", TIME_PER_NUMBER, // "onupdate", "OniTweenUpdate", // "onupdatetarget", gameObject, // "oncomplete", "OniTweenScaleComplete", // "oncompletetarget", gameObject)); // --_count; // } // else // { // NotificationCenter.Post(NotificationType.GetLife); // iTween.MoveTo(gameObject, iTween.Hash( // "islocal", true, // "x", GameConstants.GAME_WIDTH, // "time", 0.5f, // "easetype", iTween.EaseType.easeInBack, // "onupdate", "OniTweenUpdate", // "onupdatetarget", gameObject)); // } // } //} void UpdateLabel() { if (_count > 0) { _label.text = _count.ToString(); } /*else if (_count == 0) { _label.text = "GO!"; }*/ } void OniTweenUpdate() { _label.MarkAsChangedLite(); } void OniTweenScaleComplete() { _nextAnimationTick = RecordingManager.Ticks+1; _lastRectTickCalculation = RecordingManager.Ticks + 1; } }