Newer
Older
Hierarchical-Task-Network-Unity-3D / Assets / Scripts / Example / PaymentArea.cs
using System.Collections;
using System.Collections.Generic;
using UnityEditor.Search;
using UnityEngine;

public class PaymentArea : WorldInteractableObject
{
    [Header("Queue Settings")]
    [SerializeField] private string targetTag = "Owner";
    [SerializeField] private Transform firstQueuePosition;
    [SerializeField] private Vector3 queueDirection = new Vector3(0f, 0f, -1f); // queue goes backward
    [SerializeField] private float spacing = 1.5f;
    [SerializeField] private int maxQueueLength = 5;

    private Worldstate worldState;
    [Header("Runtime State")]
    [SerializeField] private List<Customer> customerQueue = new List<Customer>();
    [SerializeField] private bool isPaymentAvailable = false;

    public Transform FirstQueuePosition => firstQueuePosition;



    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        worldState = Worldstate.Instance;
    }

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

    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag(targetTag))
        {
            isPaymentAvailable = true;
            worldState.Set("PaymentAvailable", isPaymentAvailable);
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.CompareTag(targetTag))
        {
            isPaymentAvailable = false;
            worldState.Set("PaymentAvailable", isPaymentAvailable);
        }
    }

    public bool FindCustomer(Customer customer) => customerQueue.Find(p => p == customer);

    public void RequestJoinQueue(Customer customer)
    {
        if (!customerQueue.Contains(customer))
        {
            customerQueue.Add(customer);
            UpdateQueuePositions();
        }
    }

    public void LeaveQueue(Customer customer)
    {
        if (customerQueue.Remove(customer))
        {
            UpdateQueuePositions();
        }
    }

    public bool IsFirstInQueue(Customer customer)
    {
        float distance = Vector3.Distance(customer.transform.position, firstQueuePosition.transform.position);
        //Debug.LogError($"Customer Queue Number: {customerQueue.Count} \n Is this customer the first position of the queue: {customerQueue[0] == customer} \n Is Payment Available: {isPaymentAvailable}");
        return customerQueue.Count > 0 && customerQueue[0] == customer && isPaymentAvailable && distance <= 0.6f;
    }

    private void UpdateQueuePositions()
    {
        for (int i = 0; i < customerQueue.Count; i++)
        {
            //Creates a target position for the customer based on the customer queue size
            Vector3 offset = queueDirection.normalized * spacing * i;
            Vector3 targetPosition = firstQueuePosition.position + offset;

            //Queue takes possession of the Agent
            customerQueue[i].NavMeshAgent.SetDestination(targetPosition);
        }
    }



    public void Pay(float amount) => ShopManager.Instance.AddRevenue(amount);
}