show player energy, change daytimecontroller to TimeManager

This commit is contained in:
Vladimir Koshevarov
2022-11-17 16:12:51 +02:00
parent 6b8d74932c
commit be3161d7e4
15 changed files with 859 additions and 223 deletions
+21 -22
View File
@@ -1,22 +1,21 @@
using Assets.Scripts.Actions.Interfaces;
namespace Assets.Scripts.Actions
{
public class Eat : BaseAction, ISellableItem
{
public double Price { get; private set; }
private int _energyPerTick;
public Eat(int duration, int energyPerTick, double price) : base(duration)
{
Price = price;
_energyPerTick = energyPerTick;
}
public override void ApplyAction(PlayerController playerController)
{
playerController.foodEnergy.increase(_energyPerTick);
}
}
}
using Assets.Scripts.Actions.Interfaces;
namespace Assets.Scripts.Actions
{
public class Eat : BaseAction, ISellableItem
{
public float Price { get; private set; }
private int _energyPerTick;
public Eat(int duration, int energyPerTick, float price) : base(duration)
{
Price = price;
_energyPerTick = energyPerTick;
}
public override void ApplyAction(PlayerController playerController)
{
playerController.foodEnergy.increase(_energyPerTick);
}
}
}
@@ -3,6 +3,6 @@ namespace Assets.Scripts.Actions.Interfaces
{
internal interface ISellableItem
{
public double Price { get; }
public float Price { get; }
}
}
+15 -15
View File
@@ -1,15 +1,15 @@
namespace Assets.Scripts.Actions
{
public class Relax : BaseAction
{
private int _energyPerTick;
public Relax(int duration, int energyPerTick) : base(duration)
{
_energyPerTick = energyPerTick;
}
public override void ApplyAction(PlayerController playerController)
{
playerController.restEnergy.increase(_energyPerTick);
}
}
}
namespace Assets.Scripts.Actions
{
public class Relax : BaseAction
{
private int _energyPerTick;
public Relax(int duration, int energyPerTick) : base(duration)
{
_energyPerTick = energyPerTick;
}
public override void ApplyAction(PlayerController playerController)
{
playerController.energy.increase(_energyPerTick);
}
}
}
+14 -6
View File
@@ -1,11 +1,9 @@
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class GameUIController : MonoBehaviour
{
[SerializeField]
private DateTimeController _dateTimeController;
[SerializeField]
private PlayerController _playerController;
@@ -15,23 +13,33 @@ public class GameUIController : MonoBehaviour
[SerializeField]
private TextMeshProUGUI _moneyText;
[SerializeField]
public Slider _energy;
// Start is called before the first frame update
void Start()
private void OnEnable()
{
TimeManager.OnMinuteChanged += UpdateTime;
}
private void OnDisable()
{
TimeManager.OnMinuteChanged -= UpdateTime;
}
// Update is called once per frame
void Update()
{
_moneyText.text = $"${_playerController.money.Value}";
UpdateTime();
_energy.value = _playerController.energy.Value;
}
private void UpdateTime()
{
if (_timeText != null)
{
_timeText.text = _dateTimeController.CurrentTime.ToString("HH:mm");
_timeText.text = TimeManager.CurrentTime.ToString(@"hh\:mm");
}
}
}
+12 -10
View File
@@ -4,10 +4,10 @@ using UnityEngine;
public class PlayerController : MonoBehaviour
{
public Stat money = new Stat("Money", 100.00);
public Stat money = new Stat("Money", 100.0f);
public Stat rentAccount = new Stat("Rent Account", 0);
public Stat foodEnergy = new Stat("Food Energy", 0);
public Stat restEnergy = new Stat("Rest Energy", 0);
public Stat energy = new Stat("Energy", 100);
// bank
public Stat bankAccount = new Stat("Bank Account", 0);
@@ -40,11 +40,15 @@ public class PlayerController : MonoBehaviour
public Stat tvItem = new Stat("Tv Item", 1500, 1);
public Stat laptopItem = new Stat("Laptop Item", 3000, 1);
private void OnEnable()
{
TimeManager.OnMinuteChanged += DecreaseEnergy;
}
private int _day;
public int Clock;
private void OnDisable()
{
TimeManager.OnMinuteChanged -= DecreaseEnergy;
}
// Start is called before the first frame update
void Start()
{
@@ -53,13 +57,11 @@ public class PlayerController : MonoBehaviour
// Update is called once per frame
void Update()
{
}
public void StartNewDay()
public void DecreaseEnergy()
{
Clock = 24;
energy.deduct(0.096f); // 24 hours it's 100, 100/1440=~0.096 per minute
}
public bool TryBuyAction(BaseAction action)
+41 -41
View File
@@ -1,41 +1,41 @@
public class Stat
{
public string Name { get; set; }
public double Value { get; set; }
public double Price { get; set; }
public double Quantity { get; set; }
public Stat(string name, double startValue)
{
Name = name;
Value = startValue;
}
public Stat(string name, double price, double quantity)
{
Name = name;
Price = price;
Quantity = quantity;
}
public void increase(double byAmount)
{
Value += byAmount;
}
public bool deduct(double amount)
{
if (Value >= amount)
{
Value -= amount;
return true;
}
return false;
}
public void forceDeduct(double amount)
{
Value -= amount;
}
}
public class Stat
{
public string Name { get; set; }
public float Value { get; set; }
public double Price { get; set; }
public double Quantity { get; set; }
public Stat(string name, float startValue)
{
Name = name;
Value = startValue;
}
public Stat(string name, float price, float quantity)
{
Name = name;
Price = price;
Quantity = quantity;
}
public void increase(float byAmount)
{
Value += byAmount;
}
public bool deduct(float amount)
{
if (Value >= amount)
{
Value -= amount;
return true;
}
return false;
}
public void forceDeduct(float amount)
{
Value -= amount;
}
}
@@ -1,106 +1,116 @@
using System;
using UnityEngine;
public class DateTimeController : MonoBehaviour
{
[SerializeField]
private float _timeMultiplier;
[SerializeField]
private float _startHour;
[SerializeField]
private Light _sunLight;
[SerializeField]
private float _sunriseHour;
[SerializeField]
private float _sunsetHour;
[SerializeField]
private Color _dayAmbientLight;
[SerializeField]
private Color _nightAmbientLight;
[SerializeField]
private AnimationCurve _lightChangeCurve;
[SerializeField]
private float _maxSunLightIntensity;
[SerializeField]
private Light _moonLight;
[SerializeField]
private float _maxMoonLightIntensity;
private TimeSpan _sunriseTime;
private TimeSpan _sunsetTime;
private DateTime _currentTime;
public DateTime CurrentTime => _currentTime;
// Start is called before the first frame update
void Start()
{
_currentTime = DateTime.Now.Date + TimeSpan.FromHours(_startHour);
_sunriseTime = TimeSpan.FromHours(_sunriseHour);
_sunsetTime = TimeSpan.FromHours(_sunsetHour);
}
// Update is called once per frame
void Update()
{
UpdateTime();
RotateSun();
}
private void UpdateTime()
{
_currentTime = _currentTime.AddSeconds(Time.deltaTime * _timeMultiplier);
}
private void RotateSun()
{
float sunLightRotation;
if (_currentTime.TimeOfDay > _sunriseTime && _currentTime.TimeOfDay < _sunsetTime)
{
TimeSpan sunriseToSunsetDuration = CalculateTimeDifference(_sunriseTime, _sunsetTime);
TimeSpan timeSinceSunrise = CalculateTimeDifference(_sunriseTime, _currentTime.TimeOfDay);
double percentage = timeSinceSunrise.TotalMinutes / sunriseToSunsetDuration.TotalMinutes;
sunLightRotation = Mathf.Lerp(0, 180, (float)percentage);
}
else
{
TimeSpan nightDuration = CalculateTimeDifference(_sunsetTime, _sunriseTime);
TimeSpan timeSinceSunset = CalculateTimeDifference(_sunsetTime, _currentTime.TimeOfDay);
double percentage = timeSinceSunset.TotalMinutes / nightDuration.TotalMinutes;
sunLightRotation = Mathf.Lerp(180, 360, (float)percentage);
}
_sunLight.transform.rotation = Quaternion.AngleAxis(sunLightRotation, Vector3.right);
}
private void UpdateLightSettings()
{
float dotProduct = Vector3.Dot(_sunLight.transform.forward, Vector3.down);
_sunLight.intensity = Mathf.Lerp(0, _maxSunLightIntensity, _lightChangeCurve.Evaluate(dotProduct));
_moonLight.intensity = Mathf.Lerp(_maxMoonLightIntensity, 0, _lightChangeCurve.Evaluate(dotProduct));
RenderSettings.ambientLight = Color.Lerp(_nightAmbientLight, _dayAmbientLight, _lightChangeCurve.Evaluate(dotProduct));
}
private TimeSpan CalculateTimeDifference(TimeSpan from, TimeSpan to)
{
TimeSpan diff = to - from;
if (diff.TotalSeconds < 0)
{
diff += TimeSpan.FromHours(24);
}
return diff;
}
}
using System;
using UnityEngine;
public class TimeManager : MonoBehaviour
{
public static Action OnMinuteChanged;
[SerializeField]
private float _startHour;
[SerializeField]
private Light _sunLight;
[SerializeField]
private float _sunriseHour;
[SerializeField]
private float _sunsetHour;
[SerializeField]
private Color _dayAmbientLight;
[SerializeField]
private Color _nightAmbientLight;
[SerializeField]
private AnimationCurve _lightChangeCurve;
[SerializeField]
private float _maxSunLightIntensity;
[SerializeField]
private Light _moonLight;
[SerializeField]
private float _maxMoonLightIntensity;
private TimeSpan _sunriseTime;
private TimeSpan _sunsetTime;
private float _timer;
[SerializeField]
private float _minuteToRealTime = 0.5f;
public static TimeSpan CurrentTime { get; private set; }
// Start is called before the first frame update
void Start()
{
_timer = _minuteToRealTime;
CurrentTime = TimeSpan.Zero + TimeSpan.FromHours(_startHour);
_sunriseTime = TimeSpan.FromHours(_sunriseHour);
_sunsetTime = TimeSpan.FromHours(_sunsetHour);
}
// Update is called once per frame
void Update()
{
UpdateTime();
//RotateSun();
}
private void UpdateTime()
{
_timer -= Time.deltaTime;
if (_timer <= 0)
{
CurrentTime = CurrentTime.Add(TimeSpan.FromMinutes(1));
OnMinuteChanged?.Invoke();
_timer = _minuteToRealTime;
}
}
//private void RotateSun()
//{
// float sunLightRotation;
// if (_currentTime.TimeOfDay > _sunriseTime && _currentTime.TimeOfDay < _sunsetTime)
// {
// TimeSpan sunriseToSunsetDuration = CalculateTimeDifference(_sunriseTime, _sunsetTime);
// TimeSpan timeSinceSunrise = CalculateTimeDifference(_sunriseTime, _currentTime.TimeOfDay);
// double percentage = timeSinceSunrise.TotalMinutes / sunriseToSunsetDuration.TotalMinutes;
// sunLightRotation = Mathf.Lerp(0, 180, (float)percentage);
// }
// else
// {
// TimeSpan nightDuration = CalculateTimeDifference(_sunsetTime, _sunriseTime);
// TimeSpan timeSinceSunset = CalculateTimeDifference(_sunsetTime, _currentTime.TimeOfDay);
// double percentage = timeSinceSunset.TotalMinutes / nightDuration.TotalMinutes;
// sunLightRotation = Mathf.Lerp(180, 360, (float)percentage);
// }
// _sunLight.transform.rotation = Quaternion.AngleAxis(sunLightRotation, Vector3.right);
//}
private void UpdateLightSettings()
{
float dotProduct = Vector3.Dot(_sunLight.transform.forward, Vector3.down);
_sunLight.intensity = Mathf.Lerp(0, _maxSunLightIntensity, _lightChangeCurve.Evaluate(dotProduct));
_moonLight.intensity = Mathf.Lerp(_maxMoonLightIntensity, 0, _lightChangeCurve.Evaluate(dotProduct));
RenderSettings.ambientLight = Color.Lerp(_nightAmbientLight, _dayAmbientLight, _lightChangeCurve.Evaluate(dotProduct));
}
private TimeSpan CalculateTimeDifference(TimeSpan from, TimeSpan to)
{
TimeSpan diff = to - from;
if (diff.TotalSeconds < 0)
{
diff += TimeSpan.FromHours(24);
}
return diff;
}
}