Beispiel #1
0
 public void StructTest()
 {
     var game = NewGameBuilder.Create()
                .SetMap(new DemoMap())
                .SetPlayerName("PLayer 1")
                .SetPlayerName("Player 2")
                .Build();
     var moveManager = new MoveManager(game);
     var i           = moveManager.GetBestMove(game.GetCurrentPlayer(), game.GetCurrentPlayer().Units[0]);
 }
Beispiel #2
0
        public GameViewModel()
        {
            Messenger.Default.Register <GameMessage>(this, message =>
            {
                Game     = message.Game;
                isReplay = message.IsReplay;
                RaisePropertyChanged("IsEnabled");
                InitializeGame();

                if (isReplay)
                {
                    Game.RewindGame();
                    ReplayGame();
                }
            });

            ActionCommand = new RelayCommand <TileViewModel>(tileViewModel =>
            {
                // If it is a selection.
                if (SelectedUnit == null && tileViewModel.HasUnit && Game.GetCurrentPlayer().Units.Contains(tileViewModel.Units[0].Unit))
                {
                    CleanReachablePositions();
                    CleanBestMoves();
                    SelectedUnit = tileViewModel.Units[0];

                    // Sets the new reachable positions.
                    var positions = SelectedUnit.Unit.FindReachablePositions(Game.Map);
                    // Selected unit Position
                    var sP = SelectedUnit.Unit.Position;
                    foreach (var position in positions)
                    {
                        var index      = position.X * Game.Map.Size + position.Y;
                        bool canAttack = sP.IsClose(position);
                        var ennemy     = Game.GetCurrentPlayer() == Game.Players[0] ? Game.Players[1] : Game.Players[0];
                        var ennemyHere = false;
                        foreach (var u in ennemy.Units)
                        {
                            if (!u.IsDead() && u.Position.Equals(position))
                            {
                                ennemyHere = true;
                            }
                        }

                        if ((canAttack && ennemyHere) || !ennemyHere)
                        {
                            Tiles[index].IsReachable = true;
                        }
                        else
                        {
                            Tiles[index].IsReachable = false;
                        }

                        Tiles[index].RaisePropertyChanged("IsReachable");
                    }
                    MoveManager moveManager    = new MoveManager(Game);
                    IList <Position> bestMoves = moveManager.GetBestMove(Game.GetCurrentPlayer(), SelectedUnit.Unit);
                    foreach (var position in bestMoves)
                    {
                        var index = position.X * Game.Map.Size + position.Y;
                        Tiles[index].IsBestMove = true;
                        Tiles[index].RaisePropertyChanged("IsBestMove");
                    }
                }
                // If it is an action.
                else if (SelectedUnit != null && tileViewModel.IsReachable)
                {
                    var index = Tiles.IndexOf(tileViewModel);

                    // Attack action.
                    if (tileViewModel.HasUnit && !Game.GetCurrentPlayer().Units.Contains(tileViewModel.Units[0].Unit))
                    {
                        // Selects the best defender.
                        var defender = tileViewModel.Units[0];
                        for (var i = 1; i < tileViewModel.Units.Count; i++)
                        {
                            if (tileViewModel.Units[i].Unit.GetRealDefensePoints() > defender.Unit.GetRealDefensePoints())
                            {
                                defender = tileViewModel.Units[i];
                            }
                        }
                        var attack = new AttackAction(Game.Map, SelectedUnit.Unit, defender.Unit);
                        Game.DoAction(attack);

                        var loser           = attack.Loser;
                        var winnerViewModel = loser == SelectedUnit.Unit ? defender : SelectedUnit;
                        if (loser.IsDead())
                        {
                            foreach (var t in Tiles)
                            {
                                for (int i = 0; i < t.Units.Count; i++)
                                {
                                    if (t.Units[i].Unit == loser)
                                    {
                                        t.Units.RemoveAt(i);
                                        t.Units.Add(winnerViewModel);
                                    }
                                    else if (t.Units[i].Unit == winnerViewModel.Unit)
                                    {
                                        t.Units.RemoveAt(i);
                                    }
                                }
                                t.RaisePropertyChanged("Units");
                            }
                        }

                        foreach (var unitViewModel in Player1.Units)
                        {
                            unitViewModel.RaisePropertyChanged("");
                        }
                        foreach (var unitViewModel in Player2.Units)
                        {
                            unitViewModel.RaisePropertyChanged("");
                        }
                    }
                    // Move action.
                    else
                    {
                        var mapSize = Game.Map.Size;
                        var posDest = new Position(index / mapSize, index % mapSize);


                        var path = SelectedUnit.Unit.GetWay(Game.Map, posDest);
                        Game.DoAction(new MoveAction(Game.Map, SelectedUnit.Unit, posDest));
                        SelectedUnit.RaisePropertyChanged("Unit");
                        AnimateUnitMove(SelectedUnit, path);
                    }

                    SelectedUnit = null;
                    CleanReachablePositions();
                    CleanBestMoves();
                }
                else
                {
                    CleanReachablePositions();
                    CleanBestMoves();
                    SelectedUnit = null;
                }
            });

            NextTurnCommand = new RelayCommand(async() =>
            {
                if (Game.NextPlayer())
                {
                    RaisePropertyChanged("Game");
                    Player1.RaisePropertyChanged("Player");
                    Player2.RaisePropertyChanged("Player");
                    Messenger.Default.Send(new CurrentPlayerMessage(Game.GetCurrentPlayer()));
                }
                else
                {
                    var dialog         = new Views.Dialogs.WinnerDialog();
                    dialog.DataContext = new Dialogs.WinnerDialogViewModel(Game.GetWinner().Name);

                    var path = (string)await DialogHost.Show(dialog, "RootDialog");
                    if (path != null)
                    {
                        ;
                    }

                    ExitGame();
                }
            });

            SaveGameDialogCommand = new RelayCommand(async() =>
            {
                var dialog = new Views.Dialogs.SaveGameDialog();

                var path = (string)await DialogHost.Show(dialog, "RootDialog");
                if (path != null)
                {
                    var directoryPath = $"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}/Saves";
                    if (!Directory.Exists(directoryPath))
                    {
                        Directory.CreateDirectory(directoryPath);
                    }
                    Game.SaveGame($"{directoryPath}/{path}.o");
                }
            });

            LoadGameDialogCommand = new RelayCommand(async() =>
            {
                var dialog = new Views.Dialogs.LoadGameDialog();

                var path = (string)await DialogHost.Show(dialog, "RootDialog");
                if (path != null)
                {
                    Game = Game.LoadGame(path);
                    InitializeGame();
                }
            });

            ExitGameDialogCommand = new RelayCommand(async() =>
            {
                if (!isReplay)
                {
                    var dialog = new Views.Dialogs.ConfirmDialog();

                    if (await DialogHost.Show(dialog, "RootDialog") != null)
                    {
                        ExitGame();
                    }
                }
                else
                {
                    ExitGame();
                }
            });
        }