Input Buffer System
unknown
csharp
a year ago
6.3 kB
7
Indexable
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class InputBuffer : MonoBehaviour
{
public Text specialMoveText; // Display for special move execution
public GameObject buttonPrefab; // Prefab for displaying buffer icons
public Transform bufferDisplayParent; // Parent for buffer display UI
// Define button sprites
public Sprite upImage, downImage, leftImage, rightImage, lightImage, mediumImage, heavyImage, specialImage;
// Input buffer settings
private List<string> buffer = new List<string>();
private float bufferStoreTime = 0.4f;
private int maxBufferSize = 10;
private float lastInputTime;
// Dictionary to store virtual buttons mapped to Unity's Input system
private Dictionary<string, string> inputButtons = new Dictionary<string, string>
{
{"up", "Vertical"},
{"down", "Vertical"},
{"left", "Horizontal"},
{"right", "Horizontal"},
{"light", "LightAttack"},
{"medium", "MediumAttack"},
{"heavy", "HeavyAttack"},
{"special", "SpecialAttack"}
};
// Special moves
private List<SpecialMove> 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"})
};
private Dictionary<string, bool> specialMoveExecuted;
void Start()
{
lastInputTime = Time.time;
specialMoveExecuted = new Dictionary<string, bool>();
foreach (var move in specialMoves)
{
specialMoveExecuted[move.name] = false;
}
}
void Update()
{
HandleInput();
ClearBufferIfNeeded();
UpdateBufferDisplay();
UpdateSpecialMoveDisplay();
}
void HandleInput()
{
// Movement inputs
float vertical = Input.GetAxisRaw("Vertical");
float horizontal = Input.GetAxisRaw("Horizontal");
// Check directional inputs based on axis values
if (vertical > 0) AddInput("up");
else if (vertical < 0) AddInput("down");
if (horizontal > 0) AddInput("right");
else if (horizontal < 0) AddInput("left");
// Check for attack button presses
if (Input.GetButtonDown("LightAttack")) AddInput("light");
if (Input.GetButtonDown("MediumAttack")) AddInput("medium");
if (Input.GetButtonDown("HeavyAttack")) AddInput("heavy");
if (Input.GetButtonDown("SpecialAttack")) AddInput("special");
// Check for special moves in buffer
foreach (var move in specialMoves)
{
if (IsSequenceMatch(buffer, move.sequence))
{
specialMoveExecuted[move.name] = true;
}
}
// Update last input time
lastInputTime = Time.time;
}
void AddInput(string input)
{
buffer.Add(input);
if (buffer.Count > maxBufferSize)
{
buffer.RemoveAt(0); // Keep buffer size within max limit
}
}
void ClearBufferIfNeeded()
{
if (Time.time - lastInputTime >= bufferStoreTime)
{
buffer.Clear();
foreach (var move in specialMoves)
{
specialMoveExecuted[move.name] = false;
}
}
}
void UpdateBufferDisplay()
{
// Clear existing buttons
foreach (Transform child in bufferDisplayParent)
{
Destroy(child.gameObject);
}
// Display buffer images in sequence
float xPosition = 20f;
foreach (var button in buffer)
{
GameObject buttonImage = Instantiate(buttonPrefab, bufferDisplayParent);
buttonImage.transform.localPosition = new Vector3(xPosition, 0, 0);
// Set the correct sprite based on button
SpriteRenderer spriteRenderer = buttonImage.GetComponent<SpriteRenderer>();
spriteRenderer.sprite = GetButtonSprite(button);
xPosition += spriteRenderer.bounds.size.x + 10f;
}
}
Sprite GetButtonSprite(string button)
{
switch (button)
{
case "up": return upImage;
case "down": return downImage;
case "left": return leftImage;
case "right": return rightImage;
case "light": return lightImage;
case "medium": return mediumImage;
case "heavy": return heavyImage;
case "special": return specialImage;
default: return null;
}
}
void UpdateSpecialMoveDisplay()
{
string displayText = "";
foreach (var move in specialMoves)
{
if (specialMoveExecuted[move.name])
{
displayText += move.name + " Executed!\n";
}
}
specialMoveText.text = displayText;
}
bool IsSequenceMatch(List<string> buffer, List<string> sequence)
{
if (buffer.Count < sequence.Count) return false;
for (int i = 0; i < sequence.Count; i++)
{
if (buffer[buffer.Count - sequence.Count + i] != sequence[i])
{
return false;
}
}
return true;
}
}
// Helper class for defining special moves
[System.Serializable]
public class SpecialMove
{
public string name;
public List<string> sequence;
public SpecialMove(string name, List<string> sequence)
{
this.name = name;
this.sequence = sequence;
}
}
Editor is loading...
Leave a Comment