Newer
Older
Hierarchical-Task-Network-Unity-3D / Assets / Scripts / HTN / Worldstate.cs
using System;
using System.Collections.Generic;
using UnityEngine;

public class Worldstate : MonoBehaviour
{
    public Dictionary<string, object> Facts = new();
    private static Worldstate instance;

    // ================== WORLD KNOWLEDGE ==================
    [Header("Shop Sections")]
    [SerializeField] private List<SectionHandler> sections;
    [SerializeField] private PaymentArea paymentArea;

    [Header("Products: ")]
    [SerializeField] private List<Product> products = new List<Product>();

    [Header("Shop Exit")]
    [SerializeField] private Transform shopExit;

    // ================== PROPERTIES ==================
    public static Worldstate Instance => instance;
    public List<Product> Products => products;

    /// <summary>
    /// Get's a Worldstate fact
    /// </summary>
    /// <param name="key">The name of the Fact</param>
    /// <returns>The Fact if it exists</returns>
    public T Get<T>(string key)
    {
        return Facts.ContainsKey(key) ? (T)Facts[key] : default;
    }

    /// <summary>
    /// Set's a Worldstate fact
    /// </summary>
    /// <param name="key">The name of the Fact</param>
    /// <param name="value">The value of the Fact</param>
    public void Set(string key, object value)
    {
        Facts[key] = value;
    }

    /// <summary>
    /// Check's if a fact exists
    /// </summary>
    /// <param name="key">The name of the Fact</param>
    /// <param name="value">The value of the Fact</param>
    /// <returns>If the Fact exist returns true, otherwise returns false</returns>
    public bool Has(string key, object value)
    {
        return Facts.ContainsKey(key) && Facts[key].Equals(value);
    }

    /// <summary>
    /// Check's if a fact exists
    /// </summary>
    /// <param name="key">The name of the Fact</param>
    /// <param name="value">The value of the Fact</param>
    /// <returns>If the Fact exist returns true and the value, otherwise returns false</returns>
    public bool Has(string key, out object value)
    {
        if (Facts.ContainsKey(key))
        {
            value = Facts[key];
            return true;
        }

        value = null;
        return false;
    }

    // ================== BUILT IN METHODS ==================
    void Awake()
    {
        if (instance == null)
        {
            instance = this;
        }

    }

    private void Start()
    {
        //Add world knowledge
        foreach (SectionHandler handler in sections)
        {
            Set(handler.SectionName, handler);
            Debug.Log($"Section {handler.name} was added to the world state");
        }


        Set("ExitArea", shopExit);
        Set("PaymentArea", paymentArea);

        foreach (SectionHandler handler in sections)
        {
            products.Add(handler.Product);
        }
        products.Sort();
    }


    public void OnDebug()
    {
        foreach (string keys in Facts.Keys) { 
            Debug.Log("Name: " + keys);
        }
    }
}