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