public static void Process3() { InputQueue pInputQueue = new InputQueue(); int count = inQ.Count; for (int i = 0; i < count; i++) { //read header QueueHdr qH = inQ.Dequeue(); Message msg = null; switch (qH.type) { case Queue_type.QUEUE_SHIP_BOMB: msg = new Ship_Create_Bomb_Message((Ship_Create_Bomb_Message)qH.data); break; case Queue_type.QUEUE_SHIP_MISSILE: msg = new Ship_Create_Missile_Message((Ship_Create_Missile_Message)qH.data); break; case Queue_type.QUEUE_SHIP_IMPULSE: msg = new Ship_Impulse_Message((Ship_Impulse_Message)qH.data); break; case Queue_type.QUEUE_SHIP_ROT: msg = new Ship_Rot_Message((Ship_Rot_Message)qH.data); break; case Queue_type.QUEUE_PHYSICS_BUFFER: msg = new PhysicsBuffer_Message((PhysicsBuffer_Message)qH.data); break; case Queue_type.QUEUE_EVENT: msg = new Event_Message((Event_Message)qH.data); break; } msg.execute(); } }
private void checkInput() { newState = Keyboard.GetState(); P1newPadState = GamePad.GetState(PlayerIndex.One); P2newPadState = GamePad.GetState(PlayerIndex.Two); if (oldState.IsKeyDown(Keys.D) || P1oldPadState.IsButtonDown(Buttons.DPadRight)) { player1.playerShip.physicsObj.body.Rotation += 0.1f; Ship_Rot_Message msg = new Ship_Rot_Message(player1, 0.1f); Debug.WriteLine("D player1.playerShip.physicsObj.body.Rotation = " + player1.playerShip.physicsObj.body.Rotation); OutputQueue.add(msg); } if (oldState.IsKeyDown(Keys.A) || P1oldPadState.IsButtonDown(Buttons.DPadLeft)) { player1.playerShip.physicsObj.body.Rotation -= 0.1f; Ship_Rot_Message msg = new Ship_Rot_Message(player1, -0.1f); Debug.WriteLine("A player1.playerShip.physicsObj.body.Rotation = " + player1.playerShip.physicsObj.body.Rotation); OutputQueue.add(msg); } if (oldState.IsKeyDown(Keys.W) || P1oldPadState.IsButtonDown(Buttons.DPadUp)) { Ship Player1Ship = player1.playerShip; Vector2 direction = new Vector2((float)(Math.Cos(Player1Ship.physicsObj.body.GetAngle())), (float)(Math.Sin(Player1Ship.physicsObj.body.GetAngle()))); Debug.WriteLine("direction - W - = " + direction); //Vector2 direction = new Vector2(.6f, .5f); direction.Normalize(); float x = (float)(Math.Cos(Player1Ship.physicsObj.body.GetAngle())); float y = (float)(Math.Sin(Player1Ship.physicsObj.body.GetAngle())); Debug.WriteLine("x = " + x); Debug.WriteLine("y = " + y); direction *= shipSpeed; Debug.WriteLine("direction - W - afterNormal = " + direction); // No action, send a message thru queue //Player1Ship.physicsObj.body.ApplyLinearImpulse(direction, Player1Ship.physicsObj.body.GetWorldCenter()); Ship_Impulse_Message msg = new Ship_Impulse_Message(player1, direction); OutputQueue.add(msg); } if ((oldState.IsKeyDown(Keys.X) && newState.IsKeyUp(Keys.X)) || (P1oldPadState.IsButtonDown(Buttons.A) && P1newPadState.IsButtonUp(Buttons.A))) { if (player1.state == PlayerState.alive && player1.missileAvailable()) { // player1.createMissile(); Ship_Create_Missile_Message msg = new Ship_Create_Missile_Message(player1); OutputQueue.add(msg); } } if (oldState.IsKeyDown(Keys.C) && newState.IsKeyUp(Keys.C) || (P1oldPadState.IsButtonDown(Buttons.B) && P1newPadState.IsButtonUp(Buttons.B))) { if (player1.state == PlayerState.alive && BombManager.Instance().bombAvailable(PlayerID.one)) { // GameObjManager.Instance().createBomb(PlayerID.one); Ship_Create_Bomb_Message msg = new Ship_Create_Bomb_Message(player1); OutputQueue.add(msg); } } if (oldState.IsKeyDown(Keys.Right) || P2oldPadState.IsButtonDown(Buttons.DPadRight)) { // player2.playerShip.physicsObj.body.Rotation += 0.1f; Ship_Rot_Message msg = new Ship_Rot_Message(player2, 0.1f); Debug.WriteLine("Right player2.playerShip.physicsObj.body.Rotation = " + player2.playerShip.physicsObj.body.Rotation); OutputQueue.add(msg); } if (oldState.IsKeyDown(Keys.Left) || P2oldPadState.IsButtonDown(Buttons.DPadLeft)) { //player2.playerShip.physicsObj.body.Rotation -= 0.1f; Ship_Rot_Message msg = new Ship_Rot_Message(player2, -0.1f); Debug.WriteLine("Left player1.playerShip.physicsObj.body.Rotation = " + player2.playerShip.physicsObj.body.Rotation); OutputQueue.add(msg); } if (oldState.IsKeyDown(Keys.Up) || P2oldPadState.IsButtonDown(Buttons.DPadUp)) { Ship Player2Ship = player2.playerShip; Vector2 direction = new Vector2((float)(Math.Cos(Player2Ship.physicsObj.body.GetAngle())), (float)(Math.Sin(Player2Ship.physicsObj.body.GetAngle()))); //Vector2 direction = new Vector2(-.5f, .5f); Debug.WriteLine("direction - up - = " + direction); float x = (float)(Math.Cos(Player2Ship.physicsObj.body.GetAngle())); float y = (float)(Math.Sin(Player2Ship.physicsObj.body.GetAngle())); Debug.WriteLine("x = " + x); Debug.WriteLine("y = " + y); direction.Normalize(); direction *= shipSpeed; // Player2Ship.physicsObj.body.ApplyLinearImpulse(direction, Player2Ship.physicsObj.body.GetWorldCenter()); // No action, send a message thru queue //Player1Ship.physicsObj.body.ApplyLinearImpulse(direction, Player1Ship.physicsObj.body.GetWorldCenter()); Ship_Impulse_Message msg = new Ship_Impulse_Message(player2, direction); OutputQueue.add(msg); } if ((oldState.IsKeyDown(Keys.OemQuestion) && newState.IsKeyUp(Keys.OemQuestion)) || (P2oldPadState.IsButtonDown(Buttons.A) && P2newPadState.IsButtonUp(Buttons.A))) { if (player2.state == PlayerState.alive && player2.missileAvailable()) { // player2.createMissile(); Ship_Create_Missile_Message msg = new Ship_Create_Missile_Message(player2); OutputQueue.add(msg); } } if (oldState.IsKeyDown(Keys.OemPeriod) && newState.IsKeyUp(Keys.OemPeriod) || (P2oldPadState.IsButtonDown(Buttons.B) && P2newPadState.IsButtonUp(Buttons.B))) { if (player2.state == PlayerState.alive && BombManager.Instance().bombAvailable(PlayerID.two)) { // GameObjManager.Instance().createBomb(PlayerID.two); Ship_Create_Bomb_Message msg = new Ship_Create_Bomb_Message(player2); OutputQueue.add(msg); } } else { } P1oldPadState = P1newPadState; P2oldPadState = P2newPadState; oldState = newState; }
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; } } } }
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) { // Look up the tank associated with whoever sent this packet. //Tank remoteTank = sender.Tag as Tank; Ship remoteShip = sender.Tag as Ship; // Read the latest inputs controlling this tank. // remoteTank.TankInput = packetReader.ReadVector2(); // remoteTank.TurretInput = packetReader.ReadVector2(); QueueHdr qH; qH.inSeqNum = packetReader.ReadInt32(); qH.outSeqNum = packetReader.ReadInt32(); qH.type = (Queue_type)packetReader.ReadInt32(); //qH.data = packetReader.ReadVector2(); switch (qH.type) { case Queue_type.QUEUE_SHIP_BOMB: Ship_Create_Bomb_Message msg = new Ship_Create_Bomb_Message(player1); qH.data = msg; InputQueue.add(qH); break; case Queue_type.QUEUE_SHIP_MISSILE: Ship_Create_Missile_Message msg2 = new Ship_Create_Missile_Message(player1); qH.data = msg2; InputQueue.add(qH); break; case Queue_type.QUEUE_SHIP_IMPULSE: Ship_Impulse_Message sim = new Ship_Impulse_Message(player1, packetReader.ReadVector2()); // qH.data = packetReader.ReadVector2(); qH.data = sim; // Ship_Impulse_Message msg3 = new Ship_Impulse_Message((Ship_Impulse_Message)qH.data); // msg = new Ship_Impulse_Message((Ship_Impulse_Message)qH.data); // qH.data = msg3; InputQueue.add(qH); break; case Queue_type.QUEUE_SHIP_ROT: Ship_Rot_Message rotMessage = new Ship_Rot_Message(player1, packetReader.ReadSingle()); // float? ReadDouble() // qH.data = packetReader.ReadVector2(); qH.data = rotMessage; // Ship_Rot_Message msg4 = new Ship_Rot_Message((Ship_Rot_Message)qH.data); // qH.data = msg4; InputQueue.add(qH); // msg = new Ship_Rot_Message((Ship_Rot_Message)qH.data); break; case Queue_type.QUEUE_EVENT: break; } } } }
public Ship_Rot_Message(Ship_Rot_Message msg) : base(msg.id) { this.rotation = msg.rotation; }
// 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; } } }