public void CopyValuesFrom(CellLayer <T> anotherLayer) { if (Size != anotherLayer.Size || GridT != anotherLayer.GridT) { throw new ArgumentException("layers must have a matching size and shape (grid type).", "anotherlayer"); } if (CellEntryChanged != null) { throw new InvalidOperationException("Cannot copy values when there are listeners attached to the CellEntryChanged event."); } Array.Copy(anotherLayer.entries, entries, entries.Length); }
/// <summary> /// /// </summary> /// <param name="initialCellValueFactory"></param> /// <param name="size"></param> /// <param name="mapGridT"></param> /// <returns></returns> public static CellLayer <T> CreateInstance(Func <MPos, T> initialCellValueFactory, Size size, MapGridT mapGridT) { var cellLayer = new CellLayer <T>(mapGridT, size); for (var v = 0; v < size.Height; v++) { for (var u = 0; u < size.Width; u++) { var mpos = new MPos(u, v); cellLayer[mpos] = initialCellValueFactory(mpos); } } return(cellLayer); }
public Map(ModData modData, IReadOnlyPackage package) { this.modData = modData; Package = package; if (!Package.Contains("map.yaml") || !Package.Contains("map.bin")) { throw new InvalidDataException("Not a valid map\n File:{0}".F(package.Name)); } var yaml = new MiniYaml(null, MiniYaml.FromStream(Package.GetStream("map.yaml"), package.Name)); foreach (var field in YamlFields) { field.Deserialize(this, yaml.Nodes); } if (MapFormat != SupportedMapFormat) { throw new InvalidDataException("Map format {0} is not supported. \n File:{1}".F(MapFormat, package.Name)); } PlayerDefinitions = MiniYaml.NodesOrEmpty(yaml, "Players"); ActorDefinitions = MiniYaml.NodesOrEmpty(yaml, "Actors"); Grid = modData.Manifest.Get <MapGrid>(); var size = new Size((int)MapSize.X, (int)MapSize.Y); //Layer Tiles = new CellLayer <TerrainTile>(Grid.Type, size); Resources = new CellLayer <ResourceTile>(Grid.Type, size); Height = new CellLayer <byte>(Grid.Type, size); using (var s = Package.GetStream("map.bin")) { var header = new BinaryDataHeader(s, MapSize); if (header.TilesOffset > 0) { s.Position = header.TilesOffset; for (var i = 0; i < MapSize.X; i++) { for (var j = 0; j < MapSize.Y; j++) { var tile = s.ReadUInt16(); var index = s.ReadUInt8(); if (index == byte.MaxValue) { index = (byte)(i % 4 + (j % 4) * 4); } Tiles[new MPos(i, j)] = new TerrainTile(tile, index); } } } if (header.ResourcesOffset > 0) { s.Position = header.ResourcesOffset; for (var i = 0; i < MapSize.X; i++) { for (var j = 0; j < MapSize.Y; j++) { var type = s.ReadUInt8(); var density = s.ReadUInt8(); Resources[new MPos(i, j)] = new ResourceTile(type, density); } } } if (header.HeightsOffset > 0) { s.Position = header.HeightsOffset; for (var i = 0; i < MapSize.X; i++) { for (var j = 0; j < MapSize.Y; j++) { Height[new MPos(i, j)] = s.ReadUInt8().Clamp((byte)0, Grid.MaximumTerrainHeight); } } } } if (Grid.MaximumTerrainHeight > 0) { Tiles.CellEntryChanged += UpdateProjection; Height.CellEntryChanged += UpdateProjection; } PostInit(); Uid = ComputeUID(Package); }