Adding Networking to the Game
We are almost done adding networking support to Ghost Arena 3D.
The last obstacle before we are finished with this chapter is to
update the gameplay screen to include the networking features. In
this section we will take a look at all of the updated and new code
for the gameplay screen.
New Variables and Objects
The gameplay screen has several new variables. These include a
copy of the game’s NetworkSession, a packet reader and writer, the
GameType, a HUD object for displaying the second player’s gamer
picture and score, and a new Player object. The gameOver variable
was moved from the Update() function to the class level so that
multiple functions can access this state. The local player was
renamed from player1 to localPlayer and a new Player object called
networkPlayer was created for the remote player. A flag for whether
the player has shot their weapon is also stored on the class level,
and a flag indicating the player fired is sent over the network.
Because the callback fire from the action map system can call Fire()
at any time, we do not want the flag indicating the player shot their
weapon to be reset before it was sent over the network. Therefore,
a copy is saved and is only reset once this local player has sent that
information to the network player. The original flag for whether a
player shot their weapon was used to control when the shot audio
was played and if a collision between the player’s view vector and
the ghost exists (meaning the player shot the ghost).
The packet reader and writer objects are used to prepare data to
be transmitted over the network. The packet reader is used when
reading data that was sent over the Internet, while the packer
writer is used to prepare data for transmission. These objects only
488 Chapter 14

Get Essential XNA Game Studio 2.0 Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.