Creating the structure for multiplayer
You must be fed up with networking theory by now, and you actually want to see some code! Right, it is now time to start the concrete implementation of our game server.
In order to extend our game and add an online mode to it, we went ahead and created a new state MultiplayerGameState
, which is very similar to GameState
at first. In the following code snippet, you see the important parts of the class definition. As you see, the three typical state-related methods to draw, update, and handle events are again there, as well as the variables for the world, window, and texture holder. Several variables are new related to the management of different players and socket connections:
class MultiplayerGameState : public State { public: MultiplayerGameState(StateStack& stack,Context context, bool isHost); virtual void draw(); virtual bool update(sf::Time dt); virtual bool handleEvent(const sf...