public delegate float Func(object funcArg); public class HOFunctor { float _deltaTime = 0; Func _func = null; object _funcArg = null; Guid _id = Guid.Empty; public HOFunctor(float deltaTime, Func func, object funcArg) { _id = Guid.NewGuid(); _deltaTime = deltaTime; _func = func; _funcArg = funcArg; } public HOFunctor(float deltaTime, Func func) : this(deltaTime, func, null) { } public bool Process(float deltaTime) { if (_func != null) { _deltaTime -= deltaTime; if (_deltaTime <= 0) { _deltaTime = _func(_funcArg); } } return _deltaTime > 0; } public Guid ID { get { return _id; } } }
public class HOFunctorMgr { #region Private fields private static HOFunctorMgr _instance = null; protected Dictionary<Guid, HOFunctor>[] _functors = { new Dictionary<Guid, HOFunctor>(), new Dictionary<Guid, HOFunctor>() }; int _currentIndex = 0; private HOFunctorMgr() { } #endregion public static HOFunctorMgr Instance { get { if (_instance == null) { _instance = new HOFunctorMgr(); } return _instance; } } #region Public methods public Guid AddFunctor(float deltaTime, Func func, object funcArg) { return AddFunctor(new HOFunctor(deltaTime, func, funcArg)); } public Guid AddFunctor(float deltaTime, Func func) { return AddFunctor(new HOFunctor(deltaTime, func, null)); } public Guid AddFunctor(HOFunctor functor) { if (functor != null && !_functors[_currentIndex].ContainsKey(functor.ID)) { _functors[_currentIndex].Add(functor.ID, functor); return functor.ID; } return Guid.Empty; } public void ProcessFunctors(float delta) { int indexToProcess = _currentIndex; _currentIndex ^= 1; foreach (HOFunctor f in _functors[indexToProcess].Values) { if (f.Process(delta)) { AddFunctor(f); } } _functors[indexToProcess].Clear(); } public void RemoveFunctor(Guid id) { if (_functors[0].ContainsKey(id)) { _functors[0].Remove(id); } if (_functors[1].ContainsKey(id)) { _functors[1].Remove(id); } } #endregion }
GameEntities.HOFunctorMgr.Instance.AddFunctor(2.0f, arg => { HidePuzzleWindow(); return 0.0f; }, null );
... int k = 5; HOFunctorMgr.Instance.AddFunctor(5, arg => { GameMap.Instance.AddScreenMessage(string.Format("{0}", arg)); if (k-- >= 0) { return 5; } return 0; }, "test");
static float MyMethod(object arg) { if (arg != null) { ... } return 0; } HOFunctorMgr.Instance.AddFunctor(10, MyMethod, someObject);
using System; using System.Collections.Generic; using System.Text; namespace GameEntities { public delegate float Func(object funcArg); public class HOFunctor { float _deltaTime = 0; Func _func = null; object _funcArg = null; Guid _id = Guid.Empty; public HOFunctor(float deltaTime, Func func, object funcArg) { _id = Guid.NewGuid(); _deltaTime = deltaTime; _func = func; _funcArg = funcArg; } public HOFunctor(float deltaTime, Func func) : this(deltaTime, func, null) { } public bool Process(float deltaTime) { if (_func != null) { _deltaTime -= deltaTime; if (_deltaTime <= 0) { _deltaTime = _func(_funcArg); } } return _deltaTime > 0; } public Guid ID { get { return _id; } } } public class HOFunctorMgr { #region Private fields private static HOFunctorMgr _instance = null; protected Dictionary<Guid, HOFunctor>[] _functors = { new Dictionary<Guid, HOFunctor>(), new Dictionary<Guid, HOFunctor>() }; int _currentIndex = 0; private HOFunctorMgr() { } #endregion public static HOFunctorMgr Instance { get { if (_instance == null) { _instance = new HOFunctorMgr(); } return _instance; } } #region Public methods public Guid AddFunctor(float deltaTime, Func func, object funcArg) { return AddFunctor(new HOFunctor(deltaTime, func, funcArg)); } public Guid AddFunctor(float deltaTime, Func func) { return AddFunctor(new HOFunctor(deltaTime, func, null)); } public Guid AddFunctor(HOFunctor functor) { if (functor != null && !_functors[_currentIndex].ContainsKey(functor.ID)) { _functors[_currentIndex].Add(functor.ID, functor); return functor.ID; } return Guid.Empty; } public void ProcessFunctors(float delta) { int indexToProcess = _currentIndex; _currentIndex ^= 1; foreach (HOFunctor f in _functors[indexToProcess].Values) { if (f.Process(delta)) { AddFunctor(f); } } _functors[indexToProcess].Clear(); } public void RemoveFunctor(Guid id) { if (_functors[0].ContainsKey(id)) { _functors[0].Remove(id); } if (_functors[1].ContainsKey(id)) { _functors[1].Remove(id); } } #endregion } }
Source: https://habr.com/ru/post/121244/
All Articles