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

Company Atop the Clouds – Co-op Multiplayer

As we mentioned, the packet identifier, which clearly states the packet type, depending on whether it was sent by the client or the server, is fixed to be always asf::Int32 value, so we begin by unpacking that header. Now, since that number matches directly with the members of the corresponding enumerator, we perform a switch on it. The preceding code snippet does not contain all cases that we handle, so we can better understand the flow of the server logic and later analyze each packet properly.

This is exactly what defines the interaction between peers and the rules of the gameplay in conformance with the network protocol we created. For example, when we get a packet from the client of type Client::PlayerEvent, we already know from the "specification" that we can find in the packet's data two sf::Int32 variables: the aircraft identifier and the action identifier that matches directly the one in the Player.hpp file. This way, when we read such a packet, we can broadcast it back to all peers so they all make the aircraft X perform action Y at the same time.

Studying our protocol

Let's attempt a deeper understanding of the logic of our server by checking what each server packet exactly means.

Every packet in the Server::PacketType enum is formed by the bullet's title as its identifier. That assumed, we explain the following packed parameters:

BroadcastMessage: This takes a std::string and is used to send a message to all clients, which they would show on the screen for some seconds.

SpawnSelf: This takes a sf::Int32 value for the aircraft identifier and two float values for its initial position. It is used to order the peer to spawn its player one's aircraft.

InitialState: This takes two float values, the world height and the initial scrolling in it, then a sf::Int32 value with the count of aircraft in the world; then for each, it takes a sf::Int32 identifier and two float values with the position of the airplane.

PlayerEvent: This takes two sf::Int32 variables: the aircraft identifier and the action identifier, as declared in Player. This is used to inform all peers that plane X has triggered an action.

PlayerRealtimeChange: This is same as PlayerEvent, but for real-time actions. This means that we are changing an ongoing state to either true or false, so we add a Boolean value to the parameters.

PlayerConnect: This is same as SpawnSelf, but indicates that an aircraft from a different client is entering the world.

PlayerDisconnect: This takes one sf::Int32 value with the aircraft identifier to be destroyed.

[ 256 ]

www.it-ebooks.info

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