private void StartLocalGame(EnumGameType type, IList<Player> players) { LocalGame game = GameFactory.CreateGame(type) as LocalGame; game.Container = m_gameContainer; foreach (Player player in players) { if (null == player.PlayFiled) player.PlayFiled = new PlayPanel(false); this.InitPlayer(player); game.AddPlayer(player); } if (null != m_game) m_game.Dispose(); m_game = game; game.New(); this.Refresh(); game.Start(); }
private void StartClientGame(List<Player> players, string clientGameInfo) { // client game info format : name={0},server_ip={1},server_port={2} string[] info = clientGameInfo.Split(new char[] { ',', '=' }); Debug.Assert(info.Length == 6 && players != null && players.Count > 0 && !string.IsNullOrEmpty(clientGameInfo)); if (string.IsNullOrEmpty(info[3]) || string.IsNullOrEmpty(info[5])) { MessageBox.Show("You did not specify the correct host information.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } if (null != m_game) m_game.Dispose(); ClientGame game = (m_game = GameFactory.CreateGame(EnumGameType.Client) as TetrisGame) as ClientGame; game.Container = m_gameContainer; game.Joined += new EventHandler<PlayerEventArgs>(ClientGame_Joined); game.Left += new EventHandler<PlayerEventArgs>(ClientGame_Left); game.PlayerJoined += new EventHandler<PlayerEventArgs>(ClientGame_PlayerJoined); game.PlayerLeft += new EventHandler<PlayerEventArgs>(ClientGame_PlayerLeft); game.GameElapsed += new EventHandler(ClientGame_GameElapsed); Player player = players[0]; // first player as local player player.Name = info[1]; player.PlayFiled = new PlayPanel(true); this.InitPlayer(player); game.AddPlayer(player); game.Connect(info[3], Convert.ToInt32(info[5])); this.Text = this.Text + " - Client"; this.Refresh(); }
private void StartHostGame(List<Player> players, string hostGameInfo) { // host game info format : name={0},ip={1},port={2},max_players={3} string[] info = hostGameInfo.Split(new char[] { ',', '=' }); Debug.Assert(info.Length == 8 && players != null && players.Count > 0 && !string.IsNullOrEmpty(hostGameInfo)); int port = Convert.ToInt32(info[5]); int max = Convert.ToInt32(info[7]); Debug.Assert(max >= 2 && max < GameSetting.LIMITED_MAX_PLAYERS); if (null != m_serverGame) m_serverGame.Dispose(); m_serverGame = GameFactory.CreateGame(EnumGameType.Host) as ServerGame; InitServerCommands(); m_serverGame.PlayerJoined += new EventHandler<PlayerEventArgs>(ServerGame_PlayerJoined); m_serverGame.PlayerLeaved += new EventHandler<PlayerEventArgs>(ServerGame_PlayerLeaved); m_serverGame.PlayerPrepared += new EventHandler<PlayerEventArgs>(ServerGame_PlayerPrepared); m_serverGame.ServicePort = port; m_serverGame.MaxPlayers = max; m_serverGame.StartService(); // A host is also a client when hosting a server game. IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(info[3]), port); if (null != m_game) m_game.Dispose(); ClientGame game = (m_game = GameFactory.CreateGame(EnumGameType.Client) as TetrisGame) as ClientGame; game.Joined += new EventHandler<PlayerEventArgs>(ClientGame_Joined); game.Left += new EventHandler<PlayerEventArgs>(ClientGame_Left); game.Left += new EventHandler<PlayerEventArgs>(ClientGame_Left); game.PlayerJoined += new EventHandler<PlayerEventArgs>(ClientGame_PlayerJoined); game.GameElapsed += new EventHandler(ClientGame_GameElapsed); game.Container = m_gameContainer; Player player = players[0]; // first player as local player player.Name = info[1]; player.HostName = Dns.GetHostName(); player.PlayFiled = new PlayPanel(true); this.InitPlayer(player); game.AddPlayer(player); game.Connect("localhost", port); this.Text = this.Text + " - Server"; this.Refresh(); //WaitForClientForm dlg = new WaitForClientForm(); //DialogResult result = dlg.ShowDialog(); //if (result == DialogResult.OK) { // m_serverGame.Start(); //} else { // game.Disconnect(); // m_serverGame.Stop(); // game.Dispose(); // m_serverGame.Dispose(); // m_game = null; // m_serverGame = null; // return; //} }
private void MainForm_FormClosed(object sender, FormClosedEventArgs e) { if (null != m_game) { m_game.Dispose(); m_game = null; } if (null != m_serverGame) { m_serverGame.Dispose(); m_serverGame = null; } }