Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
SFML Game Development.pdf
Скачиваний:
194
Добавлен:
28.03.2016
Размер:
4.19 Mб
Скачать

Chapter 10

This is the basis for all cheat prevention in online servers. It works effectively and is an important area that must never be neglected. For the sake of simplicity, in our

implementation, the server is not entirely authoritative, so cheating would be possible.

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::Event& event);

 

...

private:

 

void

updateBroadcastMessage(

 

sf::Time elapsedTime);

void

handlePacket(sf::Int32 packetType,

 

sf::Packet& packet);

private:

typedef std::unique_ptr<Player> PlayerPtr;

private:

 

 

World

 

mWorld;

sf::RenderWindow&

 

mWindow;

TextureHolder&

 

mTextureHolder;

 

 

 

 

[ 245 ]

 

 

 

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]