-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
chen
committed
Oct 12, 2017
0 parents
commit a6228dc
Showing
18 changed files
with
742 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio 15 | ||
VisualStudioVersion = 15.0.26430.16 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommonCLib", "CommonCLib\CommonCLib.csproj", "{C5D43B81-84EF-4CAD-B8DC-0E6184EE0232}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{C5D43B81-84EF-4CAD-B8DC-0E6184EE0232}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{C5D43B81-84EF-4CAD-B8DC-0E6184EE0232}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{C5D43B81-84EF-4CAD-B8DC-0E6184EE0232}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{C5D43B81-84EF-4CAD-B8DC-0E6184EE0232}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace CommonCLib.CEvent | ||
{ | ||
// todo: 待优化 | ||
// 用内存换参数 | ||
public delegate void VoidDelegate(MyEventArgs args); | ||
public class MyEventArgs : EventArgs | ||
{ | ||
// 几种基本类型集合 | ||
private List<int> _intPar; | ||
private List<bool> _boolPar; | ||
private List<string> _strPar; | ||
private List<float> _floatPar; | ||
|
||
public List<int> IntPar { get { return _intPar; } } | ||
public List<bool> BoolPar { get { return _boolPar; } } | ||
public List<string> StrPar { get { return _strPar; } } | ||
public List<float> FloatPar { get { return _floatPar; } } | ||
public MyEventArgs() | ||
{ | ||
_intPar = new List<int>(); | ||
_boolPar = new List<bool>(); | ||
_strPar = new List<string>(); | ||
_floatPar = new List<float>(); | ||
} | ||
public void AddInt(int value) | ||
{ | ||
_intPar.Add(value); | ||
} | ||
public void AddBool(bool value) | ||
{ | ||
_boolPar.Add(value); | ||
} | ||
public void AddFloat(float value) | ||
{ | ||
_floatPar.Add(value); | ||
} | ||
public void AddStr(string value) | ||
{ | ||
_strPar.Add(value); | ||
} | ||
} | ||
|
||
public class EventMgr | ||
{ | ||
#region Single | ||
private EventMgr() { Init(); } | ||
private static EventMgr _ins; | ||
public static EventMgr Ins | ||
{ | ||
get | ||
{ | ||
if (_ins == null) _ins = new EventMgr(); | ||
return _ins; | ||
} | ||
} | ||
#endregion | ||
|
||
private Dictionary<string, List<VoidDelegate>> _events; | ||
|
||
private void Init() | ||
{ | ||
_events = new Dictionary<string, List<VoidDelegate>>(); | ||
} | ||
|
||
public void RegistEvent(string eventName, VoidDelegate voidDelegate) | ||
{ | ||
if (_events == null) _events = new Dictionary<string, List<VoidDelegate>>(); | ||
if (_events.ContainsKey(eventName)) | ||
_events[eventName].Add(voidDelegate); | ||
else | ||
{ | ||
List<VoidDelegate> ds = new List<VoidDelegate>(); | ||
ds.Add(voidDelegate); | ||
_events.Add(eventName, ds); | ||
} | ||
} | ||
|
||
public void NotifyEvent(string eventName, MyEventArgs args) | ||
{ | ||
if (_events == null || _events[eventName] == null) return; | ||
foreach (var e in _events[eventName]) | ||
{ | ||
e(args); | ||
} | ||
} | ||
|
||
public void RemoveEvent(string eventName, VoidDelegate d) | ||
{ | ||
if (_events == null || _events[eventName] == null) return; | ||
if (_events.ContainsKey(eventName) && _events[eventName].Contains(d)) | ||
{ | ||
_events[eventName].Remove(d); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace CommonLib.CFSM | ||
{ | ||
public delegate void CVoidDelegate(); | ||
public delegate void CVoidDelegateState(IState state); | ||
public delegate void CVoidDelegateFloat(float f); | ||
|
||
public class CState : IState | ||
{ | ||
public event CVoidDelegateState OnEnter; | ||
public event CVoidDelegateState OnExit; | ||
public event CVoidDelegateFloat OnUpdate; | ||
public event CVoidDelegateFloat OnLateUpdate; | ||
public event CVoidDelegate OnFixedUpdate; | ||
|
||
public string Name | ||
{ | ||
get { return _name; } | ||
} | ||
|
||
public string Tag | ||
{ | ||
get { return _tag; } | ||
set { _tag = value; } | ||
} | ||
|
||
public IStateMechine Parent | ||
{ | ||
get { return _parent; } | ||
set { _parent = value; } | ||
} | ||
|
||
public float Timer | ||
{ | ||
get { return _timer; } | ||
} | ||
|
||
public List<ITransition> Transitions | ||
{ | ||
get { return _transitions; } | ||
} | ||
|
||
public CState(string name) | ||
{ | ||
_name = name; | ||
|
||
} | ||
|
||
public void AddTransition(ITransition t) | ||
{ | ||
if(t != null && !_transitions.Contains(t)) | ||
{ | ||
_transitions.Add(t); | ||
} | ||
} | ||
|
||
public virtual void EnterCallback(IState prev) | ||
{ | ||
_timer = 0; | ||
if (OnEnter != null) | ||
{ | ||
OnEnter(prev); | ||
} | ||
} | ||
|
||
public virtual void ExitCallback(IState next) | ||
{ | ||
if(OnExit != null) | ||
{ | ||
OnExit(next); | ||
} | ||
_timer = 0; | ||
} | ||
|
||
public virtual void UpdateCallback(float deltaTime) | ||
{ | ||
_timer += deltaTime; | ||
if (OnUpdate != null) | ||
{ | ||
OnUpdate(deltaTime); | ||
} | ||
} | ||
|
||
public virtual void LateUpdateCallback(float deltaTime) | ||
{ | ||
if (OnLateUpdate != null) | ||
{ | ||
OnLateUpdate(deltaTime); | ||
} | ||
} | ||
|
||
public virtual void FixedUpdateCallback() | ||
{ | ||
if(OnFixedUpdate != null) | ||
{ | ||
OnFixedUpdate(); | ||
} | ||
} | ||
|
||
private string _name, _tag; | ||
private IStateMechine _parent; | ||
private float _timer; | ||
private List<ITransition> _transitions; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace CommonLib.CFSM | ||
{ | ||
public class CStateMechine : CState, IStateMechine | ||
{ | ||
public IState CurState | ||
{ | ||
get { return _curState; } | ||
} | ||
|
||
public IState DefaultState | ||
{ | ||
get { return _defaultState; } | ||
set | ||
{ | ||
if(!_states.Contains(value)) | ||
{ | ||
AddState(value); | ||
} | ||
_defaultState = value; | ||
} | ||
} | ||
|
||
public CStateMechine(string name, IState defaultState) : base(name) | ||
{ | ||
_states = new List<IState>(); | ||
_defaultState = defaultState; | ||
} | ||
|
||
public void AddState(IState state) | ||
{ | ||
if(state != null && !_states.Contains(state)) | ||
{ | ||
_states.Add(state); | ||
state.Parent = this; | ||
if(_defaultState == null) | ||
{ | ||
_defaultState = state; | ||
} | ||
} | ||
} | ||
|
||
public void RemoveState(IState state) | ||
{ | ||
if(_curState == state) | ||
{ | ||
return; | ||
} | ||
if (state != null && _states.Contains(state)) | ||
{ | ||
_states.Remove(state); | ||
state.Parent = null; | ||
if(_defaultState == state) | ||
{ | ||
_defaultState = _states.Count > 0 ? _states[1] : null; | ||
} | ||
} | ||
} | ||
|
||
public override void UpdateCallback(float deltaTime) | ||
{ | ||
if(_isTransition) | ||
{ | ||
if(_t.TransitionCallback()) | ||
{ | ||
DoTransition(_t); | ||
_isTransition = false; | ||
} | ||
return; | ||
} | ||
|
||
base.UpdateCallback(deltaTime); | ||
|
||
if(_curState == null) | ||
{ | ||
_curState = _defaultState; | ||
} | ||
|
||
var ts = _curState.Transitions; | ||
int count = ts.Count; | ||
for(int i = 0; i < count; i++) | ||
{ | ||
var t = ts[i]; | ||
if(t.ShouldBegin()) | ||
{ | ||
_isTransition = true; | ||
_t = t; | ||
return; | ||
} | ||
} | ||
|
||
_curState.UpdateCallback(deltaTime); | ||
} | ||
|
||
public override void LateUpdateCallback(float deltaTime) | ||
{ | ||
base.LateUpdateCallback(deltaTime); | ||
|
||
_curState.LateUpdateCallback(deltaTime); | ||
} | ||
|
||
public override void FixedUpdateCallback() | ||
{ | ||
base.FixedUpdateCallback(); | ||
|
||
_curState.FixedUpdateCallback(); | ||
} | ||
|
||
|
||
public IState GetStateWithName(string name) | ||
{ | ||
foreach(var s in _states) | ||
{ | ||
if(s.Name == name) | ||
{ | ||
return s; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public IState GetStateWithTag(string tag) | ||
{ | ||
foreach(var s in _states) | ||
{ | ||
if(s.Tag == tag) | ||
{ | ||
return s; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private IState _curState, _defaultState; | ||
private List<IState> _states; | ||
|
||
// 是否进行过渡 | ||
private bool _isTransition = false; | ||
// 当前正在进行的过渡 | ||
private ITransition _t; | ||
|
||
/// <summary> | ||
/// 开始进行过渡 | ||
/// </summary> | ||
private void DoTransition(ITransition t) | ||
{ | ||
_curState.ExitCallback(t.To); | ||
_curState = t.To; | ||
_curState.EnterCallback(t.From); | ||
} | ||
} | ||
} |
Oops, something went wrong.