public VGDLSprite() { physicstype = "GRID"; wMult = hMult = 1.0f; physics = null; gravity = 0.0f; friction = 0.0f; speed = 0; stationary = false; cooldown = 0; color = VGDLColors.GetRandomColor(); only_active = false; name = null; is_static = false; is_avatar = false; is_stochastic = false; is_from_avatar = false; mass = 1; shrinkfactor = 1.0f; autotiling = false; randomtiling = -1; frameRate = -1; frameRemaining = 0; currentFrame = -1; is_oriented = false; draw_arrow = false; orientation = VGDLUtils.VGDLDirections.NONE.getDirection(); lastmove = 0; invisible = "false"; rotateInPlace = false; isFirstTick = true; disabled = false; limitHealthPoints = 1000; rotation = 0.0f; max_speed = -1.0f; }
/// <summary> /// Parse arguments from argument list and set their values on the object given. /// </summary> /// <param name="obj"></param> /// <param name="args"></param> /// <exception cref="ArgumentException"></exception> private static void parseParameters(object obj, IEnumerable <KeyValuePair <string, string> > args, bool suppressWarnings = false) { var type = obj.GetType(); var fields = type.GetFields(); var fieldMap = new Dictionary <string, FieldInfo>(); foreach (var field in fields) { if (fieldMap.ContainsKey(field.Name)) { Debug.LogError("Key [" + field.Name + "] already in fieldMap for: " + type); } fieldMap.Add(field.Name, field); } foreach (var keyValuePair in args) { if (fieldMap.ContainsKey(keyValuePair.Key)) { var fieldInfo = fieldMap[keyValuePair.Key]; if (keyValuePair.Value.Contains("[")) { //value seems to be array, validate that. if (keyValuePair.Value.Contains("]")) { var trimmed = keyValuePair.Value.Trim('[', ']'); //find list of value strings var values = trimmed.Split(new [] { ' ' }, StringSplitOptions.RemoveEmptyEntries); //Reflect the ParseToArray method var methodDefinition = typeof(VGDLParser).GetMethod("ParseToArray"); //Generate the generic method, based on the field type // ReSharper disable once PossibleNullReferenceException var parseMethod = methodDefinition.MakeGenericMethod(fieldInfo.FieldType.GetElementType()); //Invoke the parse method with our values. var array = parseMethod.Invoke(null, new object[] { values }); fieldInfo.SetValue(obj, array); //this keyvalue pair has been handled, continue to the next one. continue; } else { throw new ArgumentException("Parsing parameter [" + keyValuePair.Key + "] failed, array missing ']'"); } } if (fieldInfo.FieldType.IsAssignableFrom(typeof(Color))) { //Special case for color parsing! Color color; var success = VGDLColors.ParseColor(keyValuePair.Value, out color); if (success) { fieldInfo.SetValue(obj, color); } else { Debug.LogWarning("Color [" + keyValuePair.Value + "] not found, using magenta instead"); fieldInfo.SetValue(obj, Color.magenta); } } else if (fieldInfo.FieldType.IsAssignableFrom(typeof(Vector2))) { //Special case for orientation parsing! var direction = VGDLUtils.ParseVGDLDirection(keyValuePair.Value); if (direction != VGDLUtils.VGDLDirections.NIL.getDirection()) { fieldInfo.SetValue(obj, direction); } else { Debug.LogWarning("Direction [" + keyValuePair.Value + "] not found, using 'LEFT' instead"); fieldInfo.SetValue(obj, Vector2.left); } } else { try { fieldInfo.SetValue(obj, Convert.ChangeType(keyValuePair.Value, fieldInfo.FieldType)); } catch (Exception e) { Debug.LogError("Unable to parse [" + keyValuePair.Value + "] for field [" + keyValuePair.Key + " with type: " + fieldInfo.FieldType.Name + "] on " + type.Name); throw; } } } else { //We suppress warnings for TimeEffects, because of the way they are defined //eg. [sprite1 TIME > transformToAll stype=portal stypeTo=goalPortal nextExecution=500 timer=500 repeating=False] //This means that these parameters don't all belong to the timer, some belong to the desired effect. if (!suppressWarnings) { Debug.LogWarning("Field [" + keyValuePair.Key + "] does not exist on " + type.Name); } else { if (verbose) { Debug.Log("Parameter [" + keyValuePair.Key + "] ignored on " + type.Name); } } } } }