Advertisement
KTVX94

FSM Arena

Mar 19th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.64 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5.  
  6. public class CharacterFSM<Conditions>
  7. {
  8.     public CharState<Conditions> currentState;
  9.     public void Update()
  10.     {
  11.         currentState.OnUpdate();
  12.     }
  13.  
  14.     public void FirstState(CharState<Conditions> first)
  15.     {
  16.         currentState = first;
  17.         currentState.OnEnter();
  18.     }
  19.  
  20.     public void Feed(Conditions cond)
  21.     {
  22.         CharState<Conditions> nextState = currentState.GetTransition(cond);
  23.         if (nextState != null)
  24.         {
  25.             currentState.OnExit();
  26.             currentState = nextState;
  27.             currentState.OnEnter();
  28.         }
  29.     }
  30.  
  31.     public void CheckIfExists()
  32.     {
  33.         Debug.Log("I Exist");
  34.     }
  35.  
  36.  
  37.  
  38. }
  39.  
  40. public class CharState<Conditions>
  41. {
  42.     public string name;
  43.     public CharacterBase myself;
  44.  
  45.     public Action OnEnter = delegate { };
  46.     public Action OnUpdate = delegate { };
  47.     public Action OnExit = delegate { };
  48.  
  49.     private Dictionary<Conditions, CharState<Conditions>> transitions = new Dictionary<Conditions, CharState<Conditions>>();
  50.  
  51.     public CharState(string name, CharacterBase self)
  52.     {
  53.         this.name = name;
  54.     }
  55.  
  56.     public CharState<Conditions> GetTransition(Conditions cond)
  57.     {
  58.         if (transitions.ContainsKey(cond))
  59.         {
  60.             Debug.Log(transitions[cond].name);
  61.             return transitions[cond];
  62.         }
  63.         else Debug.Log("no hay transition");
  64.         return null;
  65.     }
  66.  
  67.     public void AddTransition(Conditions cond, CharState<Conditions> next)
  68.     {
  69.         transitions[cond] = next;
  70.     }
  71.  
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement