Moved steepness calculation

This commit is contained in:
Tiberiu Chibici 2015-05-21 11:49:04 +03:00
parent 7bd2b7b255
commit e51c74944d
2 changed files with 27 additions and 15 deletions

View File

@ -11,7 +11,7 @@ namespace TransportGame.Model
[XmlRoot("map")]
public class Map
{
private float[,] grid;
private float[,] heightmap;
private float[,] population;
#region Properties
@ -36,7 +36,7 @@ namespace TransportGame.Model
{
get
{
return grid;
return heightmap;
}
}
@ -48,11 +48,11 @@ namespace TransportGame.Model
{
get
{
return grid.ToByteArray();
return heightmap.ToByteArray();
}
set
{
grid = value.GetFloatMatrix();
heightmap = value.GetFloatMatrix();
}
}
@ -60,13 +60,13 @@ namespace TransportGame.Model
/// Gets width of heightmap
/// </summary>
[XmlIgnore]
public int Width { get { return (grid == null) ? 0 : grid.GetLength(0); } }
public int Width { get { return (heightmap == null) ? 0 : heightmap.GetLength(0); } }
/// <summary>
/// Gets height of heightmap
/// </summary>
[XmlIgnore]
public int Height { get { return (grid == null) ? 0 : grid.GetLength(1); } }
public int Height { get { return (heightmap == null) ? 0 : heightmap.GetLength(1); } }
/// <summary>
/// Gets the population map
@ -139,7 +139,7 @@ namespace TransportGame.Model
/// <param name="height">Height</param>
public Map(int width, int height)
{
grid = new float[width, height];
heightmap = new float[width, height];
}
#endregion
@ -152,7 +152,7 @@ namespace TransportGame.Model
/// <returns>Value</returns>
public float GetHeight(int x, int y)
{
return grid[x, y] * Biome.Height;
return heightmap[x, y] * Biome.Height;
}
/// <summary>
@ -163,7 +163,7 @@ namespace TransportGame.Model
/// <param name="value">Value</param>
public void SetHeight(int x, int y, float value)
{
grid[x, y] = value / Biome.Height;
heightmap[x, y] = value / Biome.Height;
}
/// <summary>
@ -187,5 +187,22 @@ namespace TransportGame.Model
{
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;
}
}
}

View File

@ -123,12 +123,7 @@ public class TerrainGeneratorScript : MonoBehaviour
float height = map.GetHeight(ix, iy);
// Get steepness
int safex = (ix == 0) ? 1 : ix;
int safey = (iy == 0) ? 1 : iy;
float dx = map.GetHeight(safex - 1, safey) - height;
float dy = map.GetHeight(safex, safey - 1) - height;
float steepness = dx * dx + dy * dy;
float steepness = map.GetSteepness(ix, iy);
// Go through each texture layer
float[] weights = new float[terrainData.alphamapLayers];