Exemple #1
0
        void ServerReadInputFromClients(LocalNetworkGamer gamer)
        {
            while (gamer.IsDataAvailable)
            {
                NetworkGamer sender;
                gamer.ReceiveData(network.PacketReader, out sender);

                if (!sender.IsLocal)
                {
                    Tank t = sender.Tag as Tank;
                    bool shotFired = network.PacketReader.ReadBoolean();
                    if (shotFired)
                    {
                        Bullet b = new Bullet();
                        b.Position = network.PacketReader.ReadVector2();
                        b.Velocity = network.PacketReader.ReadVector2();
                        t.bullets.Add(b);
                    }

                    t.Position = network.PacketReader.ReadVector2();
                    t.Rotation = network.PacketReader.ReadDouble();
                    t.Score = network.PacketReader.ReadInt32();
                    t.MapScreenX = network.PacketReader.ReadInt32();
                    t.MapScreenY = network.PacketReader.ReadInt32();
                }
            }
        }
Exemple #2
0
        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);
        }
Exemple #3
0
 void ClientReadGameStateFromServer(LocalNetworkGamer gamer)
 {
     while (gamer.IsDataAvailable)
     {
         NetworkGamer sender;
         gamer.ReceiveData(network.PacketReader, out sender);
         foreach (NetworkGamer remoteGamer in network.Session.AllGamers)
         {
             Tank t = remoteGamer.Tag as Tank;
             t.Position = network.PacketReader.ReadVector2();
             t.Rotation = network.PacketReader.ReadDouble();
             bool a = network.PacketReader.ReadBoolean();
             if (a)
             {
                 Bullet b = new Bullet();
                 b.Position = network.PacketReader.ReadVector2();
                 b.Velocity = network.PacketReader.ReadVector2();
                 t.bullets.Add(b);
             }
             Bullet c = new Bullet();
         }
     }
 }