//Estructuras de control public virtual IEnumerator repetir() { Transform theRobot = (Transform)Init.robotInstance; RobotBehaviour behaviour = (RobotBehaviour)theRobot.GetComponent <RobotBehaviour>(); int i = 0; Type type = behaviour.GetType(); int n = int.Parse((string)arguments[0]); string sentenceName = (string)arguments[1]; if (n > i) { for (i = 0; i < n; i++) { MethodInfo methodInfo = type.GetMethod(sentenceName); behaviour.StartCoroutine(methodInfo.Name, 0); } } else { UI.runtimeErrorMsg = I18N.getValue("the_number_of_repetitions_was_not_specified"); } yield return(new WaitForSeconds(0)); //Fin de ejecucion UI.executingCurrentLine = false; }
/** Retorna el estado del robot */ public string getRobotStatus() { Transform theRobot = (Transform)Init.robotInstance; int posAv = Mathf.RoundToInt(theRobot.position.x); int posCa = Mathf.RoundToInt(theRobot.position.z); return(I18N.getValue("posavenue") + posAv + ", " + I18N.getValue("posstreet") + posCa + ", " + I18N.getValue("heading") + getHeading(theRobot) + "\n " + I18N.getValue("flowers_corner") + Init.city[posAv - 1, posCa - 1].flowers + ", " + I18N.getValue("papers_corner") + Init.city[posAv - 1, posCa - 1].papers + " - " + I18N.getValue("flowers_bag") + flores + ", " + I18N.getValue("papers_bag") + papeles + " "); }
/** Efectiviza la animacion de la instruccion */ void executeLine(int lineNo) { // FIXME: Aqui deberia delegarse al robot a fin de que realice la animacion string status = I18N.getValue("exec_line") + (currentLine + 1) + ": " + sentences[lineNo]; statusText = status; // Invocar ejecucion visual via reflection try { object result = null; // Recuperar el BigBang, y a partir de alli el Robot que se tenga configurado Transform theRobot = (Transform)Init.robotInstance; RobotBehaviour behaviour = (RobotBehaviour)theRobot.GetComponent <RobotBehaviour>(); Type type = behaviour.GetType(); // Pruebas para argumentos. Esto igualmente se recibe desde libreria string sentence = (string)sentences[lineNo]; string sentenceName = sentence.Substring(0, sentence.Contains("(") ? sentence.IndexOf("(") : sentence.Length); // Cargar los parametros segun la instruccion que sea. FIXME: Deshardcode if (sentence.Contains("(")) { behaviour.resetArguments(); string sentenceArgs = sentence.Substring(sentence.IndexOf("("), sentence.Length - sentence.IndexOf("(")); sentenceArgs = sentenceArgs.Replace("(", "").Replace(")", "").Replace(" ", ""); string[] args = sentenceArgs.Split(","[0]); for (int i = 0; i < args.Length; i++) { behaviour.addArgument(args[i]); } } MethodInfo methodInfo = type.GetMethod(sentenceName); // ParameterInfo[] parameters = methodInfo.GetParameters(); // Invocar a la corutina encargada de ejecutar la visualizacion behaviour.StartCoroutine(methodInfo.Name, 0); } catch (Exception e) { Debug.Log("Exception!! " + e.ToString()); statusText = I18N.getValue("unknown_line") + (currentLine + 1) + ": " + sentences[currentLine]; run = false; } }
/** Retorna el heading del robot */ public string getHeading(Transform theRobot) { int heading = Mathf.RoundToInt(theRobot.transform.rotation.eulerAngles.y); switch (heading) { case 0: return(I18N.getValue("north")); case 90: return(I18N.getValue("east")); case 180: return(I18N.getValue("south")); case 270: return(I18N.getValue("west")); } return("-"); }
/** Ejecucion de una instruccion */ IEnumerator executeStep() { step = true; currentLine++; if (currentLine == sentences.Count - 1) { statusText = I18N.getValue("finished"); currentLine = -1; run = false; ended = true; // Invocar a la corutina encargada de ejecutar la visualizacion Transform theRobot = (Transform)Init.robotInstance; RobotBehaviour behaviour = (RobotBehaviour)theRobot.GetComponent <RobotBehaviour>(); behaviour.StartCoroutine("finalizar", 0); } else if (sentences[currentLine] != null && sentences[currentLine].ToString().Length > 0) { executingCurrentLine = true; executeLine(currentLine); } // Mientras que este ejecutando, esperar while (executingCurrentLine) { yield return(new WaitForSeconds(1 - currentRunningSpeed)); } step = false; // Hubo un error? if (runtimeErrorMsg != null) { angry = true; statusText = "Error ejecutando linea " + (currentLine + 1) + ": " + sentences[currentLine] + ". " + runtimeErrorMsg; run = false; ended = true; currentLine = -1; } }
/** Renders the Config Menu */ void renderConfig() { float rowSpace = 1.5f; float row = 0f; // Botonera principal int i = 0; if (GUI.Button(new Rect(margin + i++ *buttonWidth, margin + buttonHeight * row, buttonWidth, margin + buttonHeight), I18N.getValue("accept"), styleButton)) { currentState = STATE_EDITING; } GUI.Box(new Rect(margin + i * buttonWidth, margin + buttonHeight * row, Screen.width - (2 * margin + i++ *buttonWidth), margin + buttonHeight), I18N.getValue("set_title"), styleButton); // Nueva fila row += rowSpace; // ========================================= FLOWERS! ======================================= i = 0; GUI.Box(new Rect(margin + i * buttonWidth, margin + buttonHeight * row, Screen.width - (2 * margin + i++ *buttonWidth), margin + buttonHeight * (rowSpace * 2)), I18N.getValue("set_flowers") + I18N.getValue("wildcard")); // Nueva fila row += rowSpace; // Configuracion de flores en esquina i = 1; GUI.Label(new Rect(margin + i++ *buttonWidth / 2, margin + buttonHeight * row, buttonWidth, margin + buttonHeight), I18N.getValue("avenue")); config_flower_av = GUI.TextField(new Rect(margin + i++ *buttonWidth / 2, margin + buttonHeight * row, buttonWidth / 2, margin + buttonHeight), config_flower_av); i++; GUI.Label(new Rect(margin + i++ *buttonWidth / 2, margin + buttonHeight * row, buttonWidth, margin + buttonHeight), I18N.getValue("street")); config_flower_st = GUI.TextField(new Rect(margin + i++ *buttonWidth / 2, margin + buttonHeight * row, buttonWidth / 2, margin + buttonHeight), config_flower_st); i++; GUI.Label(new Rect(margin + i++ *buttonWidth / 2, margin + buttonHeight * row, buttonWidth, margin + buttonHeight), I18N.getValue("count")); config_flower_no = GUI.TextField(new Rect(margin + i++ *buttonWidth / 2, margin + buttonHeight * row, buttonWidth / 2, margin + buttonHeight), config_flower_no); i++; if (GUI.Button(new Rect(margin + i++ *buttonWidth / 2, margin + buttonHeight * row, buttonWidth, margin + buttonHeight), I18N.getValue("set"))) { Corner.setCorner(config_flower_av, config_flower_st, config_flower_no, false); } // Nueva fila row += rowSpace; row += rowSpace; // ========================================= PAPERS! ======================================= i = 0; GUI.Box(new Rect(margin + i * buttonWidth, margin + buttonHeight * row, Screen.width - (2 * margin + i++ *buttonWidth), margin + buttonHeight * (rowSpace * 2)), I18N.getValue("set_papers") + I18N.getValue("wildcard")); // Nueva fila row += rowSpace; // Configuracion de papeles en esquina i = 1; GUI.Label(new Rect(margin + i++ *buttonWidth / 2, margin + buttonHeight * row, buttonWidth, margin + buttonHeight), I18N.getValue("avenue")); config_paper_av = GUI.TextField(new Rect(margin + i++ *buttonWidth / 2, margin + buttonHeight * row, buttonWidth / 2, margin + buttonHeight), config_paper_av); i++; GUI.Label(new Rect(margin + i++ *buttonWidth / 2, margin + buttonHeight * row, buttonWidth, margin + buttonHeight), I18N.getValue("street")); config_paper_st = GUI.TextField(new Rect(margin + i++ *buttonWidth / 2, margin + buttonHeight * row, buttonWidth / 2, margin + buttonHeight), config_paper_st); i++; GUI.Label(new Rect(margin + i++ *buttonWidth / 2, margin + buttonHeight * row, buttonWidth, margin + buttonHeight), I18N.getValue("count")); config_paper_no = GUI.TextField(new Rect(margin + i++ *buttonWidth / 2, margin + buttonHeight * row, buttonWidth / 2, margin + buttonHeight), config_paper_no); i++; if (GUI.Button(new Rect(margin + i++ *buttonWidth / 2, margin + buttonHeight * row, buttonWidth, margin + buttonHeight), I18N.getValue("set"))) { Corner.setCorner(config_paper_av, config_paper_st, config_paper_no, true); } // Nueva fila row += rowSpace; row += rowSpace; // ========================================= BAG! ======================================= i = 0; GUI.Box(new Rect(margin + i * buttonWidth, margin + buttonHeight * row, Screen.width - (2 * margin + i++ *buttonWidth), margin + buttonHeight * (rowSpace * 2)), I18N.getValue("inthebag")); // Nueva fila row += rowSpace; // Configuracion de papeles en esquina i = 1; GUI.Label(new Rect(margin + i++ *buttonWidth / 2, margin + buttonHeight * row, buttonWidth, margin + buttonHeight), I18N.getValue("flowers_bag")); config_bag_flowers = GUI.TextField(new Rect(margin + i++ *buttonWidth / 2, margin + buttonHeight * row, buttonWidth / 2, margin + buttonHeight), config_bag_flowers); i++; GUI.Label(new Rect(margin + i++ *buttonWidth / 2, margin + buttonHeight * row, buttonWidth, margin + buttonHeight), I18N.getValue("papers_bag")); config_bag_papers = GUI.TextField(new Rect(margin + i++ *buttonWidth / 2, margin + buttonHeight * row, buttonWidth / 2, margin + buttonHeight), config_bag_papers); i++; i++; i++; i++; if (GUI.Button(new Rect(margin + i++ *buttonWidth / 2, margin + buttonHeight * row, buttonWidth, margin + buttonHeight), I18N.getValue("set"))) { Init.getRobotBehaviour().flores = int.Parse(config_bag_flowers); Init.getRobotBehaviour().papeles = int.Parse(config_bag_papers); } // Nueva fila row += rowSpace; row += rowSpace; // ========================================= LANGUAGE! ======================================= i = 0; GUI.Box(new Rect(margin + i * buttonWidth, margin + buttonHeight * row, Screen.width - (2 * margin + i++ *buttonWidth), margin + buttonHeight * (rowSpace * 2)), I18N.getValue("language")); // Nueva fila row += rowSpace; // Configuracion de idioma i = 1; GUI.Label(new Rect(margin + i++ *buttonWidth / 2, margin + buttonHeight * row, buttonWidth * 2, margin + buttonHeight), I18N.getValue("lang_selected")); langSelected = GUI.TextField(new Rect(margin + i++ *buttonWidth, margin + buttonHeight * row, buttonWidth, margin + buttonHeight), langSelected); i++; if (GUI.Button(new Rect(margin + i++ *buttonWidth, margin + buttonHeight * row, buttonWidth, margin + buttonHeight), I18N.getValue("lang_en"))) { langSelected = I18N.getValue("lang_en"); I18N.setLang("en_US"); } if (GUI.Button(new Rect(margin + i++ *buttonWidth, margin + buttonHeight * row, buttonWidth, margin + buttonHeight), I18N.getValue("lang_es"))) { langSelected = I18N.getValue("lang_es"); I18N.setLang("es_AR"); } }
/** Renders the Running Menu */ void renderRunning() { // Botonera principal int i = 0; if (!ended && GUI.Button(new Rect(margin + i * buttonWidth, margin, buttonWidth, margin + buttonHeight), run ? I18N.getValue("pause") : I18N.getValue("resume"), styleButton) && informarMessage == null) { run = !run; } i++; if (GUI.Button(new Rect(margin + i++ *buttonWidth, margin, buttonWidth, margin + buttonHeight), I18N.getValue("stop"), styleButton) && informarMessage == null) { currentState = STATE_EDITING; } // Camara if (GUI.Button(new Rect(margin + i++ *buttonWidth, margin, buttonWidth, margin + buttonHeight), ((Camera)cameras[currentCamera]).name, styleButton)) { currentCamera++; if (currentCamera == CAMERA_VR) { currentCamera++; } if (currentCamera >= cameras.Count) { currentCamera = 0; } setCurrentCamera(currentCamera); } // Linea de estado del robot GUI.TextArea(new Rect(margin + i++ *buttonWidth, margin, Screen.width - (2 * margin + (i - 1) * buttonWidth), margin + buttonHeight * 2), Init.getRobotBehaviour().getRobotStatus(), styleTextArea); // Velocidad GUI.Label(new Rect(margin, Screen.height / 2 + buttonHeight * 4, buttonWidth * 2, buttonHeight + margin), I18N.getValue("speed")); currentRunningSpeed = GUI.VerticalScrollbar(new Rect(margin, Screen.height / 2 - buttonHeight * 4, margin, buttonHeight * 8), currentRunningSpeed, .1f, 1f, 0f); // Zoom GUI.Label(new Rect(Screen.width - buttonWidth / 2 - margin, Screen.height / 2 + buttonHeight * 4, buttonWidth, buttonHeight + margin), I18N.getValue("zoom")); zoom = GUI.VerticalScrollbar(new Rect(Screen.width - margin * 2, Screen.height / 2 - buttonHeight * 4, margin, buttonHeight * 8), zoom, 1f, .5f, 10f); // Paneo GUI.Label(new Rect(Screen.width / 2 - buttonWidth - margin * 6, Screen.height - margin * 4 - buttonHeight * 2, buttonWidth, buttonHeight + margin), I18N.getValue("pan")); pan = GUI.HorizontalScrollbar(new Rect(Screen.width / 2 - buttonWidth, Screen.height - margin * 3 - buttonHeight * 2, buttonWidth * 2, margin), pan, 3f, -15f, 15f); // Linea de ejecucion if (styleOK == null) { // Textura para error Texture2D aTexture = new Texture2D(1, 1); aTexture.SetPixel(0, 0, Color.white); aTexture.wrapMode = TextureWrapMode.Repeat; aTexture.Apply(); // Mensajes OK y ERROR styleOK = new GUIStyle(GUI.skin.textArea); styleKO = new GUIStyle(GUI.skin.textArea); // Colores de Estilos OK y ERROR styleOK.normal.textColor = Color.white; styleOK.fontSize = styleTextArea.fontSize; styleKO.normal.textColor = Color.red; styleKO.fontSize = styleTextArea.fontSize; styleKO.normal.background = aTexture; } GUI.TextArea(new Rect(margin, Screen.height - 2 * margin - buttonHeight, Screen.width - 2 * margin, margin + buttonHeight), statusText, getStatusStyle()); if (Input.GetKey(KeyCode.Escape)) { Application.Quit(); } // Informar dialog if (informarMessage != null) { GUI.TextArea(new Rect(margin * 4, Screen.height / 2 - buttonHeight * 2, Screen.width - 8 * margin, buttonHeight * 4), informarMessage, styleTextArea); if (GUI.Button(new Rect(margin * 4, Screen.height / 2 + buttonHeight * 2, Screen.width - 8 * margin, buttonHeight * 2), "OK", styleTextArea)) { informarMessage = null; } } }
/** Renders the Editing Menu */ void renderEditing() { if (vrmod == false) { XRSettings.enabled = false; vrmod = true; setCurrentCamera(2); GvrPointerInputModule.Pointer.overridePointerCamera = ((Camera)cameras[2]); } // Botonera principal int i = 0; if (GUI.Button(new Rect(margin + i++ *buttonWidth, margin, buttonWidth, margin + buttonHeight), I18N.getValue("reset"), styleButton)) { SceneManager.LoadScene(SceneManager.GetActiveScene().name, LoadSceneMode.Single); } if (GUI.Button(new Rect(margin + i++ *buttonWidth, margin, buttonWidth, margin + buttonHeight), I18N.getValue("run"), styleButton)) { parseCode(); currentState = STATE_RUNNING; run = true; step = false; ended = false; } if (GUI.Button(new Rect(margin + i++ *buttonWidth, margin, 2 * buttonWidth, margin + buttonHeight), I18N.getValue("VR"), styleButton)) { vrmod = false; GameObject.FindGameObjectWithTag("Menu").transform.GetChild(0).gameObject.SetActive(true); GameObject.FindGameObjectWithTag("Menu").transform.GetChild(1).gameObject.SetActive(true); GameObject.FindGameObjectWithTag("Menu").transform.GetChild(2).gameObject.SetActive(true); currentState = STATE_VR; } // if (GUI.Button (new Rect (margin + i++ * buttonWidth, margin, buttonWidth, margin + buttonHeight), I18N.getValue("open"), styleButton)) { // var path = EditorUtility.OpenFilePanel(I18N.getValue("open_file"), "", "txt"); // if (path.Length != 0) { // Debug.Log ("Reading data from: " + path); // readCode(path); // } // } // if (GUI.Button (new Rect (margin + i++ * buttonWidth, margin, buttonWidth, margin + buttonHeight), I18N.getValue("save"), styleButton)) { // var path = EditorUtility.SaveFilePanel (I18N.getValue("save_file"), "", I18N.getValue("filename"), "txt"); // if (path.Length != 0) { // Debug.Log ("Writing data to: " + path); // writeCode (path); // } // } i++; if (GUI.Button(new Rect(margin + i++ *buttonWidth, margin, buttonWidth, margin + buttonHeight), I18N.getValue("settings"), styleButton)) { currentState = STATE_CONFIG; return; } if (GUI.Button(new Rect(margin + i++ *buttonWidth, margin, buttonWidth, margin + buttonHeight), I18N.getValue("quit"), styleButton)) { Application.Quit(); } GUI.Box(new Rect(margin + i * buttonWidth, margin, Screen.width - (2 * margin + i++ *buttonWidth), margin + buttonHeight), I18N.getValue("edit_title"), styleButton); // Visualizacion de codigo fuente sourceCode = GUI.TextArea(new Rect(margin, buttonHeight + 3 * margin, Screen.width - 2 * margin, Screen.height - 4 * margin - buttonHeight), sourceCode, styleTextArea); }
/** * El robot deposita un papel */ public virtual IEnumerator depositarPapel() { // Depositar papel en la esquina. Vector3 pos = getRobotPosition(); if (papeles == 0) { UI.runtimeErrorMsg = I18N.getValue("no_papers_bag"); } else { papeles--; Init.city[(int)pos.x - 1, (int)pos.z - 1].incPapers(); } // ======== Movimiento brazos robot. TODO: modularizar (ver problema de uso con Coroutines) ======== // Recuperar el robot Transform theRobot = (Transform)Init.robotInstance; Transform cuerpo = theRobot.Find("CuerpoRobot"); Transform brazoIzq = cuerpo.Find("BrazoIzq"); Transform brazoDer = cuerpo.Find("BrazoDer"); Vector3 defPosIzq = brazoIzq.position; Vector3 defPosDer = brazoDer.position; // Brazos abajo! Vector3 startPosIzq = brazoIzq.position; Vector3 startPosDer = brazoDer.position; Vector3 endPosIzq = new Vector3(brazoIzq.position.x, brazoIzq.position.y - .15f, brazoIzq.position.z); Vector3 endPosDer = new Vector3(brazoDer.position.x, brazoDer.position.y - .15f, brazoDer.position.z); float journeyLengthIzq = Vector3.Distance(startPosIzq, endPosIzq); float startTimeIzq = Time.time; float speedIzq = UI.currentRunningSpeed * 5; while (Vector3.Distance(endPosIzq, brazoIzq.position) > 0.001f) { float distCoveredIzq = (Time.time - startTimeIzq) * speedIzq; float fracJourneyIzq = distCoveredIzq / journeyLengthIzq; brazoIzq.position = Vector3.Lerp(startPosIzq, endPosIzq, fracJourneyIzq); brazoDer.position = Vector3.Lerp(startPosDer, endPosDer, fracJourneyIzq); yield return(new WaitForSeconds(0)); } // Dust! Transform throttle = theRobot.Find("Dust"); ParticleSystem ps = (ParticleSystem)throttle.GetComponent <ParticleSystem>(); ps.Emit(10); yield return(new WaitForSeconds(0)); // Brazos arriba! endPosIzq = startPosIzq; endPosDer = startPosDer; startPosIzq = brazoIzq.position; startPosDer = brazoDer.position; journeyLengthIzq = Vector3.Distance(startPosIzq, endPosIzq); startTimeIzq = Time.time; speedIzq = UI.currentRunningSpeed * 5; while (Vector3.Distance(endPosIzq, brazoIzq.position) > 0.001f) { float distCoveredIzq = (Time.time - startTimeIzq) * speedIzq; float fracJourneyIzq = distCoveredIzq / journeyLengthIzq; brazoIzq.position = Vector3.Lerp(startPosIzq, endPosIzq, fracJourneyIzq); brazoDer.position = Vector3.Lerp(startPosDer, endPosDer, fracJourneyIzq); yield return(new WaitForSeconds(0)); } // Dejar posicion de brazos tal cual estaba brazoIzq.position = defPosIzq; brazoDer.position = defPosDer; // ======== FIN Movimiento brazos robot. ======== yield return(new WaitForSeconds(0)); // Fin de ejecucion UI.executingCurrentLine = false; }