using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
namespace TransportGame.Model
{
[XmlRoot("map")]
public class Map
{
private float[,] grid;
///
/// Gets or sets the water level
///
[XmlElement("waterLevel")]
public float WaterLevel { get; set; }
///
/// Gets or sets the biome
///
[XmlElement("biome")]
public Biome Biome { get; set; }
///
/// Gets the heights array
///
[XmlIgnore()]
public float[,] Heights
{
get
{
return grid;
}
}
///
/// Gets or sets the heights as raw bytes
///
///
/// Bytes are stored as such:
///
/// Offset Size Content
/// ------------------------
/// 0 4 Width
/// 4 8 Height
/// 8 var 32bit floating point values
///
///
[XmlElement("heights")]
public byte[] HeightsRaw
{
get
{
List bytes = new List();
bytes.AddRange(BitConverter.GetBytes(Width));
bytes.AddRange(BitConverter.GetBytes(Height));
for (int x = 0; x < Width; x++)
for (int y = 0; y < Height; y++)
bytes.AddRange(BitConverter.GetBytes(grid[x, y]));
return bytes.ToArray();
}
set
{
int pos = 0;
int w = BitConverter.ToInt32(value, pos); pos += sizeof(int);
int h = BitConverter.ToInt32(value, pos); pos += sizeof(int);
grid = new float[w, h];
for (int x = 0; x < w; x++)
for (int y = 0; y < h; y++)
{
grid[x, y] = BitConverter.ToSingle(value, pos);
pos += sizeof(float);
}
}
}
///
/// Initializes the map
///
///
/// Warning: heights array will be null.
///
public Map()
{
}
///
/// Initializes the map
///
///
///
public Map(int width, int height)
{
grid = new float[width, height];
}
///
/// Gets or sets the cell at specified position
///
/// X
/// Y
/// Cell
[XmlIgnore]
public float this[int x, int y]
{
get
{
return grid[x, y];
}
set
{
grid[x, y] = value;
}
}
///
/// Gets width of map
///
[XmlIgnore]
public int Width { get { return grid.GetLength(0); } }
///
/// Gets height of map
///
[XmlIgnore]
public int Height { get { return grid.GetLength(1); } }
///
/// Returns true if specified cell is a water cell
///
/// X
/// Y
///
public bool IsWater(int x, int y)
{
return grid[x, y] * Biome.Height <= WaterLevel;
}
}
}