Beispiel #1
0
 /// <summary>
 /// Add a special attack with multiple key combos to the DFA.
 /// </summary>
 /// <remarks>
 /// Any of the key combos trigger the attack.
 /// </remarks>
 /// <param name="keyCombos">Array of key combos that trigger this attack.</param>
 /// <param name="attackCallback">Callback to be returned when the attack is triggered.</param>
 public void AddSpecialAttack(KeyCode[][] keyCombos, AttackCallback attackCallback)
 {
     foreach (KeyCode[] keyCombo in keyCombos)
     {
         AddSpecialAttack(keyCombo, attackCallback);
     }
 }
Beispiel #2
0
        /// <summary>
        /// Add a new special attack with a single key combo to the DFA.
        /// </summary>
        /// <remarks>
        /// Only the specified key combo will trigger the attack (unless you add more key combos).
        /// </remarks>
        /// <param name="keyCombo">Key combo that triggers this attack.</param>
        /// <param name="attackCallback">Callback to be returned when the attack is triggered.</param>
        public void AddSpecialAttack(KeyCode[] keyCombo, AttackCallback attackCallback)
        {
            int state = 0;

            foreach (KeyCode key in keyCombo)
            {
                // TODO: Handle keys not added by AddKeyCode().
                int idx = _keyCodeMap[key];

                if (_stateChart[state][idx] == 0)
                {   // We need to add a new state.
                    int newState = AddState();

                    // Add the transition.
                    _stateChart[state][idx] = newState;
                }

                // Follow the transition.
                state = _stateChart[state][idx];
            }

            // Good, we should now be in the final state.
            // TODO: Handle key combo collisions.
            _attackMap[state] = attackCallback;
        }
Beispiel #3
0
    public IEnumerator DelayAttack(AttackCallback attackCallback, Vector3 direction, float delay)
    {
        yield return(new WaitForSeconds(delay));

        attackCallback(direction);
        yield break;
    }
Beispiel #4
0
        /// <summary>
        /// Feed the currently pressed key to the DFA.
        /// </summary>
        /// <remarks>
        /// If a special attack is detected, <see cref="DetectedSpecialAttack"/> is set
        /// to a non-NULL value. You can then call that callback to trigger the attack.
        /// </remarks>
        public void Check()
        {
            // No special attack detected yet, so make sure the last
            // detected action is cleared.
            _detectedSpecialAttack = null;

            // Check for the pressed key.
            KeyCode pressedKey = KeyCode.None;

            foreach (KeyCode key in _keyCodeMap.Keys)
            {
                if (ControlKeysManager.GetKeyDown(key))
                {
                    pressedKey = key;
                    break;
                }
            }

            if (pressedKey == KeyCode.None)
            {   // No (known) key pressed.
                return;
            }

            if (_lastTimePress > 0 && Time.time - _lastTimePress > COMBOTIME)
            {   // Timeout. Combo rejected.
                _currentState = 0;
            }

            _lastTimePress = Time.time;

            // This key maps to which index?
            int keyIndex = _keyCodeMap[pressedKey];

            // Determine the next state.
            _currentState = _stateChart[_currentState][keyIndex];

            // And, is it a special attack?
            if (!_attackMap.TryGetValue(_currentState, out _detectedSpecialAttack))
            {   // Nope. It's a normal state.
                return;
            }

            // Yes, it is a special attack! We are done.
            foreach (int nextState in _stateChart[_currentState])
            {
                if (nextState != 0)
                {   // We cannot return to state 0 as there may be another special attack
                    // after this one.
                    // DO NOT RESET THE STATE HERE. State needs to be kept so that
                    // e. g. the triple attack works.
                    return;
                }
            }

            // There is no special attack after this one; return to state 0.
            _currentState = 0;
        }