Advertisement
Pro_Unit

UtiltiyAIExample

Aug 15th, 2023
830
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.56 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. using System.Collections.Generic;
  4.  
  5. namespace AIScripts.UtilityAI
  6. {
  7.     public interface IAction
  8.     {
  9.         void Execute();
  10.  
  11.         float GetUtility();
  12.     }
  13.  
  14.     public class AnimalAI : MonoBehaviour
  15.     {
  16.         [SerializeField] private float _hunger;
  17.  
  18.         [SerializeField] private float _thirst;
  19.  
  20.         [SerializeField] private float _fatigue;
  21.  
  22.         private List<IAction> _actions;
  23.  
  24.         private void Start()
  25.         {
  26.             _actions = new List<IAction>
  27.             {
  28.                 new SleepAction(this),
  29.                 new EatAction(this),
  30.                 new DrinkAction(this),
  31.                 new HuntAction(this)
  32.             };
  33.         }
  34.  
  35.         private void Update()
  36.         {
  37.             IAction bestAction = GetBestAction();
  38.             bestAction.Execute();
  39.         }
  40.  
  41.         private IAction GetBestAction()
  42.         {
  43.             float bestUtility = float.MinValue;
  44.             IAction bestAction = null;
  45.  
  46.             foreach(IAction action in _actions)
  47.             {
  48.                 float utility = action.GetUtility();
  49.  
  50.                 if (utility > bestUtility)
  51.                 {
  52.                     bestUtility = utility;
  53.                     bestAction = action;
  54.                 }
  55.             }
  56.  
  57.             return bestAction;
  58.         }
  59.  
  60.         private class SleepAction : IAction
  61.         {
  62.             private readonly AnimalAI _animal;
  63.  
  64.             public SleepAction(AnimalAI animal) =>
  65.                 _animal = animal;
  66.  
  67.             public void Execute()
  68.             {
  69.                 Debug.Log("Sleeping");
  70.                 _animal._fatigue -= Time.deltaTime;
  71.                 _animal._hunger += Time.deltaTime * 0.5f;
  72.                 _animal._thirst += Time.deltaTime * 0.5f;
  73.             }
  74.  
  75.             public float GetUtility()
  76.             {
  77.                 return _animal._fatigue;
  78.             }
  79.         }
  80.  
  81.         private class EatAction : IAction
  82.         {
  83.             private readonly AnimalAI _animal;
  84.  
  85.             public EatAction(AnimalAI animal) =>
  86.                 _animal = animal;
  87.  
  88.             public void Execute()
  89.             {
  90.                 _animal._hunger -= Time.deltaTime;
  91.                 _animal._fatigue += Time.deltaTime * 0.5f;
  92.                 _animal._thirst += Time.deltaTime * 0.5f;
  93.             }
  94.  
  95.             public float GetUtility() => _animal._hunger;
  96.         }
  97.        
  98.         private class DrinkAction : IAction
  99.         {
  100.             private readonly AnimalAI _animal;
  101.  
  102.             public DrinkAction(AnimalAI animal) =>
  103.                 _animal = animal;
  104.  
  105.             public void Execute()
  106.             {
  107.                 _animal._thirst -= Time.deltaTime;
  108.                 _animal._fatigue += Time.deltaTime * 0.5f;
  109.                 _animal._hunger += Time.deltaTime * 0.5f;
  110.             }
  111.  
  112.             public float GetUtility() => _animal._thirst;
  113.         }
  114.  
  115.         private class HuntAction : IAction
  116.         {
  117.             private readonly AnimalAI _animal;
  118.  
  119.             public HuntAction(AnimalAI animal) =>
  120.                 _animal = animal;
  121.  
  122.             public void Execute()
  123.             {
  124.                 _animal._fatigue += Time.deltaTime * 0.5f;
  125.                 _animal._hunger -= Time.deltaTime;
  126.                 _animal._thirst += Time.deltaTime * 0.5f;
  127.             }
  128.  
  129.             public float GetUtility() =>
  130.                 _animal._hunger;
  131.         }
  132.     }
  133. }
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement