Newer
Older
Hierarchical-Task-Network-Unity-3D / Assets / Scripts / Example / PrimitiveTasks / ProceedPayment.cs
using UnityEngine;

public class ProceedPayment : PrimitiveTask
{
    // ================== VARIABLES ==================

    private float paymentValue;

    // ================== CONSTRUCTOR ==================
    public ProceedPayment(float paymentValue) => this.paymentValue = paymentValue;

    // ================== TASK ACTION ==================
    public override void Execute(HTNPlanner planner)
    {
        //We get the world state
        Worldstate worldState = planner.WorldState;

        if (worldState != null)
        {
            if (worldState.Has("PaymentArea", out object value)) //Checks for the payment are 
            {
                //if the value exists, we cast it
                PaymentArea area = value as PaymentArea;

                //if cast is successful
                if (area != null) {
                    area.Pay(planner.Customer.Basket.CurrentValue); //Proceeds to do the payment
                    hasRun = true;
                    return;
                }

                //else throw error
                Debug.LogError($"{nameof(PaymentArea)} is NULL. Maybe wrong casting!");
            }

        }

        else
        {
            Debug.LogError("The WorldState is Null!");
        }
    }

    // ================== BUILDER METHOD ==================
    // Optional convenience method
    public static PrimitiveTaskBuilder Create(float paymentValue) => PrimitiveTaskBuilder.For(new ProceedPayment(paymentValue));
}