Пример #1
0
        // Default to just targeting creatures. If you specify a targetPos that will be the start, otherwise it will be the position
        // of the first valid target if not specified.
        public Targeting(Action <Coord> onTargetSelected, Coord targetPos = null, Func <Coord, bool> targetValidator = null)
        {
            _onTargetSelected = onTargetSelected;

            _targetValidator = targetValidator ?? DEFAULT_TARGET_VALIDATOR;
            // Create list of valid target locations in FOV, according to our selector
            _validTargets = new List <Coord>(GrimDank.Instance.TestLevel.CurrentFOV.Where(_targetValidator));

            if (_validTargets.Count == 0)
            {
                MessageLog.Write("No valid targets.");
                InputStack.Remove(this);
                GrimDank.Instance.TestLevel.Targeter = null;
            }

            if (targetPos != null) // -1 if the original given position isn't a valid target, otherwise we start at that point in the list.
            {
                _currentTargetIndex = _validTargets.FindIndex(c => c == targetPos);
                TargetPos           = targetPos;
            }
            else // There are valid targets but we didn't select a position so default to first target in list
            {
                _currentTargetIndex = 0;
                TargetPos           = _validTargets[_currentTargetIndex];
            }
        }
Пример #2
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            InputStack.Update(gameTime);

            TurnManager.Update(gameTime);

            base.Update(gameTime);
        }
Пример #3
0
        public bool HandleKeyboard(Keys key, ModifierState modifierState)
        {
            Direction dirToMove = Controls.Move(key);
            bool      handled   = true;

            if (dirToMove == Direction.NONE)
            {
                switch (key)
                {
                case Keys.Enter:
                    if (_targetValidator(TargetPos))     // CurrentPos is valid
                    {
                        InputStack.Remove(this);
                        GrimDank.Instance.TestLevel.Targeter = null;
                        _onTargetSelected(TargetPos);
                    }
                    else
                    {
                        MessageLog.Write("Invalid target.");
                    }

                    break;

                case Keys.Add:
                    // This works even if we started at -1.
                    if (_validTargets.Count != 0)
                    {
                        _currentTargetIndex = MathHelpers.WrapAround(_currentTargetIndex + 1, _validTargets.Count);
                        TargetPos           = _validTargets[_currentTargetIndex];
                    }
                    break;

                case Keys.Escape:
                    InputStack.Remove(this);
                    GrimDank.Instance.TestLevel.Targeter = null;
                    break;

                default:
                    handled = false;
                    break;
                }
            }
            // Here we purposely do NOT reset the _currentTargetIndex to -1, to preserve the starting point in case the user presses + sometime again
            // in the future (we pick up where we left off)
            else
            {
                TargetPos += dirToMove;
            }

            return(handled);
        }
Пример #4
0
 public GlobalKeyHandler()
 {
     InputStack.Add(this);
 }