View previous topic :: View next topic |
Author |
Message |
clayasaurus
Joined: 21 May 2004 Posts: 857
|
Posted: Tue Apr 03, 2007 10:52 am Post subject: Abstract State Machine... |
|
|
What do you think? This is just a 'first draft', I think it might work, but havn't tried it.
Code: |
////////////////////////////////
/// Generalized state machine //
////////////////////////////////
/// State machine class that is made up of a bunch of states
class StateMachine
{
private:
State[] states;
uint currState;
public:
/// Add a state to the state machine
void add(uint num, delegate userfn)
{
states.length = states.length+1;
states[states.length-1] = new State(num, userfn);
}
/// initialize current state
void initState(uint num)
{
currState = num;
}
/// run the state machine
void process()
{
// call the code on the current state, while determining what the next state is
currState = states[currState].fn();
}
}
/// A single state in the state machine
class State
{
uint stateNum;
// this user defined code will determine which state to go to next by returning the state to 'goto' next
uint delegate/slot fn();
}
// list of different states
enum
{
A=0, B, C, D, E, F
}
class StateManager
{
uint A(){}
uint B(){}
etc...
}
StateMachine mach;
StateManager code;
// the user function will determine to stay on current state, or move to the next state
mach.add(A, code.A());
mach.add(B, code.B());
mach.add(C, ...);
mach.add(D, ...);
mach.add(E, ...);
mach.add(F, ...);
// initialize the state to state A
mach.initState(A);
// process the user code in the given state and run the user code plugged into it
mach.process();
|
StateMachine holds an array of different states. Each state has a number 'NAME' best used with enums, and user code it can run to determine what is the next state should be, while running the code for the current state.
User code will probably be plugged in with delegates.
Missing anything? |
|
Back to top |
|
|
davidb
Joined: 03 Apr 2007 Posts: 15
|
Posted: Wed Apr 04, 2007 4:11 pm Post subject: |
|
|
Hi clayasaurus,
just a short question for me to understand the concept:
How can code.A() decide on a new state?
Is the fact that code.A() becomes a part of mach
(as a delegate or whatever) permitting code.A()
access to private: State[] states?
Or are all the states absolutely fixed and hardcoded
in the methods of the class StateManager?
david |
|
Back to top |
|
|
clayasaurus
Joined: 21 May 2004 Posts: 857
|
Posted: Wed Apr 04, 2007 7:15 pm Post subject: |
|
|
Bear with me, please, because I'm not even sure if this 'state' machine is the type Christian was talking about... but..
The states would be decided upon by the user with enums that start at zero like
Code: |
enum
{
USER_STATE_1 = 0,
USER_STATE_2,
USER_STATE_3
}
|
Then the user would code their class
Code: |
class StateManager
{
int processStateOne()
{
if (stay in this state)
return USER_STATE_1;
if (go to state 2)
return USER_STATE_2;
if (go to state 3)
return USER_STATE_3;
}
int processStateTwo()
{
// etc...
}
}
|
An then, would set up the states like...
Code: |
StateMachine sm;
StateManager m;
sm.add(USER_STATE_1, m.processStateOne)
sm.add(USER_STATE_2, m.processStateTwo)
|
the process state functions will be constantly returning the current state, whether they decide to stay in the same state, or go to any other state. The process function for state machine will always be processing the code in the current state.
This concept is so simple, I am wondering whether it even deserves its own class, or if I'm missing something. I'm sure I could code up a simple vending machine example if it is still unclear how this works.
~ Clay |
|
Back to top |
|
|
ChristianK
Joined: 26 Sep 2006 Posts: 159 Location: Berlin, Germany
|
Posted: Thu Apr 05, 2007 12:11 pm Post subject: |
|
|
What I wanted to abstract away is basically the input and animation of the player in asteroids:
Still animation
- turning, shooting
- to Moving
Moving animation
- turning, shooting
- to Still
Exploding animation
- to Dead after animation is done
Dead animation
But even though your design looks okay, I can't come up with something that doesn't actually make the code more complicated. Maybe it wasn't such a good idea? |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|