Skip to content

Commit ce440cc

Browse files
committed
Merge branch 'master' of https://github.com/UnterrainerInformatik/FiniteStateMachine into feature/builder
Conflicts: StateMachine/Fluent/Api/FluentImplementation.cs StateMachine/Transition.cs StateMachine/TransitionModel.cs
2 parents 50d3e32 + 3ac6e2e commit ce440cc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1457
-275
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// ***************************************************************************
2+
// This is free and unencumbered software released into the public domain.
3+
//
4+
// Anyone is free to copy, modify, publish, use, compile, sell, or
5+
// distribute this software, either in source code form or as a compiled
6+
// binary, for any purpose, commercial or non-commercial, and by any
7+
// means.
8+
//
9+
// In jurisdictions that recognize copyright laws, the author or authors
10+
// of this software dedicate any and all copyright interest in the
11+
// software to the public domain. We make this dedication for the benefit
12+
// of the public at large and to the detriment of our heirs and
13+
// successors. We intend this dedication to be an overt act of
14+
// relinquishment in perpetuity of all present and future rights to this
15+
// software under copyright law.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20+
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21+
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
// OTHER DEALINGS IN THE SOFTWARE.
24+
//
25+
// For more information, please refer to <http://unlicense.org>
26+
// ***************************************************************************
27+
28+
using JetBrains.Annotations;
29+
30+
namespace MonoGameStateMachine.Api
31+
{
32+
[PublicAPI]
33+
public interface BuilderFluent<TS, TT, TD>
34+
{
35+
/// <summary>
36+
/// Enables the stack and turns this Finite-State-Machine (FSM) into a Stack-Based-FSM (SBFSM).<br />
37+
/// Beware that you will have to specify "ClearsStack()" on some of the states, as otherwise the stack will grow and
38+
/// never be cleared.
39+
/// </summary>
40+
BuilderFluent<TS, TT, TD> EnableStack();
41+
42+
/// <summary>
43+
/// Sets a global transition to a state.<br />
44+
/// This will generate a transision that will be triggered regardless of the current state's position in the graph and
45+
/// regardless of the transitions connected to that state.<p />
46+
/// Think of it as a 'catch all' transition.<br />
47+
/// Usually you would use such global transitions to reset a graph when ESC is pressed or something like that.
48+
/// </summary>
49+
/// <param name="state">The state the global transition should lead to.</param>
50+
GlobalTransitionFluent<TS, TT, TD> GlobalTransitionTo(TS state);
51+
52+
/// <summary>
53+
/// Generates a new state.<br />
54+
/// An FSM can have multiple states connected via transitions.<br />
55+
/// Only a single transition is active at any given time and that transition will receive 'Update(TData)' calls and
56+
/// will be checked for transitions that may be triggered by an input, which, in turn, would activate the next state.
57+
/// </summary>
58+
/// <param name="state">The state.</param>
59+
StateFluent<TS, TT, TD> State(TS state);
60+
61+
/// <summary>
62+
/// Builds this instance of an FSM (or SBFSM).
63+
/// </summary>
64+
Fsm<TS, TT> Build();
65+
}
66+
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
// ***************************************************************************
2+
// This is free and unencumbered software released into the public domain.
3+
//
4+
// Anyone is free to copy, modify, publish, use, compile, sell, or
5+
// distribute this software, either in source code form or as a compiled
6+
// binary, for any purpose, commercial or non-commercial, and by any
7+
// means.
8+
//
9+
// In jurisdictions that recognize copyright laws, the author or authors
10+
// of this software dedicate any and all copyright interest in the
11+
// software to the public domain. We make this dedication for the benefit
12+
// of the public at large and to the detriment of our heirs and
13+
// successors. We intend this dedication to be an overt act of
14+
// relinquishment in perpetuity of all present and future rights to this
15+
// software under copyright law.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20+
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21+
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
// OTHER DEALINGS IN THE SOFTWARE.
24+
//
25+
// For more information, please refer to <http://unlicense.org>
26+
// ***************************************************************************
27+
28+
using System;
29+
using System.Collections.Generic;
30+
using Microsoft.Xna.Framework;
31+
using StateMachine.Events;
32+
33+
namespace MonoGameStateMachine.Api
34+
{
35+
public class FluentImplementation<TS, TT> : StateMachine.Fluent.Api.FluentImplementation<TS, TT, GameTime>,
36+
TransitionStateFluent<TS, TT, GameTime>, GlobalTransitionBuilderFluent<TS, TT, GameTime>
37+
{
38+
public Dictionary<Tuple<TS, TS>, List<Timer<TS>>> AfterEntries { get; set; } = new Dictionary<Tuple<TS, TS>, List<Timer<TS>>>();
39+
public List<Timer<TS>> GlobalAfterEntries { get; set; } = new List<Timer<TS>>();
40+
41+
public FluentImplementation(TS startState) : base(startState)
42+
{
43+
}
44+
45+
public new Fsm<TS, TT> Build()
46+
{
47+
base.Build();
48+
var m = new Fsm<TS, TT>(FsmModel);
49+
m.AfterEntries = AfterEntries;
50+
m.GlobalAfterEntries = GlobalAfterEntries;
51+
return m;
52+
}
53+
54+
public TransitionStateFluent<TS, TT, GameTime> After(float amount, TimeUnit timeUnit)
55+
{
56+
List<Timer<TS>> l;
57+
var key = currentTransition;
58+
if (!AfterEntries.TryGetValue(key, out l))
59+
{
60+
l = new List<Timer<TS>>();
61+
AfterEntries.Add(key, l);
62+
}
63+
l.Add(new Timer<TS>(key.Item2, amount, timeUnit));
64+
return this;
65+
}
66+
67+
public TransitionStateFluent<TS, TT, GameTime> AfterGlobal(float amount, TimeUnit timeUnit)
68+
{
69+
var key = currentGlobalTransition;
70+
GlobalAfterEntries.Add(new Timer<TS>(key.Item1, amount, timeUnit));
71+
return this;
72+
}
73+
74+
public new StateFluent<TS, TT, GameTime> State(TS state)
75+
{
76+
base.State(state);
77+
return this;
78+
}
79+
80+
public new TransitionFluent<TS, TT, GameTime> TransitionTo(TS state)
81+
{
82+
base.TransitionTo(state);
83+
return this;
84+
}
85+
86+
public new TransitionFluent<TS, TT, GameTime> PopTransition()
87+
{
88+
base.PopTransition();
89+
return this;
90+
}
91+
92+
public new TransitionStateFluent<TS, TT, GameTime> On(TT trigger)
93+
{
94+
base.On(trigger);
95+
return this;
96+
}
97+
98+
public new TransitionStateFluent<TS, TT, GameTime> If(Func<IfArgs<TS>, bool> condition)
99+
{
100+
base.If(condition);
101+
return this;
102+
}
103+
104+
public new StateFluent<TS, TT, GameTime> OnEnter(
105+
Action<StateChangeArgs<TS, TT, GameTime>> stateChangeArgs)
106+
{
107+
base.OnEnter(stateChangeArgs);
108+
return this;
109+
}
110+
111+
public new StateFluent<TS, TT, GameTime> OnExit(
112+
Action<StateChangeArgs<TS, TT, GameTime>> stateChangeArgs)
113+
{
114+
base.OnExit(stateChangeArgs);
115+
return this;
116+
}
117+
118+
public new StateFluent<TS, TT, GameTime> Update(Action<UpdateArgs<TS, TT, GameTime>> updateArgs)
119+
{
120+
base.Update(updateArgs);
121+
return this;
122+
}
123+
124+
public new StateFluent<TS, TT, GameTime> ClearsStack()
125+
{
126+
base.ClearsStack();
127+
return this;
128+
}
129+
130+
public new BuilderFluent<TS, TT, GameTime> EnableStack()
131+
{
132+
base.EnableStack();
133+
return this;
134+
}
135+
136+
public new GlobalTransitionFluent<TS, TT, GameTime> GlobalTransitionTo(TS state)
137+
{
138+
base.GlobalTransitionTo(state);
139+
return this;
140+
}
141+
142+
public new GlobalTransitionBuilderFluent<TS, TT, GameTime> OnGlobal(TT trigger)
143+
{
144+
base.OnGlobal(trigger);
145+
return this;
146+
}
147+
148+
public new GlobalTransitionBuilderFluent<TS, TT, GameTime> IfGlobal(
149+
Func<IfArgs<TS>, bool> condition)
150+
{
151+
base.IfGlobal(condition);
152+
return this;
153+
}
154+
}
155+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// ***************************************************************************
2+
// This is free and unencumbered software released into the public domain.
3+
//
4+
// Anyone is free to copy, modify, publish, use, compile, sell, or
5+
// distribute this software, either in source code form or as a compiled
6+
// binary, for any purpose, commercial or non-commercial, and by any
7+
// means.
8+
//
9+
// In jurisdictions that recognize copyright laws, the author or authors
10+
// of this software dedicate any and all copyright interest in the
11+
// software to the public domain. We make this dedication for the benefit
12+
// of the public at large and to the detriment of our heirs and
13+
// successors. We intend this dedication to be an overt act of
14+
// relinquishment in perpetuity of all present and future rights to this
15+
// software under copyright law.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20+
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21+
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
// OTHER DEALINGS IN THE SOFTWARE.
24+
//
25+
// For more information, please refer to <http://unlicense.org>
26+
// ***************************************************************************
27+
28+
using JetBrains.Annotations;
29+
30+
namespace MonoGameStateMachine.Api
31+
{
32+
[PublicAPI]
33+
public interface GlobalTransitionBuilderFluent<TS, TT, TD> :
34+
GlobalTransitionFluent<TS, TT, TD>, BuilderFluent<TS, TT, TD>
35+
{
36+
}
37+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// ***************************************************************************
2+
// This is free and unencumbered software released into the public domain.
3+
//
4+
// Anyone is free to copy, modify, publish, use, compile, sell, or
5+
// distribute this software, either in source code form or as a compiled
6+
// binary, for any purpose, commercial or non-commercial, and by any
7+
// means.
8+
//
9+
// In jurisdictions that recognize copyright laws, the author or authors
10+
// of this software dedicate any and all copyright interest in the
11+
// software to the public domain. We make this dedication for the benefit
12+
// of the public at large and to the detriment of our heirs and
13+
// successors. We intend this dedication to be an overt act of
14+
// relinquishment in perpetuity of all present and future rights to this
15+
// software under copyright law.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20+
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21+
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
// OTHER DEALINGS IN THE SOFTWARE.
24+
//
25+
// For more information, please refer to <http://unlicense.org>
26+
// ***************************************************************************
27+
28+
using System;
29+
using JetBrains.Annotations;
30+
using StateMachine.Events;
31+
32+
namespace MonoGameStateMachine.Api
33+
{
34+
[PublicAPI]
35+
public interface GlobalTransitionFluent<TS, TT, TD>
36+
{
37+
/// <summary>
38+
/// Specifies the trigger, that has to be served as input in order to walk the global transition you're currently
39+
/// describing.
40+
/// </summary>
41+
/// <param name="trigger">The trigger.</param>
42+
GlobalTransitionBuilderFluent<TS, TT, TD> OnGlobal(TT trigger);
43+
44+
/// <summary>
45+
/// Specifies the condition, that has to be met, in addition to the trigger, to walk the global transition you're
46+
/// currently describing.
47+
/// </summary>
48+
/// <param name="condition">The condition.</param>
49+
GlobalTransitionBuilderFluent<TS, TT, TD> IfGlobal(Func<IfArgs<TS>, bool> condition);
50+
51+
/// <summary>
52+
/// Automatically walks the transition you're currently describing, if the specified amount of time has passed.
53+
/// </summary>
54+
/// <param name="amount">The amount.</param>
55+
/// <param name="timeUnit">The time unit.</param>
56+
/// <returns></returns>
57+
TransitionStateFluent<TS, TT, TD> AfterGlobal(float amount, TimeUnit timeUnit);
58+
}
59+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// ***************************************************************************
2+
// This is free and unencumbered software released into the public domain.
3+
//
4+
// Anyone is free to copy, modify, publish, use, compile, sell, or
5+
// distribute this software, either in source code form or as a compiled
6+
// binary, for any purpose, commercial or non-commercial, and by any
7+
// means.
8+
//
9+
// In jurisdictions that recognize copyright laws, the author or authors
10+
// of this software dedicate any and all copyright interest in the
11+
// software to the public domain. We make this dedication for the benefit
12+
// of the public at large and to the detriment of our heirs and
13+
// successors. We intend this dedication to be an overt act of
14+
// relinquishment in perpetuity of all present and future rights to this
15+
// software under copyright law.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20+
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21+
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
// OTHER DEALINGS IN THE SOFTWARE.
24+
//
25+
// For more information, please refer to <http://unlicense.org>
26+
// ***************************************************************************
27+
28+
using System;
29+
using JetBrains.Annotations;
30+
using StateMachine.Events;
31+
32+
namespace MonoGameStateMachine.Api
33+
{
34+
[PublicAPI]
35+
public interface StateFluent<TS, TT, TD> : BuilderFluent<TS, TT, TD>
36+
{
37+
/// <summary>
38+
/// Adds a new transition to the state, you currently describe.
39+
/// </summary>
40+
/// <param name="state">The state the transition will lead to.</param>
41+
TransitionFluent<TS, TT, TD> TransitionTo(TS state);
42+
43+
/// <summary>
44+
/// Adding a transition that, being triggered, will result in the last state on the stack being popped and set to be
45+
/// the current one.<br />
46+
/// For this to work 'EnableStack' has to be set (this machine has to be a Stack-Based-FSM (SBFSM)).
47+
/// </summary>
48+
TransitionFluent<TS, TT, TD> PopTransition();
49+
50+
/// <summary>
51+
/// Called when the state, you currently describe, is entered.
52+
/// </summary>
53+
/// <param name="stateChangeArgs">The state change arguments.</param>
54+
StateFluent<TS, TT, TD> OnEnter(Action<StateChangeArgs<TS, TT, TD>> stateChangeArgs);
55+
56+
/// <summary>
57+
/// Called when the state, you currently describe, is exited.
58+
/// </summary>
59+
/// <param name="stateChangeArgs">The state change arguments.</param>
60+
StateFluent<TS, TT, TD> OnExit(Action<StateChangeArgs<TS, TT, TD>> stateChangeArgs);
61+
62+
/// <summary>
63+
/// Called when the FSM's 'Update(TData)' method is called and the state, you currently describe, is active.
64+
/// </summary>
65+
/// <param name="updateArgs">The update arguments.</param>
66+
StateFluent<TS, TT, TD> Update(Action<UpdateArgs<TS, TT, TD>> updateArgs);
67+
68+
/// <summary>
69+
/// Clears the stack when the state, you currently describe, is entered.
70+
/// </summary>
71+
StateFluent<TS, TT, TD> ClearsStack();
72+
}
73+
}

0 commit comments

Comments
 (0)