public static HexLayer HexLayerFromXml(GameResourceManager gameResourceManager, XElement layerElement) { var name = layerElement.Attribute("name").Value; var mapSize = MathUtil.ParseSize(layerElement.Element("MapSize").Value); var hexSize = MathUtil.ParseSize(layerElement.Element("HexSize").Value); var edgeLength = int.Parse(layerElement.Element("EdgeLength").Value); var hexReferences = GetHexReferences(gameResourceManager, layerElement.Element("HexDefinitionReferences")).ToList(); var hexes = GetRowsFromXml(layerElement.Element("Hexes")); var layer = new HexLayer(name, mapSize, hexSize, edgeLength); XmlRepository.BaseFromXml(layer, layerElement); int x = 0; foreach (var row in hexes) { int y = 0; foreach (var element in row) { layer[x, y++] = hexReferences.Single(r => r.Id == element).Definition; } x++; } return layer; }
private static IEnumerable<HexReference> CreateHexReferences(HexLayer hexLayer) { var hexDefinitions = new List<HexDefinition>(); for (var i = 0; i < hexLayer.MapSize.Width; i++) for (var j = 0; j < hexLayer.MapSize.Height; j++) { hexDefinitions.Add(hexLayer[i, j]); } return hexDefinitions .Distinct() .Select((x, i) => new HexReference { Id = i, Definition = x }); }
public static XElement ToXml(HexLayer hexLayer) { var hexReferences = CreateHexReferences(hexLayer).ToList(); return new XElement("HexLayer", XmlRepository.LayerBaseToXml(hexLayer), new XElement("MapSize", hexLayer.MapSize), new XElement("HexSize", hexLayer.HexSize), new XElement("EdgeLength", hexLayer.TopEdgeLength), new XElement("HexDefinitionReferences", hexReferences.Select(x => new XElement("Reference", new XAttribute("id", x.Id), new XAttribute("sheetName", x.Definition.SheetName), new XAttribute("name", x.Definition.Name)))), new XElement("Hexes", GetRowsXml(hexLayer, hexReferences))); }
private static IEnumerable<XElement> GetRowsXml(HexLayer hexLayer, List<HexReference> hexReferences) { for (var i = 0; i < hexLayer.MapSize.Width; i++) { var row = new int[hexLayer.MapSize.Height]; for (var j = 0; j < hexLayer.MapSize.Height; j++) { row[j] = hexReferences.Single(x => x.Definition == hexLayer[i, j]).Id; } yield return new XElement("Row", string.Join(", ", row)); } }
private HexLayer CreateHexLayer() { var texture = this.ResourceManager.GetTexture(@"Sandbox\HexSheet"); var sheet = new HexSheet(texture, "Hexes", new Size(68, 60)); var red = sheet.CreateHexDefinition("red", new Point(55, 30)); var yellow = sheet.CreateHexDefinition("yellow", new Point(163, 330)); var purple = sheet.CreateHexDefinition("purple", new Point(488, 330)); //sheet.Save(@"C:\Users\Pascal\Dev\DotNet\GitHub\XNAGameEngine2D\Color HexSheet.xml"); this.ResourceManager.AddHexSheet(sheet); var hexLayer = new HexLayer("Hex", new Size(4, 4), new Size(68, 60), 42); hexLayer[2, 0] = purple; hexLayer[2, 1] = purple; hexLayer[2, 2] = yellow; hexLayer[0, 1] = red; hexLayer[1, 1] = red; hexLayer.ParallaxScrollingVector = new Vector(4.0f, 0.5f); hexLayer.Offset = new Vector(-25, 0); return hexLayer; }