Hi All:

I'm wondering if anyone knows any usefull links to articles I can read on coding multiplayer for XNA. I'm looking for something that would teach me how to code for more than two players. The idea of what I'm working on is for a standard 4v4 deathmatch over a local network with hopes of expanding to other gamemodes, etc. Any help appreciated ^_^

Right now i have a simple network class that I'm looking to expand further upon, change, and do science with.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.GamerServices;

namespace XNAFighter
{
    class Networking
    {

        //-----------Variables-----------------//

        public static NetworkSession     networkSession;
        public static PacketWriter       packetWriter = new PacketWriter();
        public static PacketReader       packetReader = new PacketReader();
        public static LocalNetworkGamer  localGamer;

        //  public static GameState         state;
        static protected DummyPlayer    otherPlayer;
        public static Random            randomInt;

        static protected Game1          game;
        public static bool              Network = false;

        protected Dictionary<string, NetworkGamer> listOfLocalPlayers = new Dictionary<string, NetworkGamer>();

        //-------------Functions-------------//

        public Networking()
        {

        }

        //-----------------------------------------------------------------------------//

        protected NetworkGamer GetRemoteGamers()
        {

            foreach (NetworkGamer gamer in networkSession.AllGamers)
            {
                if (!gamer.IsLocal)
                {
                    return gamer;
                }

            }
            return null;
        }

        //-----------------------------------------------------------------------------//

        public void Update_Start(GameTime _gameTime)
        {

            //get local gamer
            localGamer = networkSession.LocalGamers[0];
            for(int i = 0; i < networkSession.AllGamers.Count; i++)
            {

                string text = networkSession.LocalGamers[i].Gamertag;
                NetworkGamer remotePlayer = GetRemoteGamers();

                if (listOfLocalPlayers.ContainsKey(text))
                {
                    break;
                }
                else
                {
                    listOfLocalPlayers.Add(text, remotePlayer);
                }
            }

            //check for gamestart key or button press
            //only if there are two players

            if (networkSession.AllGamers.Count == 2)
            {
                if (localGamer.IsHost)
                {

                    int hold = randomInt.Next();
                    packetWriter.Write((int)MessageType.RandSeed);
                    packetWriter.Write(hold);
                    localGamer.SendData(packetWriter, SendDataOptions.Reliable);
                    game.InitializeObjectList(hold);
                    Game1.state = GameState.Lobby;

                }
            }

            ProcessIncomingData(_gameTime);
        }

        //-----------------------------------------------------------------------------//

        public void ProcessIncomingData(GameTime _gameTime)
        {

            localGamer = networkSession.LocalGamers[0];

            while (localGamer.IsDataAvailable)
            {
                NetworkGamer sender;
                localGamer.ReceiveData(packetReader, out sender);
                localGamer.EnableSendVoice(sender, true);

                if (!sender.IsLocal)
                {

                    MessageType messageType = (MessageType)packetReader.ReadInt32();

                    switch (messageType)
                    {
                        case MessageType.StartGame:
                            {
                                Game1.state = GameState.InGame;
                                break;
                            }
                        case MessageType.UpdatePlayerPos:
                            {
                                otherPlayer.transform.position = packetReader.ReadVector3();
                                break;
                            }
                        case MessageType.UpdatePlayerRot:
                            {
                                otherPlayer.transform.rotation = new Quaternion(packetReader.ReadVector3(), (float)packetReader.ReadDouble());
                                break;
                            }
                        case MessageType.SpawnBullet:
                            {
                                otherPlayer.DummyFire();
                                break;
                            }
                        case MessageType.RandSeed:
                            {
                                game.InitializeObjectList(packetReader.ReadInt32());
                                Game1.state = GameState.Lobby;
                                break;
                            }
                        case MessageType.Suicide:
                            {
                                Game1.thierScore--; //temporary
                                break;
                            }
                        default:
                            {
                                throw new Exception("Unkown messageType");

                            }
                    }
                }
            }
        }

        //-----------------------------------------------------------------------------//

        public void LobbyUpdate()
        {
            ProcessIncomingData(Game1.GT);

            if (Game1.CurrentKeyboardState.IsKeyDown(Keys.Enter) != Game1.PrevKeyboardState.IsKeyDown(Keys.Enter)
                || Game1.CurrentGamePadState.IsButtonDown(Buttons.Start) != Game1.PrevGamePadState.IsButtonDown(Buttons.Start))
            {
                if (Game1.CurrentKeyboardState.IsKeyDown(Keys.Enter) || Game1.CurrentGamePadState.IsButtonDown(Buttons.Start))
                {
                    localGamer.IsReady = !localGamer.IsReady;
                }
            }

            if (localGamer.IsHost)
            {
                if (networkSession.IsEveryoneReady)
                {
                    packetWriter.Write((int)MessageType.StartGame);
                    localGamer.SendData(packetWriter, SendDataOptions.Reliable);
                    Game1.state = GameState.InGame;
                }
            }
        }

        //-----------------------------------------------------------------------------//

    }//end class
}//end namespace

Recommended Answers

All 3 Replies

I don't have any experience with either, but it's worth you thinking about and knowing the answer as you search around the internet for help:
What platform(s) are you targeting? All of the same options might not be available depending on if you're aiming at PC and/or XBox 360 and/or WinPhone and/or Zune (is that still an option, I'm not sure).

We'll be using PC and games for windows. This is just a fun project among friends ^^

Is funny you mention Zune :P I almost threw mine away trying to put songs in it...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.