Added files
This commit is contained in:
parent
53dbf25843
commit
7ffa9ac886
@ -1,54 +1,54 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using TransportGame.Model;
|
using TransportGame.Model;
|
||||||
using TransportGame.Utils;
|
using TransportGame.Utils;
|
||||||
|
|
||||||
namespace TransportGame.Business
|
namespace TransportGame.Business
|
||||||
{
|
{
|
||||||
public static class BiomeManager
|
public static class BiomeManager
|
||||||
{
|
{
|
||||||
private static List<Biome> biomes = new List<Biome>();
|
private static List<Biome> biomes = new List<Biome>();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets all the loaded biomes
|
/// Gets all the loaded biomes
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static IEnumerable<Biome> Biomes
|
public static IEnumerable<Biome> Biomes
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return biomes;
|
return biomes;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Loads the biomes from the Biome directory.
|
/// Loads the biomes from the Biome directory.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static void LoadBiomes()
|
public static void LoadBiomes()
|
||||||
{
|
{
|
||||||
foreach (var file in Directory.GetFiles(ConfigurationManager.BiomeDirectory, "*.xml", SearchOption.AllDirectories))
|
foreach (var file in Directory.GetFiles(ConfigurationManager.BiomeDirectory, "*.xml", SearchOption.AllDirectories))
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Open file
|
// Open file
|
||||||
var stream = File.OpenRead(file);
|
var stream = File.OpenRead(file);
|
||||||
|
|
||||||
// Try to deserialize biome
|
// Try to deserialize biome
|
||||||
XmlSerializer serializer = new XmlSerializer(typeof(Biome));
|
XmlSerializer serializer = new XmlSerializer(typeof(Biome));
|
||||||
var biome = (Biome)serializer.Deserialize(stream);
|
var biome = (Biome)serializer.Deserialize(stream);
|
||||||
|
|
||||||
// Add it to biome list
|
// Add it to biome list
|
||||||
biomes.Add(biome);
|
biomes.Add(biome);
|
||||||
Logger.Info("Loaded biome '{0}' from file '{1}'.", biome.Name, file);
|
Logger.Info("Loaded biome '{0}' from file '{1}'.", biome.Name, file);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Logger.Exception(ex);
|
Logger.Exception(ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,137 +1,137 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using TransportGame.Utils;
|
using TransportGame.Utils;
|
||||||
|
|
||||||
namespace TransportGame.Noise
|
namespace TransportGame.Noise
|
||||||
{
|
{
|
||||||
public abstract class NoiseGenerator
|
public abstract class NoiseGenerator
|
||||||
{
|
{
|
||||||
public float Scale { get; set; }
|
public float Scale { get; set; }
|
||||||
public float Low { get; set; }
|
public float Low { get; set; }
|
||||||
public float High { get; set; }
|
public float High { get; set; }
|
||||||
public float NonLinearPower { get; set; }
|
public float NonLinearPower { get; set; }
|
||||||
public int Octaves { get; set; }
|
public int Octaves { get; set; }
|
||||||
public float Persistence { get; set; }
|
public float Persistence { get; set; }
|
||||||
|
|
||||||
private float seedX, seedY;
|
private float seedX, seedY;
|
||||||
|
|
||||||
public NoiseGenerator()
|
public NoiseGenerator()
|
||||||
{
|
{
|
||||||
System.Random random = new System.Random();
|
System.Random random = new System.Random();
|
||||||
seedX = random.NextSingle(-10000f, 10000f);
|
seedX = random.NextSingle(-10000f, 10000f);
|
||||||
seedY = random.NextSingle(-10000f, 10000f);
|
seedY = random.NextSingle(-10000f, 10000f);
|
||||||
|
|
||||||
Scale = 1f;
|
Scale = 1f;
|
||||||
Low = -1f;
|
Low = -1f;
|
||||||
High = 1f;
|
High = 1f;
|
||||||
NonLinearPower = 1f;
|
NonLinearPower = 1f;
|
||||||
Octaves = 1;
|
Octaves = 1;
|
||||||
Persistence = .5f;
|
Persistence = .5f;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Produces noise in the [-1,1] interval
|
/// Produces noise in the [-1,1] interval
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="x"></param>
|
/// <param name="x"></param>
|
||||||
/// <param name="y"></param>
|
/// <param name="y"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
protected abstract float GenerateNoise(float x, float y);
|
protected abstract float GenerateNoise(float x, float y);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Generates noise
|
/// Generates noise
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="x">X coordinate</param>
|
/// <param name="x">X coordinate</param>
|
||||||
/// <param name="y">Y coordinate</param>
|
/// <param name="y">Y coordinate</param>
|
||||||
/// <param name="low">Minimum value</param>
|
/// <param name="low">Minimum value</param>
|
||||||
/// <param name="high">Maximum value</param>
|
/// <param name="high">Maximum value</param>
|
||||||
/// <param name="scale">Scale</param>
|
/// <param name="scale">Scale</param>
|
||||||
/// <param name="octaves">Number of octaves (layers)</param>
|
/// <param name="octaves">Number of octaves (layers)</param>
|
||||||
/// <param name="persistence">Persistence (impact of each layer)</param>
|
/// <param name="persistence">Persistence (impact of each layer)</param>
|
||||||
/// <param name="nonLinearPower">Non-linearity</param>
|
/// <param name="nonLinearPower">Non-linearity</param>
|
||||||
/// <returns>Noise</returns>
|
/// <returns>Noise</returns>
|
||||||
public float Generate(float x, float y, float low, float high, float scale, int octaves, float persistence, float nonLinearPower)
|
public float Generate(float x, float y, float low, float high, float scale, int octaves, float persistence, float nonLinearPower)
|
||||||
{
|
{
|
||||||
float value = 0;
|
float value = 0;
|
||||||
int freq = 1;
|
int freq = 1;
|
||||||
float amp = 1;
|
float amp = 1;
|
||||||
float maxAmp = 0;
|
float maxAmp = 0;
|
||||||
|
|
||||||
for (int i = 0; i < octaves; ++i)
|
for (int i = 0; i < octaves; ++i)
|
||||||
{
|
{
|
||||||
value += GenerateNoise(seedX + freq * scale * x, seedY + freq * scale * y) * amp;
|
value += GenerateNoise(seedX + freq * scale * x, seedY + freq * scale * y) * amp;
|
||||||
maxAmp += amp;
|
maxAmp += amp;
|
||||||
amp *= persistence;
|
amp *= persistence;
|
||||||
freq *= 2;
|
freq *= 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bring to [0,1]
|
// Bring to [0,1]
|
||||||
value = (value / maxAmp) / 2f + .5f;
|
value = (value / maxAmp) / 2f + .5f;
|
||||||
|
|
||||||
// Raise to non-linear power
|
// Raise to non-linear power
|
||||||
value = Mathf.Pow(value, nonLinearPower);
|
value = Mathf.Pow(value, nonLinearPower);
|
||||||
|
|
||||||
// Bring to required interval
|
// Bring to required interval
|
||||||
value = value * (high - low) + low;
|
value = value * (high - low) + low;
|
||||||
|
|
||||||
// Done
|
// Done
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Generates noise
|
/// Generates noise
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="x">X coordinate</param>
|
/// <param name="x">X coordinate</param>
|
||||||
/// <param name="y">Y coordinate</param>
|
/// <param name="y">Y coordinate</param>
|
||||||
/// <param name="low">Minimum value</param>
|
/// <param name="low">Minimum value</param>
|
||||||
/// <param name="high">Maximum value</param>
|
/// <param name="high">Maximum value</param>
|
||||||
/// <param name="scale">Scale</param>
|
/// <param name="scale">Scale</param>
|
||||||
/// <param name="octaves">Number of octaves (layers)</param>
|
/// <param name="octaves">Number of octaves (layers)</param>
|
||||||
/// <param name="persistence">Persistence (impact of each layer)</param>
|
/// <param name="persistence">Persistence (impact of each layer)</param>
|
||||||
/// <returns>Noise</returns>
|
/// <returns>Noise</returns>
|
||||||
public float Generate(float x, float y, float low, float high, float scale, int octaves, float persistence)
|
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);
|
return Generate(x, y, low, high, scale, octaves, persistence, NonLinearPower);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Generates noise
|
/// Generates noise
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="x">X coordinate</param>
|
/// <param name="x">X coordinate</param>
|
||||||
/// <param name="y">Y coordinate</param>
|
/// <param name="y">Y coordinate</param>
|
||||||
/// <param name="low">Minimum value</param>
|
/// <param name="low">Minimum value</param>
|
||||||
/// <param name="high">Maximum value</param>
|
/// <param name="high">Maximum value</param>
|
||||||
/// <param name="scale">Scale</param>
|
/// <param name="scale">Scale</param>
|
||||||
/// <returns>Noise</returns>
|
/// <returns>Noise</returns>
|
||||||
public float Generate(float x, float y, float low, float high, float scale)
|
public float Generate(float x, float y, float low, float high, float scale)
|
||||||
{
|
{
|
||||||
return Generate(x, y, low, high, scale, Octaves, Persistence, NonLinearPower);
|
return Generate(x, y, low, high, scale, Octaves, Persistence, NonLinearPower);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Generates noise
|
/// Generates noise
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="x">X coordinate</param>
|
/// <param name="x">X coordinate</param>
|
||||||
/// <param name="y">Y coordinate</param>
|
/// <param name="y">Y coordinate</param>
|
||||||
/// <param name="low">Minimum value</param>
|
/// <param name="low">Minimum value</param>
|
||||||
/// <param name="high">Maximum value</param>
|
/// <param name="high">Maximum value</param>
|
||||||
/// <returns>Noise</returns>
|
/// <returns>Noise</returns>
|
||||||
public float Generate(float x, float y, float low, float high)
|
public float Generate(float x, float y, float low, float high)
|
||||||
{
|
{
|
||||||
return Generate(x, y, low, high, Scale, Octaves, Persistence, NonLinearPower);
|
return Generate(x, y, low, high, Scale, Octaves, Persistence, NonLinearPower);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Generates noise
|
/// Generates noise
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="x">X coordinate</param>
|
/// <param name="x">X coordinate</param>
|
||||||
/// <param name="y">Y coordinate</param>
|
/// <param name="y">Y coordinate</param>
|
||||||
/// <returns>Noise</returns>
|
/// <returns>Noise</returns>
|
||||||
public float Generate(float x, float y)
|
public float Generate(float x, float y)
|
||||||
{
|
{
|
||||||
return Generate(x, y, Low, High, Scale, Octaves, Persistence, NonLinearPower);
|
return Generate(x, y, Low, High, Scale, Octaves, Persistence, NonLinearPower);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace TransportGame.Noise
|
namespace TransportGame.Noise
|
||||||
{
|
{
|
||||||
public class PerlinNoiseGenerator : NoiseGenerator
|
public class PerlinNoiseGenerator : NoiseGenerator
|
||||||
{
|
{
|
||||||
protected override float GenerateNoise(float x, float y)
|
protected override float GenerateNoise(float x, float y)
|
||||||
{
|
{
|
||||||
return Mathf.PerlinNoise(x, y) * 2 - 1.0f;
|
return Mathf.PerlinNoise(x, y) * 2 - 1.0f;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user