using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Serialization; using TransportGame.Model.Road; using TransportGame.Utils; namespace TransportGame.Model { [XmlRoot("map")] public class Map { private float[,] grid; private float[,] population; #region Properties /// /// Gets or sets the biome /// [XmlElement("biome")] public Biome Biome { get; set; } /// /// Gets or sets the water level /// [XmlElement("waterLevel")] public float WaterLevel { get; set; } /// /// Gets the heights array in range [0,1] /// [XmlIgnore()] public float[,] Heightmap { get { return grid; } } /// /// Gets or sets the heights as raw bytes /// [XmlElement("heightmap")] public byte[] HeightmapRaw { get { return grid.ToByteArray(); } set { grid = value.GetFloatMatrix(); } } /// /// Gets width of heightmap /// [XmlIgnore] public int Width { get { return (grid == null) ? 0 : grid.GetLength(0); } } /// /// Gets height of heightmap /// [XmlIgnore] public int Height { get { return (grid == null) ? 0 : grid.GetLength(1); } } /// /// Gets the population map /// [XmlIgnore()] public float[,] Population { get { return population; } set { population = value; } } /// /// Gets or sets the population as raw bytes /// [XmlElement("population")] public byte[] PopulationRaw { get { return population.ToByteArray(); } set { population = value.GetFloatMatrix(); } } /// /// Gets width of population map /// [XmlIgnore] public int PopulationWidth { get { return (population == null) ? 0 : population.GetLength(0); } } /// /// Gets height of population map /// [XmlIgnore] public int PopulationHeight { get { return (population == null) ? 0 : population.GetLength(1); } } /// /// Gets or sets the articulation road network /// [XmlElement("roadNetwork")] public RoadNetwork RoadNetwork { get; set; } #endregion #region Constructors /// /// Initializes the map /// /// /// Warning: heights array will be null. /// public Map() { } /// /// Initializes the map /// /// Width /// Height public Map(int width, int height) { grid = new float[width, height]; } #endregion /// /// Gets the cell at specified position in range [0, Biome.Height] /// /// X /// Y /// Value public float GetHeight(int x, int y) { return grid[x, y] * Biome.Height; } /// /// Sets the height at specified position in range [0, Biome.Height] /// /// X /// Y /// Value public void SetHeight(int x, int y, float value) { grid[x, y] = value / Biome.Height; } /// /// Returns true if specified cell is a water cell /// /// X /// Y /// public bool IsWater(int x, int y) { return GetHeight(x, y) <= WaterLevel; } /// /// Returns true if given coordinates is inside the map /// /// X /// Y /// True if coordinates are inside the map public bool IsInside(int x, int y) { return x >= 0 && y >= 0 && x < Width && y < Height; } } }