70 lines
2.3 KiB
C#
Raw Normal View History

2021-07-01 03:08:38 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
2021-07-01 03:08:38 +08:00
public class Machine : Interactive
2021-07-01 03:08:38 +08:00
{
// Start is called before the first frame update
private Text codeTextView;
private IndexRecoder indexRecoder;
2021-07-01 03:08:38 +08:00
void Start()
{
//m_interface = GameObject.Find("MachinePanel");
codeTextView = m_interface.GetComponentInChildren<Text>();
indexRecoder = FindObjectOfType<IndexRecoder>();
2021-07-01 03:08:38 +08:00
}
// Update is called once per frame
void Update()
{
}
//从Player类发消息来调用这个函数temp为接受到的字符可能是. || -。
public override void Coding(string temp)
{
codeTextView.text += temp;//给当前输入总体加上刚输入的字符
if(codeTextView.text.Length >= 8) ClearChecker(codeTextView.text);//当长度超过8位每打一位就检查一下是否有连续的八个点
if(codeTextView.text.Length % 4 == 0) Translate(codeTextView.text);//每输入四位就翻译一下
}
//译者函数。输入一串字符串在这里会对照数值记录者中的codeBook翻译没有的code会被翻译成X
//每当输入总体的长度是四的倍数的时候才会调用这个翻译函数因为每个汉字单元都是4位
private void Translate(string code)
{
string result = "";
for(int i = 0; i < code.Length/4; i++)
{
string temp = code.Substring(i*4,4);
try{
result += indexRecoder.codeBook[temp];
}
catch
{
//Debug.Log("字典里没找到这个字,我得给翻译结果里加个叉叉");
result += "X";
}
}
Debug.Log("翻译的结果是:"+result);
}
//清除检查者函数。从自身Codeing函数调用每次有新字符输入的时候就调用。
//检查输入总体中是否存在连续的八个点,有则清空当前输入的所有东西
private void ClearChecker(string code)
{
int hasClearer = code.IndexOf("........");
if(hasClearer != -1)
{
codeTextView.text = "";
Debug.Log("检查到连续的八个点,清除所有输入内容");
}
}
2021-07-01 03:08:38 +08:00
}