Skip to content

Instantly share code, notes, and snippets.

@NNNIC
Last active August 17, 2018 00:02
Show Gist options
  • Select an option

  • Save NNNIC/9ce0d867a5f738d33b472719bed6c6ec to your computer and use it in GitHub Desktop.

Select an option

Save NNNIC/9ce0d867a5f738d33b472719bed6c6ec to your computer and use it in GitHub Desktop.
Event machine is a task or process that has both event-manager and state machine. It is for Unity.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
public class EventMachine {
#region event manager
protected EventManager m_evman;
public EventManager EventMan { get { return m_evman; } }
#endregion
#region constructor
public StateManager m_sm { get; private set; }
public EventMachine(Type t)
{
m_sm = (StateManager)Activator.CreateInstance(t);
m_evman = new EventManager();
m_sm.SetEventMan(m_evman);
}
#endregion
#region Framework
public void Start()
{
m_sm.Start();
}
public void Update () {
m_evman.Update();
m_sm.update();
}
public bool IsEnd()
{
return m_sm.IsEnd();
}
#endregion
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EventManager {
public Queue<object> m_eventlist { get; private set; }
public object CUR { get; private set; }
public bool existEvent { get { return m_eventlist!=null && m_eventlist.Count > 0; } }
public int countEvent { get { return m_eventlist!=null ? m_eventlist.Count : 0; } }
public EventManager()
{
m_eventlist = new Queue<object>();
CUR = null;
}
public void Push(object @event)
{
if (m_eventlist==null) m_eventlist = new Queue<object>();
m_eventlist.Enqueue(@event);
}
public void Update()
{
CUR = null;
if (m_eventlist!=null && m_eventlist.Count>0)
{
CUR = m_eventlist.Dequeue();
}
}
}
using System;
using UnityEngine;
public class StateManager : StateManagerGetEventManInterface
{
public EventManager m_eventman { get; private set; }
Action<bool> m_curfunc;
Action<bool> m_nextfunc;
Action<bool> m_tempfunc;
bool m_noWait;
public virtual void Start() { }
public virtual bool IsEnd() { return true; }
public void SetEventMan(EventManager eventman) { m_eventman = eventman; }
public EventManager GetEventMan() { return m_eventman; }
public void update()
{
while(true)
{
var bFirst = false;
if (m_nextfunc!=null)
{
m_curfunc = m_nextfunc;
m_nextfunc = null;
bFirst = true;
Debug.Log("Into .. " + m_curfunc.Method.Name);
}
m_noWait = false;
if (m_curfunc!=null)
{
m_curfunc(bFirst);
}
if (!m_noWait) break;
}
}
public void Goto(Action<bool> func)
{
m_nextfunc = func;
}
public bool CheckState(Action<bool> func)
{
return m_curfunc == func;
}
// for tempfunc
public void SetNextState(Action<bool> func)
{
m_tempfunc = func;
}
public void GoNextState()
{
m_nextfunc = m_tempfunc;
m_tempfunc = null;
}
public bool HasNextState()
{
return m_tempfunc != null;
}
public void NoWait()
{
m_noWait = true;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public interface StateManagerGetEventManInterface {
EventManager GetEventMan();
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour {
EventMachine m_em;
void Start () {
m_em = new EventMachine(typeof(TestControl));
m_em.Start();
}
void Update () {
m_em.Update();
}
private void OnGUI()
{
if (GUILayout.Button("BUT A"))
{
m_em.EventMan.Push("BUT_A");
}
if (GUILayout.Button("BUT B"))
{
m_em.EventMan.Push("BUT_B");
}
if (GUILayout.Button("BUT C"))
{
m_em.EventMan.Push("BUT_C");
}
}
}
@NNNIC
Copy link
Copy Markdown
Author

NNNIC commented Aug 6, 2018

modified

@NNNIC
Copy link
Copy Markdown
Author

NNNIC commented Aug 6, 2018

add StateManagerGetEventManInterface.cs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment