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

public class Method
{
    // ================== Properties =========================
    /// <summary>
    ///     Used for debugging and identification purposes
    /// </summary>
    public string Name { get; }
    public List<Condition> Preconditions { get; } = new();
    public List<ITask> SubTasks { get; } = new();

    // ================== Constructor =========================
    public Method(string name)
    {
        Name = name;
    }

    /// <summary>
    ///     Adds a condition
    /// </summary>
    // ================== Conditions =========================
    public Method AddPrecondition(Condition condition)
    {
        Preconditions.Add(condition);
        return this;
    }

    /// <summary>
    ///     Adds a subtask
    /// </summary>
    // ================== Add Sub-Tasks =========================
    public Method AddSubTask(ITask task)
    {
        SubTasks.Add(task);
        return this;
    }

    /// <summary>
    ///     Checks if all the preconditions are met based on the Worldstate
    /// </summary>
    // ================== Conditions =========================
    public bool ArePreconditionsMet(HTNPlanner planner)
    {
        foreach (Condition condition in Preconditions) 
        {
            //If a condition is false 
            if (!condition.IsConditionMet(planner))
            {
                Debug.Log($"The Precondition {condition.Name} is not met!");
                return false;
            }
        }

        return true;
    }
}