gather-and-defend/Assets/Scripts/ResourceManager.cs
Ader Alisma 01 e03abf24ec Avancement sur coroutine de génération de ressources.
Changé la coroutine de ResourceMaker vers ResourceManager.

Changé ResourceMaker pour permettre d'augmenter ou de réduire la quantité de resources produites.
2023-05-21 19:18:35 -04:00

152 lines
3.3 KiB
C#

using UnityEngine;
using System.Collections;
/// <summary>
/// Sert d'inventaire et gère l'accès aux ressources
/// </summary>
public class ResourceManager : MonoBehaviour
{
private static ResourceManager _instance = null;
private int _rockAmount = 0;
private int _woodAmount = 0;
private int _foodAmount = 0;
private int _rockGenerationAmount = 0;
private int _woodGenerationAmount = 0;
private int _foodGenerationAmount = 0;
private bool _generatingResources = true;
public enum AddOrRemove
{
Add,
Remove
};
private const int MAX = 100;
private const int MIN = 0;
public ResourceManager() { }
public static ResourceManager Instance
{
get
{
return _instance;
}
}
private void Awake()
{
if (_instance != null && _instance != this)
{
Destroy(this);
}
else
{
_instance = this;
StartCoroutine(Generate());
}
}
public int RockAmount
{
set
{
if (_rockAmount + value < MAX)
{
_rockAmount += value;
}
}
get { return _rockAmount; }
}
public int WoodAmount
{
set
{
if (_woodAmount + value < MAX)
{
_woodAmount += value;
}
}
get { return _woodAmount; }
}
public int FoodAmount
{
set
{
if (_foodAmount + value < MAX)
{
_foodAmount += value;
}
}
get { return _foodAmount; }
}
public void RockGenerationAmount(int amount, AddOrRemove choice)
{
if (choice == AddOrRemove.Add)
{
_rockAmount += amount;
}
else
{
_rockAmount -= amount;
}
}
public void WoodGenerationAmount(int amount, AddOrRemove choice)
{
if (choice == AddOrRemove.Add)
{
_woodAmount += amount;
}
else
{
_woodAmount -= amount;
}
}
public void FoodGenerationAmount(int amount, AddOrRemove choice)
{
if( choice == AddOrRemove.Add)
{
_foodAmount += amount;
}
else
{
_foodAmount -= amount;
}
}
public void Remove(int rock, int wood, int food)
{
_rockAmount = _rockAmount - rock < MIN ? MIN : _rockAmount - rock;
_rockAmount = _rockAmount - wood < MIN ? MIN : _rockAmount - wood;
_foodAmount = _foodAmount - food < MIN ? MIN : _foodAmount - food;
}
public bool EnoughFor(int rock, int wood, int food = 0)
{
if (rock > _rockAmount || wood > _woodAmount || food > _foodAmount)
{
return false;
}
return true;
}
private IEnumerator Generate()
{
while (_generatingResources)
{
_rockAmount = _rockGenerationAmount;
_woodAmount = _woodGenerationAmount;
_foodAmount = _foodGenerationAmount;
Debug.Log("Generating...");
yield return new WaitForSeconds(3f);
}
}
private void OnDestroy()
{
_generatingResources = false;
StopCoroutine(Generate());
}
}