LifeManager.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. public class LifeManager
  5. {
  6. public static void AdShowing()
  7. {
  8. _lifes += 1;
  9. }
  10. public static int Lifes
  11. {
  12. get { return _lifes; }
  13. }
  14. private static int _lifes;
  15. public static void Reset()
  16. {
  17. _lifes = 2;
  18. }
  19. public static void BottleCap()
  20. {
  21. _lifes += 1;
  22. }
  23. public static void Consume()
  24. {
  25. _lifes--;
  26. }
  27. public static void AddlifeInviteFriends()
  28. {
  29. _lifes += 1;
  30. }
  31. //public const int MINITES_TO_NEXT_LIFE = 10;
  32. /* public const int MAX_LIFES = 2;
  33. private static LifeManager _instance;
  34. public static LifeManager Instance {
  35. get
  36. {
  37. if (!_instance)
  38. {
  39. var go = new GameObject("_lifeManager");
  40. _instance = go.AddComponent<LifeManager>();
  41. DontDestroyOnLoad(go);
  42. }
  43. return _instance;
  44. }
  45. }
  46. public int Lives
  47. {
  48. get { return PlayerPrefs.GetInt("Lives", MAX_LIFES); }
  49. set
  50. {
  51. if (Lives == value)
  52. {
  53. return;
  54. }
  55. PlayerPrefs.SetInt("Lives", value);
  56. PlayerPrefs.Save();
  57. }
  58. }
  59. public bool Consume()
  60. {
  61. if (Lives <= 0)
  62. {
  63. return false;
  64. }
  65. Lives--;
  66. return true;
  67. }
  68. public void BottleCap()
  69. {
  70. Lives ++;
  71. if (Lives >= MAX_LIFES)
  72. {
  73. NextLifeAppear = DateTime.MinValue;
  74. }
  75. }
  76. public string TimeToNextLife
  77. {
  78. get
  79. {
  80. if (Lives >= MAX_LIFES)
  81. {
  82. return "full";
  83. }
  84. if (NextLifeAppear == DateTime.MinValue)
  85. {
  86. return "10:00";
  87. }
  88. var delta = NextLifeAppear - DateTime.Now;
  89. return string.Format("{0}:{1}",delta.Minutes.ToString("00"),delta.Seconds.ToString("00"));
  90. }
  91. }
  92. private DateTime NextLifeAppear;
  93. // Use this for initialization
  94. void Start ()
  95. {
  96. //FacebookAccess.init();
  97. if (_instance && _instance != this)
  98. {
  99. Destroy(gameObject);
  100. return;
  101. }
  102. _instance = this;
  103. Lives = PlayerPrefs.GetInt("Lives", MAX_LIFES);
  104. if (Lives < MAX_LIFES)
  105. {
  106. var nexLife = long.Parse(PlayerPrefs.GetString("NextLife", "0"));
  107. if (nexLife == 0)
  108. {
  109. NextLifeAppear = DateTime.Now + TimeSpan.FromMinutes(MINITES_TO_NEXT_LIFE);
  110. }
  111. else
  112. {
  113. NextLifeAppear = DateTime.FromBinary(nexLife);
  114. while (NextLifeAppear < DateTime.Now)
  115. {
  116. Lives ++;
  117. NextLifeAppear += TimeSpan.FromMinutes(MINITES_TO_NEXT_LIFE);
  118. if (Lives >= MAX_LIFES)
  119. {
  120. NextLifeAppear = DateTime.MinValue;
  121. break;
  122. }
  123. }
  124. }
  125. }
  126. else
  127. {
  128. NextLifeAppear = DateTime.MinValue;
  129. }
  130. StartCoroutine(GetLife());
  131. DontDestroyOnLoad(gameObject);
  132. }
  133. private IEnumerator GetLife()
  134. {
  135. while (transform)
  136. {
  137. yield return new WaitForSeconds(1f);
  138. if (Lives < MAX_LIFES)
  139. {
  140. if (NextLifeAppear == DateTime.MinValue)
  141. {
  142. NextLifeAppear = DateTime.Now + TimeSpan.FromMinutes(MINITES_TO_NEXT_LIFE);
  143. }
  144. if (NextLifeAppear < DateTime.Now)
  145. {
  146. Lives ++;
  147. NextLifeAppear = DateTime.Now + TimeSpan.FromMinutes(MINITES_TO_NEXT_LIFE);
  148. }
  149. }
  150. }
  151. }
  152. private void OnDestroy()
  153. {
  154. PlayerPrefs.SetInt("Lives", Lives);
  155. PlayerPrefs.SetString("NextLife", NextLifeAppear.ToBinary().ToString());
  156. PlayerPrefs.Save();
  157. }*/
  158. }