In this course we just check if enough time has passed and refill all of our points in one go.
I’m trying to set it up so that energy will start refilling once energy != maxEnergy and will add 1 every so often based on the timer.
Sounds like a simple fix right? (maybe it is but boy has it been giving me heck)
We’re just changing it to add 1 energy after the duration right? but…
In this lesson we’re only updating the energy and max energy when we press play and when the game is loaded.
I promise I put a lot of time into trying to get this to work but it’s not going well and I’m not sure seeing the code will help you help me but here you go… (yes I know I got rid of the string ref keys because of all the recoding I was doing. Just trying to get it to work and I’ll fix later lol)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;
using System;
public class MainMenu : MonoBehaviour
{
[SerializeField] private TMP_Text highScoreText;
[SerializeField] private TMP_Text energyText;
[SerializeField] private int maxEnergy = 3;
[SerializeField] private int energyRechargeDur = 10;
private DateTime nextEnergyTime;
private DateTime lastEnergyTime;
private bool isRestoring = false;
[SerializeField] private int energy;
public void Start()
{
int highScore = PlayerPrefs.GetInt(ScoreSystem.HighScoreKey, 0);
UpdateEnergy();
energyText.text = energy.ToString() + "/" + maxEnergy.ToString();
if (!PlayerPrefs.HasKey("currentEnergy"))
{
PlayerPrefs.SetInt("currentEnergy", maxEnergy);
Load();
StartCoroutine(RestoreEnergy());
}
Load();
}
private void Update()
{
if (energy < maxEnergy)
{
StartCoroutine(RestoreEnergy());
}
}
public void UseEnergy()
{
if (energy >= 1)
{
energy--;
UpdateEnergy();
if (isRestoring == false)
{
if (energy + 1 == maxEnergy)
{
nextEnergyTime = AddDuration(DateTime.Now, energyRechargeDur);
}
StartCoroutine(RestoreEnergy());
}
}
else
{
Debug.Log("Insufficient Energy!!");
}
}
private void Play()
{
UseEnergy();
SceneManager.LoadScene(1);
}
private IEnumerator RestoreEnergy()
{
UpdateEnergyTimer();
isRestoring = true;
while (energy <= maxEnergy)
{
DateTime currentDateTime = DateTime.Now;
DateTime nextDateTime = nextEnergyTime;
bool isEnergyAdding = false;
while (currentDateTime > nextDateTime)
{
if (energy < maxEnergy)
{
isEnergyAdding = true;
energy++;
UpdateEnergy();
DateTime timeToAdd = lastEnergyTime > nextDateTime ? lastEnergyTime : nextDateTime;
nextDateTime = AddDuration(timeToAdd, energyRechargeDur);
}
else
{
yield return null;
}
}
if (isEnergyAdding == true)
{
lastEnergyTime = DateTime.Now;
nextEnergyTime = nextDateTime;
}
UpdateEnergyTimer();
UpdateEnergy();
Save();
}
isRestoring = false;
yield return null;
}
private DateTime AddDuration(DateTime datetime, int energyRechargeDur)
{
return datetime.AddSeconds(energyRechargeDur);
}
private void UpdateEnergyTimer()
{
if (energy >= maxEnergy)
{
energy = maxEnergy;
return;
}
TimeSpan time = nextEnergyTime - DateTime.Now;
string timeValue = String.Format("{0:D2:{1:D1}", time.Minutes, time.Seconds);
return;
}
private void UpdateEnergy()
{
Save();
energyText.text = (energy.ToString() + "/" + maxEnergy.ToString());
return;
}
private DateTime StringToDate(string datetime)
{
if (String.IsNullOrEmpty(datetime))
{
return DateTime.Now;
}
else
{
return DateTime.Parse(datetime);
}
}
private void Load()
{
energy = PlayerPrefs.GetInt("currentEnergy");
nextEnergyTime = StringToDate(PlayerPrefs.GetString("nextEnergyTime"));
lastEnergyTime = StringToDate(PlayerPrefs.GetString("lastEnergyTime"));
}
private void Save()
{
PlayerPrefs.SetInt("energy", energy);
PlayerPrefs.SetString("nextEnergyTime", nextEnergyTime.ToString());
PlayerPrefs.SetString("lastEnergyTime", lastEnergyTime.ToString());
}
}