Advertisement
Guest User

PseudoPlanner

a guest
Apr 28th, 2025
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.39 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Unity.Entities.UniversalDelegates;
  5. using Unity.VisualScripting;
  6. using UnityEngine;
  7.  
  8. public class PseudoPlanner : MonoBehaviour
  9. {
  10.  
  11.     public string PlanName;
  12.     [SerializeField] int index;
  13.     public BotMotor botMotor; // the bot's state machine that controls the actions
  14.     public AIFlags flags; // the flags that will be checked in the plan's monitor functions
  15.     public PseudoPlannerBasePlan curPlan;
  16.     PseudoPlannerBasePlan defaultPlan = new PlanIdle();
  17.  
  18.     void Start()
  19.     {
  20.         botMotor = GetComponent<BotMotor>(); // Assumes there's a State Machine component attached
  21.  
  22.         //Setting up the entry plan
  23.         curPlan = defaultPlan;
  24.         curPlan.planner = this;
  25.         curPlan.Enter();
  26.         PlanName = curPlan.ToString();
  27.         index = curPlan.index;
  28.     }
  29.  
  30.     //Monitoring the advance/retreat/break out conditions
  31.     void FixedUpdate()
  32.     {
  33.         curPlan.Run();
  34.         curPlan.MonitorAdvance();
  35.         curPlan.MonitorBreak();
  36.  
  37.         index = curPlan.index;
  38.     }
  39.  
  40.  
  41.     //External function to switch the plans
  42.     public void SwitchPlan(PseudoPlannerBasePlan plan)
  43.     {
  44.         curPlan.Exit();
  45.         ResetDelays();
  46.        
  47.         curPlan = plan;
  48.         curPlan.planner = this;
  49.         curPlan.Enter();
  50.        
  51.         PlanName = curPlan.ToString();
  52.     }
  53.  
  54.     private List<Coroutine> delayCoroutines = new List<Coroutine>();
  55.     private Dictionary<Action, Coroutine> activeDelays = new Dictionary<Action, Coroutine>();
  56.  
  57.     public void Delay(float time, Action callback)
  58.     {
  59.         if (activeDelays.ContainsKey(callback))
  60.             return;
  61.  
  62.         var delayCoroutine = StartCoroutine(DelayCoroutine(time, callback));
  63.         delayCoroutines.Add(delayCoroutine);
  64.         activeDelays[callback] = delayCoroutine;
  65.     }
  66.  
  67.     IEnumerator DelayCoroutine(float time, Action callback)
  68.     {
  69.         yield return new WaitForSeconds(time);
  70.         callback?.Invoke();
  71.  
  72.         if (activeDelays.ContainsKey(callback))
  73.         {
  74.             delayCoroutines.Remove(activeDelays[callback]);
  75.             activeDelays.Remove(callback);
  76.         }
  77.     }
  78.  
  79.     public void ResetDelays()
  80.     {
  81.         foreach (var coroutine in delayCoroutines)
  82.             StopCoroutine(coroutine);
  83.  
  84.         delayCoroutines.Clear();
  85.         activeDelays.Clear();
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement