using System.Collections.Generic; using CleverCrow.Fluid.BTs.Trees; using UnityEngine; namespace CleverCrow.Fluid.BTs.Tasks { public abstract class TaskBase : GenericTaskBase, ITask { private bool _init; private bool _start; private bool _exit; private int _lastTickCount; private bool _active; public string Name { get; set; } public bool Enabled { get; set; } = true; public GameObject Owner { get; set; } public IBehaviorTree ParentTree { get; set; } public List Children { get; } = null; public TaskStatus LastStatus { get; private set; } public override TaskStatus Update () { base.Update(); UpdateTicks(); if (!_init) { Init(); _init = true; } if (!_start) { Start(); _start = true; _exit = true; } var status = GetUpdate(); LastStatus = status; if (status != TaskStatus.Continue) { if (_active) ParentTree?.RemoveActiveTask(this); Exit(); } else if (!_active) { ParentTree?.AddActiveTask(this); _active = true; } return status; } private void UpdateTicks () { if (ParentTree == null) { return; } if (_lastTickCount != ParentTree.TickCount) { Reset(); } _lastTickCount = ParentTree.TickCount; } /// /// Reset the node to be re-used /// public void Reset () { _active = false; _start = false; _exit = false; } public void End () { Exit(); } protected virtual TaskStatus GetUpdate () { return TaskStatus.Failure; } private void Init () { OnInit(); } /// /// Triggers the first time this node is run or after a hard reset /// protected virtual void OnInit () { } private void Start () { OnStart(); } /// /// Run every time this node begins /// protected virtual void OnStart () { } private void Exit () { if (_exit) { OnExit(); } Reset(); } /// /// Triggered when this node is complete /// protected virtual void OnExit () { } } }