public static void Join(NetMatch match, bool server) { if (ConnectedMatch != null) { Leave(); } ConnectedMatch = match; StartListening(server); }
public static void Leave() { if (ConnectedMatch == null) { return; } SyncUpdate(NetUpdateType.Leave, null); SendUpdate = false; if (ConnectedMatch.OwnerName == Game.Player.Name) { Listener.Stop(); } ReceiverThread.Abort(); if (StopEventPerPlayer != null) { StopEventPerPlayer(null, null); } foreach (Thread t in PlayerThreads) { t.Abort(); } PlayerThreads.Clear(); ConnectedMatch = null; }
public static void Start(GameStartMode mode) { switch (mode) { case GameStartMode.SinglePlayer: MSGBox.ShowMSGBox("New singleplayer game", "Size:", MSGBoxType.SizeInput, new EventHandler <string>(delegate(object s, string inp) { int input = int.Parse(inp); if (input > 5) { GameRenderer.Panel.CreateGraphics().FillRectangle(new SolidBrush(Color.Black), new Rectangle(new Point(), GameRenderer.Panel.Size)); //int xy = GameRenderer.Panel.Size.Width / input; //GameSize = new Point(xy, xy); if (GameRenderer.Panel.Size.Width / input < 3 || GameRenderer.Panel.Size.Height / input < 3) { Game.Paused = true; return; } GameSize = new Point(GameRenderer.Panel.Size.Width / input, GameRenderer.Panel.Size.Height / input); Game.Reset(); Form1.TimerEnabled = true; } else { Game.Paused = true; } })); break; case GameStartMode.MultiPlayer: if (Game.Player.Name == "Player") { MSGBox.ShowMSGBox("Please change your username from default.", "", MSGBoxType.Text); break; } MSGBox.ShowMSGBox("New multiplayer game", "Name:\nMax. players:", MSGBoxType.MultipleInput, new EventHandler <string>(delegate(object s, string input) { string[] strs = input.Split('\n'); int num = 0; if (int.TryParse(strs[1], out num)) { var match = new NetMatch { Name = strs[0], MaxPlayers = num, OwnerIP = Network.GetIPs(), OwnerName = Player.Name }; match.Players.Add(Game.Player); Game.Reset(); Network.CreateGame(match); } else { Game.Paused = true; } })); break; case GameStartMode.Connect: if (Game.Player.Name == "Player") { MSGBox.ShowMSGBox("Please change your username from default.", "", MSGBoxType.Text); break; } //string matches = Network.Matches.Combine<NetMatch, string>((match, combiner) => combiner += match.Name + " " + match.Players.Count + "/" + match.MaxPlayers + " " + match.OwnerName + "\n"); string inputs = "IP:"; MSGBox.ShowMSGBox("Connect to game", inputs, MSGBoxType.MultipleInput, new EventHandler <string>(delegate(object s, string input) { IPAddress address; if (IPAddress.TryParse(input.Replace("\n", ""), out address)) { Game.Reset(); Network.Connect(new NetMatch { OwnerIP = new IPAddress[] { address } }); //Game.Reset(false); - On successfull connect event } else { Game.Paused = true; } })); break; default: throw new ArgumentException(); } }
public static void Connect(NetMatch match) { Join(match, false); }
public static void CreateGame(NetMatch match) { Join(match, true); }