Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <boost/msm/back/state_machine.hpp>
- #include <boost/msm/front/state_machine_def.hpp>
- #include <boost/msm/front/functor_row.hpp>
- #include <boost/msm/front/euml/common.hpp>
- #include <boost/msm/front/euml/operator.hpp>
- using namespace std;
- namespace msm = boost::msm;
- namespace mpl = boost::mpl;
- using namespace msm::front;
- using namespace msm::front::euml;
- //----------------------------------------------------------------
- // Events
- //----------------------------------------------------------------
- struct next_animation {};
- struct start_game {};
- struct host_user {};
- struct character_selected
- {
- character_selected( int element )
- : element_(element)
- {}
- const int element() const
- {
- return element_;
- }
- int element_;
- };
- struct play_again {};
- struct back_to_menu {};
- struct exit_introduction {};
- //----------------------------------------------------------------
- // Events (END)
- //----------------------------------------------------------------
- struct Holder
- {
- struct state_machine_ : public msm::front::state_machine_def<state_machine_>
- {
- Holder& holder_;
- state_machine_( Holder& holder )
- : holder_(holder)
- {}
- Holder& holder()
- {
- return holder_;
- }
- struct intro_state_machine_ : public msm::front::state_machine_def<intro_state_machine_>
- {
- Holder* holder_;
- intro_state_machine_()
- : holder_( nullptr )
- {
- }
- intro_state_machine_( Holder& holder )
- : holder_( boost::addressof(holder) )
- {}
- Holder& holder()
- {
- return *holder_;
- }
- struct LogoAnimation : public msm::front::state<>
- {
- template <typename Event, typename FSM>
- void on_entry(Event const&, FSM& fsm )
- {
- std::cout << "entering: LogoAnimation" << std::endl;
- }
- };
- struct WaitingAnimation : public msm::front::state<>
- {
- template <typename Event, typename FSM>
- void on_entry(Event const&, FSM& fsm )
- {
- std::cout << "entering: WaitingAnimation" << std::endl;
- }
- };
- struct SelectBackgroundCheckUser : public msm::front::state<>, public msm::front::explicit_entry<0>
- {
- template <typename Event, typename FSM>
- void on_entry(Event const&, FSM& fsm )
- {
- std::cout << "entering: SelectBackgroundCheckUser" << std::endl;
- }
- };
- struct EndIntroduction : public msm::front::state<>
- {
- template <typename Event, typename FSM>
- void on_entry(Event const& ,FSM& fsm)
- {
- std::cout << "entering: EndIntroduction" << std::endl;
- fsm.holder().sm().process_event( exit_introduction() );
- }
- };
- typedef LogoAnimation initial_state;
- struct transition_table : mpl::vector<
- Row < LogoAnimation, next_animation, WaitingAnimation, none, none >
- , Row < WaitingAnimation, next_animation, SelectBackgroundCheckUser, none, none >
- , Row < SelectBackgroundCheckUser, host_user, EndIntroduction, none, none >
- > {};
- }; // Introduction StateMachine END
- struct game_state_machine_ : public msm::front::state_machine_def<game_state_machine_>
- {
- struct Initial : public msm::front::state<>
- {
- template <typename Event, typename FSM>
- void on_entry(Event const&, FSM& fsm )
- {
- std::cout << "entering: Initial" << std::endl;
- }
- };
- struct StaringGame : public msm::front::state<>
- {
- template <typename Event, typename FSM>
- void on_entry(Event const& event, FSM& fsm )
- {
- std::cout << "entering: StaringGame" << std::endl;
- std::cout << "event.element(): " << event.element() << std::endl;
- std::cout << "event typeid: " << typeid(event).name() << std::endl;
- }
- };
- struct StartTurn: public msm::front::state<>
- {
- template <typename Event, typename FSM>
- void on_entry(Event const& event, FSM& fsm )
- {
- std::cout << "entering: StartTurn" << std::endl;
- std::cout << "event.element(): " << event.element() << std::endl;
- std::cout << "event typeid: " << typeid(event).name() << std::endl;
- }
- };
- typedef Initial initial_state;
- struct transition_table : mpl::vector<
- Row < Initial, character_selected, StaringGame, none, none >
- , Row < StaringGame, character_selected, StartTurn, none, none >
- > {};
- };
- typedef msm::back::state_machine<intro_state_machine_> IntroSM;
- typedef msm::back::state_machine<game_state_machine_> GameSM;
- struct Initial : public msm::front::state<>
- {
- template <typename Event, typename FSM>
- void on_entry(Event const&, FSM& fsm )
- {
- std::cout << "entering: Initial" << std::endl;
- }
- };
- struct End : public msm::front::state<>
- {
- template <typename Event, typename FSM>
- void on_entry(Event const& ,FSM&)
- {
- std::cout << "entering: End" << std::endl;
- }
- };
- typedef Initial initial_state;
- struct transition_table : mpl::vector<
- Row < Initial, next_animation, IntroSM, none, none >
- , Row < IntroSM, exit_introduction, GameSM, none, none >
- // , Row < GameSM, back_to_menu, IntroSM::direct<intro_state_machine_::SelectBackgroundCheckUser>, none, none >
- > {};
- };
- typedef msm::back::state_machine<state_machine_> StateMachine;
- Holder()
- : sm_( boost::msm::back::states_ << state_machine_::IntroSM(boost::ref(*this))
- , boost::ref(*this) )
- {}
- StateMachine sm_;
- StateMachine& sm()
- {
- return sm_;
- }
- };
- void test()
- {
- Holder holder;
- typename Holder::StateMachine& p = holder.sm();
- p.start();
- p.process_event( next_animation() );
- p.process_event( next_animation() );
- p.process_event( next_animation() );
- p.process_event( host_user() );
- // p.process_event( exit_introduction() ); // is called inside EndIntroduction state
- p.process_event( character_selected(97) );
- p.process_event( character_selected(98) );
- p.stop();
- }
- int main( /*int argc, char *argv[] */ )
- {
- test();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement