/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { // TODO: Add your update logic here if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { this.Exit(); } GraphicsDevice.Clear(Color.Black); HandleInput(); if (networkSession == null) { // If we are not in a network session, update the // menu screen that will let us create or join one. UpdateMenuScreen(); } else { // If we are in a network session, update it. UpdateNetworkSession(); } base.Update(gameTime); if (state == gameState.game) { //Input Queue -> msg process InputQueue.Process3(); world.Step((float)gameTime.ElapsedGameTime.TotalSeconds, 5, 8); // Out 0) contact listener fires collisions // events created in world step //Out 1) physics event(contact listener) msg -> Output queue if (Game1.networkSession != null) { if (Game1.networkSession.IsHost) { // create Buffer PhysicsBuffer[] physicsBuff = new PhysicsBuffer[PhysicsMan.Instance().getCount()]; int num = PhysicsMan.Instance().getCount(); // Update PhysicsObject to PhysicsBuffer for a message transmission PhysicsMan.PushToBuffer(ref physicsBuff); //// out 2) physics buffer msg => OutputQueue PhysicsBuffer_Message msg = new PhysicsBuffer_Message(ref physicsBuff); PhysicsBuffer_Message_outQueue.add(msg); } } // ON remote --------------------------------------------- // read Physics Buffer from OutputQueue // no physics simulator // On Both---------------------------------------------- // Out 3) input msg -> OutputQueue // InputQueue.Update ?? maybe this is checkinput()??? checkInput(); //OutputQueue -> InputQueue OutputQueue.PushToNetwork(); // I put this down here to get pBuffGlobal to have the same amount of items as number of physics bodies // InputQueue.Process3(); //Both---------------------------------------------- //PhsicsBuffer to GameObject PhysicsMan.Update(ref PhysicsBuffer_Message.pBuffGlobal); //PhysicsMan.Instance().Update(); ScoreManager.Instance().Update(); GameObjManager.Instance().Update(world); Timer.Process(gameTime); } Game1.Camera.Update(gameTime); }
void ClientReadGameStateFromServer(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); // This packet contains data about all the players in the session. // We keep reading from it until we have processed all the data. while (packetReader.Position < packetReader.Length) { if (packetReader.Length <= 2) { break; } // Read the state of one tank from the network packet. // byte gamerId = packetReader.ReadByte(); //Vector2 position = packetReader.ReadVector2(); //float tankRotation = packetReader.ReadSingle(); //float turretRotation = packetReader.ReadSingle(); QueueHdr qH; qH.inSeqNum = packetReader.ReadInt32(); qH.outSeqNum = packetReader.ReadInt32(); qH.type = (Queue_type)packetReader.ReadInt32(); // int incomingCount = packetReader.ReadInt32(); //Debug.WriteLine("qH.inSeqNum ServerRead = " + qH.inSeqNum); //Debug.WriteLine("qH.outSeqNum ServerRead = " + qH.outSeqNum); //Debug.WriteLine("qH.type ServerRead = " + qH.type); //Debug.WriteLine("incomingCount ServerRead = " + incomingCount); //////////////////////// send to input queue on client //PhysicsBuffer[] physicsBuff2 = new PhysicsBuffer[incomingCount]; switch (qH.type) { case Queue_type.QUEUE_PHYSICS_BUFFER: if (qH.type == Queue_type.QUEUE_PHYSICS_BUFFER) { int incomingCount = packetReader.ReadInt32(); PhysicsBuffer[] physicsBuff2 = new PhysicsBuffer[incomingCount]; for (int y = 0; y < incomingCount; y++) { //PhysicsBuffer_Message localPhysicsBuffer_Message = new PhysicsBuffer_Message((PhysicsBuffer_Message)qH.data); //PhysicsBuffer myPhysicsBuffer = new PhysicsBuffer(); //myPhysicsBuffer.id = packetReader.ReadInt32(); //myPhysicsBuffer.position = packetReader.ReadVector2(); //myPhysicsBuffer.rotation = (float)packetReader.ReadSingle(); //Push to buffer physicsBuff2[y].id = packetReader.ReadInt32(); physicsBuff2[y].position = packetReader.ReadVector2(); physicsBuff2[y].rotation = (float)packetReader.ReadSingle(); } // send this msg to Input Queue inQ so client can read the messages PhysicsBuffer_Message msg = new PhysicsBuffer_Message(ref physicsBuff2); PhysicsBuffer_Message_inQueue.add(msg); } break; case Queue_type.QUEUE_SHIP_MISSILE: Ship_Create_Missile_Message msgMissile = new Ship_Create_Missile_Message(player2); qH.data = msgMissile; InputQueue.add(qH); break; case Queue_type.QUEUE_SHIP_BOMB: Ship_Create_Bomb_Message msgBomb = new Ship_Create_Bomb_Message(player2); qH.data = msgBomb; InputQueue.add(qH); break; case Queue_type.QUEUE_SHIP_IMPULSE: Vector2 impulse = packetReader.ReadVector2(); Ship_Impulse_Message msgImpulse = new Ship_Impulse_Message(player2, impulse); qH.data = msgImpulse; InputQueue.add(qH); break; case Queue_type.QUEUE_SHIP_ROT: float rotation = packetReader.ReadSingle(); Ship_Rot_Message msgRotation = new Ship_Rot_Message(player2, rotation); qH.data = msgRotation; InputQueue.add(qH); break; case Queue_type.QUEUE_EVENT: //msg = new Event_Message((Event_Message)qH.data); int idA = packetReader.ReadInt32(); // Debug.WriteLine("idA =" + idA); int idB = packetReader.ReadInt32(); // Debug.WriteLine("idA =" + idB); Vector2 collisionPt = packetReader.ReadVector2(); // Debug.WriteLine("idA =" + collisionPt); Event_Message eventMsg = new Event_Message(idA, idB, collisionPt); qH.data = eventMsg; InputQueue.add(qH); break; } } } }
// PacketWriter packetWriter2 = new PacketWriter(); public static void PushToNetwork() { PacketWriter packetWriter2 = new PacketWriter(); int count = outQ.Count; for (int i = 0; i < count; i++) { // Read the header QueueHdr qH = outQ.Dequeue(); switch (qH.type) { case Queue_type.QUEUE_SHIP_BOMB: packetWriter2.Write(qH.inSeqNum); packetWriter2.Write(qH.outSeqNum); packetWriter2.Write((int)qH.type); if (Game1.networkSession != null) { if (Game1.networkSession.IsHost) { InputQueue.add(qH); } if (Game1.networkSession.IsHost) { LocalNetworkGamer server = (LocalNetworkGamer)Game1.networkSession.Host; server.SendData(packetWriter2, SendDataOptions.InOrder); } } foreach (LocalNetworkGamer gamer in Game1.networkSession.LocalGamers) { if (!Game1.networkSession.IsHost) { InputQueue.add(qH); // Write our latest input state into a network packet. //packetWriter2.Write(qH.inSeqNum); //packetWriter2.Write(qH.outSeqNum); //packetWriter2.Write((int)qH.type); // packetWriter2.Write((int)qH.data); // Send our input data to the server.// gamer.SendData(packetWriter2, SendDataOptions.InOrder, Game1.networkSession.Host); } } break; case Queue_type.QUEUE_SHIP_MISSILE: packetWriter2.Write(qH.inSeqNum); packetWriter2.Write(qH.outSeqNum); packetWriter2.Write((int)qH.type); if (Game1.networkSession != null) { if (Game1.networkSession.IsHost) { InputQueue.add(qH); } if (Game1.networkSession.IsHost) { LocalNetworkGamer server = (LocalNetworkGamer)Game1.networkSession.Host; server.SendData(packetWriter2, SendDataOptions.InOrder); } } foreach (LocalNetworkGamer gamer in Game1.networkSession.LocalGamers) { if (!Game1.networkSession.IsHost) { InputQueue.add(qH); // Write our latest input state into a network packet. //packetWriter2.Write(qH.inSeqNum); //packetWriter2.Write(qH.outSeqNum); //packetWriter2.Write((int)qH.type); // packetWriter2.Write((int)qH.data); // Send our input data to the server. gamer.SendData(packetWriter2, SendDataOptions.InOrder, Game1.networkSession.Host); } } break; case Queue_type.QUEUE_SHIP_IMPULSE: packetWriter2.Write(qH.inSeqNum); packetWriter2.Write(qH.outSeqNum); packetWriter2.Write((int)qH.type); Ship_Impulse_Message sim = (Ship_Impulse_Message)qH.data; packetWriter2.Write((Vector2)sim.impulse); Debug.WriteLine("Outputq - sim.impulse = " + sim.impulse); Debug.WriteLine("Outputq - sim.X = " + sim.impulse.X); Debug.WriteLine("Outputq - sim.Y = " + sim.impulse.Y); if (Game1.networkSession != null) { if (Game1.networkSession.IsHost) { InputQueue.add(qH); } if (Game1.networkSession.IsHost) { LocalNetworkGamer server = (LocalNetworkGamer)Game1.networkSession.Host; server.SendData(packetWriter2, SendDataOptions.InOrder); } } foreach (LocalNetworkGamer gamer in Game1.networkSession.LocalGamers) { if (!Game1.networkSession.IsHost) { InputQueue.add(qH); ///just added 207pm // Write our latest input state into a network packet. //packetWriter2.Write(qH.inSeqNum); //packetWriter2.Write(qH.outSeqNum); //packetWriter2.Write((int)qH.type); //Ship_Impulse_Message sim = (Ship_Impulse_Message)qH.data; //packetWriter2.Write((Vector2)sim.impulse); // Send our input data to the server. gamer.SendData(packetWriter2, SendDataOptions.InOrder, Game1.networkSession.Host); } } break; case Queue_type.QUEUE_SHIP_ROT: //send to input queue //packetWriter2.Write(qH.inSeqNum); // packetWriter2.Write(qH.outSeqNum); // packetWriter2.Write((int)qH.type); // Ship_Impulse_Message sim = (Ship_Impulse_Message)qH.data; // packetWriter2.Write((Vector2)sim.impulse); packetWriter2.Write(qH.inSeqNum); packetWriter2.Write(qH.outSeqNum); packetWriter2.Write((int)qH.type); Ship_Rot_Message rotMessage = (Ship_Rot_Message)qH.data; packetWriter2.Write(rotMessage.rotation); Debug.WriteLine(" OutputQ - rotMessage.rotation = " + rotMessage.rotation); if (Game1.networkSession != null) { if (Game1.networkSession.IsHost) { InputQueue.add(qH); } } if (Game1.networkSession.IsHost) { LocalNetworkGamer server = (LocalNetworkGamer)Game1.networkSession.Host; server.SendData(packetWriter2, SendDataOptions.InOrder); } foreach (LocalNetworkGamer gamer in Game1.networkSession.LocalGamers) { if (!Game1.networkSession.IsHost) { // Write our latest input state into a network packet. //packetWriter2.Write(qH.inSeqNum); //packetWriter2.Write(qH.outSeqNum); //packetWriter2.Write((int)qH.type); //Ship_Rot_Message rotMessage = (Ship_Rot_Message)qH.data; //packetWriter2.Write(rotMessage.rotation); // packetWriter2.Write((Vector2)qH.data); // Send our input data to the server. gamer.SendData(packetWriter2, SendDataOptions.InOrder, Game1.networkSession.Host); } } break; case Queue_type.QUEUE_PHYSICS_BUFFER: if (Game1.networkSession != null) { if (Game1.networkSession.IsHost) { InputQueue.add(qH); } } if (Game1.networkSession != null) { // packetWriter2.Write(gamer.Id); packetWriter2.Write(qH.inSeqNum); packetWriter2.Write(qH.outSeqNum); packetWriter2.Write((int)qH.type); //Debug.WriteLine("qH.inSeqNum from PushToNetwork = " + qH.inSeqNum); //Debug.WriteLine("qH.outSeqNum from PushToNetwork = " + qH.outSeqNum); //Debug.WriteLine("qH.type from PushToNetwork = " + qH.type); //PhysicsBuffer[] localPhysicsBuff = new PhysicsBuffer[]; PhysicsBuffer_Message PhysicsBuff_MessageInQueueHdr = new PhysicsBuffer_Message((PhysicsBuffer_Message)qH.data); //get the count from the buffer packetWriter2.Write(PhysicsBuff_MessageInQueueHdr.count); //Debug.WriteLine("PhysicsBuff_MessageInQueueHdr.count = " + PhysicsBuff_MessageInQueueHdr.count); PhysicsBuffer[] localPhysicsBuff = new PhysicsBuffer[PhysicsBuff_MessageInQueueHdr.count]; //get the the physics buffer struct of id, position , rotation out localPhysicsBuff = PhysicsBuff_MessageInQueueHdr.pBuff; for (int j = 0; j < PhysicsBuff_MessageInQueueHdr.count; j++) { PhysicsBuffer myPhysicsBuffer = new PhysicsBuffer(); myPhysicsBuffer.id = localPhysicsBuff[j].id; myPhysicsBuffer.position = localPhysicsBuff[j].position; myPhysicsBuffer.rotation = localPhysicsBuff[j].rotation; packetWriter2.Write(myPhysicsBuffer.id); packetWriter2.Write(myPhysicsBuffer.position); packetWriter2.Write(myPhysicsBuffer.rotation); //Debug.WriteLine("myPhysicsBuffer.id = " + myPhysicsBuffer.id); //Debug.WriteLine("myPhysicsBuffer.position = " + myPhysicsBuffer.position); //Debug.WriteLine("myPhysicsBuffer.rotation = " + myPhysicsBuffer.rotation); } if (Game1.networkSession != null) { if (Game1.networkSession.IsHost) { LocalNetworkGamer server = (LocalNetworkGamer)Game1.networkSession.Host; server.SendData(packetWriter2, SendDataOptions.InOrder); } } // if this is the client machine, need to send to server } // Send our input data to the server. //if (Game1.networkSession != null) //{ // if (Game1.networkSession.IsHost) // { // foreach (LocalNetworkGamer gamer in Game1.networkSession.LocalGamers) // { // gamer.SendData(packetWriter2, // SendDataOptions.InOrder, Game1.networkSession.); // } // } //} break; case Queue_type.QUEUE_EVENT: if (Game1.networkSession != null) { if (Game1.networkSession.IsHost) { InputQueue.add(qH); } } if (Game1.networkSession != null) { // Write our latest input state into a network packet. packetWriter2.Write(qH.inSeqNum); packetWriter2.Write(qH.outSeqNum); packetWriter2.Write((int)qH.type); // packetWriter2.Write((int)qH.data); Event_Message eventMsg = new Event_Message((Event_Message)qH.data); packetWriter2.Write(eventMsg.GameID_A); // Debug.WriteLine("eventMsg " + eventMsg.GameID_A); packetWriter2.Write(eventMsg.GameID_B); // Debug.WriteLine("eventMsg " + eventMsg.GameID_B); packetWriter2.Write(eventMsg.collision_pt); // Debug.WriteLine("eventMsg " + eventMsg.collision_pt); if (Game1.networkSession != null) { if (Game1.networkSession.IsHost) { LocalNetworkGamer server = (LocalNetworkGamer)Game1.networkSession.Host; server.SendData(packetWriter2, SendDataOptions.InOrder); } } // Send our input data to the server. // gamer.SendData(packetWriter2, // SendDataOptions.InOrder, Game1.networkSession.Host); } //foreach (LocalNetworkGamer gamer in Game1.networkSession.LocalGamers) //{ // if (!Game1.networkSession.IsHost) // { // // Write our latest input state into a network packet. // packetWriter2.Write(qH.inSeqNum); // packetWriter2.Write(qH.outSeqNum); // packetWriter2.Write((int)qH.type); // // Ship_Rot_Message rotMessage = (Ship_Rot_Message)qH.data; // // packetWriter2.Write(rotMessage.rotation); // // packetWriter2.Write((Vector2)qH.data); // // Send our input data to the server. // gamer.SendData(packetWriter2, // SendDataOptions.InOrder, Game1.networkSession.Host); // } //} break; default: break; } } }