/// <summary> /// Helper for updating a locally controlled gamer. /// </summary> void UpdateLocalGamer(LocalNetworkGamer gamer, GameTime gameTime, bool sendPacketThisFrame) { // Look up what tank is associated with this local player. Tank tank = gamer.Tag as Tank; // Read the inputs controlling this tank. PlayerIndex playerIndex = gamer.SignedInGamer.PlayerIndex; Vector2 tankInput; Vector2 turretInput; ReadTankInputs(playerIndex, out tankInput, out turretInput); // Update the tank. tank.UpdateLocal(tankInput, turretInput); // Periodically send our state to everyone in the session. if (sendPacketThisFrame) { tank.WriteNetworkPacket(packetWriter, gameTime); gamer.SendData(packetWriter, SendDataOptions.InOrder); } }
protected virtual void UpdateLocalGamer(LocalNetworkGamer gamer, GameTime gameTime, bool sendPacketThisFrame) { Player p = gamer.Tag as Player; if (p != null) { //p.UpdateLocalPlayer(gameTime); // Periodically send our state to everyone in the session. if (sendPacketThisFrame) { p.WriteNetworkPacket(packetWriter, gameTime); gamer.SendData(packetWriter, SendDataOptions.InOrder); } } }
private void UpdateLocalGamer(LocalNetworkGamer gamer) { while (outQueue.Count > 0) { QueueHdr hdr = outQueue.Dequeue(); switch (hdr.type) { default: case HeaderType.Location: int lType = (int)hdr.type; LocationHdr lHdr = (LocationHdr)hdr.pHdr; packetWriter.Write(lType); packetWriter.Write(lHdr.goIndex); packetWriter.Write(lHdr.position); packetWriter.Write(lHdr.rotation); break; case HeaderType.Input: int iType = (int)hdr.type; InputHdr iHdr = (InputHdr)hdr.pHdr; packetWriter.Write(iType); packetWriter.Write((int)iHdr.input); packetWriter.Write((int)iHdr.player); if (iHdr.input == InputType.Collision) { ColHdr cHdr = (ColHdr)iHdr.colInfo; packetWriter.Write(cHdr.goID1); packetWriter.Write(cHdr.goID2); packetWriter.Write(cHdr.pos); } break; } gamer.SendData(packetWriter, SendDataOptions.InOrder, Game1.Network.Session.Host); } }
/// <summary> /// Helper for updating a locally controlled gamer. /// </summary> void UpdateLocalGamer (LocalNetworkGamer gamer) { // Look up what tank is associated with this local player. Tank localTank = gamer.Tag as Tank; if (localTank != null) { // Update the tank. ReadTankInputs(localTank, gamer.SignedInGamer.PlayerIndex); localTank.Update(); // Write the tank state into a network packet. packetWriter.Write(localTank.Position); packetWriter.Write(localTank.TankRotation); packetWriter.Write(localTank.TurretRotation); // Send the data to everyone in the session. gamer.SendData(packetWriter, SendDataOptions.InOrder); } }
/// <summary> /// Helper for updating a locally controlled gamer. /// </summary> void UpdateLocalGamer(LocalNetworkGamer gamer) { // Look up what tank is associated with this local player. Player player = gamer.Tag as Player; GamePadState gamePad = GamePad.GetState(gamer.SignedInGamer.PlayerIndex); player.X = gamePad.ThumbSticks.Left.X; player.Y = gamePad.ThumbSticks.Left.Y; // Write the tank state into a network packet. packetWriter.Write(player.Position); // Send the data to everyone in the session. gamer.SendData(packetWriter, SendDataOptions.InOrder); }
void UpdateLocalGamer(LocalNetworkGamer gamer, Tank t, GameTime gameTime) { hostFireShot = false; timer = timer + (float)gameTime.ElapsedGameTime.TotalMilliseconds; singleShotTimer = singleShotTimer + (float)gameTime.ElapsedGameTime.TotalMilliseconds; if (timer > interval) { shotCount = 0; timer = 0; } bool shotFired = false; Bullet bul = null; t.Velocity = Vector2.Zero; if (input.IsButtonDown(Buttons.A)) { buttonADown = true; } if (!input.IsButtonDown(Buttons.A)) { buttonADown = false; } // east if (input.IsButtonDown(Buttons.LeftThumbstickRight)) { // north east if (input.IsButtonDown(Buttons.LeftThumbstickUp)) { t.Velocity = new Vector2(3.0f, -3.0f); t.Rotation = -(Math.PI / 4); } // south east else if (input.IsButtonDown(Buttons.LeftThumbstickDown)) { t.Velocity = new Vector2(3.0f, 3.0f); t.Rotation = (Math.PI / 4); } // east else { t.Velocity = new Vector2(3.0f, 0.0f); t.Rotation = 0; } } // west if (input.IsButtonDown(Buttons.LeftThumbstickLeft)) { // north west if (input.IsButtonDown(Buttons.LeftThumbstickUp)) { t.Velocity = new Vector2(-3.0f, -3.0f); t.Rotation = -(3 * Math.PI / 4); } // south west else if (input.IsButtonDown(Buttons.LeftThumbstickDown)) { t.Velocity = new Vector2(-3.0f, 3.0f); t.Rotation = (3 * Math.PI / 4); } // west else { t.Velocity = new Vector2(-3.0f, 0.0f); t.Rotation = Math.PI; } } if (input.IsButtonDown(Buttons.LeftThumbstickDown) && (!input.IsButtonDown(Buttons.LeftThumbstickLeft)) && (!input.IsButtonDown(Buttons.LeftThumbstickRight))) { t.Velocity = new Vector2(0.0f, 3.0f); t.Rotation = Math.PI / 2; } if (input.IsButtonDown(Buttons.LeftThumbstickUp) && (!input.IsButtonDown(Buttons.LeftThumbstickLeft)) && (!input.IsButtonDown(Buttons.LeftThumbstickRight))) { t.Velocity = new Vector2(0.0f, -3.0f); t.Rotation = -Math.PI / 2; } if (input.IsButtonDown(Buttons.B)) { if (singleShotTimer > singleShotInterval && shotCount <= 2) { shotFired = true; double a = Math.Cos(t.Rotation); double c = Math.Sin(t.Rotation); Bullet b = new Bullet(); b.Position = t.Position; b.Velocity = new Vector2((float)(3 * a), (float)(3 * c)); b.Position += 5 * b.Velocity; t.bullets.Add(b); bul = b; singleShotTimer = 0; shotCount++; totalShotsFired++; hostFireShot = true; hostBulletPosition = b.Position; hostBulletVelocity = b.Velocity; } } foreach (Bullet b in t.bullets) { b.Position += b.Velocity; } foreach (NetworkGamer g in network.Session.AllGamers) { if (!g.IsLocal) { Tank t2 = g.Tag as Tank; foreach (Bullet b in t2.bullets) { b.Position += b.Velocity; } } } Vector2 newPos = t.Position + t.Velocity; bool changePosX = true; bool changePosY = true; if (t.PositionX <= 26) { if (map.map[(int)t.MapScreenX, (int)t.MapScreenY].WestWall == 49) { changePosX = false; t.PositionX = 27; } } if (t.PositionX >= 240 - 26) { if (map.map[(int)t.MapScreenX, (int)t.MapScreenY].EastWall == 49) { changePosX = false; t.PositionX = 240 - 27; } } if (t.positionY <= 26) { if (map.map[(int)t.MapScreenX, (int)t.MapScreenY].NorthWall == 49) { changePosY = false; t.positionY = 27; } } if (t.positionY >= 240 - 27) { if (map.map[(int)t.MapScreenX, (int)t.MapScreenY].SouthWall == 49) { changePosY = false; t.positionY = 240 - 28; } } if (changePosX) t.PositionX = t.PositionX + t.Velocity.X; if (changePosY) t.positionY = t.positionY + t.Velocity.Y; if ((t.Position.X > (240 - 16))) // on the right edge of the screen { t.PositionX = 16; t.GlobalPositionX = t.GlobalPositionX + 16; t.MapScreenX++; } if ((t.Position.X < (16))) // on the left edge of the screen { t.PositionX = 240 - 16; t.GlobalPositionX = t.GlobalPositionX - 16; t.MapScreenX--; } if ((t.Position.Y > (240 - 16))) // on the bottom edge of the screen { t.positionY = 16; t.GlobalPositionY = t.GlobalPositionY + 16; t.MapScreenY++; } if ((t.positionY < 16)) { t.positionY = 240 - 16; t.GlobalPositionY = t.GlobalPositionY - 16; t.MapScreenY--; } t.GlobalPosition = t.GlobalPosition + t.Velocity; Bullet bulletToDelete = new Bullet(); bool bulletHit = false; foreach (NetworkGamer gamer2 in network.Session.AllGamers) { if (!gamer2.IsLocal) { Tank gamerTank = gamer2.Tag as Tank; Rectangle tankRect = new Rectangle((int)gamerTank.PositionX, (int)gamerTank.positionY, 32, 32); boundRect = new Rectangle((int)gamerTank.PositionX - 16, (int)gamerTank.positionY - 16, 32, 32); foreach (Bullet b in t.bullets) { Rectangle bulletRect = new Rectangle((int)b.PositionX, (int)b.positionY, 5, 5); if (tankRect.Intersects(bulletRect) && !bulletHit) { b.PositionX = b.positionY = 400; bulletToDelete = b; t.Score++; bulletHit = true; } } } } t.bullets.Remove(bulletToDelete); if (buttonADown) { WorldView = true; } else { WorldView = false; } network.PacketWriter.Write(shotFired); if (shotFired) { network.PacketWriter.Write(bul.Position); network.PacketWriter.Write(bul.Velocity); } network.PacketWriter.Write(t.Position); network.PacketWriter.Write(t.Rotation); network.PacketWriter.Write(t.Score); network.PacketWriter.Write(t.MapScreenX); network.PacketWriter.Write(t.MapScreenY); gamer.SendData(network.PacketWriter, SendDataOptions.InOrder); }
//Client sending local player's ship data to server private void SendPlayerShipData(LocalNetworkGamer gamer) { int playerShipCount = EntityManager.get().playerShips.Count; PlayerShip ship = null; for (int i = 0; i < playerShipCount; i++) { if (((PlayerShip)EntityManager.get().playerShips[i]).ownerID == gamer.Id) { ship = (PlayerShip)EntityManager.get().playerShips[i]; } } //If we didn't find the ship for the local player... if (ship == null) { return; } packetWriter.Write((Int32)GamePacketType.PlayerShip); packetWriter.Write(playerShipCount); packetWriter.Write(ship.entityID); packetWriter.Write((Int32)EntityEventType.State); packetWriter.Write(ship.hull); packetWriter.Write(ship.position); packetWriter.Write(ship.heading); packetWriter.Write(ship.velocity); packetWriter.Write(ship.rotation); // Send the data to server of the the session. gamer.SendData(packetWriter, SendDataOptions.InOrder, session.Host); }
private void handleChangeToTeam(LocalNetworkGamer gamer, int team) { if ((int)gamer.Tag == team) return; gamer.Tag = team; _packetWriter.Write(team); gamer.SendData(_packetWriter, SendDataOptions.InOrder); }
//Server sending player ship data to all other clients private void BroadCastPlayerShipData(LocalNetworkGamer server) { packetWriter.Write((Int32)GamePacketType.PlayerShip); int playerShipCount = EntityManager.get().playerShips.Count; packetWriter.Write(playerShipCount); for (int i = 0; i < playerShipCount; i++) { PlayerShip ship = (PlayerShip)EntityManager.get().playerShips[i]; packetWriter.Write(ship.entityID); packetWriter.Write((Int32)EntityEventType.State); packetWriter.Write(ship.hull); packetWriter.Write(ship.position); packetWriter.Write(ship.heading); packetWriter.Write(ship.velocity); packetWriter.Write(ship.rotation); } // Send data of all player ships to all clients server.SendData(packetWriter, SendDataOptions.InOrder); }
/// <summary> /// Helper for updating a locally controlled gamer. /// </summary> void UpdateLocalGamer(LocalNetworkGamer gamer, GameTime TheGameTime) { // Look up what tank is associated with this local player. Player localPlayer = gamer.Tag as Player; // Update the tank. mPlayerSprite.UpdateMovement(aCurrentKeyboardState); localPlayer.Update(TheGameTime, this.Content); // Write the tank state into a network packet. packetWriter.Write(localPlayer.Position); packetWriter.Write(localPlayer.Rotation); //packetWriter.Write(localTank.TurretRotation); // Send the data to everyone in the session. gamer.SendData(packetWriter, SendDataOptions.InOrder); }
private void RemotePlayersSendPackets(LocalNetworkGamer gamer) { if (!networkSession.IsHost) { // Write our latest input state into a network packet. packetWriter.Write(Player.camera.Position); packetWriter.Write((double)Player.camera.HeadingDegrees); packetWriter.Write((double)Player.camera.PitchDegrees); packetWriter.Write((Int16)Player.CurrentGun); //packetWriter.Write((Int16)Player.CurrentHealth); packetWriter.Write((Int16)Shots.Shots.Count); foreach (Shot shot in Shots.Shots) { packetWriter.Write((Int16)shot.Caliber); packetWriter.Write((Int16)shot.TimeToDie); packetWriter.Write((Vector3)shot.Tracer.Direction); packetWriter.Write((Vector3)shot.Tracer.Position); } packetWriter.Write((Int16)HappyBarMode); packetWriter.Write((Boolean)Player.Dead); gamer.SendData(packetWriter, SendDataOptions.InOrder, networkSession.Host); } }
/// <summary> /// Helper for updating a locally controlled gamer. /// </summary> void UpdateLocalGamer(LocalNetworkGamer gamer) { // Look up what tank is associated with this local player, // and read the latest user inputs for it. The server will // later use these values to control the tank movement. NetworkPlayer localPlayer = gamer.Tag as NetworkPlayer; localPlayer.Update(); // Only send if we are not the server. There is no point sending packets // to ourselves, because we already know what they will contain! if (!networkSession.IsHost) { // Write our latest input state into a network packet. packetWriter.Write(localPlayer.Position); // Send our input data to the server. gamer.SendData(packetWriter, SendDataOptions.InOrder, networkSession.Host); } }
void UpdateLocalGamer(LocalNetworkGamer gamer, GameTime gameTime) { /* Look up what player is associated with this local player, * and read the latest user inputs for it. The server will later * use these values to control the player movement. */ Player localPlayer = gamer.Tag as Player; Vector2 positionInput; Vector4 rotationInput; ReadPlayerInput(gamer.SignedInGamer.PlayerIndex, out positionInput, out rotationInput); /* Only send if we are not the server. There is no point sending packets * to ourselves, because we already know what they will contain */ localPlayer.UpdateLocal(positionInput, rotationInput, gameTime); if (!_networkSession.IsHost) { foreach (Gamer remoteGamer in _networkSession.RemoteGamers) { Player player = remoteGamer.Tag as Player; player.UpdateRemote(_framesBetweenPackets, _enablePrediction, gameTime); } localPlayer.ClientWriteNetworkPacket(_packetWriter); gamer.SendData(_packetWriter, SendDataOptions.InOrder, _networkSession.Host); } }