Newer
Older
Hierarchical-Task-Network-Unity-3D / Assets / Scripts / ShopKeeper / ShopManager.cs
using UnityEngine;
using System.Collections.Generic;
using UnityEditorInternal.Profiling.Memory.Experimental;

public class ShopManager : MonoBehaviour
{
    //Variables
    private static ShopManager instance;
    [SerializeField] private int maxCostumers;
    [SerializeField] private int currentCostumers;

    [Header("World Objects")]
    [SerializeField] private Customer customerPrefab;
    [SerializeField] private Transform instanciateArea; //The area where the customers should be placed

    [SerializeField] private List<Product> products;
    [SerializeField] private static Dictionary<int, float> catalog = new Dictionary<int, float>();

    //Shop Finance
    [SerializeField] private float shopRevenue;
    /*Add more details based on the income
     Maybe calculate other things lile*/

    public delegate void OnRevenueChanged(float value);
    public event OnRevenueChanged onPaymentReceived;

    //Properties
    public static ShopManager Instance => instance;
    public static Dictionary<int, float> Catalog => catalog;


    private void Awake()
    {
        if (instance == null) instance = this;
    }

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        InitializeItems();
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    //Instanciates a customer GameObject
    private void AddCostumer()
    {
        if (currentCostumers < maxCostumers)
        {
            //Instanciate the Agent
            currentCostumers++;
        }
    }


    //After a sold product the revenue increases
    public void AddRevenue(float value)
    {
        shopRevenue += value;
        onPaymentReceived?.Invoke(shopRevenue);
    }

    //A customer might steal products of the shop
    public void RemoveRevenue(float value) => shopRevenue -= value;


    //Puts the products in a dictionary
    private void InitializeItems()
    {
        foreach (Product product in products)
        {
            if (!catalog.ContainsKey(product.ID)) catalog.Add(product.ID, product.ProductPrice);
        }
    }
}