/// <summary> /// 检测材质是否能反应 /// </summary> /// <param name="mn">材质</param> /// <returns>是否反应</returns> private bool MatterCheck(MatterName mn) { if (mn.rctCondition == thisMatter.rctCondition) { List <ChemistyMatter> input = new List <ChemistyMatter> //用于存储自己和反应物 { new ChemistyMatter { name = thisMatter.matterName, state = thisMatter.matterState }, new ChemistyMatter { name = mn.matterName, state = mn.matterState } }; List <ChemistyMatter> output; //用于接受返回的生成物 //匹配成功,更新反应列表 if (rctMng.CheckReaction(input, thisMatter.rctCondition, out output)) { ChemicalEqu equ = new ChemicalEqu { input = input, conditon = thisMatter.rctCondition, output = output }; //更新 cmtEqus.Add(equ); //ui.AddEqu(equ, this.transform); return(true); } else { return(false); } } return(false); }
/// <summary> /// 合成表达式并装配到UI /// </summary> /// <param name="equ">化学表达式</param> /// <param name="tr">反应位置的transform</param> private void MakeOutEqu(ChemicalEqu equ) { string print = null; pic.SetActive(true); tx.SetActive(true); //合成表达式 for (int i = 0; i < equ.input.Count; i++) { print += equ.input[i].name; if (i < equ.input.Count - 1) { print += "+"; } } print += "→"; for (int i = 0; i < equ.output.Count; i++) { print += equ.output[i].name; if (i < equ.output.Count - 1) { print += "+"; } } text.text = print; //this.transform.position = mainCmr.WorldToScreenPoint(activeTr.position) + new Vector3(offset.x, offset.y); }
/// <summary> /// 检测方程式成立接口 /// </summary> /// <param name="input">反应物List</param> /// <param name="condition">反应条件</param> /// <param name="output">接收生成物List</param> /// <param name="index">序号</param> /// <returns>若存在这样的反应,返回true</returns> public bool CheckReaction(List <ChemistyMatter> input, ReactionCondition condition, out List <ChemistyMatter> output, out int index) { output = new List <ChemistyMatter>(); ChemicalEqu equ = new ChemicalEqu(); int i; for (i = 0; i < cmtEqu.Count; i++) { if (InputEqual(cmtEqu[i].input, input) && cmtEqu[i].conditon == condition) { equ = cmtEqu[i]; break; } } index = 0; if (i == cmtEqu.Count) { //Debug.Log("CNM"); return(false); } output = new List <ChemistyMatter>(equ.output); index = i; //Debug.Log("CCCCC"); return(true); }
/// <summary> /// 检测材质是否能反应 /// </summary> /// <param name="mn">材质</param> /// <returns>是否反应</returns> private bool MatterCheck(MatterName mn) { activeEquIndex = 0; //Debug.Log("MKMP"); if (mn.rctCondition == thisMatter.rctCondition) { List <ChemistyMatter> input = new List <ChemistyMatter> //用于存储自己和反应物 { new ChemistyMatter { name = thisMatter.matterName, state = thisMatter.matterState }, new ChemistyMatter { name = mn.matterName, state = mn.matterState } }; List <ChemistyMatter> output; //用于接受返回的生成物 //匹配成功,更新反应列表 int index; //Debug.Log(input[0].name + input[1].name + thisMatter.rctCondition.ToString()); if (rctMng.CheckReaction(input, thisMatter.rctCondition, out output, out index)) { ChemicalEqu equ = new ChemicalEqu { input = input, conditon = thisMatter.rctCondition, output = output }; //更新 cmtEqus.Add(equ); ui.AddEqu(index, this.transform); return(true); } else { //Debug.Log("F"); return(false); } } return(false); }
//private ChemicalEqu? activeEqu; //当前使用的方程式 //public KeyCode SwitchKey; //切换显示的按钮 //private List<Pair<ChemicalEqu, Transform>> equList; //保存待输出的方程式 //Debug用 //[SerializeField] //private int ListCount; ///// <summary> ///// List比对 ///// </summary> //private bool CheckListEqu(List<ChemistyMatter> a, List<ChemistyMatter> b) //{ // if (a.Count != b.Count) // { // return false; // } // //Debug.Log("Begin"); // int count = 0; // int count2 = 0; // foreach (ChemistyMatter m in b) // { // for (int i = 0; i < a.Count; i++) // { // if (a[i].name == m.name && a[i].state == m.state) // { // count = count + 1; // break; // } // //Debug.Log(a[i].name); // //Debug.Log(a[i].name.Length); // } // } // foreach (ChemistyMatter m in a) // { // for (int i = 0; i < a.Count; i++) // { // if (b[i].name == m.name && b[i].state == m.state) // { // count2 = count2 + 1; // break; // } // //Debug.Log(a[i].name); // //Debug.Log(a[i].name.Length); // } // } // if (count != b.Count || count2 != b.Count) // { // //Debug.Log("CNNN"); // //Debug.Log(count); // return false; // } // return true; //} /// <summary> /// 化学方程式比对 /// </summary> //private bool CheckEqu(ChemicalEqu a, ChemicalEqu b) //{ // bool[] check = new bool[3]; // check[0] = CheckListEqu(a.input, b.input); // check[1] = CheckListEqu(a.output, b.output); // check[2] = a.conditon == b.conditon; // if (check[0] && check[1] && check[2]) // return true; // return false; //} /// <summary> /// 显示方程式的接口 /// </summary> /// <param name="equ">要显示的方程式</param> /// <param name="tr">要附加在GameObject的Transform</param> public void AddEqu(ChemicalEqu equ, Transform tr) { activeTr = tr; //activeEqu = equ; MakeOutEqu(equ); OpenPrintOutEqu(); //if (!equList.Exists(x=>CheckEqu(x.first,equ))) //{ // equList.Add(new Pair<ChemicalEqu, Transform>{ first = equ, Second = tr}); // if (equList.Count == 1) // { // activeEqu = equ; // activeTr = tr; // MakeOutEqu(equ); // OpenPrintOutEqu(); // } //} //else //{ // return false; //} //return true; }
//private void OnTriggerExit2D(Collider2D collision) //{ // if (collision.gameObject.tag == "Matter") // { // //获取OBJ所在的序号 // int objIndex = objList.LastIndexOf(collision.gameObject); // MatterName mn = collision.gameObject.GetComponent<MatterName>(); // //删除列表中的该OBJ要素 // objMatterList_old.RemoveAt(objIndex); // objMatterList.RemoveAt(objIndex); // objList.RemoveAt(objIndex); // //删除所有该OBJ参与的反应 // cmtEqus.RemoveAll(x => x.input.Count >= 2 && x.input[1].name == mn.matterName); // } //} ///// <summary> ///// 材质比较函数 ///// </summary> //private bool MatterEqu(MatterName a, MatterName b) //{ // if (a.matterName == b.matterName && a.matterState == b.matterState && a.rctCondition == b.rctCondition) // return true; // return false; //} private void Update() { //遍历材质,找到变化的材质 for (int i = 0; i < objList.Count; i++) { if (objMatterList[i].rctCondition != objMatterList_old[i]) { //若之前有,删除,重新检查 cmtEqus.RemoveAll(x => x.input.Exists(m => m.name == objMatterList[i].matterName)); MatterCheck(objMatterList[i]); //更新对应oldlist objMatterList_old[i] = objMatterList[i].rctCondition; } } //检查状态是否变化 if (thisCondition_old != thisMatter.rctCondition) { nowWaitFrame = waitFrame; //刷新计时 thisCondition_old = thisMatter.rctCondition; //更新旧的 cmtEqus.RemoveAll(x => x.input.Count == 1); //删除自分解反应 abnormalChecked = false; } //自身状态变化重新检测 if (abnormalChecked == false) { //清空 cmtEqus.Clear(); //重新检查 for (int i = 0; i < objList.Count; i++) { MatterCheck(objMatterList[i]); } } //检查针对自身的分解反应 if (thisMatter.rctCondition != ReactionCondition.normal && abnormalChecked == false) { //activeEquIndex = 0; abnormalChecked = true; List <ChemistyMatter> input = new List <ChemistyMatter> //用于存储自己 { new ChemistyMatter { name = thisMatter.matterName, state = thisMatter.matterState }, }; List <ChemistyMatter> output; //用于接受返回的生成物 //匹配成功,更新反应列表 if (rctMng.CheckReaction(input, thisMatter.rctCondition, out output)) { ChemicalEqu equ = new ChemicalEqu { input = input, conditon = thisMatter.rctCondition, output = output }; //更新 cmtEqus.Add(equ); //ui.AddEqu(equ, this.transform); } } //不为normal状态且有反应可发生,证明应有计时 if (thisMatter.rctCondition != ReactionCondition.normal && cmtEqus.Count > 0) { //时间到,反应 if (nowWaitFrame <= 0) { MakeReaction(); } //计时 else { nowWaitFrame--; } } }
//private void OnTriggerEnter2D(Collider2D collision) //{ // //加入碰撞列表,将材质加入列表 // if (collision.gameObject.tag == "Matter") // { // objList.Add(collision.gameObject); // MatterName mn = collision.gameObject.GetComponent<MatterName>(); // objMatterList.Add(mn); // objMatterList_old.Add(mn.rctCondition); // MatterCheck(mn); // } //} //private void OnTriggerExit2D(Collider2D collision) //{ // if (collision.gameObject.tag == "Matter") // { // //获取OBJ所在的序号 // int objIndex = objList.LastIndexOf(collision.gameObject); // Debug.Log(objIndex); // MatterName mn = collision.gameObject.GetComponent<MatterName>(); // //删除列表中的该OBJ要素 // objMatterList_old.RemoveAt(objIndex); // objMatterList.RemoveAt(objIndex); // objList.RemoveAt(objIndex); // //删除所有该OBJ参与的反应 // cmtEqus.RemoveAll(x => x.input.Count >= 2 && x.input[1].name == mn.matterName); // activeEquIndex = 0; // } //} ///// <summary> ///// 材质比较函数 ///// </summary> //private bool MatterEqu(MatterName a, MatterName b) //{ // if (a.matterName == b.matterName && a.matterState == b.matterState && a.rctCondition == b.rctCondition) // return true; // return false; //} private void Update() { //遍历材质,找到变化的材质 for (int i = 0; i < objList.Count; i++) { if (objMatterList[i].rctCondition != objMatterList_old[i]) { //若之前有,删除,重新检查 cmtEqus.RemoveAll(x => x.input.Exists(m => m.name == objMatterList[i].matterName)); MatterCheck(objMatterList[i]); //更新对应oldlist objMatterList_old[i] = objMatterList[i].rctCondition; } } //检查状态是否变化 if (thisCondition_old != thisMatter.rctCondition) { thisCondition_old = thisMatter.rctCondition; cmtEqus.RemoveAll(x => x.input.Count == 1); //nowWaitFrame = waitFrame; abnormalChecked = false; } //自身状态变化重新检测 if (abnormalChecked == false) { //清空 cmtEqus.Clear(); //重新检查 for (int i = 0; i < objList.Count; i++) { MatterCheck(objMatterList[i]); } } //检查针对自身的分解反应 if (thisMatter.rctCondition != ReactionCondition.normal && abnormalChecked == false) { activeEquIndex = 0; //Debug.Log("wtf"); abnormalChecked = true; List <ChemistyMatter> input = new List <ChemistyMatter> //用于存储自己 { new ChemistyMatter { name = thisMatter.matterName, state = thisMatter.matterState }, }; List <ChemistyMatter> output; //用于接受返回的生成物 //匹配成功,更新反应列表 int index; if (rctMng.CheckReaction(input, thisMatter.rctCondition, out output, out index)) { //Debug.Log("OOOOKKKK"); ChemicalEqu equ = new ChemicalEqu { input = input, conditon = thisMatter.rctCondition, output = output }; //更新 cmtEqus.Add(equ); //if (thisMatter.rctCondition == ReactionCondition.condense || // (thisMatter.rctCondition == ReactionCondition.heat && input.Count == 1 && output.Count == 1)) //{ // activeEquIndex = cmtEqus.FindIndex(x => ReactionManager.InputEqual(x.input, equ.input) && x.conditon == equ.conditon); // MakeReaction(0); // return; //} if (thisMatter.rctCondition == ReactionCondition.condense || thisMatter.rctCondition == ReactionCondition.evaporation) { activeEquIndex = cmtEqus.Count - 1; MakeReaction(0); } else { ui.AddEqu(index, this.transform); } } } //切换使用的反应方程式 abnormalChecked = true; if (cmtEqus.Count == 0) { activeEquIndex = null; ui.ClosePrintOutEqu(); } ////切换方程式 //if (Input.GetKeyDown(changeEquKey) && activeEquIndex != null) //{ // activeEquIndex = (activeEquIndex + 1) > (cmtEqus.Count - 1) ? 0 : (activeEquIndex + 1); // ui.AddEqu(cmtEqus[activeEquIndex.Value], this.transform); //} if (activeEquIndex != null) { if (Choose1) { Choose1 = false; ui.ClosePrintOutEqu(); MakeReaction(0); } else if (Choose2) { Choose2 = false; ui.ClosePrintOutEqu(); MakeReaction(1); } else if (Choose3) { Choose3 = false; ui.ClosePrintOutEqu(); MakeReaction(2); } } }
private void Start() { realText = EquationText.text; //提取文件数据 cmtEqu = new List <ChemicalEqu>(); //储存方程式 char[] t = new char[50]; //提取句子容器 //遍历数据 for (int i = 0; i < realText.Length; i++) { int j = 0; //句子容器累加 int k = 0; //提取一行 while (i < realText.Length && realText[i] != '\n') { t[j++] = realText[i++]; } char[] m = new char[50]; //提取物质容器 ChemistyMatter cm = new ChemistyMatter(); //储存物质中介 ChemicalEqu equ = new ChemicalEqu { input = new List <ChemistyMatter>(), output = new List <ChemistyMatter>() }; //储存方程中介 bool isInput = true; //判断反应物生成物 int index = 0; //用于物质容器 for (; k < j; k++) { char[] mm; switch (t[k]) { case '+': m[index - 1] = '\0'; mm = new char[index - 1]; CharCopy(mm, m, index - 1); cm.name = new string(mm); m.Initialize(); index = 0; switch (t[k - 1]) { case 'g': cm.state = MatterState.gas; break; case 'l': cm.state = MatterState.liquid; break; case 's': cm.state = MatterState.solid; break; } if (isInput) { equ.input.Add(cm); } else { equ.output.Add(cm); } break; case '[': m[index - 1] = '\0'; mm = new char[index - 1]; CharCopy(mm, m, index - 1); cm.name = new string(mm); m.Initialize(); index = 0; switch (t[k - 1]) { case 'g': cm.state = MatterState.gas; break; case 'l': cm.state = MatterState.liquid; break; case 's': cm.state = MatterState.solid; break; } equ.input.Add(cm); isInput = false; switch (t[k + 1]) { case 'N': equ.conditon = ReactionCondition.normal; break; case 'L': equ.conditon = ReactionCondition.light; break; case 'H': equ.conditon = ReactionCondition.heat; break; case 'E': equ.conditon = ReactionCondition.electrify; break; case 'C': equ.conditon = ReactionCondition.condense; break; case 'V': equ.conditon = ReactionCondition.evaporation; break; } k += 2; break; default: m[index++] = t[k]; break; } } switch (m[index - 2]) { case 'g': cm.state = MatterState.gas; break; case 'l': cm.state = MatterState.liquid; break; case 's': cm.state = MatterState.solid; break; } m[index - 2] = '\0'; char[] cmm = new char[index - 2]; CharCopy(cmm, m, index - 2); cm.name = new string(cmm); equ.output.Add(cm); cmtEqu.Add(equ); } ////输出实验 //foreach (ChemicalEqu equ in cmtEqu) //{ // foreach (ChemistyMatter mt in equ.input) // { // Debug.Log(mt.name); // Debug.Log(mt.name.Length); // switch (mt.state) // { // case MatterState.gas: // Debug.Log("气体"); // break; // case MatterState.liquid: // Debug.Log("液体"); // break; // case MatterState.solid: // Debug.Log("固体"); // break; // } // } // switch (equ.conditon) // { // case ReactionCondition.heat: // Debug.Log("加热"); // break; // case ReactionCondition.light: // Debug.Log("点燃"); // break; // case ReactionCondition.normal: // Debug.Log("常态"); // break; // } // foreach (ChemistyMatter mt in equ.output) // { // Debug.Log(mt.name); // Debug.Log(mt.name.Length); // switch (mt.state) // { // case MatterState.gas: // Debug.Log("气体"); // break; // case MatterState.liquid: // Debug.Log("液体"); // break; // case MatterState.solid: // Debug.Log("固体"); // break; // } // } //} //Debug.Log(cmtEqu.Count); //Debug.Log(cmtEqu[1].input[0].name + cmtEqu[1].input[1].name); }