So, I'm building a random platformer game using SFML for my first real graphical game, and I'm somewhat unsure of how to deal with input to the player and how that interacts with the player character's state.
Currently, I have an abstract State class that contains an Animation (basically some sprites with looping properties, but that's not really relevant). It has two virtual functions in it:
AdvanceTime() which takes an unsigned int of the number of ms that have passed, which basically updates which frame of animation it needs to be in.
HandleInput() which takes an sf::Input and basically changes the state (or not) depending on what buttons are pushed (i.e. pressing left or right when in the IDLE state should put you in the WALKING state)
However, there are two things that make this odd:
1) This means that it would be difficult to change the player, since the State class doesn't have direct access to the Player class
2) I have to make a bunch of derived classes for each State the player could be in (possibly duplicating stuff if some States are similar but slightly different, like RUNNING and WALKING)
Should I just keep going this way or is there some better way? I'm not above redesigning the whole system, so if that's what it takes to make it simpler, go ahead and suggest it.
Maybe a more event-based design? Your main loop could have access to a vector of event handlers, then at every frame it could iterate over them and send them events. The PC object's event handler could look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
void PC::handle_event(Event &e){
//(Not necessarily legal. I can never remember the syntax for member function pointers.)
//void (*PC::current_state)(Event &);
this->current_state(e);
}
void PC::idle_handle_event(Event &e){
if (e.is_key_down()){
//do some other stuff
this->current_state=&PC::moving_handle_event;
}
//...
}