city-generation/Game/Assets/Scripts/Model/Map.cs

209 lines
5.4 KiB
C#
Raw Normal View History

2015-03-13 15:36:12 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
2015-05-20 08:26:46 +00:00
using TransportGame.Model.Road;
using TransportGame.Utils;
2015-03-13 15:36:12 +00:00
namespace TransportGame.Model
{
[XmlRoot("map")]
public class Map
{
2015-05-21 08:49:04 +00:00
private float[,] heightmap;
2015-05-20 08:26:46 +00:00
private float[,] population;
2015-03-13 15:36:12 +00:00
2015-05-20 08:26:46 +00:00
#region Properties
2015-03-13 15:36:12 +00:00
/// <summary>
/// Gets or sets the biome
/// </summary>
[XmlElement("biome")]
public Biome Biome { get; set; }
2015-05-20 08:26:46 +00:00
/// <summary>
/// Gets or sets the water level
/// </summary>
[XmlElement("waterLevel")]
public float WaterLevel { get; set; }
2015-03-13 15:36:12 +00:00
/// <summary>
2015-03-23 19:17:09 +00:00
/// Gets the heights array in range [0,1]
2015-03-13 15:36:12 +00:00
/// </summary>
[XmlIgnore()]
2015-05-20 08:26:46 +00:00
public float[,] Heightmap
2015-03-13 15:36:12 +00:00
{
get
{
2015-05-21 08:49:04 +00:00
return heightmap;
2015-03-13 15:36:12 +00:00
}
}
/// <summary>
/// Gets or sets the heights as raw bytes
/// </summary>
2015-05-20 08:26:46 +00:00
[XmlElement("heightmap")]
public byte[] HeightmapRaw
2015-03-13 15:36:12 +00:00
{
get
{
2015-05-21 08:49:04 +00:00
return heightmap.ToByteArray();
2015-05-20 08:26:46 +00:00
}
set
{
2015-05-21 08:49:04 +00:00
heightmap = value.GetFloatMatrix();
2015-05-20 08:26:46 +00:00
}
}
2015-03-13 15:36:12 +00:00
2015-05-20 08:26:46 +00:00
/// <summary>
/// Gets width of heightmap
/// </summary>
[XmlIgnore]
2015-05-21 08:49:04 +00:00
public int Width { get { return (heightmap == null) ? 0 : heightmap.GetLength(0); } }
2015-03-13 15:36:12 +00:00
2015-05-20 08:26:46 +00:00
/// <summary>
/// Gets height of heightmap
/// </summary>
[XmlIgnore]
2015-05-21 08:49:04 +00:00
public int Height { get { return (heightmap == null) ? 0 : heightmap.GetLength(1); } }
2015-05-20 08:26:46 +00:00
/// <summary>
/// Gets the population map
/// </summary>
[XmlIgnore()]
public float[,] Population
{
get
{
return population;
2015-03-13 15:36:12 +00:00
}
set
{
2015-05-20 08:26:46 +00:00
population = value;
2015-03-13 15:36:12 +00:00
}
}
2015-05-20 08:26:46 +00:00
/// <summary>
/// Gets or sets the population as raw bytes
/// </summary>
[XmlElement("population")]
public byte[] PopulationRaw
{
get
{
return population.ToByteArray();
}
set
{
population = value.GetFloatMatrix();
}
}
/// <summary>
/// Gets width of population map
/// </summary>
[XmlIgnore]
public int PopulationWidth { get { return (population == null) ? 0 : population.GetLength(0); } }
/// <summary>
/// Gets height of population map
/// </summary>
[XmlIgnore]
public int PopulationHeight { get { return (population == null) ? 0 : population.GetLength(1); } }
/// <summary>
/// Gets or sets the articulation road network
/// </summary>
[XmlElement("roadNetwork")]
public RoadNetwork RoadNetwork { get; set; }
#endregion
#region Constructors
2015-03-13 15:36:12 +00:00
/// <summary>
/// Initializes the map
/// </summary>
/// <remarks>
/// Warning: heights array will be null.
/// </remarks>
public Map()
{
}
/// <summary>
/// Initializes the map
/// </summary>
2015-05-20 08:26:46 +00:00
/// <param name="width">Width</param>
/// <param name="height">Height</param>
2015-03-13 15:36:12 +00:00
public Map(int width, int height)
{
2015-05-21 08:49:04 +00:00
heightmap = new float[width, height];
2015-03-13 15:36:12 +00:00
}
2015-05-20 08:26:46 +00:00
#endregion
2015-03-13 15:36:12 +00:00
/// <summary>
2015-05-20 08:26:46 +00:00
/// Gets the cell at specified position in range [0, Biome.Height]
2015-03-13 15:36:12 +00:00
/// </summary>
/// <param name="x">X</param>
/// <param name="y">Y</param>
2015-05-20 08:26:46 +00:00
/// <returns>Value</returns>
public float GetHeight(int x, int y)
2015-03-13 15:36:12 +00:00
{
2015-05-21 08:49:04 +00:00
return heightmap[x, y] * Biome.Height;
2015-03-13 15:36:12 +00:00
}
/// <summary>
2015-05-20 08:26:46 +00:00
/// Sets the height at specified position in range [0, Biome.Height]
2015-03-13 15:36:12 +00:00
/// </summary>
2015-05-20 08:26:46 +00:00
/// <param name="x">X</param>
/// <param name="y">Y</param>
/// <param name="value">Value</param>
public void SetHeight(int x, int y, float value)
{
2015-05-21 08:49:04 +00:00
heightmap[x, y] = value / Biome.Height;
2015-05-20 08:26:46 +00:00
}
2015-03-13 15:36:12 +00:00
/// <summary>
/// Returns true if specified cell is a water cell
/// </summary>
/// <param name="x">X</param>
/// <param name="y">Y</param>
/// <returns></returns>
public bool IsWater(int x, int y)
{
2015-05-20 08:26:46 +00:00
return GetHeight(x, y) <= WaterLevel;
2015-03-23 19:17:09 +00:00
}
/// <summary>
/// Returns true if given coordinates is inside the map
/// </summary>
/// <param name="x">X</param>
/// <param name="y">Y</param>
/// <returns>True if coordinates are inside the map</returns>
public bool IsInside(int x, int y)
{
return x >= 0 && y >= 0 && x < Width && y < Height;
2015-03-13 15:36:12 +00:00
}
2015-05-21 08:49:04 +00:00
/// <summary>
/// Gets steepness in specified point
/// </summary>
/// <param name="x">X</param>
/// <param name="y">Y</param>
/// <returns>Steepness</returns>
public float GetSteepness(int x, int y)
{
if (x == 0) x++;
if (y == 0) y++;
float dx = GetHeight(x - 1, y) - GetHeight(x, y);
float dy = GetHeight(x, y - 1) - GetHeight(x, y);
return dx * dx + dy * dy;
}
2015-03-13 15:36:12 +00:00
}
}