public override void InitializeAgent() { environment.InitializeEnvironment(); rb = GetComponent <Rigidbody>(); wheels = GetComponent <WheelDrive>(); if (useSimplePhysics) { Destroy(wheels); WheelCollider[] wheelColliders = FindObjectsOfType <WheelCollider>(); foreach (WheelCollider wheelCollider in wheelColliders) { Destroy(wheelCollider); } rb.constraints = RigidbodyConstraints.FreezeRotation; } shovel = GetComponent <ShovelControl>(); vision = GetComponent <RobotVision>(); vision.InitializeDebrisArray(environment); sensors = GetComponent <RobotSensors>(); dropZone = environment.GetDropZone(); displayRewards = FindObjectOfType <DisplayRewards>(); startPosition = transform.position; startRotation = transform.rotation; lastCheckedPosition = startPosition; //AgentReset(); }
/// <summary> /// Dada una expresion, reconoce los nombres de sensores presentes en ella, /// y sustituye esos literales por sus valores. /// </summary> /// <param name="expression"></param> /// <returns></returns> private string ParseSensorData(string expression) { if (expression == null || expression == "") { return(""); } Tokenizer tokenizer = new Tokenizer(expression); var enumerator = tokenizer.GetTokens().GetEnumerator(); while (enumerator.MoveNext()) { Token current = enumerator.Current; string possibleSensorName = ""; string possibleSensorProperty = ""; if (current != null && current.Type == TokenType.Name) { possibleSensorName = current.Value; enumerator.MoveNext(); if (enumerator.Current.Type == TokenType.Dot) { enumerator.MoveNext(); if (enumerator.Current.Type == TokenType.Name) { possibleSensorProperty = enumerator.Current.Value; } else { throw new Exception("Invalid expression"); } } } if (possibleSensorName != "" && possibleSensorProperty != "") { string fullSensorLiteral = possibleSensorName + "." + possibleSensorProperty; possibleSensorName = possibleSensorName.ToLower(); possibleSensorProperty = possibleSensorProperty.ToLower(); bool ok = false; //Comienza por parsear los colores. if (possibleSensorName == "color") { Color c = Color.FromName(possibleSensorProperty); if (c.IsKnownColor) { expression.Replace(fullSensorLiteral, c.ToArgb().ToString()); } else { throw new Exception("A specified color is not supported or does not exist."); } } else { // Se usa Reflection para obtener todos los sensores del robot con sus correspondientes valores. RobotSensors conjunto = Robot.Sensors; var typeOfSensores = conjunto.GetType(); PropertyInfo[] sensoresProperties = typeOfSensores.GetProperties(); string result = ""; foreach (PropertyInfo info in sensoresProperties) { var l = info.GetValue(conjunto, null); Type currentSensorType = l.GetType(); if (currentSensorType.Name.ToLower() == possibleSensorName) { foreach (PropertyInfo sensorProperties in currentSensorType.GetProperties()) { if (sensorProperties.Name.ToLower() == possibleSensorProperty) { var value = sensorProperties.GetValue(l, null); if (value != null) { result = value.ToString(); expression = expression.Replace(fullSensorLiteral, result); ok = true; break; } } } } } if (!ok) { throw new Exception("A specified sensor or property does not exist."); } } } } return(expression); }