| int ClientID; //client ID is determined when player joins lobby. |
| |
| //Looking around at the structure of the NetRumble example, I'm |
| //thinking about redoing a few things from my project ;). Until then, |
| //this shows how I'm storing variables being passed between |
| //the clients and server. |
| |
| Vector2[4] leftStick; |
| Vector2[4] rightStick; |
| double[4] leftTrigger; |
| double[4] rightTrigger; |
| |
| Vector3[4] handledPos; |
| Vector3[4] handledVelocity_Trans; |
| Vector3[4] handledVelocity_Rot; |
| |
| void UpdateLocalGamer(LocalNetworkGamer gamer) |
| { |
| if (!networkSession.IsHost) |
| { |
| packetWriter.Write(gamer.Id); |
| packetWriter.Write(ClientID); |
| packetWriter.Write(leftStick[ClientID]); |
| packetWriter.Write(rightStick[ClientID]); |
| packetWriter.Write(leftTrigger[ClientID]); |
| packetWriter.Write(rightTrigger[ClientID]); |
| |
| // Send our input data to the server. |
| gamer.SendData(packetWriter, |
| SendDataOptions.InOrder, networkSession.Host); |
| } |
| } |
| |
| void UpdateServer() |
| { |
| // Loop over all the players in the session, not just the local ones! |
| foreach (NetworkGamer gamer in networkSession.AllGamers) |
| { |
| // Write the tank state into the output network packet. |
| packetWriter.Write(gamer.Id); |
| packetWriter.Write(handledPos[0]); |
| packetWriter.Write(handledPos[1]); |
| packetWriter.Write(handledPos[2]); |
| packetWriter.Write(handledPos[3]); |
| packetWriter.Write(handledVelocity_Trans[0]); |
| packetWriter.Write(handledVelocity_Trans[1]); |
| packetWriter.Write(handledVelocity_Trans[2]); |
| packetWriter.Write(handledVelocity_Trans[3]); |
| packetWriter.Write(handledVelocity_Rot[0]); |
| packetWriter.Write(handledVelocity_Rot[1]); |
| packetWriter.Write(handledVelocity_Rot[2]); |
| packetWriter.Write(handledVelocity_Rot[3]); |
| } |
| |
| // Send the combined data for all tanks to everyone in the session. |
| LocalNetworkGamer server = (LocalNetworkGamer)networkSession.Host; |
| server.SendData(packetWriter, SendDataOptions.InOrder); |
| } |
| |
| void ServerReadInputFromClients(LocalNetworkGamer gamer) |
| { |
| // Keep reading as long as incoming packets are available. |
| while (gamer.IsDataAvailable) |
| { |
| NetworkGamer sender; |
| |
| // Read a single packet from the network. |
| gamer.ReceiveData(packetReader, out sender); |
| |
| if (!sender.IsLocal) |
| { |
| byte gamerId = packetReader.ReadByte(); |
| byte gamerClientID = packetReader.ReadByte(); |
| leftStick[gamerClientID] = packetReader.ReadVector2(); |
| rightStick[gamerClientID] = packetReader.ReadVector2(); |
| leftTrigger[gamerClientID] = packetReader.ReadDouble(); |
| rightTrigger[gamerClientID] = packetReader.ReadDouble(); |
| } |
| } |
| } |
| |
| void ClientReadGameStateFromServer(LocalNetworkGamer gamer) |
| { |
| while (gamer.IsDataAvailable) |
| { |
| NetworkGamer sender; |
| |
| // Read a single packet from the network. |
| gamer.ReceiveData(packetReader, out sender); |
| |
| while (packetReader.Position < packetReader.Length) |
| { |
| // Read the state of one tank from the network packet. |
| byte gamerId = packetReader.ReadByte(); |
| handledPos[0] = packetReader.ReadVector3(); |
| handledPos[1] = packetReader.ReadVector3(); |
| handledPos[2] = packetReader.ReadVector3(); |
| handledPos[3] = packetReader.ReadVector3(); |
| handledVelocity_Trans[0] = packetReader.ReadVector3(); |
| handledVelocity_Trans[1] = packetReader.ReadVector3(); |
| handledVelocity_Trans[2] = packetReader.ReadVector3(); |
| handledVelocity_Trans[3] = packetReader.ReadVector3(); |
| handledVelocity_Rot[0] = packetReader.ReadVector3(); |
| handledVelocity_Rot[1] = packetReader.ReadVector3(); |
| handledVelocity_Rot[2] = packetReader.ReadVector3(); |
| handledVelocity_Rot[3] = packetReader.ReadVector3(); |
| } |
| } |
| } |