/// <summary> /// Starts a new game with the player data from this form. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void bStartNew_Click(object sender, EventArgs e) { // check player count int playerCount; if ((playerCount = GetPlayerCount()) < 2 || playerCount > 4) { MessageBox.Show("Unsupported number of players"); return; } // load config file (.thc) try { GameSettings.LoadFromFile(@"thebes_config.thc"); } catch (Exception exception) { if (exception is FileNotFoundException) { UIConfig.ErrorDialog("File not found error:\n" + exception.Message); } else if (exception is FormatException) { UIConfig.ErrorDialog("Error processing the config file:\n" + exception.Message); } else { UIConfig.ErrorDialog("Unknown error reading the config file:\n" + exception.Message); } return; } // check for invalid namess if (AnyInvalidNames()) { MessageBox.Show("Each player has to have a distinct name"); return; } UIGame game = new UIGame(playerCount); // Create Players Dictionary <IPlayer, PlayerColor> players = new Dictionary <IPlayer, PlayerColor>(); Player player; foreach (PlayerInput playerInput in playerInputs) { if (playerInput.Selected()) { if (playerInput.IsHuman()) { player = new Player( playerInput.PlayerName(), GameSettings.Places.OfType <IDigSite>().ToList(), GameSettings.StartingPlace, UIConfig.ErrorDialog, game.AvailableCards.ChangeDisplayedCards, game.AvailableCards.GiveCard, game.Deck.Discard, game.ActiveExhibitions.GiveExhibition, game.DrawTokens, game.PlayersOnWeek ); } else { player = new AIPlayer( playerInput.PlayerName(), GameSettings.Places.OfType <IDigSite>().ToList(), GameSettings.StartingPlace, GameSettings.Places, UIConfig.ErrorDialog, game.AvailableCards.ChangeDisplayedCards, game.AvailableCards.GiveCard, game.Deck.Discard, game.ActiveExhibitions.GiveExhibition, game.DrawTokens, game.PlayersOnWeek ); IAI ai = (IAI)playerInput.Type.Assembly.CreateInstance(playerInput.Type.FullName, false, 0, null, new object[] { playerCount }, null, null); ((AIPlayer)player).Init(ai); } players.Add(player, playerInput.Color); } } // initialize game try { game.Initialize(players); } catch (Exception exception) { UIConfig.ErrorDialog("Error initializing the game:\n" + exception.Message); return; } // open game form this.Hide(); GameForm gameForm = new GameForm(game); gameForm.ShowDialog(); this.Close(); }
/// <summary> /// Called from constructor ot initialize the form /// </summary> /// <param name="game"></param> private void Initialize(IUIGame game) { this.SuspendLayout(); this.game = game; playerDisplays = new List <PlayerDisplay>(); layout = ThebesUI.Layout.ParseLayout("layout.json"); // player displays foreach (IPlayer player in game.Players) { PlayerDisplay pd = new PlayerDisplay(); pd.Width = 368; pd.Height = 152; pd.Initialize(player, layout, game.Colors[player]); playerDisplays.Add(pd); flpPlayerDisplay.Controls.Add(pd); } // background pBoard.BackgroundImage = Image.FromFile(UIConfig.IMG_FOLDER + "board.jpg"); Controls.Add(pBoard); // display cards Rectangle pbDims; for (int i = 0; i < displayCards.Length; i++) { pbDims = layout.DisplayedCards[i]; displayCards[i] = new PictureBox() { Location = pbDims.topLeft, Width = pbDims.Width, Height = pbDims.Height, Visible = true, SizeMode = PictureBoxSizeMode.StretchImage, BackColor = Color.Transparent }; displayCards[i].Click += pbCard_Click; pBoard.Controls.Add(displayCards[i]); } // exhibitions for (int i = 0; i < exhibitions.Length; i++) { pbDims = layout.DisplayedExhibitions[i]; exhibitions[i] = new PictureBox() { Location = pbDims.topLeft, Width = pbDims.Width, Height = pbDims.Height, Visible = true, SizeMode = PictureBoxSizeMode.StretchImage, BackColor = Color.Transparent }; exhibitions[i].Click += pbExhibition_Click; pBoard.Controls.Add(exhibitions[i]); } //tooltips for (int i = 0; i < cardToolTips.Length; i++) { cardToolTips[i] = new ToolTip(); } for (int i = 0; i < exhibitionToolTips.Length; i++) { exhibitionToolTips[i] = new ToolTip(); } // week counter smallPieces = new Dictionary <IPlayer, TransparentPictureBox>(); foreach (IPlayer player in game.Players) { pbDims = layout.WeekCounter[0]; smallPieces.Add(player, new TransparentPictureBox() { Location = pbDims.RectanglePositionCenter(34, 30), Width = 34, Height = 30, Visible = true, SizeMode = PictureBoxSizeMode.StretchImage, BackColor = Color.Transparent, Image = Image.FromFile(UIConfig.IMG_FOLDER + $"p_small_{game.Colors[player]}.png") }); pBoard.Controls.Add(smallPieces[player]); } // player pieces bigPieces = new Dictionary <IPlayer, TransparentPictureBox>(); foreach (IPlayer player in game.Players) { pbDims = layout.Places[player.CurrentPlace.Name]; bigPieces.Add(player, new TransparentPictureBox() { Location = pbDims.RectanglePositionCenter(35, 71), Width = 35, Height = 71, Visible = true, SizeMode = PictureBoxSizeMode.StretchImage, BackColor = Color.Transparent, Image = Image.FromFile(UIConfig.IMG_FOLDER + $"p_big_{game.Colors[player]}.png") }); bigPieces[player].Click += new EventHandler(pBoard_Click); // to enable click through pBoard.Controls.Add(bigPieces[player]); } // bonus tokens bonusTokens = new Dictionary <IDigSite, TransparentPictureBox>(); foreach (KeyValuePair <IDigSite, IToken> kvp in game.BonusTokens) { if (kvp.Value != null) { ITokenView tokenView = UIGame.ToView(kvp.Value); pbDims = layout.Places[kvp.Key.Name]; bonusTokens.Add(kvp.Key, new TransparentPictureBox() { Location = pbDims.RectanglePositionCenter(45, 45), Width = 45, Height = 45, Visible = true, SizeMode = PictureBoxSizeMode.StretchImage, BackColor = Color.Transparent, Image = Image.FromFile(UIConfig.IMG_FOLDER + tokenView.FileName) }); bonusTokens[kvp.Key].Click += new EventHandler(pBoard_Click); // to enable click through pBoard.Controls.Add(bonusTokens[kvp.Key]); } } // year counter pbDims = layout.YearCounter[(game.ActivePlayer.Time.CurrentYear % 10) - 1]; yearCounter = new TransparentPictureBox() { Location = pbDims.RectanglePositionCenter(34, 30), Width = 34, Height = 30, Visible = true, SizeMode = PictureBoxSizeMode.StretchImage, BackColor = Color.Transparent, Image = Image.FromFile(UIConfig.IMG_FOLDER + $"p_small_black.png") }; pBoard.Controls.Add(yearCounter); UpdateBoard(); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ResumeLayout(false); }