Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
1,948
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.99 KB | None | 0 0
  1. // With a little help from UnityGems
  2.  
  3. using UnityEngine;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7.  
  8. /// <summary>
  9. /// God damn this thing is useful srsly live. laugh. love. it.
  10. /// </summary>
  11. public class SimpleStateMachine : MonoBehaviour
  12. {
  13.     public bool DebugGui;
  14.     public Vector2 DebugGuiPosition;
  15.  
  16.     public string DebugGuiTitle = "Simple Machine";
  17.    
  18.     protected Enum queueCommand;
  19.  
  20.     void OnGUI()
  21.     {
  22.         if (DebugGui)
  23.         {
  24.             GUI.Box(new Rect(DebugGuiPosition.x, DebugGuiPosition.y, 200, 50), DebugGuiTitle);
  25.  
  26.             GUI.TextField(new Rect(DebugGuiPosition.x + 10, DebugGuiPosition.y + 20, 180, 20), string.Format("State: {0}", currentState));
  27.         }
  28.     }
  29.  
  30.     protected float timeEnteredState;
  31.  
  32.     public class State
  33.     {
  34.         public Action DoUpdate = DoNothing;
  35.         public Action DoFixedUpdate = DoNothing;
  36.         public Action DoLateUpdate = DoNothing;
  37.         public Action DoManualUpdate = DoNothing;
  38.         public Action enterState = DoNothing;
  39.         public Action exitState = DoNothing;
  40.  
  41.         public Enum currentState;
  42.     }
  43.  
  44.     public State state = new State();
  45.  
  46.     public Enum currentState
  47.     {
  48.         get
  49.         {
  50.             return state.currentState;
  51.         }
  52.         set
  53.         {
  54.             if (state.currentState == value)
  55.                 return;
  56.  
  57.             ChangingState();
  58.             state.currentState = value;
  59.             ConfigureCurrentState();
  60.         }
  61.     }
  62.  
  63.     [HideInInspector]
  64.     public Enum lastState;
  65.  
  66.     void ChangingState()
  67.     {
  68.         lastState = state.currentState;
  69.         timeEnteredState = Time.time;
  70.     }
  71.  
  72.     void ConfigureCurrentState()
  73.     {
  74.         if (state.exitState != null)
  75.         {
  76.             state.exitState();
  77.         }
  78.  
  79.         //Now we need to configure all of the methods
  80.         state.DoUpdate = ConfigureDelegate<Action>("Update", DoNothing);
  81.         state.DoFixedUpdate = ConfigureDelegate<Action>("FixedUpdate", DoNothing);
  82.         state.DoLateUpdate = ConfigureDelegate<Action>("LateUpdate", DoNothing);
  83.         state.DoManualUpdate = ConfigureDelegate<Action>("ManualUpdate", DoNothing);
  84.         state.enterState = ConfigureDelegate<Action>("EnterState", DoNothing);
  85.         state.exitState = ConfigureDelegate<Action>("ExitState", DoNothing);
  86.  
  87.         if (state.enterState != null)
  88.         {
  89.             state.enterState();
  90.         }
  91.     }
  92.  
  93.     Dictionary<Enum, Dictionary<string, Delegate>> _cache = new Dictionary<Enum, Dictionary<string, Delegate>>();
  94.  
  95.     T ConfigureDelegate<T>(string methodRoot, T Default) where T : class
  96.     {
  97.  
  98.         Dictionary<string, Delegate> lookup;
  99.         if (!_cache.TryGetValue(state.currentState, out lookup))
  100.         {
  101.             _cache[state.currentState] = lookup = new Dictionary<string, Delegate>();
  102.         }
  103.         Delegate returnValue;
  104.         if (!lookup.TryGetValue(methodRoot, out returnValue))
  105.         {
  106.             var mtd = GetType().GetMethod(state.currentState.ToString() + "_" + methodRoot, System.Reflection.BindingFlags.Instance
  107.                 | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.InvokeMethod);
  108.  
  109.             if (mtd != null)
  110.             {
  111.                 returnValue = Delegate.CreateDelegate(typeof(T), this, mtd);
  112.             }
  113.             else
  114.             {
  115.                 returnValue = Default as Delegate;
  116.             }
  117.             lookup[methodRoot] = returnValue;
  118.         }
  119.         return returnValue as T;
  120.  
  121.     }
  122.  
  123.     void Update()
  124.     {
  125.         EarlyGlobalSuperUpdate();
  126.  
  127.         state.DoUpdate();
  128.  
  129.         LateGlobalSuperUpdate();
  130.     }
  131.  
  132.     void FixedUpdate()
  133.     {
  134.         state.DoFixedUpdate();
  135.     }
  136.  
  137.     void LateUpdate()
  138.     {
  139.         state.DoLateUpdate();
  140.     }
  141.  
  142.     protected virtual void EarlyGlobalSuperUpdate() { }
  143.  
  144.     protected virtual void LateGlobalSuperUpdate() { }
  145.  
  146.     static void DoNothing() { }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement