Quantcast
Channel: Community | MonoGame - Latest topics
Viewing all articles
Browse latest Browse all 6821

AnimatedSprite Combo System

$
0
0

@Dante3085 wrote:

Hi everyone,

CONCEPT

I'd like to build a Combo System for AnimatedSprite class, so that for example timed button presses in succession lead to a string of Animations (Like in Devil May Cry). I am having difficulty with the exact implementation. Any ideas would be very much appreciated!

The basic idea is to have a Combo be a tree structure comprised of ComboNodes. The designated Button for the root ComboNode leads to the execution of it's Animation and the opening of a TimeFrame in which another button can be pressed for the next ComboNode to execute.

The Combo class updates the current ComboNode for checking button presses and eventually leading to the next ComboNode. Each ComboNode can set the Sprites Animation by using the "SetAnimation(EAnimation)" method.

The lack of appropriate input inside the given timeframe leads to the Combo being reset to the root ComboNode.

RELVANT CODE

public class Combo
{
#region MemberVariables

    private ComboNode _root;
    private ComboNode _current;
    private AnimatedSprite _sprite;

    #endregion
    #region Properties

    public ComboNode Root => _root;

    public ComboNode Current
    {
        get => _current;
        set => _current = value;
    }

    public AnimatedSprite Sprite => _sprite;

    #endregion
    #region Methods

    public Combo(ComboNode root, AnimatedSprite sprite)
    {
        _root = root;
        _current = _root;
        _sprite = sprite;
    }

    public void Update(GameTime gameTime)
    {
        _current.Update(gameTime);
    }

    /// <summary>
    /// Resets the Combo to root ComboNode.
    /// </summary>
    public void Reset()
    {
        _current = _root;
    }

    public void Add(ComboNode comboNode)
    {
        throw new NotImplementedException();
    }

    public override string ToString()
    {
        throw new NotImplementedException();
    }

    #endregion
}

}

public class ComboNode
{
#region MemberVariables

    /// <summary>
    /// The Combo that this ComboNode is part of.
    /// </summary>
    private Combo _combo;

    /// <summary>
    /// Animation associated to this part of the Combo.
    /// </summary>
    private EAnimation _animation;

    /// <summary>
    /// Part of the Combo that comes after this.
    /// </summary>
    private Dictionary<Keys, ComboNode> _next;

    /// <summary>
    /// PassedTime since last ComboNode finished.
    /// </summary>
    private int _passedTime;

    /// <summary>
    /// Stores if this ComboNode has been executed.
    /// </summary>
    private bool _checkNextInput = false;

    /// <summary>
    /// When this ComboNode has finished, the Intervall describes the TimeFrame in which the corresponding
    /// Key / Button of the next ComboNode has to be pressed for it to execute.
    /// </summary>
    private Intervall _intervall;

    /// <summary>
    /// Key to execute this ComboNode.
    /// </summary>
    private Keys _key;

    /// <summary>
    /// Button to execute this ComboNode.
    /// </summary>
    private Buttons _button;

    #endregion

    /// <summary>
    /// Describes a time intervall in which the Key / Button has to be pressed to continue the Combo.
    /// </summary>
    public struct Intervall
    {
        private int _start;
        private int _end;

        public int Start => _start;
        public int End => _end;

        public Intervall(int start, int end)
        {
            _start = start;
            _end = end;
        }
    }

    public Keys Key => _key;
    public EAnimation Animation => _animation;

    public ComboNode(Combo combo, EAnimation animation, Dictionary<Keys, ComboNode> next, Intervall intervall, 
        Keys key, Buttons button)
    {
        _combo = combo;
        _animation = animation;
        _next = next;
        _intervall = intervall;
        _key = key;
        _button = button;
    }

    public void Update(GameTime gameTime)
    {
        if (!_checkNextInput)
        {
            if (InputManager.OnKeyDown(_key))
            {
                _combo.Sprite.SetAnimation(_animation);
                _checkNextInput = true;
            }
        }
        else
        {
            _passedTime += gameTime.ElapsedGameTime.Milliseconds;

            // No Input inside TimeIntervall.
            if (_passedTime > _intervall.End)
            {
                _combo.Reset();
                _checkNextInput = false;
            }

            // Look if there is / was any input for one of the next ComboNodes in the Time Intervall.
            else if (_passedTime > _intervall.Start && _passedTime < _intervall.End)
                foreach (Keys k in _next.Keys)
                    if (InputManager.OnKeyDown(k))
                        _combo.Current = _next[k];
        }
    }
}

}

///


/// Prepares the AnimatedSprite for playing the given Animation.
///

    /// <param name="name"></param>
    public void SetAnimation(EAnimation name)
    {
        // Makes sure we won't start a new annimation unless it differs from our current animation.
        if (_currentAnimation != name && _currentDirection.Equals(Direction.none))
        {
            _playingAnimation = true;
            _currentAnimation = name;
            _currentFrameIndex = 0;
            Fps = _animationFpsValues[name];
            OnAnimationStart(name);
        }
    }

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 6821

Trending Articles