Generic FSM for game in C++? [closed]
Want to improve this question? Update the question so it focuses on one 开发者_如何学JAVAproblem only by editing this post.
Closed 4 years ago.
Improve this questionI was wondering if there's a way I could code some kind of "generic" FSM for a game with C++?.
My game has a component oriented design, so I use a FSM Component.
my Finite State Machine (FSM) Component, looks more or less this way.
class gecFSM : public gecBehaviour
{
public:
//Constructors
gecFSM() { state = kEntityState_walk; }
gecFSM(kEntityState s) { state = s; }
//Interface
void setRule(kEntityState initialState, int inputAction, kEntityState resultingState);
void performAction(int action);
private:
kEntityState fsmTable[MAX_STATES][MAX_ACTIONS];
};
I would love to hear your opinions/ideas or suggestions about how to make this FSM component, generic. With generic I mean:
1) Creating the fsmTable from an xml file is really easy, I mean it's just a bunch of integers that can be loaded to create an MAX_STATESxMAX_ACTION matrix.
void gecFSM::setRule(kEntityState initialState, int inputAction, kEntityState resultingState)
{
fsmTable[initialState][inputAction] = resultingState;
}
2) But what about the "perform Action" method ?
void gecFSM::performAction(int action)
{
switch( smTable[ ownerEntity->getState() ][ action ] )
{
case WALK:
/*Walk action*/
break;
case STAND:
/*Stand action*/
break;
case JUMP:
/*Jump action*/
break;
case RUN:
/*Run action*/
break;
}
}
What if I wanted to make the "actions" and the switch generic?
This in order to avoid creating a different FSM component class for each GameEntity
? (gecFSMZombie
, gecFSMDryad
, gecFSMBoss
etc ?). That would allow me to go "Data Driven" and construct my generic FSM's from files for example.
What do you people suggest?
Have a look at the Boost Statechart Library. (formerly known as boost::fsm) It comes with a very nice photo camera example.
精彩评论