コード例 #1
0
        public InGameControl()
        {
            InitializeComponent();

            beginGamePhaseControl = new BeginGamePhaseControl
            {
                Parent = gameStateMenuPanel,
                Dock   = DockStyle.Fill
            };
            beginGamePhaseControl.OnCommitted += () =>
            {
                bool isNext = gameFlowHandler.NextPlayer();
                if (isNext)
                {
                    beginGamePhaseControl.ResetControl();
                }
                else
                {
                    beginGamePhaseControl.Hide();
                    beginRoundPhaseControl.Show();
                    GameState = GameState.RoundBeginning;
                    gameFlowHandler.PlayRound();
                }
                gameFlowHandler.Begin();
            };

            beginRoundPhaseControl = new BeginRoundPhaseControl
            {
                Parent = gameStateMenuPanel,
                Dock   = DockStyle.Fill
            };
            beginRoundPhaseControl.OnBegin += () =>
            {
                beginRoundPhaseControl.Hide();
                turnPhaseControl.Show();
                GameState = GameState.Deploying;
            };

            turnPhaseControl = new TurnPhaseControl
            {
                Parent = gameStateMenuPanel,
                Dock   = DockStyle.Fill
            };
            // reset selection when any other stage than attacking is invoked
            turnPhaseControl.OnCommitting += () =>
            {
                gameFlowHandler.ResetSelection();
            };
            turnPhaseControl.OnDeploying += () =>
            {
                gameFlowHandler.ResetSelection();
            };
            turnPhaseControl.OnCommitted += () =>
            {
                bool isNext = gameFlowHandler.NextPlayer();
                turnPhaseControl.Hide();
                beginRoundPhaseControl.Show();
                GameState = GameState.RoundBeginning;
                if (!isNext)
                {
                    gameFlowHandler.PlayRound();
                }
                if (Game.IsFinished())
                {
                    gameFlowHandler.End();
                }
                else
                {
                    gameFlowHandler.Begin();
                }
            };

            beginGamePhaseControl.Hide();
            beginRoundPhaseControl.Hide();
            turnPhaseControl.Hide();
        }
コード例 #2
0
        public void Initialize(Game game)
        {
            if (game == null)
            {
                throw new ArgumentException("Game cannot be null.");
            }

            MapImageProcessor mapImageProcessor;

            using (UtilsDbContext db = new UtilsDbContext())
            {
                MapInfo mapInfo = (from item in db.Maps
                                   where item.Id == game.Map.Id
                                   select item).First();

                mapImageProcessor = MapImageProcessor.Create(game.Map, mapInfo.ImageColoredRegionsPath,
                                                             mapInfo.ColorRegionsTemplatePath, mapInfo.ImagePath, game.IsFogOfWar);
            }

            // initialize game
            switch (game.GameType)
            {
            case GameType.SinglePlayer:
                gameFlowHandler = new SingleplayerGameFlowHandler(game, mapImageProcessor);
                break;

            case GameType.MultiplayerHotseat:
                gameFlowHandler = new HotseatGameFlowHandler(game, mapImageProcessor);
                break;

            case GameType.MultiplayerNetwork:
                throw new NotImplementedException();
            }

            // initialize map processor
            mapHandlerControl.Initialize(gameFlowHandler);
            gameFlowHandler.OnRoundPlayed += () =>
            {
                using (UtilsDbContext db = new UtilsDbContext())
                {
                    gameFlowHandler.Game.Save(db);
                }
            };
            gameFlowHandler.OnBegin += () =>
            {
                MessageBox.Show($"Player {gameFlowHandler.PlayerOnTurn.Name} is on turn.");
            };
            gameFlowHandler.OnEnd += player =>
            {
                MessageBox.Show($"Player {player.Name} has won the game!");
                beginGamePhaseControl.Hide();
                turnPhaseControl.Hide();
                beginRoundPhaseControl.Hide();
                GameState = GameState.GameEnd;
            };

            beginGamePhaseControl.Initialize(gameFlowHandler);
            turnPhaseControl.Initialize(gameFlowHandler);

            // initialize game state
            if (game.RoundNumber == 0)
            {
                GameState = GameState.GameBeginning;
                beginGamePhaseControl.Show();

                gameFlowHandler.Begin();
            }
            else if (game.IsFinished())
            {
                gameFlowHandler.End();
            }
            else
            {
                GameState = GameState.RoundBeginning;

                beginRoundPhaseControl.Show();

                gameFlowHandler.Begin();
            }
        }