Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
5.0 kB
5
Indexable
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;

public class InputBuffer : MonoBehaviour
{
    public GameObject upImage;
    public GameObject downImage;
    public GameObject leftImage;
    public GameObject rightImage;
    public GameObject lightImage;
    public GameObject mediumImage;
    public GameObject heavyImage;
    public GameObject specialImage;
    public Text specialMoveTextPrefab;

    private Dictionary<string, GameObject> buttonImages;
    private List<string> buffer = new List<string>();
    private float bufferStoreTime = 0.4f;
    private int maxBufferSize = 10;
    private float lastInputTime;

    private struct SpecialMove
    {
        public string name;
        public List<string> sequence;

        public SpecialMove(string name, List<string> sequence)
        {
            this.name = name;
            this.sequence = sequence;
        }
    }

    private List<SpecialMove> specialMoves;
    private Dictionary<string, bool> specialMoveExecuted;

    private Dictionary<string, KeyCode> VIRTUAL_BUTTONS = new Dictionary<string, KeyCode>
    {
        { "up", KeyCode.W },
        { "down", KeyCode.S },
        { "left", KeyCode.A },
        { "right", KeyCode.D },
        { "light", KeyCode.U },
        { "medium", KeyCode.I },
        { "heavy", KeyCode.J },
        { "special", KeyCode.K }
    };

    void Start()
    {
        buttonImages = new Dictionary<string, GameObject>
        {
            { "up", upImage },
            { "down", downImage },
            { "left", leftImage },
            { "right", rightImage },
            { "light", lightImage },
            { "medium", mediumImage },
            { "heavy", heavyImage },
            { "special", specialImage }
        };

        specialMoves = new List<SpecialMove>
        {
            new SpecialMove("Flame Punch", new List<string> { "down", "right", "light" }),
            new SpecialMove("Tornado Kick", new List<string> { "down", "left", "light" }),
            new SpecialMove("Rising Uppercut", new List<string> { "right", "down", "right", "special" }),
            new SpecialMove("Surging Heat", new List<string> { "down", "down", "special" }),
            new SpecialMove("Inferno Overdrive", new List<string> { "down", "down", "left", "heavy", "special" }),
            new SpecialMove("Volcanic Vortex", new List<string> { "left", "down", "right", "left", "down", "right", "heavy", "special" }),
            new SpecialMove("Quick Rise", new List<string> { "up", "up" })
        };

        specialMoveExecuted = new Dictionary<string, bool>();
        foreach (var move in specialMoves)
        {
            specialMoveExecuted[move.name] = false;
        }
    }

    void Update()
    {
        foreach (var button in VIRTUAL_BUTTONS)
        {
            if (Input.GetKeyDown(button.Value))
            {
                buffer.Add(button.Key);

                if (buffer.Count > maxBufferSize)
                {
                    buffer.RemoveAt(0);
                }

                lastInputTime = Time.time;

                foreach (var move in specialMoves)
                {
                    if (buffer.Count >= move.sequence.Count)
                    {
                        if (buffer.GetRange(buffer.Count - move.sequence.Count, move.sequence.Count).Equals(move.sequence))
                        {
                            specialMoveExecuted[move.name] = true;
                        }
                    }
                }
            }
        }

        if (Time.time - lastInputTime >= bufferStoreTime)
        {
            buffer.Clear();
            foreach (var move in specialMoves)
            {
                specialMoveExecuted[move.name] = false;
            }
        }

        DisplayBuffer();
        DisplaySpecialMoves();
    }

    void DisplayBuffer()
    {
        float xPosition = 20f;
        foreach (string button in buffer)
        {
            if (buttonImages.ContainsKey(button))
            {
                GameObject buttonImage = Instantiate(buttonImages[button], transform);
                RectTransform rt = buttonImage.GetComponent<RectTransform>();
                rt.anchoredPosition = new Vector2(xPosition, 20f);
                xPosition += rt.rect.width + 10f;
            }
        }
    }

    void DisplaySpecialMoves()
    {
        float yPosition = 60f;
        foreach (var move in specialMoves)
        {
            if (specialMoveExecuted[move.name])
            {
                Text specialMoveText = Instantiate(specialMoveTextPrefab, transform);
                specialMoveText.text = move.name + " Executed!";
                RectTransform rt = specialMoveText.GetComponent<RectTransform>();
                rt.anchoredPosition = new Vector2(20f, yPosition);
                yPosition += 40f;
            }
        }
    }
}
Leave a Comment