Energy recovery system with time in Unity.

Hi, In this video i will show You how to make energy time recovery system in unity.

CODE:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class UpdateText : MonoBehaviour
{
    public TextMeshProUGUI valueTXT;
    public void UpdateTextValue(float _value)
    {
        valueTXT.text = _value.ToString();
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;


public class TimeManager : MonoBehaviour
{
    public int maxEnergy = 5;
    public int energy;
    public float timeToRecoverEnergy;

    DateTime energySpendDateTime; //time/date when energy was spend
    float timePassed;

    UpdateText updateTXT;
    
    // Start is called before the first frame update
    void Start()
    {
        updateTXT = FindObjectOfType<UpdateText>();
        StartCoroutine(EnergyRecoveryCoroutine());
        
        LoadDate();
        updateTXT.UpdateTextValue(energy);
    }
    private void Update()
    {
        Debug.Log(energy);
    }

    public void SpendEnergy()
    {
        if (energy < maxEnergy)
        {
            if (energy <= 0)
            {
                Debug.Log("energy less than 0");
                energy = 0;
            }
            if (energy > 0)
            {
                energy--;
                //TODO: PLAY GAME
            }
            
        }
        if(energy == maxEnergy)
        {
            energy--;
            energySpendDateTime = System.DateTime.Now;
            //TODO: PLAY GAME
        }
        SaveData();
    }

    public void RecoverEnergyWithTime()
    {
        timePassed = (float)(System.DateTime.Now - energySpendDateTime).TotalSeconds;

        if (timePassed >= timeToRecoverEnergy)
        {
            energy++;
            energySpendDateTime = System.DateTime.Now;

            if (energy >= maxEnergy)
            {
                energy = maxEnergy;
            }
        }
    }

    IEnumerator EnergyRecoveryCoroutine()
    {
        while (true)
        {
            RecoverEnergyWithTime();
            updateTXT.UpdateTextValue(energy);
            yield return new WaitForSeconds(1);
        }
        
    }

    public void RecoverEnergyOnGameLoad()
    {
        timePassed = (float)(System.DateTime.Now - energySpendDateTime).TotalSeconds;

        if (timePassed >= timeToRecoverEnergy)
        {
            energy += Mathf.RoundToInt( timePassed / timeToRecoverEnergy);

            if (energy >= maxEnergy)
            {
                energy = maxEnergy;
            }
        }
        energySpendDateTime = System.DateTime.Now;
        
    }

    public void SaveData()
    {
        PlayerPrefs.SetInt("energy", energy);
        PlayerPrefs.SetString("energySpendDateTime", energySpendDateTime.ToString());
    }

    public void LoadDate()
    {
        if (PlayerPrefs.GetString("energySpendDateTime") == null)
        {
            energy = maxEnergy;
        }
        else if (PlayerPrefs.GetString("energySpendDateTime") != null)
        {
            energySpendDateTime =Convert.ToDateTime(PlayerPrefs.GetString("energySpendDateTime"));
            energy = PlayerPrefs.GetInt("energy");
            RecoverEnergyOnGameLoad();
        }
        updateTXT.UpdateTextValue(energy);

    }

    private void OnApplicationQuit()
    {
        SaveData();
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *