62 lines
1.9 KiB
C#
62 lines
1.9 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using TransportGame.Business;
|
|||
|
using TransportGame.Model;
|
|||
|
using TransportGame.Noise;
|
|||
|
using TransportGame.Utils;
|
|||
|
using TransportGame.Utils.Algorithms;
|
|||
|
using UnityEngine;
|
|||
|
|
|||
|
namespace TransportGame.Generator
|
|||
|
{
|
|||
|
public class TerrainGenerator
|
|||
|
{
|
|||
|
public NoiseGenerator Noise { get; set; }
|
|||
|
|
|||
|
private System.Random random = new System.Random();
|
|||
|
|
|||
|
public TerrainGenerator()
|
|||
|
{
|
|||
|
Noise = new PerlinNoiseGenerator();
|
|||
|
Noise.Octaves = ConfigurationManager.TerrGenConfig.NoiseOctaves;
|
|||
|
Noise.NonLinearPower = ConfigurationManager.TerrGenConfig.NoiseNonLinearPower;
|
|||
|
Noise.Scale = ConfigurationManager.TerrGenConfig.ElevationScale;
|
|||
|
}
|
|||
|
|
|||
|
public Map Generate(int width, int height)
|
|||
|
{
|
|||
|
// Create map
|
|||
|
Map map = new Map(width, height);
|
|||
|
|
|||
|
// Pick a random biome
|
|||
|
map.Biome = PickBiome();
|
|||
|
|
|||
|
// Generate elevation
|
|||
|
GenerateElevation(map);
|
|||
|
|
|||
|
// Generate water level
|
|||
|
float waterAmount = random.NextSingle(map.Biome.Moisture.Minimum, map.Biome.Moisture.Maximum);
|
|||
|
map.WaterLevel = Mathf.Pow(waterAmount, 3) * (map.Biome.HeightRange.Maximum - map.Biome.HeightRange.Minimum) + map.Biome.HeightRange.Minimum;
|
|||
|
|
|||
|
return map;
|
|||
|
}
|
|||
|
|
|||
|
private Biome PickBiome()
|
|||
|
{
|
|||
|
int biomeCount = BiomeManager.Biomes.Count();
|
|||
|
int biome = random.Next(biomeCount);
|
|||
|
|
|||
|
return BiomeManager.Biomes.ElementAt(biome);
|
|||
|
}
|
|||
|
|
|||
|
private void GenerateElevation(Map map)
|
|||
|
{
|
|||
|
for (int x = 0; x < map.Width; ++x)
|
|||
|
for (int y = 0; y < map.Height; ++y)
|
|||
|
map[x, y] = Noise.Generate(x, y, map.Biome.HeightRange.Minimum, map.Biome.HeightRange.Maximum);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|