Felix Boucher b54627196c population mechanic with art
- added art for house and UI
- put population in ResourceManager
- create house prefab
- added code for adding and removing pop depending on entity
- refactor harvesters so they inherit from ally
- modify placeholders and buttons so that only units cost population
- add events for population and resources changing
- add population relative configs to global config
- add start resources values to Levels
- add debug feature for generating resources for free
2023-10-29 19:12:32 -04:00

77 lines
2.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using GatherAndDefend.Events;
using UnityEngine.UI;
/// <summary>
/// Gère l'affichage des resources
/// </summary>
public class ResourceText : MonoBehaviour
{
private Color _populationCriticalColor = Color.red;
private Color _populationWarningColor = Color.yellow;
private Color _populationRegularColor = Color.white;
[SerializeField]
private TextMeshProUGUI _rockText;
[SerializeField]
private TextMeshProUGUI _woodText;
[SerializeField]
private TextMeshProUGUI _foodText;
[SerializeField]
private TextMeshProUGUI _populationText;
[SerializeField]
private Image _populationCriticalIndicator;
private ResourceManager _resourceManager;
void Start()
{
_resourceManager = ResourceManager.Instance;
_populationCriticalIndicator.enabled = false;
}
void Update()
{
RefreshResources();
RefreshPopulation();
}
void RefreshResources()
{
_rockText.text = _resourceManager.RockAmount.ToString();
_woodText.text = _resourceManager.WoodAmount.ToString();
_foodText.text = _resourceManager.FoodAmount.ToString();
}
void RefreshPopulation()
{
float current = _resourceManager.CurrentPopulation;
float max = _resourceManager.MaximumPopulation;
_populationCriticalIndicator.enabled = current >= max;
var chosenColor = _populationRegularColor;
if (current >= max)
{
chosenColor = _populationCriticalColor;
_populationText.fontStyle = FontStyles.Bold;
}
else if (((float)current / (float)max) > GlobalConfig.Instance.Current.populationWarningPercentage)
{
chosenColor = _populationWarningColor;
_populationText.fontStyle = FontStyles.Bold;
}
else
{
_populationText.fontStyle = 0;
}
var hexColor = $"#{ToHexString(chosenColor.r)}{ToHexString(chosenColor.g)}{ToHexString(chosenColor.b)}";
_populationText.text = $"<color={hexColor}>{_resourceManager.CurrentPopulation}</color> / {_resourceManager.MaximumPopulation}";
}
string ToHexString(float colorValue)
{
return ((int)(255 * colorValue)).ToString("X2");
}
}