private void UpdatePlayers(GameStateView gameStateView) { List <string> updatedPlayers = new List <string>(); foreach (Player player in gameStateView.Players) { if (!playerPictureBoxes.TryGetValue(player.PlayerId, out PictureBox pictureBox)) { pictureBox = CreatePlayerPictureBox(); InitializeDrawing(pictureBox); playerPictureBoxes.Add(player.PlayerId, pictureBox); } // Update labels for playing player if (player.PlayerId == this.Pid) { ScoreLabel.SetPropertyThreadSafe(() => ScoreLabel.Text, String.Format("Player {0} Score: {1}", Pid, player.Score.ToString())); if (!player.Alive) { GameStatusLabel.SetPropertyThreadSafe(() => GameStatusLabel.Text, "You are dead"); } } if (player.Alive) { if (pictureBox.Tag == null || ((Direction)pictureBox.Tag) != player.Direction) { Direction direction = pictureBox.Tag == null ? Direction.RIGHT : player.Direction; Bitmap updatedBitmap = DirectionToResource(direction); if (updatedBitmap != null) { pictureBox.Image = updatedBitmap; pictureBox.Tag = direction; } } pictureBox.Location = new Point(player.X, player.Y); } else { pictureBox.Visible = false; } updatedPlayers.Add(player.PlayerId); } // Remove removed players foreach (string playerId in playerPictureBoxes.Keys.Except(updatedPlayers).ToList()) { if (playerPictureBoxes.TryGetValue(playerId, out PictureBox pictureBox)) { this.Controls.Remove(pictureBox); playerPictureBoxes.Remove(playerId); } } }
internal void ApplyGameStateView(GameStateView gameStateView) { if (!pictureBoxesReady) { LoadPictureBoxes(gameStateView); } if (gameStateView.GameOver) { GameStatusLabel.SetPropertyThreadSafe(() => this.GameStatusLabel.Text, "Game Over!"); } else if (gameStateView.RoundId >= 0) { GameStatusLabel.SetPropertyThreadSafe(() => this.GameStatusLabel.Text, "Game On!"); } if (!gameStateView.GameOver) { //UpdateScore(gameStateView); UpdateCoins(gameStateView); UpdateGhosts(gameStateView); UpdatePlayers(gameStateView); } }