328 lines
10 KiB
C#
328 lines
10 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using System.Linq;
|
||
using System.IO;
|
||
using DG.Tweening;
|
||
using UnityEngine.Networking;
|
||
|
||
public class ZX1Controller : MonoBehaviour
|
||
{
|
||
/// <summary>
|
||
/// 题目
|
||
/// </summary>
|
||
public Text txtQuestion;
|
||
/// <summary>
|
||
/// 选项A文本
|
||
/// </summary>
|
||
public Text txtA;
|
||
/// <summary>
|
||
/// 选项B文本
|
||
/// </summary>
|
||
public Text txtB;
|
||
/// <summary>
|
||
/// 选项C文本
|
||
/// </summary>
|
||
public Text txtC;
|
||
/// <summary>
|
||
/// 选项D文本
|
||
/// </summary>
|
||
public Text txtD;
|
||
/// <summary>
|
||
/// 分数UI
|
||
/// </summary>
|
||
public Text txtScore;
|
||
/// <summary>
|
||
/// 分数星星
|
||
/// </summary>
|
||
public RectTransform rectScore;
|
||
/// <summary>
|
||
/// 背景
|
||
/// </summary>
|
||
public RectTransform rectBg;
|
||
|
||
/// <summary>
|
||
/// 下一题按钮文字与结束游戏按钮文字
|
||
/// </summary>
|
||
public Text txtNext;
|
||
/// <summary>
|
||
/// 下一题按钮
|
||
/// </summary>
|
||
public Button btnNext;
|
||
/// <summary>
|
||
/// 选项按钮
|
||
/// </summary>
|
||
public Button[] btnOptions;
|
||
/// <summary>
|
||
/// 选项面板
|
||
/// </summary>
|
||
public GameObject pnlOptions;
|
||
/// <summary>
|
||
/// 错误提示面板
|
||
/// </summary>
|
||
public GameObject pnlError;
|
||
/// <summary>
|
||
/// 错误提示
|
||
/// </summary>
|
||
public Text txtMessage;
|
||
/// <summary>
|
||
/// 返回按钮
|
||
/// </summary>
|
||
public Button btnReturn;
|
||
/// <summary>
|
||
/// 游戏结束图片
|
||
/// </summary>
|
||
public Image imgResult;
|
||
/// <summary>
|
||
/// 游戏胜利图片
|
||
/// </summary>
|
||
public Sprite imgWin;
|
||
/// <summary>
|
||
/// 游戏失败图片
|
||
/// </summary>
|
||
public Sprite imgLose;
|
||
|
||
//选项列表
|
||
List<QuestionInfo> lstQuestions = new List<QuestionInfo>();
|
||
|
||
Dictionary<string, string> dictErrorEx = new Dictionary<string, string>();
|
||
|
||
/// <summary>
|
||
/// 正确答案
|
||
/// </summary>
|
||
string answer;
|
||
|
||
/// <summary>
|
||
/// 获得分数
|
||
/// </summary>
|
||
int score;
|
||
|
||
/// <summary>
|
||
/// 已答题数量
|
||
/// </summary>
|
||
int currentQuestionCount;
|
||
|
||
/// <summary>
|
||
/// 题目总数量
|
||
/// </summary>
|
||
int allQuestionCount;
|
||
|
||
/// <summary>
|
||
/// 是否答错误
|
||
/// </summary>
|
||
bool isWrong;
|
||
|
||
/// <summary>
|
||
/// 是否已结束答题
|
||
/// </summary>
|
||
bool isOver;
|
||
|
||
Animator anim;
|
||
|
||
string basePath;
|
||
|
||
public string DataFileName = "Questions.json";
|
||
|
||
public string DataFileName_Error = "ErrorAnswer.json";
|
||
|
||
void Start()
|
||
{
|
||
basePath = CommonData.DataServer + "/ZX1/Data/";
|
||
|
||
anim = this.GetComponent<Animator>();
|
||
|
||
StartCoroutine(JsonLoader.LoadJSON(basePath + DataFileName, GetQuestionList));
|
||
StartCoroutine(JsonLoader.LoadJSON(basePath + DataFileName_Error, GetErrorAnswerList));
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取题目列表
|
||
/// </summary>
|
||
public void GetQuestionList(string data)
|
||
{
|
||
lstQuestions = JsonTools.ListFromJson<QuestionInfo>(data);
|
||
if (lstQuestions != null)
|
||
{
|
||
allQuestionCount = lstQuestions.Count;
|
||
|
||
UpdateQuestion();
|
||
}
|
||
#region
|
||
// lstQuestions = JsonMapper.ToObject<List<QuestionInfo>>(www.text);
|
||
|
||
//lstQuestions = new List<QuestionInfo>();
|
||
//lstQuestions.Add(new QuestionInfo { Title = "( )统一了台湾。", Option_A = "顺治", Option_B = "雍正", Option_C = "康熙", Option_D = "乾隆", Solution = "C" });
|
||
//lstQuestions.Add(new QuestionInfo { Title = "康熙二十三年(1684)四月,清廷在台湾设置一府三县,隶属于( )。", Option_A = "福建省", Option_B = "浙江省", Option_C = "广东省", Option_D = "江苏省", Solution = "A" });
|
||
//lstQuestions.Add(new QuestionInfo { Title = "康熙二十二年(1683)十二月二十日,施琅向康熙提呈了具有历史意义的一篇奏疏——《( )》。", Option_A = "恭陈台湾弃留疏", Option_B = "陈台湾弃留利害疏", Option_C = "筹台湾防务大概情形折", Option_D = "台北拟建一府三县折", Solution = "A" });
|
||
//lstQuestions.Add(new QuestionInfo { Title = "为了使康熙对台湾的地理形势有一个比较清楚的认识,( )绘制了台湾地图,呈康熙御览。", Option_A = "沈葆桢", Option_B = "左宗棠", Option_C = "施琅", Option_D = "李光地", Solution = "C" });
|
||
//lstQuestions.Add(new QuestionInfo { Title = "在经过长达八个月的权衡之后,康熙帝于公元( )决定将台湾纳入大清版图。", Option_A = "1683年", Option_B = "1684年", Option_C = "1685年", Option_D = "1686年", Solution = "B" });
|
||
|
||
//string data = JsonTools.ListToJson(lstQuestions);
|
||
//StreamWriter writer = File.CreateText(DataFileName);
|
||
//writer.Write(data);
|
||
//writer.Close();
|
||
#endregion
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取题目列表
|
||
/// </summary>
|
||
public void GetErrorAnswerList(string data)
|
||
{
|
||
dictErrorEx = JsonTools.DicFromJson<string, string>(data);
|
||
|
||
#region
|
||
//dictErrorEx = new Dictionary<string, string>();
|
||
//dictErrorEx.Add("顺治", "爱新觉罗·福临(1638年—1661年),满族,六岁即位,十三岁亲政,是为清军入关后的首位皇帝,在位共十八年,是康熙皇帝的父亲。");
|
||
//dictErrorEx.Add("雍正", "清世宗爱新觉罗·胤禛(1678年—1735年),清朝第五位君主(1722年—1735年在位),。康熙帝第四子,主要功绩:平定罗卜藏丹津叛乱、推行国计民生改革。");
|
||
//dictErrorEx.Add("乾隆", "爱新觉罗·弘历 (1711年—1799年),康熙的孙子,是中国有文字记载以来享年最高,实际执政时间最长的皇帝,在位期间编撰《四库全书》。");
|
||
//dictErrorEx.Add("筹台湾防务大概情形折", "1874年,日寇派兵侵占台湾南部琅懋、牡丹等地。清廷委任沈葆桢为钦差大臣,督办台湾军务。赴台当天,沈葆桢与一批大臣联名上奏《筹台湾防务大概情形折》,揭露日本侵略野心。");
|
||
//dictErrorEx.Add("台北拟建一府三县折", "1874年,日本侵台事件导致增设府县更加急迫,于是在1875年批准了沈葆桢《台北拟建一府三县折》的奏请,在大甲溪以北地区新设台北府,下辖淡水县、宜兰县、新竹县。");
|
||
//dictErrorEx.Add("沈葆桢", "沈葆桢(1820年—1879年),原名沈振宗,字幼丹,又字翰宇,汉族,福建侯官(今福建福州)人。中国近代造船、航运、海军建设事业的奠基人之一。是清朝抵抗侵略的封疆大吏林则徐之婿。同治十三年(1874年),清廷派沈葆桢为钦差大臣,赴台办理海防,兼理各国事务大臣,筹划海防事宜,办理日本撤兵交涉。");
|
||
//dictErrorEx.Add("左宗棠", "左宗棠(1812年—1885年),汉族,字季高,湖南湘阴人。晚清政治家、军事家、民族英雄,洋务派代表人物之一,与曾国藩等人并称“晚清中兴四大名臣”。参与平定太平天国运动、兴办洋务运动、镇压捻军,又主持平定陕甘同治回民起事、收复新疆并推动新疆置省");
|
||
//dictErrorEx.Add("李光地", "李光地(1642年—1718年),历任翰林院编修、翰林学士、兵部右侍郎、直隶巡抚,协助平定“三藩之乱”、“统一台湾”,康熙四十四年(1705年),拜文渊阁大学士兼吏部尚书。");
|
||
|
||
//string data = JsonTools.DicToJson(dictErrorEx);
|
||
//StreamWriter writer = File.CreateText(DataFileName_Error);
|
||
//writer.Write(data);
|
||
//writer.Close();
|
||
#endregion
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取题目
|
||
/// </summary>
|
||
void UpdateQuestion()
|
||
{
|
||
foreach (Button btn in btnOptions)
|
||
{
|
||
btn.enabled = true;
|
||
btn.transform.GetComponentInChildren<Text>().color = Color.black;
|
||
}
|
||
|
||
if (currentQuestionCount < allQuestionCount)
|
||
{
|
||
int ordinal = Random.Range(0, lstQuestions.Count);
|
||
txtQuestion.text = lstQuestions[ordinal].Title;
|
||
txtA.text = lstQuestions[ordinal].Option_A;
|
||
txtB.text = lstQuestions[ordinal].Option_B;
|
||
txtC.text = lstQuestions[ordinal].Option_C;
|
||
txtD.text = lstQuestions[ordinal].Option_D;
|
||
answer = lstQuestions[ordinal].Solution;
|
||
lstQuestions.RemoveAt(ordinal);
|
||
|
||
isWrong = false;
|
||
currentQuestionCount++;
|
||
if (currentQuestionCount == allQuestionCount)
|
||
{
|
||
txtNext.text = "结束答题";
|
||
}
|
||
}
|
||
}
|
||
public void OnBtnOptionClick(string option)
|
||
{
|
||
if (!isOver)
|
||
{
|
||
if (option == answer)
|
||
{
|
||
AudioManager.Instance.PlayAudio("right", 0.5f);
|
||
//if (!isWrong)
|
||
{
|
||
score++;
|
||
txtScore.text = "当前分数:" + score;
|
||
rectScore.sizeDelta = new Vector2(64 * score, 64);
|
||
Tween myTween = rectScore.transform.DOShakePosition(1, 4);
|
||
myTween.SetEase(Ease.OutQuint);
|
||
}
|
||
if (txtNext.text != "结束答题")
|
||
{
|
||
OnBtnNextClick();
|
||
}
|
||
else
|
||
{
|
||
ShowResult();
|
||
}
|
||
}
|
||
else
|
||
{
|
||
isWrong = true;
|
||
Button btnClicked = btnOptions.First(btn => btn.name == "btn" + option);
|
||
Text txtOption = btnClicked.transform.GetComponentInChildren<Text>();
|
||
txtOption.color = Color.red;
|
||
btnClicked.enabled = false;
|
||
|
||
AudioManager.Instance.PlayAudio("error", 0.5f);
|
||
rectScore.sizeDelta = new Vector2(64 * score, 64);
|
||
Tween myTween = btnClicked.transform.DOShakePosition(1, 4);
|
||
myTween.SetEase(Ease.OutQuint);
|
||
if (dictErrorEx.ContainsKey(txtOption.text))
|
||
{
|
||
txtMessage.text = dictErrorEx[txtOption.text];
|
||
|
||
Invoke("ShowErrorOption", 1);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
public GameObject subject;
|
||
void ShowResult()
|
||
{
|
||
imgResult.sprite = score == allQuestionCount ? imgWin : imgLose;
|
||
|
||
anim.SetTrigger("result");
|
||
|
||
// 显示答题结果
|
||
// Invoke("OnBtnCloseClick", 3);
|
||
}
|
||
|
||
void ShowErrorOption()
|
||
{
|
||
btnNext.gameObject.SetActive(false);
|
||
pnlOptions.SetActive(false);
|
||
pnlError.SetActive(true);
|
||
// rectScore.gameObject.SetActive(false);
|
||
}
|
||
|
||
public void OnBtnNextClick()
|
||
{
|
||
if (!isOver)
|
||
{
|
||
if (txtNext.text == "结束答题")
|
||
{
|
||
isOver = true;
|
||
|
||
ShowResult();
|
||
}
|
||
else
|
||
{
|
||
UpdateQuestion();
|
||
}
|
||
}
|
||
}
|
||
|
||
public void OnBtnReturnClick()
|
||
{
|
||
pnlError.SetActive(false);
|
||
pnlOptions.SetActive(true);
|
||
btnNext.gameObject.SetActive(true);
|
||
rectScore.gameObject.SetActive(true);
|
||
}
|
||
|
||
public void OnBtnCloseClick()
|
||
{
|
||
// 播放关闭动画
|
||
// anim.SetTrigger("close");
|
||
|
||
Close();
|
||
}
|
||
|
||
public void Close()
|
||
{
|
||
BaseController.HideView();
|
||
|
||
Destroy(gameObject.transform.parent.gameObject);
|
||
}
|
||
}
|