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 in range [0,1]
///
[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 in range [0, Biome.Height]
///
/// X
/// Y
/// Cell
[XmlIgnore]
public float this[int x, int y]
{
get
{
return grid[x, y] * Biome.Height;
}
set
{
grid[x, y] = value / Biome.Height;
}
}
///
/// 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 this[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;
}
}
}