feature: Added lives

This commit is contained in:
Gerard Gascón 2024-01-27 20:17:18 +01:00
parent fa82f69ed5
commit 84a34deec6
19 changed files with 1036 additions and 134 deletions

3
Assets/Scripts/Flow.meta Normal file
View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 0e73f9d340ab4a6596779492ca8fa9f1
timeCreated: 1706381395

View file

@ -0,0 +1,36 @@
using System;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;
namespace Flow {
public class Lives : MonoBehaviour {
[SerializeField] private Image[] lives;
[SerializeField] private Sprite correctLive;
[SerializeField] private Sprite wrongLive;
public int CurrentLives { private set; get; }
private void Awake() {
ResetLives();
}
public void Wrong() {
CurrentLives--;
int timesHurt = lives.Length - CurrentLives;
for (int i = 0; i < timesHurt; i++) {
lives[i].sprite = wrongLive;
}
}
public void ResetLives() {
CurrentLives = lives.Length;
foreach (Image life in lives) {
life.sprite = correctLive;
}
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4ac00191cb424148b6fbefc0c595175b
timeCreated: 1706381399

View file

@ -45,7 +45,7 @@ namespace Messaging {
}
}
public void SetMessageRealText(string text, bool animate, bool isAnswer) {
public Coroutine SetMessageRealText(string text, bool animate, bool isAnswer) {
RealText = text;
IsAnswer = isAnswer;
IsReal = true;
@ -54,17 +54,19 @@ namespace Messaging {
SetText(ref answerContainer.RealTypingCoroutine, answerContainer.realMessageText,
answerContainer.RealTextTyper, text, animate);
if(string.IsNullOrEmpty(text)) return;
if(string.IsNullOrEmpty(text)) return null;
answerContainer.container.SetActive(true);
questionContainer.container.SetActive(false);
} else {
SetText(ref questionContainer.RealTypingCoroutine, questionContainer.realMessageText,
questionContainer.RealTextTyper, text, animate);
if(string.IsNullOrEmpty(text)) return;
questionContainer.container.SetActive(true);
answerContainer.container.SetActive(false);
return answerContainer.RealTypingCoroutine;
}
SetText(ref questionContainer.RealTypingCoroutine, questionContainer.realMessageText,
questionContainer.RealTextTyper, text, animate);
if(string.IsNullOrEmpty(text)) return null;
questionContainer.container.SetActive(true);
answerContainer.container.SetActive(false);
return questionContainer.RealTypingCoroutine;
}
private void SetText(ref Coroutine routine, TMP_Text tmpText, TextTyper typer, string text, bool animate) {

View file

@ -1,7 +1,9 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Flow;
using UnityEngine;
using Random = UnityEngine.Random;
@ -13,16 +15,20 @@ namespace Messaging {
[SerializeField] private TextAsset[] jokes;
[SerializeField, Min(0)] private float sendJokeDelay = 2f;
[SerializeField, Min(0)] private float sendJokeLongDelay = 8f;
[SerializeField, Min(0)] private float initialSendJokeDelay = 4f;
private List<TextAsset> _jokesBag;
private MessageData.MessageData _currentJoke;
private int _currentJokeIndex;
private Lives _lives;
private void Awake() {
_messages = messageStructureGenerator.GenerateMessages();
_jokesBag = new List<TextAsset>(jokes);
_lives = FindObjectOfType<Lives>();
}
private void Start() {
@ -41,32 +47,44 @@ namespace Messaging {
_currentJokeIndex--;
_currentJokeIndex = Mathf.Max(_currentJokeIndex, 0);
if (message == _currentJoke.AnswerMessage.Key) {
ModifyRealMessageText(_currentJoke.QuestionMessage.Value, _currentJokeIndex, false);
ModifyRealMessageText(_currentJoke.AnswerMessage.Value, _messages.Length - 1, true);
SendRandomJoke(sendJokeDelay);
StartCoroutine(ShowRealTexts());
if(_currentJokeIndex == 0)
SendRandomJoke(sendJokeLongDelay);
else
SendRandomJoke(sendJokeDelay);
} else {
//TODO: Add lives support
_lives.Wrong();
if (_lives.CurrentLives == 0) {
StartCoroutine(ShowRealTexts());
SendRandomJoke(sendJokeLongDelay);
_lives.ResetLives();
}
}
} else {
_currentJokeIndex = _messages.Length - 1;
}
}
IEnumerator ShowRealTexts() {
yield return ModifyRealMessageText(_currentJoke.QuestionMessage.Value, _currentJokeIndex, false);
yield return ModifyRealMessageText(_currentJoke.AnswerMessage.Value, _messages.Length - 1, true);
}
private void ModifyMessageText(string message, int index, bool isAnswer) {
ModifyPreviousMessage(index);
_messages[index].SetMessageText(message, true, isAnswer);
}
private void ModifyRealMessageText(string message, int index, bool isAnswer) {
_messages[index].SetMessageRealText(message, true, isAnswer);
private Coroutine ModifyRealMessageText(string message, int index, bool isAnswer) {
return _messages[index].SetMessageRealText(message, true, isAnswer);
}
private void ModifyPreviousMessage(int index) {
private Coroutine ModifyPreviousMessage(int index) {
ModifyPreviousMessage(_messages[index].Text, _messages[index].RealText, index - 1,
_messages[index].IsAnswer, _messages[index].IsReal);
_messages[index].SetMessageText("", false, _messages[index].IsAnswer);
_messages[index].SetMessageRealText("", false, _messages[index].IsAnswer);
return _messages[index].SetMessageRealText("", false, _messages[index].IsAnswer);
}
private MessageData.MessageData GetRandomJoke() {

View file

@ -24,6 +24,9 @@ namespace Messaging.Typer {
if (ShouldShowNextCharacter(timeOfLastCharacter)) {
if (_textBox.maxVisibleCharacters <= _textBox.textInfo.characterCount) {
_textBox.maxVisibleCharacters++;
if (_textBox.text[_textBox.maxVisibleCharacters - 1] == ' ') {
_textBox.maxVisibleCharacters++;
}
timeOfLastCharacter = Time.unscaledTime;
if (_textBox.maxVisibleCharacters == _textBox.textInfo.characterCount) {
onFinish?.Invoke();