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(); } }
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; } } } }
public Event_Message(Event_Message msg) { this.GameID_A = msg.GameID_A; this.GameID_B = msg.GameID_B; this.collision_pt = msg.collision_pt; }
// 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; } } }
public void BeginContact(Contact contact) { GameObject A = (GameObject)contact.GetFixtureA().GetUserData(); GameObject B = (GameObject)contact.GetFixtureB().GetUserData(); Manifold manifold; Transform xfA; Transform xfB; float radiusA; float radiusB; contact.GetFixtureA().GetBody().GetTransform(out xfA); contact.GetFixtureB().GetBody().GetTransform(out xfB); radiusA = contact.GetFixtureA().GetShape()._radius; radiusB = contact.GetFixtureB().GetShape()._radius; contact.GetManifold(out manifold); WorldManifold worldManifold = new WorldManifold(ref manifold, ref xfA, radiusA, ref xfB, radiusB); Vector2 ptA = worldManifold._points[0]; Vector2 ptB = worldManifold._points[1]; // adding this for networking if (A.GameID == 1) { int x = 0; } System.Console.Write("v--> sending mc point A:{0} B:{1} {2} {3}\n", A.GameID, B.GameID, ptA, ptB); Debug.Assert(A != null); Debug.Assert(B != null); Event_Message msg = new Event_Message(A.GameID, B.GameID, ptA); OutputQueue.add(msg); //System.Console.Write(" point {0} {1}\n", ptA, ptB); // if (A.CollideAvailable == true && B.CollideAvailable == true) {/* * if (A.type < B.type) * { * A.Accept(B, ptA); * } * else * { * B.Accept(A, ptA); * }*/ } //if (A.type == GameObjType.p1missiles || A.type == GameObjType.p2missiles) //{ // A.CollideAvailable = false; //} //if (B.type == GameObjType.p1missiles || B.type == GameObjType.p2missiles) //{ // B.CollideAvailable = false; //} }