Example #1
0
        /// <summary>
        /// Attack as a Turn
        ///
        /// Pick who to go after
        ///
        /// Determine the Attack score
        /// Determine the Defense score
        ///
        /// Do the Attack
        ///
        /// </summary>
        /// <param name="Attacker"></param>
        /// <returns></returns>
        public bool Attack(PlayerInfoModel Attacker)
        {
            // For Attack, Choose Who
            var TargetLocation = AttackChoice(Attacker);

            if (TargetLocation[0] == Int32.MaxValue)
            {
                return(false);
            }

            int[] attackerLocation = GameBoardModel.GetPlayerLocation(Attacker);
            int   Distance         = GameBoardHelper.Distance(attackerLocation[0], attackerLocation[1], TargetLocation[0], TargetLocation[1]);

            int AttackRange = 1;

            if (Attacker.Head != null)
            {
                AttackRange = ItemIndexViewModel.Instance.GetItem(Attacker.Head).Range;
            }

            if (Distance > AttackRange || Attacker.CurrentStamina < 3)
            {
                return(false);
            }

            // Do Attack
            PlayerInfoModel TargetModel = GameBoardModel.GetPlayer(TargetLocation[0], TargetLocation[1]);

            TurnAsAttack(Attacker, TargetModel);

            CurrentAttacker = new PlayerInfoModel(Attacker);
            CurrentDefender = new PlayerInfoModel(TargetModel);

            return(true);
        }
Example #2
0
        /// <summary>
        /// Get the closest item to the specified Player
        /// </summary>
        /// <param name="player"></param>
        /// <returns></returns>
        public int[] GetClosestItem(PlayerInfoModel player)
        {
            int[] PlayerLocation = GameBoardModel.GetPlayerLocation(player);
            int[] location       = { Int32.MaxValue, Int32.MaxValue };

            // Get closest Character
            int closestDistance = Int32.MaxValue;

            for (int x = 0; x < GameBoardModel.Size; ++x)
            {
                for (int y = 0; y < GameBoardModel.Size; ++y)
                {
                    if (GameBoardModel.ItemLocations[x, y] != null)
                    {
                        int distance = GameBoardHelper.Distance(x, y, PlayerLocation[0], PlayerLocation[1]);
                        if (distance < closestDistance)
                        {
                            location[0] = x;
                            location[1] = y;
                        }
                    }
                }
            }

            return(location);
        }
Example #3
0
 private async Task CheckEnemiesMovement(bool result, double[] positions)
 {
     await GameBoardHelper.StartStaTask(() =>
     {
         _gameBoard = GameBoardHelper.GenerateEmptyGameBoard(_width, _height);
         IGameMovementChecker checker = GameBoardHelper.GetGameMovementChecker(_gameBoard);
         ISettingsProvider provider   = new TestSettingsProvider();
         IGraph graph         = new Graph(_gameBoard);
         var player           = new Mock <Player>();
         player.Object.X      = positions[0];
         player.Object.Y      = positions[1];
         var alg              = new AStarEnemyMovementAlgorithm(graph, player.Object, (int)_width, (int)_height);
         var e1               = new Enemy();
         e1.X                 = positions[2];
         e1.Y                 = positions[3];
         e1.MovementAlgorithm = alg;
         var e2               = new Enemy();
         e2.X                 = positions[4];
         e2.Y                 = positions[5];
         e2.MovementAlgorithm = alg;
         _gameBoard.Children.Add(e1);
         _gameBoard.Children.Add(e2);
         IEnemyMovementManager manager = new TimeEnemyMovementManager(new [] { e1, e2 }, checker, provider);
         manager.MoveEnemies();
         bool res = checker.CheckCollision(e1, e2);
         Assert.Equal(result, res);
     });
 }
Example #4
0
        // Attack or Move
        // Roll To Hit
        // Decide Hit or Miss
        // Decide Damage
        // Death
        // Drop Items
        // Turn Over
        #endregion Algrorithm

        /// <summary>
        /// Perform a turn and attack the target
        /// </summary>
        /// <param name="Attacker"></param>
        /// <returns></returns>
        public bool TakeTurn(PlayerInfoModel Attacker)
        {
            // Replenish Stamina
            Attacker.CurrentStamina = Attacker.BaseStamina;

            // Choose Action.  Such as Move, Attack etc.

            // while an action is possible
            // If Nearest enemy in range and have an item, then Attack
            // If in range of an item, then move onto Item

            // if we don't have an item
            if (Attacker.Head == null)
            {
                // move to item
                int[] closestItem = GetClosestItem(Attacker);
                if (closestItem[0] != Int32.MaxValue && closestItem[1] != Int32.MaxValue)
                {
                    // Move towards the Item, conserving 3 stamina for the attack
                    Attacker.CurrentStamina = MoveTowards(Attacker, closestItem, 3, GameBoardModel.GetItem(closestItem[0], closestItem[1]).Name);
                }

                int[] PlayerLocation = GameBoardModel.GetPlayerLocation(Attacker);

                int[] newestClosest = GetClosestItem(Attacker);
                if (newestClosest[0] != Int32.MaxValue && newestClosest[1] != Int32.MaxValue &&
                    GameBoardHelper.Distance(newestClosest[0], newestClosest[1], PlayerLocation[0], PlayerLocation[1]) <= 1)
                {
                    ItemModel temp = GameBoardModel.ItemLocations[newestClosest[0], newestClosest[1]];
                    Attacker.Head = temp.Id;
                    Debug.WriteLine(Attacker.Name + " picked up " + temp.Name);
                    GameBoardModel.ItemLocations[newestClosest[0], newestClosest[1]] = null;
                }
            }

            // then try an attack
            var attacks = Attack(Attacker);

            if (!attacks)
            {
                // move toward enemy
                int[] TargetLocation = AttackChoice(Attacker);

                if (TargetLocation[0] == Int32.MaxValue)
                {
                    return(attacks);
                }
            }

            BattleScore.TurnCount++;

            return(attacks);
        }
Example #5
0
 private async Task CheckMovementTest(double playerX, double playerY, Direction direction, bool result)
 {
     await GameBoardHelper.StartStaTask(() =>
     {
         _gameBoard    = GameBoardHelper.GenerateEmptyGameBoard(_width, _height);
         var checker   = GameBoardHelper.GetGameMovementChecker(_gameBoard);
         Player player = new Player()
         {
             X = playerX, Y = playerY
         };
         bool res = checker.CheckMovement(player, direction);
         Assert.Equal(result, res);
     });
 }
Example #6
0
 private async Task CheckStaticColisionBetweenPlayerAndBlocks(double playerX, double playerY, double blockX, double blockY, bool result)
 {
     await GameBoardHelper.StartStaTask(() =>
     {
         _gameBoard    = GameBoardHelper.GenerateEmptyGameBoard(_width, _height);
         var checker   = GameBoardHelper.GetGameMovementChecker(_gameBoard);
         Player player = new Player()
         {
             X = playerX, Y = playerY
         };
         Block block = _gameBoard.Elements.FirstOrDefault(b => Math.Abs(b.X - blockX) < 1 && Math.Abs(b.Y - blockY) < 1) as Block;
         bool res    = checker.CheckCollision(player, block);
         Assert.Equal(result, res);
     });
 }
Example #7
0
        /// <summary>
        /// Move towards the specified location as much as possible
        /// </summary>
        /// <param name="player"></param>
        /// <param name="location"></param>
        /// <param name="StaminaToConserve"></param>
        /// <returns></returns>
        public int MoveTowards(PlayerInfoModel player, int[] location, int StaminaToConserve, string label)
        {
            int[] PlayerLocation = GameBoardModel.GetPlayerLocation(player);
            int   Distance       = GameBoardHelper.Distance(location[0], location[1], PlayerLocation[0], PlayerLocation[1]);

            Debug.WriteLine("Distance " + Distance);

            // How far the Player can move when conserving the amount of stamina that they want to
            int CanMove = player.CurrentStamina - StaminaToConserve;

            if (CanMove < Distance)
            {
                Distance = CanMove;
            }

            // Determine how far to move in each direction
            int BlocksToMove = (int)(Math.Floor(Math.Sqrt(Math.Pow(Distance, 2) / 2)));
            int xMovement    = (location[0] - PlayerLocation[0] > 0 ? BlocksToMove : -BlocksToMove);
            int yMovement    = (location[1] - PlayerLocation[1] > 0 ? BlocksToMove : -BlocksToMove);

            // Debug.WriteLine("Move " + (PlayerLocation[0] + xMovement) + ", " + (PlayerLocation[1] + yMovement));

            int newX = PlayerLocation[0] + xMovement;

            if (PlayerLocation[0] + xMovement > 5 || PlayerLocation[0] + xMovement < 0)
            {
                newX = PlayerLocation[0];
            }

            int newY = PlayerLocation[1] + yMovement;

            if (PlayerLocation[1] + yMovement > 5 || PlayerLocation[1] + yMovement < 0)
            {
                newY = PlayerLocation[0];
            }

            // Perform the GameBoard updates
            GameBoardModel.PlayerLocations[PlayerLocation[0], PlayerLocation[1]] = null;
            GameBoardModel.PlayerLocations[newX, newY] = player;

            Debug.WriteLine(player.Name + " moved towards \"" + label + "\" (" + location[0] + ", " + location[1] + ")");

            // Update the Player stamina
            player.CurrentStamina -= BlocksToMove;
            return(player.CurrentStamina);
        }
Example #8
0
 private void ControlKeyChange(Key pressed, string pressedKey, bool result)
 {
     GameBoardHelper.StartStaTask(() =>
     {
         var accessor        = new TestHaveControlKeys();
         OptionsViewModel vm = new OptionsViewModel(accessor, new KeysValidator());
         accessor.LoadControlKeys();
         vm.ChangeKey(pressedKey);
         var args = new KeyEventArgs(Keyboard.PrimaryDevice, new Mock <PresentationSource>().Object, 0, pressed);
         vm.OnKeyDown(args);
         Assert.Equal(result, !vm.HasErrors);
         Key k = GetKeyByName(accessor, pressedKey);
         Assert.NotEqual(k, Key.None);
         if (result)
         {
             Assert.Equal(k, pressed);
         }
     });
 }
Example #9
0
 private async Task CheckIsNextElementTest(double playerX, double playerY, Direction direction, double enemyX, double enemyY, bool result)
 {
     await GameBoardHelper.StartStaTask(() =>
     {
         _gameBoard    = GameBoardHelper.GenerateEmptyGameBoard(_width, _height);
         var checker   = GameBoardHelper.GetGameMovementChecker(_gameBoard);
         Player player = new Player()
         {
             X = playerX, Y = playerY
         };
         Enemy enemy = new Enemy()
         {
             X = enemyX, Y = enemyY
         };
         _gameBoard.Children.Add(enemy);
         bool res = checker.IsElementNextTo <MovableElement>(player, direction);
         Assert.Equal(result, res);
     });
 }
Example #10
0
        /// <summary>
        /// Pick the Monster to Attack
        /// </summary>
        /// <returns></returns>
        public int[] SelectMonsterToAttack(PlayerInfoModel currentPlayer)
        {
            int[] characterLocation = GameBoardModel.GetPlayerLocation(currentPlayer);
            int[] closestLocation   = { Int32.MaxValue, Int32.MaxValue };

            if (MonsterList == null)
            {
                return(null);
            }

            if (MonsterList.Count < 1)
            {
                return(null);
            }

            // Get closest Character
            int closestDistance = Int32.MaxValue;

            for (int x = 0; x < GameBoardModel.Size; ++x)
            {
                for (int y = 0; y < GameBoardModel.Size; ++y)
                {
                    if (GameBoardModel.PlayerLocations[x, y] != null &&
                        GameBoardModel.PlayerLocations[x, y].PersonType == PersonTypeEnum.Monster)
                    {
                        int distance = GameBoardHelper.Distance(x, y, characterLocation[0], characterLocation[1]);
                        if (distance < closestDistance)
                        {
                            closestLocation[0] = x;
                            closestLocation[1] = y;
                        }
                    }
                }
            }

            return(closestLocation);
        }