Question by
falcko · Oct 25, 2015 at 04:54 PM ·
playerprefsenergy
Energy buildup system
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 ();
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612042644im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how do i store highscore locally C# (SIMPLE) 3 Answers
Saving item count with Playerprefs 1 Answer
How do I get a serialized field saved into a PlayerPref? 0 Answers
SetActive dosen't work 0 Answers
Mute Toggle Not Staying On 2 Answers