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

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();