From a4a6cccc6cfa3b34f082aa45b672dd2c5d2993aa Mon Sep 17 00:00:00 2001 From: Geri Date: Sun, 28 Jan 2024 10:54:56 +0100 Subject: [PATCH] feature: Added progress bar --- Assets/Scenes/Main.unity | 15 ++++++++++ Assets/Scripts/Flow/ProgressBar.cs | 29 +++++++++++++++++++ Assets/Scripts/Flow/ProgressBar.cs.meta | 11 +++++++ .../Scripts/Messaging/Typer/MessageManager.cs | 20 ++++++++----- 4 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 Assets/Scripts/Flow/ProgressBar.cs create mode 100644 Assets/Scripts/Flow/ProgressBar.cs.meta diff --git a/Assets/Scenes/Main.unity b/Assets/Scenes/Main.unity index 04807da..6f141b5 100644 --- a/Assets/Scenes/Main.unity +++ b/Assets/Scenes/Main.unity @@ -2474,6 +2474,7 @@ GameObject: - component: {fileID: 1352489079} - component: {fileID: 1352489078} - component: {fileID: 1352489077} + - component: {fileID: 1352489081} m_Layer: 5 m_Name: ProgressCanvas m_TagString: Untagged @@ -2564,6 +2565,20 @@ RectTransform: m_AnchoredPosition: {x: 0, y: 0} m_SizeDelta: {x: 0, y: 0} m_Pivot: {x: 0, y: 0} +--- !u!114 &1352489081 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1352489076} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: bbbaf5b51c023404885d2bbb502c3276, type: 3} + m_Name: + m_EditorClassIdentifier: + bar: {fileID: 843204721} + numberToWin: 10 --- !u!1 &1488949019 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Flow/ProgressBar.cs b/Assets/Scripts/Flow/ProgressBar.cs new file mode 100644 index 0000000..4ef575e --- /dev/null +++ b/Assets/Scripts/Flow/ProgressBar.cs @@ -0,0 +1,29 @@ +using System; +using UnityEngine; +using UnityEngine.UI; + +namespace Flow { + public class ProgressBar : MonoBehaviour { + [SerializeField] private Image bar; + + private int _progressDone; + [SerializeField, Min(0)] private int numberToWin = 10; + + private void Awake() { + UpdateFill(); + } + + public void AddProgress() { + _progressDone++; + + if (_progressDone <= numberToWin) { + UpdateFill(); + } + } + + private void UpdateFill() { + float fillPercentage = _progressDone / (float)numberToWin; + bar.fillAmount = fillPercentage; + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Flow/ProgressBar.cs.meta b/Assets/Scripts/Flow/ProgressBar.cs.meta new file mode 100644 index 0000000..1ac6dc1 --- /dev/null +++ b/Assets/Scripts/Flow/ProgressBar.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: bbbaf5b51c023404885d2bbb502c3276 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Messaging/Typer/MessageManager.cs b/Assets/Scripts/Messaging/Typer/MessageManager.cs index 9d58ca4..20fca3e 100644 --- a/Assets/Scripts/Messaging/Typer/MessageManager.cs +++ b/Assets/Scripts/Messaging/Typer/MessageManager.cs @@ -72,16 +72,18 @@ namespace Messaging { _currentJokeIndex = Mathf.Max(_currentJokeIndex, 0); if (message == _currentJoke.AnswerMessage.Key) { _sendButton.Lock(); - Coroutine routine = StartCoroutine(ShowRealTexts()); - if(_currentJokeIndex == 0) - StartCoroutine(SendRandomJoke(routine, sendJokeDelay)); - else - StartCoroutine(SendRandomJoke(routine, sendJokeDelay)); + bool isCorrect = _currentJokeIndex != 0; + Coroutine routine = StartCoroutine(ShowRealTexts(isCorrect)); + StartCoroutine(SendRandomJoke(routine, sendJokeDelay)); + + if (isCorrect) { + FindObjectOfType().AddProgress(); + } } else { _lives.Wrong(); if (_lives.CurrentLives == 0) { _sendButton.Lock(); - Coroutine routine = StartCoroutine(ShowRealTexts()); + Coroutine routine = StartCoroutine(ShowRealTexts(false)); StartCoroutine(SendRandomJoke(routine, initialSendJokeDelay)); _lives.ResetLives(); } @@ -91,9 +93,11 @@ namespace Messaging { } } - private IEnumerator ShowRealTexts() { + private IEnumerator ShowRealTexts(bool isCorrect) { yield return ModifyRealMessageText(_currentJoke.QuestionMessage.Value, _currentJokeIndex, false); - yield return ModifyMessageText(_currentJoke.AnswerMessage.Key, _messages.Length - 1, true, false); + if (!isCorrect) { + yield return ModifyMessageText(_currentJoke.AnswerMessage.Key, _messages.Length - 1, true, false); + } yield return ModifyRealMessageText(_currentJoke.AnswerMessage.Value, _messages.Length - 1, true); }