diff --git a/Game/Assets/Scripts/Business/BiomeManager.cs b/Game/Assets/Scripts/Business/BiomeManager.cs index b651604..12220a7 100644 --- a/Game/Assets/Scripts/Business/BiomeManager.cs +++ b/Game/Assets/Scripts/Business/BiomeManager.cs @@ -1,54 +1,54 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Xml.Serialization; -using TransportGame.Model; -using TransportGame.Utils; - -namespace TransportGame.Business -{ - public static class BiomeManager - { - private static List biomes = new List(); - - /// - /// Gets all the loaded biomes - /// - public static IEnumerable Biomes - { - get - { - return biomes; - } - } - - /// - /// Loads the biomes from the Biome directory. - /// - public static void LoadBiomes() - { - foreach (var file in Directory.GetFiles(ConfigurationManager.BiomeDirectory, "*.xml", SearchOption.AllDirectories)) - { - try - { - // Open file - var stream = File.OpenRead(file); - - // Try to deserialize biome - XmlSerializer serializer = new XmlSerializer(typeof(Biome)); - var biome = (Biome)serializer.Deserialize(stream); - - // Add it to biome list - biomes.Add(biome); - Logger.Info("Loaded biome '{0}' from file '{1}'.", biome.Name, file); - } - catch (Exception ex) - { - Logger.Exception(ex); - } - } - } - } -} +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Xml.Serialization; +using TransportGame.Model; +using TransportGame.Utils; + +namespace TransportGame.Business +{ + public static class BiomeManager + { + private static List biomes = new List(); + + /// + /// Gets all the loaded biomes + /// + public static IEnumerable Biomes + { + get + { + return biomes; + } + } + + /// + /// Loads the biomes from the Biome directory. + /// + public static void LoadBiomes() + { + foreach (var file in Directory.GetFiles(ConfigurationManager.BiomeDirectory, "*.xml", SearchOption.AllDirectories)) + { + try + { + // Open file + var stream = File.OpenRead(file); + + // Try to deserialize biome + XmlSerializer serializer = new XmlSerializer(typeof(Biome)); + var biome = (Biome)serializer.Deserialize(stream); + + // Add it to biome list + biomes.Add(biome); + Logger.Info("Loaded biome '{0}' from file '{1}'.", biome.Name, file); + } + catch (Exception ex) + { + Logger.Exception(ex); + } + } + } + } +} diff --git a/Game/Assets/Scripts/Noise/NoiseGenerator.cs b/Game/Assets/Scripts/Noise/NoiseGenerator.cs index 8198a6d..e854c89 100644 --- a/Game/Assets/Scripts/Noise/NoiseGenerator.cs +++ b/Game/Assets/Scripts/Noise/NoiseGenerator.cs @@ -1,137 +1,137 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using UnityEngine; -using TransportGame.Utils; - -namespace TransportGame.Noise -{ - public abstract class NoiseGenerator - { - public float Scale { get; set; } - public float Low { get; set; } - public float High { get; set; } - public float NonLinearPower { get; set; } - public int Octaves { get; set; } - public float Persistence { get; set; } - - private float seedX, seedY; - - public NoiseGenerator() - { - System.Random random = new System.Random(); - seedX = random.NextSingle(-10000f, 10000f); - seedY = random.NextSingle(-10000f, 10000f); - - Scale = 1f; - Low = -1f; - High = 1f; - NonLinearPower = 1f; - Octaves = 1; - Persistence = .5f; - } - - /// - /// Produces noise in the [-1,1] interval - /// - /// - /// - /// - protected abstract float GenerateNoise(float x, float y); - - /// - /// Generates noise - /// - /// X coordinate - /// Y coordinate - /// Minimum value - /// Maximum value - /// Scale - /// Number of octaves (layers) - /// Persistence (impact of each layer) - /// Non-linearity - /// Noise - public float Generate(float x, float y, float low, float high, float scale, int octaves, float persistence, float nonLinearPower) - { - float value = 0; - int freq = 1; - float amp = 1; - float maxAmp = 0; - - for (int i = 0; i < octaves; ++i) - { - value += GenerateNoise(seedX + freq * scale * x, seedY + freq * scale * y) * amp; - maxAmp += amp; - amp *= persistence; - freq *= 2; - } - - // Bring to [0,1] - value = (value / maxAmp) / 2f + .5f; - - // Raise to non-linear power - value = Mathf.Pow(value, nonLinearPower); - - // Bring to required interval - value = value * (high - low) + low; - - // Done - return value; - } - - /// - /// Generates noise - /// - /// X coordinate - /// Y coordinate - /// Minimum value - /// Maximum value - /// Scale - /// Number of octaves (layers) - /// Persistence (impact of each layer) - /// Noise - public float Generate(float x, float y, float low, float high, float scale, int octaves, float persistence) - { - return Generate(x, y, low, high, scale, octaves, persistence, NonLinearPower); - } - - /// - /// Generates noise - /// - /// X coordinate - /// Y coordinate - /// Minimum value - /// Maximum value - /// Scale - /// Noise - public float Generate(float x, float y, float low, float high, float scale) - { - return Generate(x, y, low, high, scale, Octaves, Persistence, NonLinearPower); - } - - /// - /// Generates noise - /// - /// X coordinate - /// Y coordinate - /// Minimum value - /// Maximum value - /// Noise - public float Generate(float x, float y, float low, float high) - { - return Generate(x, y, low, high, Scale, Octaves, Persistence, NonLinearPower); - } - - /// - /// Generates noise - /// - /// X coordinate - /// Y coordinate - /// Noise - public float Generate(float x, float y) - { - return Generate(x, y, Low, High, Scale, Octaves, Persistence, NonLinearPower); - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using UnityEngine; +using TransportGame.Utils; + +namespace TransportGame.Noise +{ + public abstract class NoiseGenerator + { + public float Scale { get; set; } + public float Low { get; set; } + public float High { get; set; } + public float NonLinearPower { get; set; } + public int Octaves { get; set; } + public float Persistence { get; set; } + + private float seedX, seedY; + + public NoiseGenerator() + { + System.Random random = new System.Random(); + seedX = random.NextSingle(-10000f, 10000f); + seedY = random.NextSingle(-10000f, 10000f); + + Scale = 1f; + Low = -1f; + High = 1f; + NonLinearPower = 1f; + Octaves = 1; + Persistence = .5f; + } + + /// + /// Produces noise in the [-1,1] interval + /// + /// + /// + /// + protected abstract float GenerateNoise(float x, float y); + + /// + /// Generates noise + /// + /// X coordinate + /// Y coordinate + /// Minimum value + /// Maximum value + /// Scale + /// Number of octaves (layers) + /// Persistence (impact of each layer) + /// Non-linearity + /// Noise + public float Generate(float x, float y, float low, float high, float scale, int octaves, float persistence, float nonLinearPower) + { + float value = 0; + int freq = 1; + float amp = 1; + float maxAmp = 0; + + for (int i = 0; i < octaves; ++i) + { + value += GenerateNoise(seedX + freq * scale * x, seedY + freq * scale * y) * amp; + maxAmp += amp; + amp *= persistence; + freq *= 2; + } + + // Bring to [0,1] + value = (value / maxAmp) / 2f + .5f; + + // Raise to non-linear power + value = Mathf.Pow(value, nonLinearPower); + + // Bring to required interval + value = value * (high - low) + low; + + // Done + return value; + } + + /// + /// Generates noise + /// + /// X coordinate + /// Y coordinate + /// Minimum value + /// Maximum value + /// Scale + /// Number of octaves (layers) + /// Persistence (impact of each layer) + /// Noise + public float Generate(float x, float y, float low, float high, float scale, int octaves, float persistence) + { + return Generate(x, y, low, high, scale, octaves, persistence, NonLinearPower); + } + + /// + /// Generates noise + /// + /// X coordinate + /// Y coordinate + /// Minimum value + /// Maximum value + /// Scale + /// Noise + public float Generate(float x, float y, float low, float high, float scale) + { + return Generate(x, y, low, high, scale, Octaves, Persistence, NonLinearPower); + } + + /// + /// Generates noise + /// + /// X coordinate + /// Y coordinate + /// Minimum value + /// Maximum value + /// Noise + public float Generate(float x, float y, float low, float high) + { + return Generate(x, y, low, high, Scale, Octaves, Persistence, NonLinearPower); + } + + /// + /// Generates noise + /// + /// X coordinate + /// Y coordinate + /// Noise + public float Generate(float x, float y) + { + return Generate(x, y, Low, High, Scale, Octaves, Persistence, NonLinearPower); + } + } +} diff --git a/Game/Assets/Scripts/Noise/PerlinNoiseGenerator.cs b/Game/Assets/Scripts/Noise/PerlinNoiseGenerator.cs index c6193e8..699e13b 100644 --- a/Game/Assets/Scripts/Noise/PerlinNoiseGenerator.cs +++ b/Game/Assets/Scripts/Noise/PerlinNoiseGenerator.cs @@ -1,16 +1,16 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using UnityEngine; - -namespace TransportGame.Noise -{ - public class PerlinNoiseGenerator : NoiseGenerator - { - protected override float GenerateNoise(float x, float y) - { - return Mathf.PerlinNoise(x, y) * 2 - 1.0f; - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using UnityEngine; + +namespace TransportGame.Noise +{ + public class PerlinNoiseGenerator : NoiseGenerator + { + protected override float GenerateNoise(float x, float y) + { + return Mathf.PerlinNoise(x, y) * 2 - 1.0f; + } + } +}