123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- using System;
- using System.Collections;
- using UnityEngine;
- public class LifeManager
- {
- public static void AdShowing()
- {
- _lifes += 1;
- }
- public static int Lifes
- {
- get { return _lifes; }
- }
- private static int _lifes;
- public static void Reset()
- {
- _lifes = 2;
- }
- public static void BottleCap()
- {
- _lifes += 1;
- }
- public static void Consume()
- {
- _lifes--;
- }
- public static void AddlifeInviteFriends()
- {
- _lifes += 1;
- }
- //public const int MINITES_TO_NEXT_LIFE = 10;
- /* public const int MAX_LIFES = 2;
- private static LifeManager _instance;
- public static LifeManager Instance {
- get
- {
- if (!_instance)
- {
- var go = new GameObject("_lifeManager");
- _instance = go.AddComponent<LifeManager>();
- DontDestroyOnLoad(go);
- }
- return _instance;
- }
- }
- public int Lives
- {
- get { return PlayerPrefs.GetInt("Lives", MAX_LIFES); }
- set
- {
- if (Lives == value)
- {
- return;
- }
- PlayerPrefs.SetInt("Lives", value);
- PlayerPrefs.Save();
- }
- }
- public bool Consume()
- {
- if (Lives <= 0)
- {
- return false;
- }
- Lives--;
- return true;
- }
- public void BottleCap()
- {
- Lives ++;
- if (Lives >= MAX_LIFES)
- {
- NextLifeAppear = DateTime.MinValue;
- }
- }
- public string TimeToNextLife
- {
- get
- {
- if (Lives >= MAX_LIFES)
- {
- return "full";
- }
- if (NextLifeAppear == DateTime.MinValue)
- {
- return "10:00";
- }
- var delta = NextLifeAppear - DateTime.Now;
- return string.Format("{0}:{1}",delta.Minutes.ToString("00"),delta.Seconds.ToString("00"));
- }
- }
- private DateTime NextLifeAppear;
- // Use this for initialization
- void Start ()
- {
- //FacebookAccess.init();
- if (_instance && _instance != this)
- {
- Destroy(gameObject);
- return;
- }
- _instance = this;
- Lives = PlayerPrefs.GetInt("Lives", MAX_LIFES);
- if (Lives < MAX_LIFES)
- {
- var nexLife = long.Parse(PlayerPrefs.GetString("NextLife", "0"));
- if (nexLife == 0)
- {
- NextLifeAppear = DateTime.Now + TimeSpan.FromMinutes(MINITES_TO_NEXT_LIFE);
- }
- else
- {
- NextLifeAppear = DateTime.FromBinary(nexLife);
- while (NextLifeAppear < DateTime.Now)
- {
- Lives ++;
- NextLifeAppear += TimeSpan.FromMinutes(MINITES_TO_NEXT_LIFE);
- if (Lives >= MAX_LIFES)
- {
- NextLifeAppear = DateTime.MinValue;
- break;
- }
- }
- }
- }
- else
- {
- NextLifeAppear = DateTime.MinValue;
- }
- StartCoroutine(GetLife());
- DontDestroyOnLoad(gameObject);
- }
- private IEnumerator GetLife()
- {
- while (transform)
- {
- yield return new WaitForSeconds(1f);
- if (Lives < MAX_LIFES)
- {
- if (NextLifeAppear == DateTime.MinValue)
- {
- NextLifeAppear = DateTime.Now + TimeSpan.FromMinutes(MINITES_TO_NEXT_LIFE);
- }
- if (NextLifeAppear < DateTime.Now)
- {
- Lives ++;
- NextLifeAppear = DateTime.Now + TimeSpan.FromMinutes(MINITES_TO_NEXT_LIFE);
- }
- }
- }
- }
- private void OnDestroy()
- {
- PlayerPrefs.SetInt("Lives", Lives);
- PlayerPrefs.SetString("NextLife", NextLifeAppear.ToBinary().ToString());
- PlayerPrefs.Save();
- }*/
- }
|