Newer
Older
Hierarchical-Task-Network-Unity-3D / Assets / Scripts / ShopKeeper / Customer / Customer.cs
using NUnit.Framework;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.AI;

public class Customer : Agent
{
    //Variables
    private int customerId;
    private Worldstate worldState;
    [SerializeField] private int maxProductsType; //The max amount of products
    [SerializeField] private int maxProductQuantity = 10; //The max amount product quantity

    public Dictionary<int, int> wishList = new Dictionary<int, int>();
    [SerializeField] private Basket basket = new Basket();

    [Header("Debugging")]
    [SerializeField] List<string> wishListDebug = new();

    //Properties
    public int CustomerID => customerId;
    public Basket Basket => basket;
    public bool IsWishListEmpty => wishList.Count == 0;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        worldState = Worldstate.Instance;
        Debug.LogError("NUMBER OF PRODUCTS: " + worldState.Products.Count);
        maxProductsType = worldState.Products.Count;
        CreateWishList(); //Generates a wishlist
        navMeshAgent.obstacleAvoidanceType = ObstacleAvoidanceType.NoObstacleAvoidance;
    }

    public void Update()
    {
        //Debug.LogError("Wish List Items: " + wishList.Count);
    }

    //Creates a random index for the products and for the amount
    private void CreateWishList()
    {
        // Get the catalog of available products
        List<Product> catalog = new List<Product>(worldState.Products);

        // Randomize how many product types the customer will want
        int max_products = Random.Range(1, maxProductsType + 1); // inclusive max

        if (wishList.Count == 0)
        {
            // Shuffle the catalog to randomize product selection
            catalog = catalog.OrderBy(_ => Random.value).ToList();

            for (int i = 0; i < Mathf.Min(max_products, catalog.Count); i++)
            {
                Product product = catalog[i];
                int quantity = Random.Range(1, maxProductQuantity + 1);

                wishList[product.ID] = quantity;
                wishListDebug.Add(product.ItemName);
            }
        }
    }

    public void PrintWishlist()
    {
        foreach (var kvp in wishList)
        {
            Debug.Log($"The Dictionary has {kvp.Key} with the amount of {kvp.Value}");
        }
    }
}