Adding Network Support for the Meteors in Font

Drawer PDF-417 2d barcode in Font Adding Network Support for the Meteors

Adding Network Support for the Meteors
Printing PDF-417 2d Barcode In None
Using Barcode generator for Font Control to generate, create PDF 417 image in Font applications.
www.OnBarcode.com
Paint ECC200 In None
Using Barcode maker for Font Control to generate, create Data Matrix image in Font applications.
www.OnBarcode.com
The game s meteors are represented by two distinct classes: the Meteor class, which represents the sprite of the meteor itself, and the MeteorsManager class, which represents the entire meteor field in the game. Each class changes the game state in its own way, and you ll alter its code to add the network game support separately. In the Meteor class, only the PutinStartPosition and Update methods change the attributes of an instance. So, you ll change these methods. But which message will be sent to represent a meteor state In Rock Rain, each meteor updates only its position on the screen, so you can send a message with an 'R' header and the message Index, Position. Each meteor on the screen sends this message, to inform the client of its position in the game. Because the value of the Index property can identify each meteor, let s send them together so that the client knows about which meteor position it s being informed. As explained in 5, the server keeps the entire state of the game in this case, the meteors positions. First, add and initialize an instance of the NetworkHelper class, as you ve done before. Change the PutinStartPosition method: /// <summary> /// Initialize meteor position and velocity /// </summary> public void PutinStartPosition() { // Only the server can set the meteor attributes if ((networkHelper.NetworkGameSession == null) || (networkHelper.NetworkGameSession.IsHost)) { position.X = random.Next(Game.Window.ClientBounds.Width currentFrame.Width); position.Y = 0; YSpeed = 1 + random.Next(9); XSpeed = random.Next(3) - 1; } }
Creating Barcode In None
Using Barcode generator for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com
Barcode Generation In None
Using Barcode maker for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com
CHAPTER 6 ROCK RAIN LIVE!
ANSI/AIM Code 128 Encoder In None
Using Barcode generator for Font Control to generate, create Code 128 Code Set B image in Font applications.
www.OnBarcode.com
Make ANSI/AIM Code 39 In None
Using Barcode encoder for Font Control to generate, create Code-39 image in Font applications.
www.OnBarcode.com
Following is the code for the Update method: /// <summary> /// Update the meteor position /// </summary> public override void Update(GameTime gameTime) { // Check if the meteor is still visible if ((position.Y >= Game.Window.ClientBounds.Height) || (position.X >= Game.Window.ClientBounds.Width) || (position.X <= 0)) { PutinStartPosition(); } // Move meteor position.Y += Yspeed; position.X += Xspeed; // Send the meteor info to the client if ((networkHelper.NetworkGameSession != null) && (networkHelper.NetworkGameSession.IsHost)) { networkHelper.ServerPacketWriter.Write('R'); networkHelper.ServerPacketWriter.Write(index); networkHelper.ServerPacketWriter.Write(position); } base.Update(gameTime); } This is another message that is sent only by the server, so you handle it in the HandleServerData method of the ActionScene class, as you did with the other messages: case 'R': int meteorId = networkHelper.ServerPacketReader.ReadInt32(); meteors.AllMeteors[meteorId].Position = networkHelper.ServerPacketReader.ReadVector2(); break; In the preceding code, you just set the position of the specified meteor with the position value sent in the message. Because you re doing this for each meteor, you ll then synchronize the position of all of them during the game. Another state change situation occurs when a new meteor is added to the scene. The MeteorsManager class performs this operation. The client must be notified about a new object added by the server, so that the client can see a new meteor with the same characteristics. Add and initialize an instance of the NetworkHelper object and change the Start method of the MeteorsManager class:
EAN / UCC - 13 Creator In None
Using Barcode encoder for Font Control to generate, create GS1 - 13 image in Font applications.
www.OnBarcode.com
Code 93 Full ASCII Drawer In None
Using Barcode printer for Font Control to generate, create USS Code 93, USS 93 image in Font applications.
www.OnBarcode.com
CHAPTER 6 ROCK RAIN LIVE!
PDF 417 Reader In VB.NET
Using Barcode recognizer for .NET framework Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
PDF417 Encoder In Java
Using Barcode encoder for Android Control to generate, create PDF-417 2d barcode image in Android applications.
www.OnBarcode.com
/// <summary> /// Start the meteors' rain /// </summary> public void Start() { if ((networkHelper.NetworkGameSession == null) || (networkHelper.NetworkGameSession.IsHost)) { // Initialize a counter elapsedTime = TimeSpan.Zero; // Add the meteors for (int i = 0; i < STARTMETEORCOUNT; i++) { AddNewMeteor(); } } } This way, only the server is able to add new meteors, and when a new meteor is added you must send a message to inform the client. This message contains the attributes of a meteor. It has the header 'M' and the message Index, Position, Horizontal Speed, Vertical Speed. Then change the AddNewMeteor method to send a message with the other attributes of the new meteor: /// <summary> /// Add a new meteor in the scene /// </summary> /// <returns>the new meteor</returns> private Meteor AddNewMeteor() { Meteor newMeteor = new Meteor(Game, ref meteorTexture); newMeteor.Initialize(); meteors.Add(newMeteor); newMeteor.Index = meteors.Count-1; // Send the new meteor info to the client if ((networkHelper.NetworkGameSession != null) && (networkHelper.NetworkGameSession.IsHost)) { networkHelper.ServerPacketWriter.Write('M'); networkHelper.ServerPacketWriter.Write(newMeteor.Index); networkHelper.ServerPacketWriter.Write(newMeteor.Position); networkHelper.ServerPacketWriter.Write(newMeteor.XSpeed); networkHelper.ServerPacketWriter.Write(newMeteor.YSpeed); }b return newMeteor; }
Code-128 Creation In Java
Using Barcode drawer for Android Control to generate, create Code 128 image in Android applications.
www.OnBarcode.com
Barcode Creator In Java
Using Barcode drawer for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
CHAPTER 6 ROCK RAIN LIVE!
UCC-128 Creation In None
Using Barcode creator for Software Control to generate, create USS-128 image in Software applications.
www.OnBarcode.com
Barcode Encoder In .NET
Using Barcode creator for ASP.NET Control to generate, create Barcode image in ASP.NET applications.
www.OnBarcode.com
And again handle this message in the HandleServerData event of the ActionScene class so that a new meteor is added to the client s meteor list: case 'M': int index = networkHelper.ServerPacketReader.ReadInt32(); Vector2 position = networkHelper.ServerPacketReader.ReadVector2(); int xspeed = networkHelper.ServerPacketReader.ReadInt32(); int yspeed = networkHelper.ServerPacketReader.ReadInt32(); meteors.AddNewMeteor(index,position,xspeed,yspeed); break; Now, the motion as well as the addition of new meteors are synchronized with the client. There s still a third point where the game state is changed: when the Update method checks if it s necessary to add a new meteor. Only the server can execute this operation, so change the method as follows: // Only the server can add new meteors if ((networkHelper.NetworkGameSession == null) || (networkHelper.NetworkGameSession.IsHost)) { CheckforNewMeteor(gameTime); } That s it! All the objects are now synchronized. The server will control all the game state and send it to the client so it keeps the game synchronized. The client will receive all the messages and change the status of its objects according to the content of these messages, to maintain an online match, just as if you were beside your opponent. Figure 6-3 illustrates the message traffic between the players. Now call that friend of yours who lives in Japan for a Rock Rain match!
Recognizing EAN / UCC - 13 In Visual Studio .NET
Using Barcode reader for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Reading Code 3/9 In Visual Basic .NET
Using Barcode scanner for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Barcode Creator In Java
Using Barcode printer for BIRT Control to generate, create Barcode image in BIRT applications.
www.OnBarcode.com
Generating USS-128 In C#
Using Barcode printer for Visual Studio .NET Control to generate, create GTIN - 128 image in Visual Studio .NET applications.
www.OnBarcode.com
Drawing Data Matrix 2d Barcode In Visual C#.NET
Using Barcode drawer for .NET Control to generate, create DataMatrix image in VS .NET applications.
www.OnBarcode.com
QR Code 2d Barcode Encoder In None
Using Barcode generation for Office Excel Control to generate, create QR Code ISO/IEC18004 image in Office Excel applications.
www.OnBarcode.com
Copyright © OnBarcode.com . All rights reserved.