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