added scoring system for the bow game
This commit is contained in:
147
Assets/_PROJECT/Components/Bow/Scripts/KeyBoardManager.cs
Normal file
147
Assets/_PROJECT/Components/Bow/Scripts/KeyBoardManager.cs
Normal file
@@ -0,0 +1,147 @@
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class KeyboardManager : MonoBehaviour
|
||||
{
|
||||
public enum ShiftMode { Lowercase, NextUppercase, Uppercase }
|
||||
|
||||
[Header("UI References")]
|
||||
public TMP_Text nameTextField;
|
||||
public TMP_Text scoreTextField;
|
||||
public Button shiftButton;
|
||||
public Button backspaceButton;
|
||||
public Button enterButton;
|
||||
public Button spaceButton;
|
||||
|
||||
[Header("Letter Buttons")]
|
||||
public List<Button> letterButtons;
|
||||
|
||||
public int inputTextLimit = 12;
|
||||
public ScoreBoard scoreBoard; // Reference to ScoreBoard
|
||||
public string placeHolderText;
|
||||
private string _input = "";
|
||||
private float _currentScore;
|
||||
private ShiftMode _shiftMode = ShiftMode.Lowercase;
|
||||
|
||||
void Start()
|
||||
{
|
||||
foreach (var button in letterButtons)
|
||||
{
|
||||
button.onClick.AddListener(() => OnLetterPressed(button));
|
||||
}
|
||||
|
||||
shiftButton.onClick.AddListener(OnShiftPressed);
|
||||
backspaceButton.onClick.AddListener(OnBackspacePressed);
|
||||
enterButton.onClick.AddListener(OnEnterPressed);
|
||||
spaceButton.onClick.AddListener(OnSpacePressed);
|
||||
|
||||
|
||||
|
||||
gameObject.SetActive(false); // Start disabled
|
||||
}
|
||||
|
||||
public void Activate(float score, Transform assignedTransform)
|
||||
{
|
||||
_currentScore = score;
|
||||
_input = "";
|
||||
nameTextField.text = placeHolderText;
|
||||
Debug.Log(_currentScore);
|
||||
Debug.Log(_currentScore.ToString());
|
||||
scoreTextField.text = _currentScore.ToString();
|
||||
gameObject.SetActive(true);
|
||||
|
||||
gameObject.transform.position = assignedTransform.position;
|
||||
gameObject.transform.rotation = assignedTransform.rotation;
|
||||
|
||||
}
|
||||
|
||||
public void DeActivate()
|
||||
{
|
||||
gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
private void addToInput(string newInput)
|
||||
{
|
||||
|
||||
if (_input.Length < inputTextLimit)
|
||||
_input += newInput;
|
||||
}
|
||||
|
||||
void OnLetterPressed(Button button)
|
||||
{
|
||||
var label = button.GetComponentInChildren<TMP_Text>();
|
||||
if (label == null) return;
|
||||
|
||||
string letter = label.text;
|
||||
|
||||
switch (_shiftMode)
|
||||
{
|
||||
case ShiftMode.Lowercase:
|
||||
addToInput(letter.ToLower());
|
||||
break;
|
||||
case ShiftMode.Uppercase:
|
||||
addToInput(letter.ToUpper());
|
||||
break;
|
||||
case ShiftMode.NextUppercase:
|
||||
addToInput(letter.ToUpper());
|
||||
_shiftMode = ShiftMode.Lowercase;
|
||||
UpdateKeyLabels();
|
||||
break;
|
||||
}
|
||||
|
||||
UpdateOutput();
|
||||
}
|
||||
|
||||
void OnShiftPressed()
|
||||
{
|
||||
_shiftMode = _shiftMode switch
|
||||
{
|
||||
ShiftMode.Lowercase => ShiftMode.NextUppercase,
|
||||
ShiftMode.NextUppercase => ShiftMode.Uppercase,
|
||||
ShiftMode.Uppercase => ShiftMode.Lowercase,
|
||||
_ => ShiftMode.Lowercase
|
||||
};
|
||||
|
||||
UpdateKeyLabels();
|
||||
}
|
||||
|
||||
void OnBackspacePressed()
|
||||
{
|
||||
if (_input.Length > 0)
|
||||
_input = _input.Substring(0, _input.Length - 1);
|
||||
UpdateOutput();
|
||||
}
|
||||
|
||||
void OnEnterPressed()
|
||||
{
|
||||
if (_input.Length > 0)
|
||||
{
|
||||
scoreBoard.SaveScore(_input, _currentScore);
|
||||
gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
void OnSpacePressed()
|
||||
{
|
||||
addToInput(" ");
|
||||
UpdateOutput();
|
||||
}
|
||||
|
||||
void UpdateOutput()
|
||||
{
|
||||
nameTextField.text = _input;
|
||||
}
|
||||
|
||||
void UpdateKeyLabels()
|
||||
{
|
||||
foreach (var button in letterButtons)
|
||||
{
|
||||
var label = button.GetComponentInChildren<TMP_Text>();
|
||||
if (label == null) continue;
|
||||
|
||||
label.text = _shiftMode == ShiftMode.Lowercase ? label.text.ToLower() : label.text.ToUpper();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user