Quantcast
Channel: Questions in topic: "playerprefs"
Viewing all articles
Browse latest Browse all 1333

Energy buildup system

$
0
0
Hi! I'm creating a new 2d game and every time a player starts/reloads a level the game takes down 1 energy point for the collection a energy points (max. is 20 points). Every 10 minutes the player should be getting an extra energy point until the maximum of 20 is reached. I have this sort of working with the use of playerPrefs but I'm not sure this is the right way of doing it. How would you guys get energy in the game? Cheers! Here is my code so far: using UnityEngine; using System.Collections; using UnityEngine.UI; using System; public class EnergyManager : MonoBehaviour { public Text energyLevelText; // Use this for initialization void Start () { //check if prefs are set if (!PlayerPrefs.HasKey ("maxEnergy")) { PlayerPrefs.SetInt("maxEnergy",20); } if (!PlayerPrefs.HasKey ("lastTimeUsed")) { PlayerPrefs.SetString("lastTimeUsed", System.DateTime.Now.ToBinary().ToString()); } if (!PlayerPrefs.HasKey ("energyLevel")) { PlayerPrefs.SetInt("energyLevel", 20); } InvokeRepeating ("RecalculateEnergy", 0.0F, 10.0F); } public void RecalculateEnergy() { //Store the current time when it starts DateTime currentDate = System.DateTime.Now; //Grab the old time from the player prefs as a long long temp = Convert.ToInt64(PlayerPrefs.GetString("lastTimeUsed")); //Convert the old time from binary to a DataTime variable DateTime oldDate = DateTime.FromBinary(temp); //Use the Subtract method and store the result as a timespan variable TimeSpan difference = currentDate.Subtract(oldDate); Debug.Log ("Difference " + difference); Debug.Log ( (difference.TotalMinutes / 10) - (difference.TotalMinutes % 10)); int energyToAdd = (int)(difference.TotalMinutes / 10) - (int)(difference.TotalMinutes % 10); //Mathf.Round( if (energyToAdd > 0) { AddEnergy(1); } energyLevelText.text = GetEnergy ().ToString (); } public int GetEnergy() { //RecalculateEnergy (); return PlayerPrefs.GetInt ("energyLevel"); } public void UseEnergy() { int energyLevel = GetEnergy (); if (energyLevel > 0) { //Store the current time when it starts DateTime currentDate = System.DateTime.Now; //Grab the old time from the player prefs as a long long temp = Convert.ToInt64(PlayerPrefs.GetString("lastTimeUsed")); //Convert the old time from binary to a DataTime variable DateTime oldDate = DateTime.FromBinary(temp); Debug.Log("oldDate: " + oldDate); //Use the Subtract method and store the result as a timespan variable TimeSpan difference = currentDate.Subtract(oldDate); Debug.Log("Difference: " + difference.TotalMinutes); if (difference.Minutes>=10) { PlayerPrefs.SetString("lastTimeUsed", System.DateTime.Now.ToBinary().ToString()); } energyLevel--; PlayerPrefs.SetInt("energyLevel", energyLevel); energyLevelText.text = GetEnergy ().ToString (); } Debug.Log ("Energy used : energylevel " + energyLevel + " last time : " + PlayerPrefs.GetString("lastTimeUsed")); RecalculateEnergy (); } public void AddEnergy(int energyToAdd) { int newEnergy = GetEnergy() + energyToAdd; if (newEnergy > PlayerPrefs.GetInt("maxEnergy")) { newEnergy = PlayerPrefs.GetInt("maxEnergy"); } PlayerPrefs.SetInt("energyLevel", newEnergy); PlayerPrefs.SetString("lastTimeUsed", System.DateTime.Now.ToBinary().ToString()); RecalculateEnergy (); } }

Viewing all articles
Browse latest Browse all 1333

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>