278 lines
9.5 KiB
C#
278 lines
9.5 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using ZXKFramework;
|
||
//临时存放考核步骤数据
|
||
public struct StepData
|
||
{
|
||
public string answer;//物体名称
|
||
public string score;//分数
|
||
public string audio;//声音
|
||
public string text;//提示文字
|
||
public bool isTrue;//是否做对
|
||
}
|
||
public class KaoHeManager : MonoBehaviour
|
||
{
|
||
List<StepData> stepList = new();//每一步考核数据的源数据
|
||
List<string> yourTrueAnswer1 = new();//该步骤你已经加分的
|
||
List<string> yourTrueAnswer2 = new();//该步骤你已经答过的
|
||
List<string> yourTrueAnswer3 = new();//该状态的所有步骤的已答过的答案
|
||
public Result result;
|
||
string moduleName;
|
||
string stepName;
|
||
string state;
|
||
string type;//计分类别
|
||
int errorTime;
|
||
int stepCount;
|
||
public Action<float> callBack;//更新分数面板的委托
|
||
public bool start;
|
||
public bool question;
|
||
public void Init()
|
||
{
|
||
start = true;
|
||
moduleName = MVC.GetModel<YiLiao.Main.GameModel>().mainData.name;
|
||
Game.Instance.eventManager.AddListener<KaoHeDataEvent>(Init_StepData);
|
||
Game.Instance.eventManager.AddListener<KaoHeEvent>(KaoHe_Step);
|
||
Game.Instance.eventManager.AddListener<QuestionEvent>(KaoHe_Question);
|
||
}
|
||
public string ToJson()
|
||
{
|
||
return UnityTools.GetJson(result);
|
||
}
|
||
public void ClearResult()
|
||
{
|
||
result = null;
|
||
}
|
||
//初始化本步骤数据,每一步都执行一下
|
||
void Init_StepData(KaoHeDataEvent e)
|
||
{
|
||
stepName = e.name;
|
||
state = e.state;
|
||
type = e.type;
|
||
stepList.Clear();
|
||
if (string.IsNullOrEmpty(e.answer)) return;
|
||
for (int i = 0; i < e.answer.Split('|').Length; i++)
|
||
{
|
||
StepData stepData = new();
|
||
stepData.answer = e.answer.Split('|')[i];
|
||
stepData.score = e.score.Split('|')[i];
|
||
if(!string.IsNullOrEmpty(e.audio)) stepData.audio = e.audio.Split('|')[i];
|
||
if (!string.IsNullOrEmpty(e.text)) stepData.text = e.text.Split('|')[i];
|
||
stepList.Add(stepData);
|
||
}
|
||
errorTime = 0;
|
||
stepCount = 0;
|
||
}
|
||
//步骤考核
|
||
void KaoHe_Step(KaoHeEvent e)
|
||
{
|
||
if (!start) return;
|
||
if (MVC.GetModel<YiLiao.Main.GameModel>().modeType == ModeType.ShiXun) return;
|
||
if (stepList.Count == 0) return;
|
||
if (question) return;
|
||
StepData stepData = stepList[stepCount];
|
||
if (TrueOrFalse(e.yourAnswer))//正确
|
||
{
|
||
if (errorTime < 3)
|
||
{
|
||
stepData.isTrue = true;
|
||
Game.Instance.eventManager.Raise(new TipEvent() { text = "" });//一旦答对,就将提示关闭
|
||
errorTime = 0;
|
||
}
|
||
else
|
||
{
|
||
stepData.isTrue = false;
|
||
}
|
||
if (!yourTrueAnswer2.Contains(e.yourAnswer))
|
||
{
|
||
yourTrueAnswer2.Add(e.yourAnswer);
|
||
}
|
||
if (!yourTrueAnswer3.Contains(e.yourAnswer))
|
||
{
|
||
yourTrueAnswer3.Add(e.yourAnswer);
|
||
}
|
||
}
|
||
else//不正确
|
||
{
|
||
if (yourTrueAnswer3.Contains(e.yourAnswer)) return;
|
||
if (errorTime < 3)
|
||
{
|
||
errorTime++;
|
||
stepData.isTrue = false;
|
||
Debug.LogWarning("回答错误" + errorTime + "次");
|
||
Game.Instance.eventManager.Raise(new PlayTrueOrFalseEvent() { isTrue = false });
|
||
}
|
||
if (errorTime == 3)
|
||
{
|
||
stepData.isTrue = false;
|
||
Debug.LogWarning("回答错误次数达到3次,次数已用完");
|
||
Game.Instance.eventManager.Raise(new PlaySoundEvent() { path = stepData.audio });
|
||
Game.Instance.eventManager.Raise(new TipEvent() { text = stepData.text });
|
||
for (int i = 0; i < stepData.answer.Split(',').Length; i++)
|
||
{
|
||
Game.Instance.eventManager.Raise(new HighLightEvent() { name = stepData.answer.Split(',')[i], visiable = true });
|
||
}
|
||
}
|
||
}
|
||
Debug.Log("stepCount=" + stepCount + " errorTime=" + errorTime + " yourAnswer=" + e.yourAnswer + " answer=" + stepData.answer + " isTrue=" + stepData.isTrue + " yourTrueAnswer2=" + yourTrueAnswer2.Count);
|
||
//记录json
|
||
if (result == null) result = new();
|
||
result.moduleName = moduleName;
|
||
Step step = result.steps.Find(s => s.state == state);
|
||
if (step == null)
|
||
{
|
||
step = new Step();
|
||
step.name = stepName;
|
||
step.state = state;
|
||
step.type = type;
|
||
for (int i = 0; i < stepList.Count; i++)
|
||
{
|
||
StepData t = stepList[i];
|
||
if (t.score.Split(',').Length == 1)
|
||
{
|
||
step.maxScore += Convert.ToSingle(t.score);
|
||
}
|
||
else
|
||
{
|
||
for (int j = 0; j < t.score.Split(',').Length; j++)
|
||
{
|
||
step.maxScore += Convert.ToSingle(t.score.Split(',')[j]);
|
||
}
|
||
}
|
||
}
|
||
result.steps.Add(step);
|
||
}
|
||
//加分
|
||
if (stepData.isTrue)
|
||
{
|
||
if (stepData.score.Split(',').Length == 1)
|
||
{
|
||
if (!yourTrueAnswer1.Contains(e.yourAnswer))
|
||
{
|
||
step.score += Convert.ToSingle(stepData.score);
|
||
Game.Instance.eventManager.Raise(new PlayTrueOrFalseEvent() { isTrue = true });
|
||
yourTrueAnswer1.Add(e.yourAnswer);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (!yourTrueAnswer1.Contains(e.yourAnswer))
|
||
{
|
||
string s = stepData.score.Split(',')[Array.IndexOf(stepData.answer.Split(','), e.yourAnswer)];
|
||
step.score += Convert.ToSingle(s);
|
||
Game.Instance.eventManager.Raise(new PlayTrueOrFalseEvent() { isTrue = true });
|
||
yourTrueAnswer1.Add(e.yourAnswer);
|
||
}
|
||
}
|
||
}
|
||
//增加下标
|
||
if (yourTrueAnswer2.Count == stepList[stepCount].answer.Split(',').Length)
|
||
{
|
||
stepCount++;
|
||
yourTrueAnswer1.Clear();
|
||
yourTrueAnswer2.Clear();
|
||
Game.Instance.eventManager.Raise(new TipEvent() { text = "" });
|
||
errorTime = 0;
|
||
if (stepCount == stepList.Count)//是否结束
|
||
{
|
||
stepList.Clear();
|
||
yourTrueAnswer3.Clear();
|
||
}
|
||
}
|
||
//回调函数
|
||
callBack?.Invoke(result.SumScore());
|
||
}
|
||
//习题考核
|
||
void KaoHe_Question(QuestionEvent e)
|
||
{
|
||
if (MVC.GetModel<YiLiao.Main.GameModel>().modeType == ModeType.ShiXun) return;
|
||
//生成json
|
||
if (result == null) result = new();
|
||
result.moduleName = moduleName;
|
||
Step step = result.steps.Find(s => s.state == e.state);
|
||
if (step == null)
|
||
{
|
||
step = new Step();
|
||
step.name = stepName;
|
||
step.state = e.state;
|
||
if (string.IsNullOrEmpty(e.type))
|
||
{
|
||
step.type = type;
|
||
}
|
||
else
|
||
{
|
||
step.type = e.type;
|
||
}
|
||
step.maxScore += e.score;
|
||
Question question = new();
|
||
question.qText = e.question;
|
||
question.qOptions = e.option;
|
||
question.qAnswer = e.answer;
|
||
question.qYourAnswer = e.yourAnswer;
|
||
question.qAnalysis = e.analysis;
|
||
step.questions.Add(question);
|
||
result.steps.Add(step);
|
||
}
|
||
else
|
||
{
|
||
Question question = step.questions.Find(q => q.qText == e.question);
|
||
if (question == null)
|
||
{
|
||
question = new();
|
||
question.qText = e.question;
|
||
question.qOptions = e.option;
|
||
question.qAnswer = e.answer;
|
||
question.qYourAnswer = e.yourAnswer;
|
||
question.qAnalysis = e.analysis;
|
||
step.questions.Add(question);
|
||
}
|
||
}
|
||
//加分
|
||
if (e.isTrue)
|
||
{
|
||
step.score += e.score;
|
||
}
|
||
Game.Instance.eventManager.Raise(new PlayTrueOrFalseEvent() { isTrue = e.isTrue });
|
||
//Debug.Log("moduleName="+result.moduleName+" totalScore="+result.SumScore() + " stepName=" + stepName);
|
||
//for (int i = 0; i < result.steps.Count; i++)
|
||
//{
|
||
// Debug.Log("name="+result.steps[i].name);
|
||
// for (int j = 0; j < result.steps[i].questions.Count; j++)
|
||
// {
|
||
// Debug.Log("name=" + result.steps[i].questions[j].qText);
|
||
// }
|
||
//}
|
||
callBack?.Invoke(result.SumScore());
|
||
}
|
||
//判断对错
|
||
private bool TrueOrFalse(string value)
|
||
{
|
||
if (stepList[stepCount].answer.Split(',').Length == 1)//不包含‘,’分割
|
||
{
|
||
if (stepList[stepCount].answer == value)
|
||
{
|
||
return true;
|
||
}
|
||
}
|
||
else//包含‘,’分割
|
||
{
|
||
for (int i = 0; i < stepList[stepCount].answer.Split(',').Length; i++)
|
||
{
|
||
if (stepList[stepCount].answer.Split(',')[i] == value)
|
||
{
|
||
return true;
|
||
}
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
private void OnDestroy()
|
||
{
|
||
if (Game.Instance)
|
||
{
|
||
Game.Instance.eventManager.RemoveListener<KaoHeDataEvent>(Init_StepData);
|
||
Game.Instance.eventManager.RemoveListener<KaoHeEvent>(KaoHe_Step);
|
||
Game.Instance.eventManager.RemoveListener<QuestionEvent>(KaoHe_Question);
|
||
}
|
||
}
|
||
} |