feature: Added message parser for better templating
This commit is contained in:
parent
5c27b5e5b6
commit
11243f1474
9 changed files with 247 additions and 13 deletions
12
Assets/Scripts/Messaging/MessageParser.cs
Normal file
12
Assets/Scripts/Messaging/MessageParser.cs
Normal file
|
@ -0,0 +1,12 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace Messaging {
|
||||
public static class MessageParser {
|
||||
private const char MessageDifferentiator = '|';
|
||||
|
||||
public static KeyValuePair<string, string> SplitMessage(string message) {
|
||||
string[] result = message.Split(MessageDifferentiator);
|
||||
return new KeyValuePair<string, string>(result[0], result[1]);
|
||||
}
|
||||
}
|
||||
}
|
3
Assets/Scripts/Messaging/MessageParser.cs.meta
Normal file
3
Assets/Scripts/Messaging/MessageParser.cs.meta
Normal file
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: d7e3a5f1cf5d487eab9b2b5e232efeda
|
||||
timeCreated: 1706346013
|
|
@ -8,24 +8,34 @@ namespace Messaging {
|
|||
public class Message : MonoBehaviour {
|
||||
public RectTransform RectTransform { private set; get; }
|
||||
public string Text => messageText.text;
|
||||
public string RealText => realMessageText.text;
|
||||
|
||||
[SerializeField] private TMP_Text messageText;
|
||||
[SerializeField] private TMP_Text realMessageText;
|
||||
|
||||
private TextTyper _textTyper;
|
||||
private TextTyper _realTextTyper;
|
||||
private Coroutine _typingCoroutine;
|
||||
private Coroutine _realTypingCoroutine;
|
||||
|
||||
private void Awake() {
|
||||
RectTransform = GetComponent<RectTransform>();
|
||||
_textTyper = new TextTyper(messageText);
|
||||
_realTextTyper = new TextTyper(realMessageText);
|
||||
}
|
||||
|
||||
public void SetMessageText(string text, bool animate) {
|
||||
this.EnsureCoroutineStopped(ref _typingCoroutine);
|
||||
public void SetMessageText(string text, bool animate) =>
|
||||
SetText(ref _typingCoroutine, messageText, _textTyper, text, animate);
|
||||
public void SetMessageRealText(string text, bool animate) =>
|
||||
SetText(ref _realTypingCoroutine, realMessageText, _realTextTyper, text, animate);
|
||||
|
||||
private void SetText(ref Coroutine routine, TMP_Text tmpText, TextTyper typer, string text, bool animate) {
|
||||
this.EnsureCoroutineStopped(ref routine);
|
||||
|
||||
if (animate)
|
||||
_typingCoroutine = StartCoroutine(_textTyper.AnimateTextIn(text, null));
|
||||
routine = StartCoroutine(typer.AnimateTextIn(text, null));
|
||||
else
|
||||
messageText.text = text;
|
||||
tmpText.text = text;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,32 +1,61 @@
|
|||
using System;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Messaging {
|
||||
public class MessageManager : MonoBehaviour{
|
||||
public class MessageManager : MonoBehaviour {
|
||||
[SerializeField] private MessageStructureGenerator messageStructureGenerator;
|
||||
|
||||
private Message[] _messages;
|
||||
|
||||
|
||||
[SerializeField] private TextAsset textAsset;
|
||||
|
||||
private void Awake() {
|
||||
_messages = messageStructureGenerator.GenerateMessages();
|
||||
}
|
||||
|
||||
private async void Start() {
|
||||
string[] fLines = textAsset.text.Split(':');
|
||||
int i = 0;
|
||||
while (Application.isPlaying) {
|
||||
Debug.Log(textAsset.text);
|
||||
CreateMessage(MessageParser.SplitMessage(fLines[i % 2]).Key);
|
||||
++i;
|
||||
await Task.Delay(2000);
|
||||
}
|
||||
}
|
||||
|
||||
public void CreateMessage(string message) {
|
||||
ModifyMessageText(message, _messages.Length - 1);
|
||||
}
|
||||
public void CreateRealMessage(string message) {
|
||||
ModifyRealMessageText(message, _messages.Length - 1);
|
||||
}
|
||||
|
||||
private void ModifyMessageText(string message, int index) {
|
||||
ModifyPreviousMessageText(_messages[index].Text, index - 1);
|
||||
_messages[index].SetMessageText(message, true);
|
||||
}
|
||||
private void ModifyRealMessageText(string message, int index) {
|
||||
ModifyPreviousRealMessageText(_messages[index].Text, index - 1);
|
||||
_messages[index].SetMessageRealText(message, true);
|
||||
}
|
||||
|
||||
private void ModifyPreviousMessageText(string message, int index) {
|
||||
if(index < 0) return;
|
||||
|
||||
if (index < 0) return;
|
||||
|
||||
ModifyPreviousMessageText(_messages[index].Text, index - 1);
|
||||
|
||||
|
||||
_messages[index].SetMessageText(message, false);
|
||||
}
|
||||
|
||||
private void ModifyPreviousRealMessageText(string message, int index) {
|
||||
if (index < 0) return;
|
||||
|
||||
ModifyPreviousRealMessageText(_messages[index].RealText, index - 1);
|
||||
|
||||
_messages[index].SetMessageRealText(message, false);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue