Exemple #1
0
        /// <summary>
        /// Whenever an action is selected by the player, changes the defaults to the new action to be used in combat.
        /// </summary>
        /// <param name="sender">The object invoking the event.</param>
        /// <param name="args">Event parameters for the ActionSelectedEvent</param>
        private void OnActionSelected(object sender, ActionSelectedEventArgs args)
        {
            var activeAction = _displayManager.GetActionFromCategory(args.CommandFocus,
                                                                     args.CategoryName,
                                                                     args.ActionFocusNumber - 1);

            _defaultsHandler.ActiveAction = new ActionStore()
            {
                TargetPositions         = activeAction.GetActionTargets(),
                CenterOfTargets         = activeAction.GetCenterOfTargetsPosition(),
                CanSwitchTargetPosition = activeAction.CanSwitchTargetPosition,
                CanTargetThroughUnits   = activeAction.CanTargetThroughUnits
            };

            if (_defaultsHandler.CurrentTargetPosition < 1 ||
                _defaultsHandler.CurrentTargetPosition > 18 ||
                !activeAction.CanTargetThroughUnits)
            {
                _defaultsHandler.CurrentTargetPosition = 13;
            }

            _defaultsHandler.CurrentTargetPositions = CombatTargeter.GetTranslatedTargetPositions(_defaultsHandler.ActiveAction.TargetPositions,
                                                                                                  _defaultsHandler.ActiveAction.CenterOfTargets,
                                                                                                  _defaultsHandler.ActiveAction.CanSwitchTargetPosition,
                                                                                                  _defaultsHandler.CurrentTargetPosition);
        }
        /// <summary>
        /// Checks and returns whether or not an action can be used at a target position.
        /// </summary>
        /// <param name="action">The action used by a character.</param>
        /// <param name="targetPosition">The center target position of the action being used.</param>
        /// <returns>Whether or not an action can be used at a target position.</returns>
        private bool IsValidTarget(ActionBase action, int targetPosition)
        {
            if (!action.CanSwitchTargetPosition && action.CenterOfTargetsPosition != targetPosition)
            {
                return(false);
            }
            if (action.CanResurrect)
            {
                if (!action.CanTargetThroughUnits && targetPosition > 10)
                {
                    // If target position is last column, check both the middle and front column for a target
                    if (targetPosition % 3 == 0 && CombatStateHandler.IsPositionOccupied(targetPosition - 2))
                    {
                        return(false);
                    }
                    if (targetPosition % 3 != 1 && CombatStateHandler.IsPositionOccupied(targetPosition - 1))
                    {
                        return(false);
                    }
                }
                var targets = CombatTargeter.GetTranslatedTargetPositions(action.TargetPositions,
                                                                          action.CenterOfTargetsPosition,
                                                                          action.CanSwitchTargetPosition,
                                                                          targetPosition);

                return(CombatStateHandler.AllCharacters.Where(chr => chr.CurrentHealth <= 0 && targets.Contains(chr.Position)).Any());
            }
            else
            {
                if (!action.CanTargetThroughUnits && targetPosition > 10)
                {
                    // If target position is last column, check both the middle and front column for a target
                    if (targetPosition % 3 == 0 && CombatStateHandler.IsPositionOccupied(targetPosition - 2, false))
                    {
                        return(false);
                    }
                    if (targetPosition % 3 != 1 && CombatStateHandler.IsPositionOccupied(targetPosition - 1, false))
                    {
                        return(false);
                    }
                }

                var targets = CombatTargeter.GetTranslatedTargetPositions(action.TargetPositions,
                                                                          action.CenterOfTargetsPosition,
                                                                          action.CanSwitchTargetPosition,
                                                                          targetPosition);

                return(CombatStateHandler.AllCharacters.Where(chr => chr.CurrentHealth > 0 && targets.Contains(chr.Position)).Any());
            }
        }
        public void GetTranslatedTargetPositions_WithModifiedCenter_ReturnsModifiedPositions()
        {
            var defaultTargets = new List <int>()
            {
                1, 2, 3, 6, 9
            };
            int  centerOfTargets          = 1;
            bool canSwitchTargetPositions = true;
            int  targetPosition           = 4;

            var expected = new List <int>()
            {
                4, 5, 6, 9
            };
            var actual = CombatTargeter.GetTranslatedTargetPositions(defaultTargets,
                                                                     centerOfTargets,
                                                                     canSwitchTargetPositions,
                                                                     targetPosition);
            var actualAsList = new List <int>(actual);

            CollectionAssert.AreEquivalent(expected, actualAsList);
        }
        public void GetTranslatedTargetPositions_WithTargetsOutOfBounds_ReturnsNoOutOfBounds()
        {
            var defaultTargets = new List <int>()
            {
                2, 4, 5, 6, 8
            };
            int  centerOfTargets          = 5;
            bool canSwitchTargetPositions = true;
            int  targetPosition           = 9;

            var expected = new List <int>()
            {
                6, 8, 9
            };
            var actual = CombatTargeter.GetTranslatedTargetPositions(defaultTargets,
                                                                     centerOfTargets,
                                                                     canSwitchTargetPositions,
                                                                     targetPosition);
            var actualAsList = new List <int>(actual);

            CollectionAssert.AreEquivalent(expected, actualAsList);
        }
        public void GetTranslatedTargetPositions_OnTopRightCorner()
        {
            var defaultTargets = new List <int>()
            {
                2, 4, 5, 6, 8
            };
            int  centerOfTargets          = 5;
            bool canSwitchTargetPositions = true;
            int  targetPosition           = 3;

            var expected = new List <int>()
            {
                2, 3, 6
            };
            var actual = CombatTargeter.GetTranslatedTargetPositions(defaultTargets,
                                                                     centerOfTargets,
                                                                     canSwitchTargetPositions,
                                                                     targetPosition);
            var actualAsList = new List <int>(actual);

            CollectionAssert.AreEquivalent(expected, actualAsList);
        }
        public void GetTranslatedTargetPositions_WithDifferentFormationAndMultipleOutOfBounds()
        {
            var defaultTargets = new List <int>()
            {
                2, 5, 6, 8
            };
            int  centerOfTargets          = 2;
            bool canSwitchTargetPositions = true;
            int  targetPosition           = 17;

            var expected = new List <int>()
            {
                17
            };
            var actual = CombatTargeter.GetTranslatedTargetPositions(defaultTargets,
                                                                     centerOfTargets,
                                                                     canSwitchTargetPositions,
                                                                     targetPosition);
            var actualAsList = new List <int>(actual);

            CollectionAssert.AreEquivalent(expected, actualAsList);
        }
        public void GetTranslatedTargetPositions_WithOneTarget_ReturnsTarget()
        {
            var defaultTargets = new List <int>()
            {
                5
            };
            int  centerOfTargets          = 5;
            bool canSwitchTargetPositions = true;
            int  targetPosition           = 5;

            var expected = new List <int>()
            {
                5
            };
            var actual = CombatTargeter.GetTranslatedTargetPositions(defaultTargets,
                                                                     centerOfTargets,
                                                                     canSwitchTargetPositions,
                                                                     targetPosition);
            var actualAsList = new List <int>(actual);

            CollectionAssert.AreEquivalent(expected, actualAsList);
        }
        /// <summary>
        /// Changes the focus target in the formation panel.
        /// </summary>
        /// <param name="changeAmount">The amount to change the focus target by.</param>
        private void ChangeFocus(int changeAmount)
        {
            if (_defaultsHandler.IsInFormationPanel && changeAmount != 0)
            {
                _defaultsHandler.CurrentTargetPosition += changeAmount;
                if (_defaultsHandler.CurrentTargetPosition < 1)
                {
                    _defaultsHandler.CurrentTargetPosition = 1;
                }
                if (_defaultsHandler.CurrentTargetPosition > 18)
                {
                    _defaultsHandler.CurrentTargetPosition = 18;
                }

                var targetPositions = CombatTargeter.GetTranslatedTargetPositions(_defaultsHandler.ActiveAction.TargetPositions,
                                                                                  _defaultsHandler.ActiveAction.CenterOfTargets,
                                                                                  _defaultsHandler.ActiveAction.CanSwitchTargetPosition ||
                                                                                  RenderFocus,
                                                                                  _defaultsHandler.CurrentTargetPosition);

                _defaultsHandler.CurrentTargetPositions = targetPositions;
            }
        }
        /// <summary>
        /// Performs a character action.
        /// </summary>
        /// <param name="commandType">The type of command that is being performed, such as Attack or Spells.</param>
        /// <param name="category">The category of the action being performed, may be left blank if the action has no categories.</param>
        /// <param name="index">The index of the action being performed.</param>
        /// <param name="targetPosition">The target position of the action being performed.</param>
        public void StartAction(Commands commandType, string category, int index, int targetPosition)
        {
            bool                isInvalidAction = false;
            ActionBase          action;
            IReadOnlyList <int> targets;

            switch (commandType)
            {
            case Commands.Attack:
                action          = DisplayManager.GetActionsFromCategory <Attack>(commandType, category)[index];
                isInvalidAction = !IsValidTarget(action, targetPosition);
                if (!isInvalidAction)
                {
                    targets = CombatTargeter.GetTranslatedTargetPositions(action.TargetPositions,
                                                                          action.CenterOfTargetsPosition,
                                                                          action.CanSwitchTargetPosition,
                                                                          targetPosition);
                    _actionController.StartAction(CombatStateHandler.CurrentRoundOrder[0], action, targets);
                }
                break;

            case Commands.Spells:
                action          = DisplayManager.GetActionsFromCategory <Spell>(commandType, category)[index];
                isInvalidAction = !IsValidTarget(action, targetPosition);
                if (!isInvalidAction)
                {
                    targets = CombatTargeter.GetTranslatedTargetPositions(action.TargetPositions,
                                                                          action.CenterOfTargetsPosition,
                                                                          action.CanSwitchTargetPosition,
                                                                          targetPosition);
                    _actionController.StartAction(CombatStateHandler.CurrentRoundOrder[0], action, targets);
                }
                break;

            case Commands.Items:
                var item = DisplayManager.GetConsumablesFromCategory(category)[index];
                isInvalidAction = !IsValidTarget(item.ItemSpell, targetPosition);
                if (!isInvalidAction)
                {
                    targets = CombatTargeter.GetTranslatedTargetPositions(item.ItemSpell.TargetPositions,
                                                                          item.ItemSpell.CenterOfTargetsPosition,
                                                                          item.ItemSpell.CanSwitchTargetPosition,
                                                                          targetPosition);
                    _actionController.StartAction(CombatStateHandler.CurrentRoundOrder[0], item.ItemSpell, targets);
                    _consumablesHandler.UseConsumable(item, CombatStateHandler.CurrentRoundOrder[0]);
                }
                break;

            case Commands.Wait:
                if (CombatStateHandler.CurrentRoundOrder.Count() == 1)
                {
                    isInvalidAction = true;
                }
                else
                {
                    CombatStateHandler.BeginWait();
                    CharacterBeginWait?.Invoke(this, new CombatLoggableEventArgs()
                    {
                        LogMessage = CombatMessenger.GetBeginWaitMessage(CombatStateHandler.CurrentRoundOrder[0].Name)
                    });
                }
                break;

            default:
                isInvalidAction = true;
                break;
            }

            if (!isInvalidAction)
            {
                EndTurn();
            }
        }