Skip to content

Commit 060d0d0

Browse files
committed
Merging projects part 3
1 parent 4944c6c commit 060d0d0

File tree

5 files changed

+54
-18
lines changed

5 files changed

+54
-18
lines changed

StateMachine/Fluent/Api/FluentImplementation.cs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ namespace StateMachine.Fluent.Api
3434
public class FluentImplementation<TS, TT, TD> : GlobalTransitionBuilderFluent<TS, TT, TD>,
3535
TransitionStateFluent<TS, TT, TD>
3636
{
37-
protected FsmModel<TS, TT, TD> FsmModel { get; set; } = new FsmModel<TS, TT, TD>();
37+
public Dictionary<Tuple<TS, TS>, List<Timer<TS>>> AfterEntries { get; set; } = new Dictionary<Tuple<TS, TS>, List<Timer<TS>>>();
38+
public List<Timer<TS>> GlobalAfterEntries { get; set; } = new List<Timer<TS>>();
39+
40+
protected FsmModel<TS, TT, TD> FsmModel { get; set; } = new FsmModel<TS, TT, TD>();
3841
protected TS startState;
3942

4043
protected Tuple<TS> currentState;
@@ -65,8 +68,27 @@ public Fsm<TS, TT, TD> Build()
6568
FsmModel.Current = FsmModel.States[startState];
6669
return new Fsm<TS, TT, TD>(FsmModel);
6770
}
68-
69-
public StateFluent<TS, TT, TD> State(TS state)
71+
72+
public TransitionStateFluent<TS, TT, TD> After(float amount, TimeUnit timeUnit)
73+
{
74+
var key = currentTransition;
75+
if (!AfterEntries.TryGetValue(key, out var l))
76+
{
77+
l = new List<Timer<TS>>();
78+
AfterEntries.Add(key, l);
79+
}
80+
l.Add(new Timer<TS>(key.Item2, amount, timeUnit));
81+
return this;
82+
}
83+
84+
public TransitionStateFluent<TS, TT, TD> AfterGlobal(float amount, TimeUnit timeUnit)
85+
{
86+
var key = currentGlobalTransition;
87+
GlobalAfterEntries.Add(new Timer<TS>(key.Item1, amount, timeUnit));
88+
return this;
89+
}
90+
91+
public StateFluent<TS, TT, TD> State(TS state)
7092
{
7193
currentState = Tuple.Create(state);
7294
if (!FsmModel.States.ContainsKey(state))
@@ -98,14 +120,12 @@ public StateFluent<TS, TT, TD> Update(Action<UpdateArgs<TS, TT, TD>> update)
98120
public GlobalTransitionFluent<TS, TT, TD> GlobalTransitionTo(TS state)
99121
{
100122
currentGlobalTransition = Tuple.Create(state);
101-
if (!globalTransitionModels.ContainsKey(currentGlobalTransition))
102-
{
103-
globalTransitionModels[currentGlobalTransition] = new TransitionModel<TS, TT>(startState, state);
104-
new TransitionModel<TS, TT>(startState, state);
105-
FsmModel.GlobalTransitions[state] =
106-
new Transition<TS, TT, TD>(globalTransitionModels[currentGlobalTransition]);
107-
}
108-
return this;
123+
if (globalTransitionModels.ContainsKey(currentGlobalTransition)) return this;
124+
125+
globalTransitionModels[currentGlobalTransition] = new TransitionModel<TS, TT>(startState, state);
126+
FsmModel.GlobalTransitions[state] =
127+
new Transition<TS, TT, TD>(globalTransitionModels[currentGlobalTransition]);
128+
return this;
109129
}
110130

111131
public GlobalTransitionBuilderFluent<TS, TT, TD> OnGlobal(TT trigger)

StateMachine/Fluent/Api/GlobalTransitionFluent.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,13 @@ public interface GlobalTransitionFluent<TS, TT, TD>
4545
/// </summary>
4646
/// <param name="condition">The condition.</param>
4747
GlobalTransitionBuilderFluent<TS, TT, TD> IfGlobal(Func<IfArgs<TS>, bool> condition);
48-
}
48+
49+
/// <summary>
50+
/// Automatically walks the transition you're currently describing, if the specified amount of time has passed.
51+
/// </summary>
52+
/// <param name="amount">The amount.</param>
53+
/// <param name="timeUnit">The time unit.</param>
54+
/// <returns></returns>
55+
TransitionStateFluent<TS, TT, TD> AfterGlobal(float amount, TimeUnit timeUnit);
56+
}
4957
}

StateMachine/Fluent/Api/TransitionFluent.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,18 @@ namespace StateMachine.Fluent.Api
3232
{
3333
public interface TransitionFluent<TS, TT, TD>
3434
{
35-
/// <summary>
36-
/// Specifies the trigger, that has to be served as input in order to walk the transition you're currently describing.
37-
/// </summary>
38-
/// <param name="trigger">The trigger.</param>
39-
TransitionStateFluent<TS, TT, TD> On(TT trigger);
35+
/// <summary>
36+
/// Automatically walks the transition you're currently describing, if the specified amount of time has passed.
37+
/// </summary>
38+
/// <param name="amount">The amount.</param>
39+
/// <param name="timeUnit">The time unit.</param>
40+
/// <returns></returns>
41+
TransitionStateFluent<TS, TT, TD> After(float amount, TimeUnit timeUnit);
42+
/// <summary>
43+
/// Specifies the trigger, that has to be served as input in order to walk the transition you're currently describing.
44+
/// </summary>
45+
/// <param name="trigger">The trigger.</param>
46+
TransitionStateFluent<TS, TT, TD> On(TT trigger);
4047

4148
/// <summary>
4249
/// Specifies the condition, that has to be met, in addition to the trigger, to walk the transition you're currently

StateMachine/Fsm.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public void Trigger(TT input)
190190
}
191191
}
192192

193-
public new void Update(TimeSpan elapsedTime)
193+
public void Update(TimeSpan elapsedTime)
194194
{
195195
// After-entries on transitions.
196196
foreach (var k in Current.Model.Transitions.Keys)

StateMachine/State.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
// For more information, please refer to <http://unlicense.org>
2626
// ***************************************************************************
2727

28+
using System;
2829
using StateMachine.Events;
2930

3031
namespace StateMachine

0 commit comments

Comments
 (0)