Example #1
0
        public void Update(OldNewInput input, Dictionary <string, Tank> tanks)
        {
            background.Update();

            ipBtn.Update(input);             // Orkar inte bry mig om polyformism..
            nameBtn.Update(input);
            colorBtn.Update(input);
            readyBtn.Update(input);
            connectBtn.Update(input);
            disconnectBtn.Update(input);
            exitBtn.Update(input);
            muteMusicBtn.Update(input);
            fullScreenBtn.Update(input);
            playerList.ForEach(p => p.Update());

            NetIncomingMessage incom;                                      // MEssage that will contain the message comming from the server

            if (connected && (incom = Game1.Client.ReadMessage()) != null) // Are there any new messanges?
            {
                if (incom.MessageType == NetIncomingMessageType.Data)      // Is it a "data" message?
                {
                    switch (incom.ReadByte())
                    {
                    case (byte)PacketTypes.LOBBYPLAYERLIST:                             // Handle a list of players (right side)
                        Debug.WriteLine("Cl-Received the playerlist");

                        bool animate = playerList.Count == 0 ? true : false;                            // Animate if the playerlist is new and empty
                        if (playerList.Count == 0 || playerList[0].Statee == PlayerListItem.State.NONE) // To keep the game from removing the initial animation
                        {
                            playerList.Clear();                                                         // Clear the "old" data

                            int incommingPlayers = incom.ReadInt32();
                            for (int k = 1; k <= incommingPlayers; k++)
                            {
                                string name  = incom.ReadString();
                                Color  color = new Color(incom.ReadByte(), incom.ReadByte(), incom.ReadByte());
                                bool   ready = incom.ReadBoolean();

                                playerList.Add(new PlayerListItem(content, new Vector2(Game1.ScreenRec.Width - 450, k * 50), name, color, ready, animate));
                            }
                            for (int i = incommingPlayers + 1; i <= 8; i++)
                            {
                                playerList.Add(new PlayerListItem(content, new Vector2(Game1.ScreenRec.Width - 450, i * 50), animate));
                            }
                        }

                        ConfirmConnection();
                        break;

                    case (byte)PacketTypes.GAMESTATE:
                        Debug.WriteLine("Cl-Reveiced gamestate change");
                        background.PlayMusic = false;
                        Notify.NewMessage("Starting Game!", Color.LightBlue);
                        Game1.gameState = (GameStates)incom.ReadByte();
                        Game1.tankname  = nameBtn.Text;

                        // Skapa alla tank klasserna
                        foreach (PlayerListItem player in playerList)
                        {
                            if (player.Name != "")
                            {
                                tanks.Add(player.Name, new Tank(content, player.Name, player.TankColor));
                            }
                        }

                        ConfirmConnection();
                        break;

                    case (byte)PacketTypes.HEARTBEAT:
                        // Respond to the Heartbeat request of the server
                        Debug.WriteLine("Cl-Received heartbeat, responding");
                        NetOutgoingMessage outmsg = Game1.Client.CreateMessage();
                        outmsg.Write((byte)PacketTypes.HEARTBEAT);
                        outmsg.Write(nameBtn.Text);
                        Game1.Client.SendMessage(outmsg, incom.SenderConnection, NetDeliveryMethod.ReliableOrdered);
                        ConfirmConnection();
                        break;

                    case (byte)PacketTypes.DISCONNECTREASON:
                        Debug.WriteLine("Cl-Deny packet received");
                        Notify.NewMessage("Disconnect reason: " + incom.ReadString(), Color.Purple);
                        Disconnect();
                        break;

                    default:
                        break;
                    }

                    lastBeat = DateTime.Now;                     // Se alla anslutningar som en heartbeat
                }
            }

            // HeartBeat
            TimeSpan timeSinceLastBeat = DateTime.Now.Subtract(lastBeat);

            if (connected && timeSinceLastBeat.TotalSeconds > 10)
            {
                Notify.NewMessage("Connection lost", Color.Red);
                Disconnect();
            }
        }
Example #2
0
 public void MuteMusic()
 {
     background.PlayMusic = background.PlayMusic ? false : true;
     Notify.NewMessage("Music is now turned " + (background.PlayMusic ? "on" : "off"), Color.Black);
 }