Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualkd7tck

Posted 11 November 2012 - 03:36 PM

Make a tree of all possible combinations, map each tree node to a state. The tree paths are state stransitions triggered by an input event like pressing a key.
You start in a nuetral game state, when you get to the door trigger text enter state. In text_enter state loop around for text. When enter key is pressed tranisition to text check state. In text check state examine the text for equality to pre defined strings, if equality is found then transition to activated state. If equality is not found in text check state then transition back to neutral games state.


Here is a roughed out system done in sfml. Incomplete and buggy, but it should get accross the idea of what I mean.
Examples are in the code, so reverse engineering it shouldn't be too hard. The State_manager.hpp should have the examples for using this system.

state.hpp
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2012 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
//	you must not claim that you wrote the original software.
//	If you use this software in a product, an acknowledgment
//	in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
//	and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_STATE_HPP
#define SFML_STATE_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
#include "Export.hpp"
#include <iostream>
#include <string>
#include <map>

namespace sf
{
class State_Event;
class State_Manager;
////////////////////////////////////////////////////////////
/// \brief The Util State Abstract class.
///
////////////////////////////////////////////////////////////
class SFML_UTIL_API State
{
public :
	////////////////////////////////////////////////////////////
	/// \brief constructor
	///
	/// \param string name
	///
	////////////////////////////////////////////////////////////
	State(std::string name);
	////////////////////////////////////////////////////////////
	/// \brief destructor
	///
	////////////////////////////////////////////////////////////
	virtual ~State(){
		if(manager != 0)
			manager->unRegisterState(this);
		if(registered_events.size() > 0){
			it = registered_events.begin();
			(*it)->unRegisterState(this);
		}
	}


	//Virtual functions for assigning user desired functionality
	////////////////////////////////////////////////////////////
	/// \brief second thing to be called every game cycle
	///
	/// \param double current time in milliseconds
	///
	/// \return 0 if successfull
	////////////////////////////////////////////////////////////
	virtual int Update (const double time) = 0;
	////////////////////////////////////////////////////////////
	/// \brief first thing to be called every game cycle
	///
	/// \return 0 if successfull
	////////////////////////////////////////////////////////////
	virtual int Events () = 0;
	////////////////////////////////////////////////////////////
	/// \brief third thing to be called every game cycle
	///
	/// \param Image, canvas to draw on
	///
	/// \return 0 if successfull
	////////////////////////////////////////////////////////////
	virtual int Draw (Image& canvas) = 0;
	////////////////////////////////////////////////////////////
	/// \brief called once when state is done
	///
	/// \return 0 if successfull
	////////////////////////////////////////////////////////////
	virtual int CleanUp () = 0;
	////////////////////////////////////////////////////////////
	/// \brief called once when state is entered
	///
	/// \return 0 if successfull
	////////////////////////////////////////////////////////////
	virtual int Init () = 0;




	////////////////////////////////////////////////////////////
	/// \brief an event was triggered
	///
	/// \param int ID of event
	///
	/// \return bool true on success
	////////////////////////////////////////////////////////////
	bool trigger(int event_id);
	////////////////////////////////////////////////////////////
	/// \brief change state name
	///
	/// \param string new name
	///
	/// \return true if successful
	////////////////////////////////////////////////////////////
	bool setName(std::string name);
	////////////////////////////////////////////////////////////
	/// \brief get state name
	///
	/// \return string name
	////////////////////////////////////////////////////////////
	std::string getName();
	////////////////////////////////////////////////////////////
	/// \brief get next state name
	///
	/// \return string name, "" if none
	////////////////////////////////////////////////////////////
	std::string nextState();
	////////////////////////////////////////////////////////////
	/// \brief change DefaultNextStateName
	///
	/// \param string new name
	///
	/// \return true if successful
	////////////////////////////////////////////////////////////
	bool setDefaultNextStateName(std::string name);
	////////////////////////////////////////////////////////////
	/// \brief check if state is done, then transition to next state
	///
	/// \return bool ,this is the triggered variable
	////////////////////////////////////////////////////////////
	bool stateDone();

	////////////////////////////////////////////////////////////
	/// \brief register a state_event with this state
	///
	/// \param State_Event*
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void registerEvent(State_Event*);

	////////////////////////////////////////////////////////////
	/// \brief unregister a state_event from this state
	/// un-registers StateChange and removes event from registered_events
	/// \param State_Event*
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void unRegisterEvent(State_Event*);

	////////////////////////////////////////////////////////////
	/// \brief register a state transition from this state node
	///
	/// \param int event id
	///
	/// \param string name of next state
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void registerStateChange(int event_id, std::string next_state_name);

	////////////////////////////////////////////////////////////
	/// \brief unregister a state transition from this state node
	///
	/// \param int event id
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void unRegisterStateChange(int event_id);
	////////////////////////////////////////////////////////////
	/// \brief set a manager for this state
	///
	/// \param State_Manager*
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void setStateManager(State_Manager*);
	////////////////////////////////////////////////////////////
	/// \brief Change Active status, active status meaning is this state running?
	///
	/// \param bool, active status true or false, the isActive variable
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void setActiveStatus(bool);
	////////////////////////////////////////////////////////////
	/// \brief Get the Active status, active status meaning is this state running?
	///
	/// \return bool, the isActive variable
	////////////////////////////////////////////////////////////
	bool getActiveStatus();
	////////////////////////////////////////////////////////////
	/// \brief reset the trigger by setting it to false
	////////////////////////////////////////////////////////////
	void resetTrigger();
protected :
	//state identifier
	std::string state_name;
	std::vector <State_Event*> registered_events;
	std::vector <State_Event*>::iterator it;
	//state to state mappings based on event_id
	std::map <int, std::string> eventIdStateTrigger;
	//the next state to run after this state ends
	std::string next_state_name;
	//if next_state_name is no longer valid
	std::string default_next_state_name;
	//if triggered then state manager will transition to next state, can only be triggered if state is active
	bool triggered;
	//if this state is currently cycling with state manager
	bool isActive;
	//pointer to the state_manager for this state
	State_Manager* manager;
};


////////////////////////////////////////////////////////////
/// \brief A NULL State.
///
////////////////////////////////////////////////////////////
class SFML_UTIL_API NULLState : public State
{
public:
	NULLState() : State("")
	{
		;
	}
	~NULLState()
	{
		;
	}
	int Update (const double time)
	{
		return 0;
	}
	int Events ()
	{
		return 0;
	}
	int Draw (Image& canvas)
	{
		return 0;
	}
	int CleanUp ()
	{
		return 0;
	}
	int Init ()
	{
		return 0;
	}
protected:
	;
};
} // namespace sf

#endif // SFML_STATE_HPP

////////////////////////////////////////////////////////////
/// \class sf::State
/// \ingroup util
///
/// sf::State represents a single state within a finite state machine. This is an abstract class.
///
/// Example:
/// \code
///
///class IntroState : public sf::State
///{
///public:
///	std::string name;
///
///	IntroState(std::string n) : sf::State(n)
///	{
///		name = n;
///	}
///
///	~IntroState()
///	{
///		;
///	}
///
///	int Update (const double time)
///	{
///		std::cout << "Intro_Update_" << name << std::endl;
///		return 0;
///	}
///
///	int Events ()
///	{
///		std::cout << "Intro_Events_" << name << std::endl;
///		return 0;
///	}
///
///	int Draw (sf::Image& canvas)
///	{
///		std::cout << "Intro_Draw_" << name << std::endl;
///		return 0;
///	}
///
///	int CleanUp ()
///	{
///		std::cout << "Intro_Cleanup_" << name << std::endl;
///		return 0;
///	}
///
///	int Init ()
///	{
///		std::cout << "Intro_Init_" << name << std::endl;
///		return 0;
///	}
///};
///
/// \endcode
///
/// \see sf::State
///
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
/// \class sf::NULLState
/// \ingroup util
///
/// sf::NULLState represents a single state within no real functionality.
///
/// Example:
/// \code
///
/// \endcode
///
/// \see sf::NULLState
///
////////////////////////////////////////////////////////////






state_event.hpp
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2012 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
//	you must not claim that you wrote the original software.
//	If you use this software in a product, an acknowledgment
//	in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
//	and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_STATE_EVENT_HPP
#define SFML_STATE_EVENT_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include "Export.hpp"
#include <iostream>
#include <string>
#include <vector>

namespace sf
{
class State;
////////////////////////////////////////////////////////////
/// \brief The Util State_Event class.
///
////////////////////////////////////////////////////////////
class SFML_UTIL_API State_Event
{
public :
	////////////////////////////////////////////////////////////
	/// \brief constructor
	///
	/// \param int id
	///
	////////////////////////////////////////////////////////////
	State_Event(int id);

	////////////////////////////////////////////////////////////
	/// \brief destructor
	///
	////////////////////////////////////////////////////////////
	~State_Event();

	////////////////////////////////////////////////////////////
	/// \brief trigger this event, and trigger all registered states
	///
	/// \return bool true on success
	////////////////////////////////////////////////////////////
	bool trigger();

	////////////////////////////////////////////////////////////
	/// \brief obtain this event's ID
	///
	/// \return int Event ID
	////////////////////////////////////////////////////////////
	int getId();
	////////////////////////////////////////////////////////////
	/// \brief change this event's ID
	///
	/// \param int id
	///
	////////////////////////////////////////////////////////////
	void setId(int id);

	////////////////////////////////////////////////////////////
	/// \brief register a state with this event
	///
	/// \param State*
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void registerState(State*);

	////////////////////////////////////////////////////////////
	/// \brief unregister a state from this event
	///
	/// \param State*
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void unRegisterState(State*);
protected :
	int event_id;
	static std::vector<State*> registered_states;
	std::vector<State*>::iterator it;
};
} // namespace sf

#endif // SFML_STATE_EVENT_HPP

////////////////////////////////////////////////////////////
/// \class sf::State_Event
/// \ingroup util
///
/// sf::State_Event represents an event that can register with multiple states.
///
/// Example:
/// \code
/// //create new state_event and register it to a State
/// sf::State_Event* enter_key_press_event = new sf::State_Event(0);
/// state_pntr->registerEvent(enter_key_press_event);
///
/// //map event to other state
/// state_pntr->registerStateChange(enter_key_press_event->getId(), state_pntr_2->getName());
///
/// //trigger an event, activates the mapping made earlier
/// enter_key_press_event->trigger();
/// \endcode
///
/// \see sf::State_Event
///
////////////////////////////////////////////////////////////







state_manager.hpp
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2012 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
//	you must not claim that you wrote the original software.
//	If you use this software in a product, an acknowledgment
//	in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
//	and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_STATE_MANAGER_HPP
#define SFML_STATE_MANAGER_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include "Export.hpp"
#include <iostream>
#include <string>
#include <map>
#include <vector>

namespace sf
{

class State;
class Image;
class Clock;
class Time;
class NULLState;
////////////////////////////////////////////////////////////
/// \brief The Util State_Manager class.
///
////////////////////////////////////////////////////////////
class SFML_UTIL_API State_Manager
{
public :
	////////////////////////////////////////////////////////////
	/// \brief constructor
	///
	////////////////////////////////////////////////////////////
	State_Manager();
	////////////////////////////////////////////////////////////
	/// \brief destructor
	///
	////////////////////////////////////////////////////////////
	~State_Manager();
	////////////////////////////////////////////////////////////
	/// \brief register a state with this state manager
	///
	/// \param State*
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void registerState(State*);
	////////////////////////////////////////////////////////////
	/// \brief unregister a state with this state manager
	///
	/// \param State*
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void unRegisterState(State*);
	////////////////////////////////////////////////////////////
	/// \brief cycle one itteration, checks all states and runs them for one pass
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void Cycle();
	////////////////////////////////////////////////////////////
	/// \brief ends state machine, and cleans everyhting up
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void Halt();
	////////////////////////////////////////////////////////////
	/// \brief set current state
	///
	/// \param State*
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void setCurrentState(State*);

	////////////////////////////////////////////////////////////
	/// \brief get current state
	///
	/// \return State*
	////////////////////////////////////////////////////////////
	State* getCurrentState();

protected :
	std::vector<State*> states;
	std::string currentState;
	std::string nextState;
	std::string previousState;
	std::vector<State*>::iterator it;
	State* baseState;
	Image* backBuffer;
	Clock* clock;
	Time* time;
};
} // namespace sf

#endif // SFML_STATE_MANAGER_HPP

////////////////////////////////////////////////////////////
/// \class sf::State_Manager
/// \ingroup util
///
/// sf::State_Manager manages a collection of states
///
/// Example:
/// \code
///
///class IntroState : public sf::State
///{
///public:
///	std::string name;
///
///	IntroState(std::string n) : sf::State(n)
///	{
///		name = n;
///	}
///
///	~IntroState()
///	{
///		;
///	}
///
///	int Update (const double time)
///	{
///		std::cout << "Intro_Update_" << name << std::endl;
///		return 0;
///	}
///
///	int Events ()
///	{
///		std::cout << "Intro_Events_" << name << std::endl;
///		return 0;
///	}
///
///	int Draw (sf::Image& canvas)
///	{
///		std::cout << "Intro_Draw_" << name << std::endl;
///		return 0;
///	}
///
///	int CleanUp ()
///	{
///		std::cout << "Intro_Cleanup_" << name << std::endl;
///		return 0;
///	}
///
///	int Init ()
///	{
///		std::cout << "Intro_Init_" << name << std::endl;
///		return 0;
///	}
///};
///
///
///
///int main() {
///
///	sf::State* sp;
///	sf::State* sp2;
///
///
///	//create two new classes from abstract type State
///	IntroState *sptr = new IntroState("Intro");
///	IntroState *nsptr = new IntroState("Level1");
///
///
///	//create event trigger
///	sf::State_Event* enter_key_press_event = new sf::State_Event(0);
///
///
///	//access Parent attributes by using State pointer
///	sp = sptr;
///	sp2 = nsptr;
///
///	sp->registerEvent(enter_key_press_event);
///	sp->registerStateChange(enter_key_press_event->getId(), sp2->getName());
///
///	//assign states to state manager
///	sf::State_Manager* sm = new sf::State_Manager();
///	sm->registerState(sp);
///	sm->registerState(sp2);
///	sm->setCurrentState(sp);
///
///
///	while(true)
///	{
///	 //detect action for trigger
///	 if(sf::Keyboard::isKeyPressed(sf::Keyboard::LControl))
///	 {
///	  enter_key_press_event->trigger();
///	 }
///
///	 sm->Cycle();//state cycle
///
///
///	 //exit game loop
///	 if(sm->getCurrentState()->getName() == nsptr->getName())
///	 {
///	  sm->Halt();
///	  break;
///	 }
///	}
///
///	//clean up
///	sptr->unRegisterEvent(enter_key_press_event);
///	enter_key_press_event->unRegisterState(sptr);
///	delete enter_key_press_event;
///	delete sptr;
///	delete nsptr;
///	delete sm;
///	return 0;
///}
///
/// \endcode
///
/// \see sf::State_Manager
///
////////////////////////////////////////////////////////////

#2kd7tck

Posted 11 November 2012 - 03:14 PM

Make a tree of all possible combinations, map each tree node to a state. The tree paths are state stransitions triggered by an input event like pressing a key.


Here is a roughed out system done in sfml. Incomplete and buggy, but it should get accross the idea of what I mean.
Examples are in the code, so reverse engineering it shouldn't be too hard. The State_manager.hpp should have the examples for using this system.

state.hpp
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2012 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
//	you must not claim that you wrote the original software.
//	If you use this software in a product, an acknowledgment
//	in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
//	and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_STATE_HPP
#define SFML_STATE_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
#include "Export.hpp"
#include <iostream>
#include <string>
#include <map>

namespace sf
{
class State_Event;
class State_Manager;
////////////////////////////////////////////////////////////
/// \brief The Util State Abstract class.
///
////////////////////////////////////////////////////////////
class SFML_UTIL_API State
{
public :
	////////////////////////////////////////////////////////////
	/// \brief constructor
	///
	/// \param string name
	///
	////////////////////////////////////////////////////////////
	State(std::string name);
	////////////////////////////////////////////////////////////
	/// \brief destructor
	///
	////////////////////////////////////////////////////////////
	virtual ~State(){
		if(manager != 0)
			manager->unRegisterState(this);
		if(registered_events.size() > 0){
			it = registered_events.begin();
			(*it)->unRegisterState(this);
		}
	}


	//Virtual functions for assigning user desired functionality
	////////////////////////////////////////////////////////////
	/// \brief second thing to be called every game cycle
	///
	/// \param double current time in milliseconds
	///
	/// \return 0 if successfull
	////////////////////////////////////////////////////////////
	virtual int Update (const double time) = 0;
	////////////////////////////////////////////////////////////
	/// \brief first thing to be called every game cycle
	///
	/// \return 0 if successfull
	////////////////////////////////////////////////////////////
	virtual int Events () = 0;
	////////////////////////////////////////////////////////////
	/// \brief third thing to be called every game cycle
	///
	/// \param Image, canvas to draw on
	///
	/// \return 0 if successfull
	////////////////////////////////////////////////////////////
	virtual int Draw (Image& canvas) = 0;
	////////////////////////////////////////////////////////////
	/// \brief called once when state is done
	///
	/// \return 0 if successfull
	////////////////////////////////////////////////////////////
	virtual int CleanUp () = 0;
	////////////////////////////////////////////////////////////
	/// \brief called once when state is entered
	///
	/// \return 0 if successfull
	////////////////////////////////////////////////////////////
	virtual int Init () = 0;




	////////////////////////////////////////////////////////////
	/// \brief an event was triggered
	///
	/// \param int ID of event
	///
	/// \return bool true on success
	////////////////////////////////////////////////////////////
	bool trigger(int event_id);
	////////////////////////////////////////////////////////////
	/// \brief change state name
	///
	/// \param string new name
	///
	/// \return true if successful
	////////////////////////////////////////////////////////////
	bool setName(std::string name);
	////////////////////////////////////////////////////////////
	/// \brief get state name
	///
	/// \return string name
	////////////////////////////////////////////////////////////
	std::string getName();
	////////////////////////////////////////////////////////////
	/// \brief get next state name
	///
	/// \return string name, "" if none
	////////////////////////////////////////////////////////////
	std::string nextState();
	////////////////////////////////////////////////////////////
	/// \brief change DefaultNextStateName
	///
	/// \param string new name
	///
	/// \return true if successful
	////////////////////////////////////////////////////////////
	bool setDefaultNextStateName(std::string name);
	////////////////////////////////////////////////////////////
	/// \brief check if state is done, then transition to next state
	///
	/// \return bool ,this is the triggered variable
	////////////////////////////////////////////////////////////
	bool stateDone();

	////////////////////////////////////////////////////////////
	/// \brief register a state_event with this state
	///
	/// \param State_Event*
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void registerEvent(State_Event*);

	////////////////////////////////////////////////////////////
	/// \brief unregister a state_event from this state
	/// un-registers StateChange and removes event from registered_events
	/// \param State_Event*
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void unRegisterEvent(State_Event*);

	////////////////////////////////////////////////////////////
	/// \brief register a state transition from this state node
	///
	/// \param int event id
	///
	/// \param string name of next state
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void registerStateChange(int event_id, std::string next_state_name);

	////////////////////////////////////////////////////////////
	/// \brief unregister a state transition from this state node
	///
	/// \param int event id
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void unRegisterStateChange(int event_id);
	////////////////////////////////////////////////////////////
	/// \brief set a manager for this state
	///
	/// \param State_Manager*
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void setStateManager(State_Manager*);
	////////////////////////////////////////////////////////////
	/// \brief Change Active status, active status meaning is this state running?
	///
	/// \param bool, active status true or false, the isActive variable
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void setActiveStatus(bool);
	////////////////////////////////////////////////////////////
	/// \brief Get the Active status, active status meaning is this state running?
	///
	/// \return bool, the isActive variable
	////////////////////////////////////////////////////////////
	bool getActiveStatus();
	////////////////////////////////////////////////////////////
	/// \brief reset the trigger by setting it to false
	////////////////////////////////////////////////////////////
	void resetTrigger();
protected :
	//state identifier
	std::string state_name;
	std::vector <State_Event*> registered_events;
	std::vector <State_Event*>::iterator it;
	//state to state mappings based on event_id
	std::map <int, std::string> eventIdStateTrigger;
	//the next state to run after this state ends
	std::string next_state_name;
	//if next_state_name is no longer valid
	std::string default_next_state_name;
	//if triggered then state manager will transition to next state, can only be triggered if state is active
	bool triggered;
	//if this state is currently cycling with state manager
	bool isActive;
	//pointer to the state_manager for this state
	State_Manager* manager;
};


////////////////////////////////////////////////////////////
/// \brief A NULL State.
///
////////////////////////////////////////////////////////////
class SFML_UTIL_API NULLState : public State
{
public:
	NULLState() : State("")
	{
		;
	}
	~NULLState()
	{
		;
	}
	int Update (const double time)
	{
		return 0;
	}
	int Events ()
	{
		return 0;
	}
	int Draw (Image& canvas)
	{
		return 0;
	}
	int CleanUp ()
	{
		return 0;
	}
	int Init ()
	{
		return 0;
	}
protected:
	;
};
} // namespace sf

#endif // SFML_STATE_HPP

////////////////////////////////////////////////////////////
/// \class sf::State
/// \ingroup util
///
/// sf::State represents a single state within a finite state machine. This is an abstract class.
///
/// Example:
/// \code
///
///class IntroState : public sf::State
///{
///public:
///	std::string name;
///
///	IntroState(std::string n) : sf::State(n)
///	{
///		name = n;
///	}
///
///	~IntroState()
///	{
///		;
///	}
///
///	int Update (const double time)
///	{
///		std::cout << "Intro_Update_" << name << std::endl;
///		return 0;
///	}
///
///	int Events ()
///	{
///		std::cout << "Intro_Events_" << name << std::endl;
///		return 0;
///	}
///
///	int Draw (sf::Image& canvas)
///	{
///		std::cout << "Intro_Draw_" << name << std::endl;
///		return 0;
///	}
///
///	int CleanUp ()
///	{
///		std::cout << "Intro_Cleanup_" << name << std::endl;
///		return 0;
///	}
///
///	int Init ()
///	{
///		std::cout << "Intro_Init_" << name << std::endl;
///		return 0;
///	}
///};
///
/// \endcode
///
/// \see sf::State
///
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
/// \class sf::NULLState
/// \ingroup util
///
/// sf::NULLState represents a single state within no real functionality.
///
/// Example:
/// \code
///
/// \endcode
///
/// \see sf::NULLState
///
////////////////////////////////////////////////////////////






state_event.hpp
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2012 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
//	you must not claim that you wrote the original software.
//	If you use this software in a product, an acknowledgment
//	in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
//	and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_STATE_EVENT_HPP
#define SFML_STATE_EVENT_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include "Export.hpp"
#include <iostream>
#include <string>
#include <vector>

namespace sf
{
class State;
////////////////////////////////////////////////////////////
/// \brief The Util State_Event class.
///
////////////////////////////////////////////////////////////
class SFML_UTIL_API State_Event
{
public :
	////////////////////////////////////////////////////////////
	/// \brief constructor
	///
	/// \param int id
	///
	////////////////////////////////////////////////////////////
	State_Event(int id);

	////////////////////////////////////////////////////////////
	/// \brief destructor
	///
	////////////////////////////////////////////////////////////
	~State_Event();

	////////////////////////////////////////////////////////////
	/// \brief trigger this event, and trigger all registered states
	///
	/// \return bool true on success
	////////////////////////////////////////////////////////////
	bool trigger();

	////////////////////////////////////////////////////////////
	/// \brief obtain this event's ID
	///
	/// \return int Event ID
	////////////////////////////////////////////////////////////
	int getId();
	////////////////////////////////////////////////////////////
	/// \brief change this event's ID
	///
	/// \param int id
	///
	////////////////////////////////////////////////////////////
	void setId(int id);

	////////////////////////////////////////////////////////////
	/// \brief register a state with this event
	///
	/// \param State*
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void registerState(State*);

	////////////////////////////////////////////////////////////
	/// \brief unregister a state from this event
	///
	/// \param State*
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void unRegisterState(State*);
protected :
	int event_id;
	static std::vector<State*> registered_states;
	std::vector<State*>::iterator it;
};
} // namespace sf

#endif // SFML_STATE_EVENT_HPP

////////////////////////////////////////////////////////////
/// \class sf::State_Event
/// \ingroup util
///
/// sf::State_Event represents an event that can register with multiple states.
///
/// Example:
/// \code
/// //create new state_event and register it to a State
/// sf::State_Event* enter_key_press_event = new sf::State_Event(0);
/// state_pntr->registerEvent(enter_key_press_event);
///
/// //map event to other state
/// state_pntr->registerStateChange(enter_key_press_event->getId(), state_pntr_2->getName());
///
/// //trigger an event, activates the mapping made earlier
/// enter_key_press_event->trigger();
/// \endcode
///
/// \see sf::State_Event
///
////////////////////////////////////////////////////////////







state_manager.hpp
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2012 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
//	you must not claim that you wrote the original software.
//	If you use this software in a product, an acknowledgment
//	in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
//	and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_STATE_MANAGER_HPP
#define SFML_STATE_MANAGER_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include "Export.hpp"
#include <iostream>
#include <string>
#include <map>
#include <vector>

namespace sf
{

class State;
class Image;
class Clock;
class Time;
class NULLState;
////////////////////////////////////////////////////////////
/// \brief The Util State_Manager class.
///
////////////////////////////////////////////////////////////
class SFML_UTIL_API State_Manager
{
public :
	////////////////////////////////////////////////////////////
	/// \brief constructor
	///
	////////////////////////////////////////////////////////////
	State_Manager();
	////////////////////////////////////////////////////////////
	/// \brief destructor
	///
	////////////////////////////////////////////////////////////
	~State_Manager();
	////////////////////////////////////////////////////////////
	/// \brief register a state with this state manager
	///
	/// \param State*
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void registerState(State*);
	////////////////////////////////////////////////////////////
	/// \brief unregister a state with this state manager
	///
	/// \param State*
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void unRegisterState(State*);
	////////////////////////////////////////////////////////////
	/// \brief cycle one itteration, checks all states and runs them for one pass
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void Cycle();
	////////////////////////////////////////////////////////////
	/// \brief ends state machine, and cleans everyhting up
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void Halt();
	////////////////////////////////////////////////////////////
	/// \brief set current state
	///
	/// \param State*
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void setCurrentState(State*);

	////////////////////////////////////////////////////////////
	/// \brief get current state
	///
	/// \return State*
	////////////////////////////////////////////////////////////
	State* getCurrentState();

protected :
	std::vector<State*> states;
	std::string currentState;
	std::string nextState;
	std::string previousState;
	std::vector<State*>::iterator it;
	State* baseState;
	Image* backBuffer;
	Clock* clock;
	Time* time;
};
} // namespace sf

#endif // SFML_STATE_MANAGER_HPP

////////////////////////////////////////////////////////////
/// \class sf::State_Manager
/// \ingroup util
///
/// sf::State_Manager manages a collection of states
///
/// Example:
/// \code
///
///class IntroState : public sf::State
///{
///public:
///	std::string name;
///
///	IntroState(std::string n) : sf::State(n)
///	{
///		name = n;
///	}
///
///	~IntroState()
///	{
///		;
///	}
///
///	int Update (const double time)
///	{
///		std::cout << "Intro_Update_" << name << std::endl;
///		return 0;
///	}
///
///	int Events ()
///	{
///		std::cout << "Intro_Events_" << name << std::endl;
///		return 0;
///	}
///
///	int Draw (sf::Image& canvas)
///	{
///		std::cout << "Intro_Draw_" << name << std::endl;
///		return 0;
///	}
///
///	int CleanUp ()
///	{
///		std::cout << "Intro_Cleanup_" << name << std::endl;
///		return 0;
///	}
///
///	int Init ()
///	{
///		std::cout << "Intro_Init_" << name << std::endl;
///		return 0;
///	}
///};
///
///
///
///int main() {
///
///	sf::State* sp;
///	sf::State* sp2;
///
///
///	//create two new classes from abstract type State
///	IntroState *sptr = new IntroState("Intro");
///	IntroState *nsptr = new IntroState("Level1");
///
///
///	//create event trigger
///	sf::State_Event* enter_key_press_event = new sf::State_Event(0);
///
///
///	//access Parent attributes by using State pointer
///	sp = sptr;
///	sp2 = nsptr;
///
///	sp->registerEvent(enter_key_press_event);
///	sp->registerStateChange(enter_key_press_event->getId(), sp2->getName());
///
///	//assign states to state manager
///	sf::State_Manager* sm = new sf::State_Manager();
///	sm->registerState(sp);
///	sm->registerState(sp2);
///	sm->setCurrentState(sp);
///
///
///	while(true)
///	{
///	 //detect action for trigger
///	 if(sf::Keyboard::isKeyPressed(sf::Keyboard::LControl))
///	 {
///	  enter_key_press_event->trigger();
///	 }
///
///	 sm->Cycle();//state cycle
///
///
///	 //exit game loop
///	 if(sm->getCurrentState()->getName() == nsptr->getName())
///	 {
///	  sm->Halt();
///	  break;
///	 }
///	}
///
///	//clean up
///	sptr->unRegisterEvent(enter_key_press_event);
///	enter_key_press_event->unRegisterState(sptr);
///	delete enter_key_press_event;
///	delete sptr;
///	delete nsptr;
///	delete sm;
///	return 0;
///}
///
/// \endcode
///
/// \see sf::State_Manager
///
////////////////////////////////////////////////////////////

#1kd7tck

Posted 11 November 2012 - 04:05 AM

Make a tree of all possible combinations, map each tree node to a state. The tree paths are state stransitions triggered by an input event like pressing a key.


Here is a roughed out system done in sfml. Incomplete and buggy, but it should get accross the idea of what I mean.
Examples are in the code, so reverse engineering it shouldn't be too hard. The State_manager.hpp should have the examples for using this system.

state.hpp
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2012 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
//    you must not claim that you wrote the original software.
//    If you use this software in a product, an acknowledgment
//    in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
//    and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_STATE_HPP
#define SFML_STATE_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
#include "Export.hpp"
#include <iostream>
#include <string>
#include <map>

namespace sf
{
class State_Event;
class State_Manager;
////////////////////////////////////////////////////////////
/// \brief The Util State Abstract class.
///
////////////////////////////////////////////////////////////
class SFML_UTIL_API State
{
public :
    ////////////////////////////////////////////////////////////
    /// \brief constructor
    ///
    /// \param string name
    ///
    ////////////////////////////////////////////////////////////
    State(std::string name);
    ////////////////////////////////////////////////////////////
    /// \brief destructor
    ///
    ////////////////////////////////////////////////////////////
    virtual ~State(){
	    if(manager != 0)
		    manager->unRegisterState(this);
	    if(registered_events.size() > 0){
		    it = registered_events.begin();
		    (*it)->unRegisterState(this);
	    }
    }


    //Virtual functions for assigning user desired functionality
    ////////////////////////////////////////////////////////////
    /// \brief second thing to be called every game cycle
    ///
    /// \param double current time in milliseconds
    ///
    /// \return 0 if successfull
    ////////////////////////////////////////////////////////////
    virtual int Update (const double time) = 0;
    ////////////////////////////////////////////////////////////
    /// \brief first thing to be called every game cycle
    ///
    /// \return 0 if successfull
    ////////////////////////////////////////////////////////////
    virtual int Events () = 0;
    ////////////////////////////////////////////////////////////
    /// \brief third thing to be called every game cycle
    ///
    /// \param Image, canvas to draw on
    ///
    /// \return 0 if successfull
    ////////////////////////////////////////////////////////////
    virtual int Draw (Image& canvas) = 0;
    ////////////////////////////////////////////////////////////
    /// \brief called once when state is done
    ///
    /// \return 0 if successfull
    ////////////////////////////////////////////////////////////
    virtual int CleanUp () = 0;
    ////////////////////////////////////////////////////////////
    /// \brief called once when state is entered
    ///
    /// \return 0 if successfull
    ////////////////////////////////////////////////////////////
    virtual int Init () = 0;




    ////////////////////////////////////////////////////////////
    /// \brief an event was triggered
    ///
    /// \param int ID of event
    ///
    /// \return bool true on success
    ////////////////////////////////////////////////////////////
    bool trigger(int event_id);
    ////////////////////////////////////////////////////////////
    /// \brief change state name
    ///
    /// \param string new name
    ///
    /// \return true if successful
    ////////////////////////////////////////////////////////////
    bool setName(std::string name);
    ////////////////////////////////////////////////////////////
    /// \brief get state name
    ///
    /// \return string name
    ////////////////////////////////////////////////////////////
    std::string getName();
    ////////////////////////////////////////////////////////////
    /// \brief get next state name
    ///
    /// \return string name, "" if none
    ////////////////////////////////////////////////////////////
    std::string nextState();
    ////////////////////////////////////////////////////////////
    /// \brief change DefaultNextStateName
    ///
    /// \param string new name
    ///
    /// \return true if successful
    ////////////////////////////////////////////////////////////
    bool setDefaultNextStateName(std::string name);
    ////////////////////////////////////////////////////////////
    /// \brief check if state is done, then transition to next state
    ///
    /// \return bool ,this is the triggered variable
    ////////////////////////////////////////////////////////////
    bool stateDone();

    ////////////////////////////////////////////////////////////
    /// \brief register a state_event with this state
    ///
    /// \param State_Event*
    ///
    /// \return void
    ////////////////////////////////////////////////////////////
    void registerEvent(State_Event*);

    ////////////////////////////////////////////////////////////
    /// \brief unregister a state_event from this state
    /// un-registers StateChange and removes event from registered_events
    /// \param State_Event*
    ///
    /// \return void
    ////////////////////////////////////////////////////////////
    void unRegisterEvent(State_Event*);

    ////////////////////////////////////////////////////////////
    /// \brief register a state transition from this state node
    ///
    /// \param int event id
    ///
    /// \param string name of next state
    ///
    /// \return void
    ////////////////////////////////////////////////////////////
    void registerStateChange(int event_id, std::string next_state_name);

    ////////////////////////////////////////////////////////////
    /// \brief unregister a state transition from this state node
    ///
    /// \param int event id
    ///
    /// \return void
    ////////////////////////////////////////////////////////////
    void unRegisterStateChange(int event_id);
    ////////////////////////////////////////////////////////////
    /// \brief set a manager for this state
    ///
    /// \param State_Manager*
    ///
    /// \return void
    ////////////////////////////////////////////////////////////
    void setStateManager(State_Manager*);
    ////////////////////////////////////////////////////////////
    /// \brief Change Active status, active status meaning is this state running?
    ///
    /// \param bool, active status true or false, the isActive variable
    ///
    /// \return void
    ////////////////////////////////////////////////////////////
    void setActiveStatus(bool);
    ////////////////////////////////////////////////////////////
    /// \brief Get the Active status, active status meaning is this state running?
    ///
    /// \return bool, the isActive variable
    ////////////////////////////////////////////////////////////
    bool getActiveStatus();
    ////////////////////////////////////////////////////////////
    /// \brief reset the trigger by setting it to false
    ////////////////////////////////////////////////////////////
    void resetTrigger();
protected :
    //state identifier
    std::string state_name;
    std::vector <State_Event*> registered_events;
    std::vector <State_Event*>::iterator it;
    //state to state mappings based on event_id
    std::map <int, std::string> eventIdStateTrigger;
    //the next state to run after this state ends
    std::string next_state_name;
    //if next_state_name is no longer valid
    std::string default_next_state_name;
    //if triggered then state manager will transition to next state, can only be triggered if state is active
    bool triggered;
    //if this state is currently cycling with state manager
    bool isActive;
    //pointer to the state_manager for this state
    State_Manager* manager;
};


////////////////////////////////////////////////////////////
/// \brief A NULL State.
///
////////////////////////////////////////////////////////////
class SFML_UTIL_API NULLState : public State
{
public:
    NULLState() : State("")
    {
	    ;
    }
    ~NULLState()
    {
	    ;
    }
    int Update (const double time)
    {
	    return 0;
    }
    int Events ()
    {
	    return 0;
    }
    int Draw (Image& canvas)
    {
	    return 0;
    }
    int CleanUp ()
    {
	    return 0;
    }
    int Init ()
    {
	    return 0;
    }
protected:
    ;
};
} // namespace sf

#endif // SFML_STATE_HPP

////////////////////////////////////////////////////////////
/// \class sf::State
/// \ingroup util
///
/// sf::State represents a single state within a finite state machine. This is an abstract class.
///
/// Example:
/// \code
///
///class IntroState : public sf::State
///{
///public:
///    std::string name;
///
///    IntroState(std::string n) : sf::State(n)
///    {
///	    name = n;
///    }
///
///    ~IntroState()
///    {
///	    ;
///    }
///
///    int Update (const double time)
///    {
///	    std::cout << "Intro_Update_" << name << std::endl;
///	    return 0;
///    }
///
///    int Events ()
///    {
///	    std::cout << "Intro_Events_" << name << std::endl;
///	    return 0;
///    }
///
///    int Draw (sf::Image& canvas)
///    {
///	    std::cout << "Intro_Draw_" << name << std::endl;
///	    return 0;
///    }
///
///    int CleanUp ()
///    {
///	    std::cout << "Intro_Cleanup_" << name << std::endl;
///	    return 0;
///    }
///
///    int Init ()
///    {
///	    std::cout << "Intro_Init_" << name << std::endl;
///	    return 0;
///    }
///};
///
/// \endcode
///
/// \see sf::State
///
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
/// \class sf::NULLState
/// \ingroup util
///
/// sf::NULLState represents a single state within no real functionality.
///
/// Example:
/// \code
///
/// \endcode
///
/// \see sf::NULLState
///
////////////////////////////////////////////////////////////






state.cpp
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2012 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
//    you must not claim that you wrote the original software.
//    If you use this software in a product, an acknowledgment
//    in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
//    and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Util.hpp>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <locale>
#include <sstream>
#include <string>
#include <iostream>
#include <fstream>
#include <cstdio>

namespace sf
{
State::State(std::string n)
{
    next_state_name = "";
    default_next_state_name = "";
    triggered = false;
    isActive = false;
    state_name = n;
    manager = 0;
}
bool State::setName(std::string name)
{
    if(name.size() < 1)
	    return false;
    state_name = name;
    return true;
}
std::string State::getName()
{
    return state_name;
}
std::string State::nextState()
{
    if(next_state_name.size() > 0)
	    return next_state_name;
    else
	    return default_next_state_name;
}

bool State::setDefaultNextStateName(std::string name)
{
    if(name.size() < 1)
	    return false;
    default_next_state_name = name;
    return true;
}
bool State::stateDone()
{
    return triggered;
}

bool State::trigger(int event_id)
{
    if(isActive && eventIdStateTrigger.find(event_id) != eventIdStateTrigger.end())
    {
	    next_state_name = eventIdStateTrigger[event_id];
	    triggered = true;
	    return true;
    }
    return false;
}

void State::registerEvent(State_Event* e)
{
    registered_events.push_back(e);
    e->registerState(this);
}

void State::unRegisterEvent(State_Event* e)
{
    int count = 0;
    if(registered_events.size() > 0)
    {
	    for ( it = registered_events.begin(); it != registered_events.end() ; it++ )
	    {
		    if((*it)->getId() == e->getId())
		    {
			    registered_events.erase(registered_events.begin() + count);
		    }
		    else
			    count++;
	    }
	    unRegisterStateChange(e->getId());
    }
}

void State::registerStateChange(int event_id, std::string next_state)
{
    eventIdStateTrigger[event_id] = next_state;
}

void State::unRegisterStateChange(int event_id)
{
    eventIdStateTrigger[event_id] = "";
}

void State::setStateManager(State_Manager* sm)
{
    manager = sm;
}

void State::setActiveStatus(bool b)
{
    isActive = b;
}
bool State::getActiveStatus()
{
    return isActive;
}

void State::resetTrigger()
{
    triggered = false;
}
} // namespace sf



state_event.hpp
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2012 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
//    you must not claim that you wrote the original software.
//    If you use this software in a product, an acknowledgment
//    in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
//    and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_STATE_EVENT_HPP
#define SFML_STATE_EVENT_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include "Export.hpp"
#include <iostream>
#include <string>
#include <vector>

namespace sf
{
class State;
////////////////////////////////////////////////////////////
/// \brief The Util State_Event class.
///
////////////////////////////////////////////////////////////
class SFML_UTIL_API State_Event
{
public :
    ////////////////////////////////////////////////////////////
    /// \brief constructor
    ///
    /// \param int id
    ///
    ////////////////////////////////////////////////////////////
    State_Event(int id);

    ////////////////////////////////////////////////////////////
    /// \brief destructor
    ///
    ////////////////////////////////////////////////////////////
    ~State_Event();

    ////////////////////////////////////////////////////////////
    /// \brief trigger this event, and trigger all registered states
    ///
    /// \return bool true on success
    ////////////////////////////////////////////////////////////
    bool trigger();

    ////////////////////////////////////////////////////////////
    /// \brief obtain this event's ID
    ///
    /// \return int Event ID
    ////////////////////////////////////////////////////////////
    int getId();
    ////////////////////////////////////////////////////////////
    /// \brief change this event's ID
    ///
    /// \param int id
    ///
    ////////////////////////////////////////////////////////////
    void setId(int id);

    ////////////////////////////////////////////////////////////
    /// \brief register a state with this event
    ///
    /// \param State*
    ///
    /// \return void
    ////////////////////////////////////////////////////////////
    void registerState(State*);

    ////////////////////////////////////////////////////////////
    /// \brief unregister a state from this event
    ///
    /// \param State*
    ///
    /// \return void
    ////////////////////////////////////////////////////////////
    void unRegisterState(State*);
protected :
    int event_id;
    static std::vector<State*> registered_states;
    std::vector<State*>::iterator it;
};
} // namespace sf

#endif // SFML_STATE_EVENT_HPP

////////////////////////////////////////////////////////////
/// \class sf::State_Event
/// \ingroup util
///
/// sf::State_Event represents an event that can register with multiple states.
///
/// Example:
/// \code
/// //create new state_event and register it to a State
/// sf::State_Event* enter_key_press_event = new sf::State_Event(0);
/// state_pntr->registerEvent(enter_key_press_event);
///
/// //map event to other state
/// state_pntr->registerStateChange(enter_key_press_event->getId(), state_pntr_2->getName());
///
/// //trigger an event, activates the mapping made earlier
/// enter_key_press_event->trigger();
/// \endcode
///
/// \see sf::State_Event
///
////////////////////////////////////////////////////////////





state_event.cpp
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2012 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
//    you must not claim that you wrote the original software.
//    If you use this software in a product, an acknowledgment
//    in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
//    and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Util.hpp>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <locale>
#include <sstream>
#include <string>
#include <iostream>
#include <fstream>
#include <cstdio>

namespace sf
{
std::vector<State*> State_Event::registered_states;
State_Event::State_Event(int id)
{
    event_id = id;
}
State_Event::~State_Event()
{
    if(State_Event::registered_states.size() > 0){
	    it = State_Event::registered_states.begin();
	    (*it)->unRegisterEvent(this);
    }
}

bool State_Event::trigger()
{
    if(State_Event::registered_states.size() > 0)
    {
	    for ( it = State_Event::registered_states.begin(); it != State_Event::registered_states.end() ; it++ )
	    {
		    (*it)->trigger(event_id);
	    }
	    return true;
    }
    return false;
}

int State_Event::getId()
{
    return event_id;
}

void State_Event::setId(int id)
{
    event_id = id;
}

void State_Event::registerState(State* s)
{
    unRegisterState(s);
    State_Event::registered_states.push_back(s);
}

void State_Event::unRegisterState(State* s)
{
    int count = 0;
    if(State_Event::registered_states.size() > 0)
    {
	    for ( it = State_Event::registered_states.begin(); it != State_Event::registered_states.end() ; it++ )
	    {
		    if((*it)->getName() == s->getName())
		    {
			    State_Event::registered_states.erase(State_Event::registered_states.begin() + count);
			    break;
		    }
		    else
			    count++;
	    }
    }
}

} // namespace sf




state_manager.hpp
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2012 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
//    you must not claim that you wrote the original software.
//    If you use this software in a product, an acknowledgment
//    in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
//    and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_STATE_MANAGER_HPP
#define SFML_STATE_MANAGER_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include "Export.hpp"
#include <iostream>
#include <string>
#include <map>
#include <vector>

namespace sf
{

class State;
class Image;
class Clock;
class Time;
class NULLState;
////////////////////////////////////////////////////////////
/// \brief The Util State_Manager class.
///
////////////////////////////////////////////////////////////
class SFML_UTIL_API State_Manager
{
public :
    ////////////////////////////////////////////////////////////
    /// \brief constructor
    ///
    ////////////////////////////////////////////////////////////
    State_Manager();
    ////////////////////////////////////////////////////////////
    /// \brief destructor
    ///
    ////////////////////////////////////////////////////////////
    ~State_Manager();
    ////////////////////////////////////////////////////////////
    /// \brief register a state with this state manager
    ///
    /// \param State*
    ///
    /// \return void
    ////////////////////////////////////////////////////////////
    void registerState(State*);
    ////////////////////////////////////////////////////////////
    /// \brief unregister a state with this state manager
    ///
    /// \param State*
    ///
    /// \return void
    ////////////////////////////////////////////////////////////
    void unRegisterState(State*);
    ////////////////////////////////////////////////////////////
    /// \brief cycle one itteration, checks all states and runs them for one pass
    ///
    /// \return void
    ////////////////////////////////////////////////////////////
    void Cycle();
    ////////////////////////////////////////////////////////////
    /// \brief ends state machine, and cleans everyhting up
    ///
    /// \return void
    ////////////////////////////////////////////////////////////
    void Halt();
    ////////////////////////////////////////////////////////////
    /// \brief set current state
    ///
    /// \param State*
    ///
    /// \return void
    ////////////////////////////////////////////////////////////
    void setCurrentState(State*);

    ////////////////////////////////////////////////////////////
    /// \brief get current state
    ///
    /// \return State*
    ////////////////////////////////////////////////////////////
    State* getCurrentState();

protected :
    std::vector<State*> states;
    std::string currentState;
    std::string nextState;
    std::string previousState;
    std::vector<State*>::iterator it;
    State* baseState;
    Image* backBuffer;
    Clock* clock;
    Time* time;
};
} // namespace sf

#endif // SFML_STATE_MANAGER_HPP

////////////////////////////////////////////////////////////
/// \class sf::State_Manager
/// \ingroup util
///
/// sf::State_Manager manages a collection of states
///
/// Example:
/// \code
///
///class IntroState : public sf::State
///{
///public:
///    std::string name;
///
///    IntroState(std::string n) : sf::State(n)
///    {
///	    name = n;
///    }
///
///    ~IntroState()
///    {
///	    ;
///    }
///
///    int Update (const double time)
///    {
///	    std::cout << "Intro_Update_" << name << std::endl;
///	    return 0;
///    }
///
///    int Events ()
///    {
///	    std::cout << "Intro_Events_" << name << std::endl;
///	    return 0;
///    }
///
///    int Draw (sf::Image& canvas)
///    {
///	    std::cout << "Intro_Draw_" << name << std::endl;
///	    return 0;
///    }
///
///    int CleanUp ()
///    {
///	    std::cout << "Intro_Cleanup_" << name << std::endl;
///	    return 0;
///    }
///
///    int Init ()
///    {
///	    std::cout << "Intro_Init_" << name << std::endl;
///	    return 0;
///    }
///};
///
///
///
///int main() {
///
///    sf::State* sp;
///    sf::State* sp2;
///
///
///    //create two new classes from abstract type State
///    IntroState *sptr = new IntroState("Intro");
///    IntroState *nsptr = new IntroState("Level1");
///
///
///    //create event trigger
///    sf::State_Event* enter_key_press_event = new sf::State_Event(0);
///
///
///    //access Parent attributes by using State pointer
///    sp = sptr;
///    sp2 = nsptr;
///
///    sp->registerEvent(enter_key_press_event);
///    sp->registerStateChange(enter_key_press_event->getId(), sp2->getName());
///
///    //assign states to state manager
///    sf::State_Manager* sm = new sf::State_Manager();
///    sm->registerState(sp);
///    sm->registerState(sp2);
///    sm->setCurrentState(sp);
///
///
///    while(true)
///    {
///	 //detect action for trigger
///	 if(sf::Keyboard::isKeyPressed(sf::Keyboard::LControl))
///	 {
///	  enter_key_press_event->trigger();
///	 }
///
///	 sm->Cycle();//state cycle
///
///
///	 //exit game loop
///	 if(sm->getCurrentState()->getName() == nsptr->getName())
///	 {
///	  sm->Halt();
///	  break;
///	 }
///    }
///
///    //clean up
///    sptr->unRegisterEvent(enter_key_press_event);
///    enter_key_press_event->unRegisterState(sptr);
///    delete enter_key_press_event;
///    delete sptr;
///    delete nsptr;
///    delete sm;
///    return 0;
///}
///
/// \endcode
///
/// \see sf::State_Manager
///
////////////////////////////////////////////////////////////





State_manager.cpp
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2012 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
//    you must not claim that you wrote the original software.
//    If you use this software in a product, an acknowledgment
//    in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
//    and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Util.hpp>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <locale>
#include <sstream>
#include <string>
#include <iostream>
#include <fstream>
#include <cstdio>

namespace sf
{
State_Manager::State_Manager()
{
    currentState = "";
    nextState = "";
    previousState = "";
    clock = new Clock();
    time = new Time();
    backBuffer = new Image();
    clock->restart();
}
State_Manager::~State_Manager()
{
    if(states.size() > 0)
	    for ( it = states.begin() ; it < states.end(); it++ ){
		    (**it).setStateManager(0);
	    }
    delete clock;
    delete time;
    delete backBuffer;
}

void State_Manager::registerState(State* s)
{
    unRegisterState(s);
    states.push_back(s);
    (*s).setStateManager(this);
}

void State_Manager::unRegisterState(State* s)
{
    int count = 0;
    for ( it = states.begin() ; it != states.end(); it++ ){
	    if((*it)->getName() == s->getName())
	    {
		    states.erase(states.begin() + count);
	    }
	    else
		    count++;
    }
}

void State_Manager::Cycle()
{
    *time = clock->getElapsedTime();
    State* cptr = 0;
    if(currentState != "")
    {
	    for ( it = states.begin() ; it != states.end(); it++ )
	    {//find current state
		    if((*it)->getName() == currentState)
			    cptr = *it;
	    }
	    if(cptr != 0 && cptr->stateDone())
	    {//if current state is done
		    if(cptr->nextState() != "")
		    {//first phase of state shift
			    cptr->setActiveStatus(false);
			    cptr->resetTrigger();
			    cptr->CleanUp();
			    nextState = cptr->nextState();
			    previousState = currentState;
			    currentState = "";
			    Cycle();//start second phase of state shift
		    }
		    cptr = 0;
	    }
    }
    else
    {
	    if(nextState != "")
	    {//second phase of state shift
		    for ( it = states.begin() ; it != states.end(); it++ )
		    {
			    if((*it)->getName() == nextState)
				    cptr = *it;
		    }
		    if(cptr != 0)
		    {
			    cptr->setActiveStatus(true);
			    currentState = nextState;
			    nextState = "";
			    cptr->Init();
		    }
	    }
    }

    //////////////////////////////////////
    //process current cycle, if no state shift
    //////////////////////////////////////
    if(cptr != 0 && cptr->getActiveStatus())
    {
	    cptr->Events();
	    cptr->Update((double)time->asMilliseconds());
	    cptr->Draw(*backBuffer);
    }
}

void State_Manager::Halt()
{
    State* cptr = 0;
    if(currentState != "")
    {
	    for ( it = states.begin() ; it != states.end(); it++ )
	    {//find current state
		    if((*it)->getName() == currentState)
			    cptr = *it;
	    }
	    cptr->setActiveStatus(false);
	    cptr->resetTrigger();
	    cptr->CleanUp();
    }
}

void State_Manager::setCurrentState(State* s)
{
    if(s != 0)
    {
	    //remove current state
	    if(currentState != "")
	    {
		    State* cptr = 0;
		    for ( it = states.begin() ; it != states.end(); it++ )
		    {//find current state
			    if((*it)->getName() == currentState)
				    cptr = *it;
		    }
		    cptr->setActiveStatus(false);
		    cptr->resetTrigger();
		    cptr->CleanUp();
		    previousState = currentState;
		    currentState = "";
	    }

	    //replace current state
	    for ( it = states.begin() ; it != states.end(); it++ )
	    {
		    if((*it)->getName() == s->getName())
		    {
			    currentState = s->getName();
			    s->setActiveStatus(true);
			    s->Init();
		    }
	    }

    }
}

State* State_Manager::getCurrentState()
{
    for ( it = states.begin() ; it != states.end(); it++ )
    {//find current state
	    if((*it)->getName() == currentState)
		    return *it;
    }
    return 0;
}

} // namespace sf

PARTNERS