/// <summary> /// Constructs a new instance of the <see cref="Entity" /> class /// with the properties populated from XML /// </summary> /// <param name="xml">XML node to read the properties from</param> /// <param name="graphicSize">Size of the graphic the entity has</param> public Entity(XmlNode xml, Size graphicSize) { if (xml == null) { throw new ArgumentNullException(nameof(xml)); } // Read required properties ID = xml.Attributes["id"] == null ? null : xml.Attributes["id"].Value; Type = xml.Attributes["type"].Value; Graphic = xml.Attributes["graphic"].Value; X = Double.Parse(xml.Attributes["x"].Value); Y = Double.Parse(xml.Attributes["y"].Value); Delay = xml.Attributes["delay"] == null ? null : (int?)int.Parse(xml.Attributes["delay"].Value); // Read extra props PowerUp = xml.Attributes["powerUp"] == null ? null : xml.Attributes["powerUp"].Value; LurcherID = xml.Attributes["lurcherId"] == null ? null : xml.Attributes["lurcherId"].Value; // Default optional types DestinationX = null; DestinationY = null; Speed = null; // Read properties based on type switch (Type) { case "alien": case "asteroid": case "background": case "debris": case "nebula": case "pirate": case "crate": case "lurcher": // Read the properties double?vX = xml.Attributes["vX"] == null ? null : (double?)Double.Parse(xml.Attributes["vX"].Value); double?vY = xml.Attributes["vY"] == null ? null : (double?)Double.Parse(xml.Attributes["vY"].Value); // There are no properties to convert if (!vX.HasValue || !vY.HasValue) { return; } // Build vectors Vector position = new Vector(X, Y); Vector velocity = new Vector(vX.Value, vY.Value); // Calculate destination Vector destination = BRMath.CalculateDestination(X, Y, graphicSize.Width, graphicSize.Height, velocity.X, velocity.Y); DestinationX = destination.X; DestinationY = destination.Y; // Calculate speed Speed = BRMath.CalculateSpeed(position, destination, velocity); break; } }
/// <summary> /// Converts the entity to XML /// </summary> /// <param name="indention">Indention to apply</param> /// <returns>Entity XML</returns> public string ToXml(int indention) { // Start building XML StringBuilder xmlBuilder = new StringBuilder(); // Open the entity xmlBuilder.AppendFormat("{0}<entity ", new string(' ', indention)); // Append ID if it exists if (!String.IsNullOrWhiteSpace(ID)) { xmlBuilder.AppendFormat("id=\"{0}\" ", ID); } // Append properties that always exist xmlBuilder.AppendFormat("type=\"{0}\" ", Type); xmlBuilder.AppendFormat("graphic=\"{0}\" ", Graphic); xmlBuilder.AppendFormat("x=\"{0}\" ", X); xmlBuilder.AppendFormat("y=\"{0}\" ", Y); // Determine which velocity props to write if (Type == "police") { xmlBuilder.AppendFormat("dX=\"{0}\" ", DestinationX); xmlBuilder.AppendFormat("dY=\"{0}\" ", DestinationY); xmlBuilder.AppendFormat("s=\"{0}\" ", Speed); } else if (DestinationX.HasValue && DestinationY.HasValue && Speed.HasValue) { // Calculate velocity Vector velocity = BRMath.CalculateVelocity(X, Y, DestinationX.Value, DestinationY.Value, Speed.Value); xmlBuilder.AppendFormat("vX=\"{0}\" ", velocity.X); xmlBuilder.AppendFormat("vY=\"{0}\" ", velocity.Y); } if (Delay.HasValue) { xmlBuilder.AppendFormat("delay=\"{0}\" ", Delay); } if (!String.IsNullOrWhiteSpace(PowerUp)) { xmlBuilder.AppendFormat("powerUp=\"{0}\" ", PowerUp); } if (!String.IsNullOrWhiteSpace(LurcherID)) { xmlBuilder.AppendFormat("lurcherId=\"{0}\" ", LurcherID); } // Close the entity xmlBuilder.Append("/>"); return(xmlBuilder.ToString()); }