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

202 lines
5.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using TransportGame.Model.Road;
using TransportGame.Utils;
using UnityEngine;
namespace TransportGame.Model
{
[XmlRoot("map")]
public class Map
{
private float[,] heightmap;
#region Properties
/// <summary>
/// Gets or sets the biome
/// </summary>
[XmlElement("biome")]
public Biome Biome { get; set; }
/// <summary>
/// Gets or sets the water level
/// </summary>
[XmlElement("waterLevel")]
public float WaterLevel { get; set; }
/// <summary>
/// Gets the heights array in range [0,1]
/// </summary>
[XmlIgnore()]
public float[,] Heightmap
{
get
{
return heightmap;
}
}
/// <summary>
/// Gets or sets the heights as raw bytes
/// </summary>
[XmlElement("heightmap")]
public byte[] HeightmapRaw
{
get
{
return heightmap.ToByteArray();
}
set
{
heightmap = value.GetFloatMatrix();
}
}
/// <summary>
/// Gets width of heightmap
/// </summary>
[XmlIgnore]
public int Width { get { return (heightmap == null) ? 0 : heightmap.GetLength(0); } }
/// <summary>
/// Gets height of heightmap
/// </summary>
[XmlIgnore]
public int Height { get { return (heightmap == null) ? 0 : heightmap.GetLength(1); } }
/// <summary>
/// Gets or sets the population map
/// </summary>
[XmlArray("populationCenters")]
[XmlArrayItem("center")]
public List<Point> PopulationCenters { get; set; }
/// <summary>
/// Gets or sets the articulation road network
/// </summary>
[XmlElement("roadNetwork")]
public RoadNetwork RoadNetwork { get; set; }
#endregion
#region Constructors
/// <summary>
/// Initializes the map
/// </summary>
/// <remarks>
/// Warning: heights array will be null.
/// </remarks>
public Map()
{
PopulationCenters = new List<Point>();
}
/// <summary>
/// Initializes the map
/// </summary>
/// <param name="width">Width</param>
/// <param name="height">Height</param>
public Map(int width, int height)
{
heightmap = new float[width, height];
PopulationCenters = new List<Point>();
}
#endregion
/// <summary>
/// Gets the cell at specified position in range [0, Biome.Height]
/// </summary>
/// <param name="x">X</param>
/// <param name="y">Y</param>
/// <returns>Value</returns>
public float GetHeight(int x, int y)
{
return heightmap[x, y] * Biome.Height;
}
/// <summary>
/// Sets the height at specified position in range [0, Biome.Height]
/// </summary>
/// <param name="x">X</param>
/// <param name="y">Y</param>
/// <param name="value">Value</param>
public void SetHeight(int x, int y, float value)
{
heightmap[x, y] = value / Biome.Height;
}
/// <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)
{
return GetHeight(x, y) <= WaterLevel;
}
/// <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;
}
/// <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;
}
/// <summary>
/// Gets population using terrain coordinates
/// </summary>
/// <param name="x">X</param>
/// <param name="y">Y</param>
/// <returns>Population</returns>
public float GetPopulation(int x, int y)
{
const int maxDistance = 400;
float value = 0;
foreach (var point in PopulationCenters)
{
int x1 = x - point.X;
int y1 = y - point.Y;
int dist = x1 * x1 + y1 * y1;
if (dist < maxDistance * maxDistance)
{
float influence = 1 - (float)dist / (float)(maxDistance * maxDistance);
influence = Mathf.Pow(influence, 3); // Ease
value = Mathf.Clamp01(value + influence);
}
}
return value;
}
}
}