Initial implementation

This commit is contained in:
Thorsten Sommer 2020-01-11 19:00:46 +01:00
parent fa838c1a7f
commit 93632f45fb
12 changed files with 257 additions and 0 deletions

View File

@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Text;
using CubicNoise.Contracts;
namespace CubicNoise.Builders
{
public sealed class NoiseBuilder
{
public NoiseTypes NoiseType { get; set; } = NoiseTypes.UNKNOWN;
public int NoiseSeed { get; set; } = new Random().Next();
public Dictionary<IParameterName, int> NoiseIntParameters = new Dictionary<IParameterName, int>();
private NoiseBuilder()
{
}
public static NoiseBuilder New()
{
return new NoiseBuilder();
}
public NoiseBuilder Type(NoiseTypes type)
{
this.NoiseType = type;
return this;
}
public NoiseBuilder Seed(int seed)
{
this.NoiseSeed = seed;
return this;
}
public NoiseBuilder Seed(string seed)
{
this.NoiseSeed = seed.GetHashCode();
return this;
}
public NoiseBuilder SetParam(IParameterName param, int value)
{
this.NoiseIntParameters[param] = value;
return this;
}
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Text;
using CubicNoise.Noisers;
namespace CubicNoise.Builders
{
public sealed class Parameters
{
private Parameters()
{
}
public static CubicNoiseParameterKinds Cubic(CubicNoiseParameterKinds kind)
{
return kind;
}
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace CubicNoise.Contracts
{
public interface INoiseEngine
{
float Get(float x);
float Get(float x, float y);
}
}

View File

@ -0,0 +1,6 @@
namespace CubicNoise.Contracts
{
public interface IParameterName
{
}
}

12
CubicNoise/Extensions.cs Normal file
View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
using CubicNoise.Noisers;
namespace CubicNoise
{
public static class Extensions
{
}
}

32
CubicNoise/NoiseEngine.cs Normal file
View File

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Text;
using CubicNoise.Builders;
using CubicNoise.Contracts;
using CubicNoise.Noisers;
namespace CubicNoise
{
public sealed class NoiseEngine : INoiseEngine
{
private readonly INoiseEngine engine;
private NoiseEngine(NoiseTypes type, int seed, Dictionary<IParameterName, int> intParameters)
{
this.engine = type switch
{
NoiseTypes.UNKNOWN => new RandomNumberEngine(seed),
NoiseTypes.CUBIC_NOISE => new CubicNoiseEngine(seed, intParameters),
_ => throw new ArgumentOutOfRangeException(nameof(type), type, "The provided noise type is unknown.")
};
}
public static NoiseEngine CreateEngine(NoiseBuilder builder) => new NoiseEngine(builder.NoiseType, builder.NoiseSeed, builder.NoiseIntParameters);
public float Get(float x) => this.engine.Get(x);
public float Get(float x, float y) => this.engine.Get(x, y);
}
}

12
CubicNoise/NoiseTypes.cs Normal file
View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace CubicNoise
{
public enum NoiseTypes
{
UNKNOWN = 0,
CUBIC_NOISE = 1_000,
}
}

View File

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Text;
using CubicNoise.Contracts;
namespace CubicNoise.Noisers
{
public sealed class CubicNoiseEngine : INoiseEngine
{
private Dictionary<IParameterName, int> intParameters;
private int seed;
public CubicNoiseEngine(int seed, Dictionary<IParameterName, int> intParameters)
{
this.intParameters = intParameters;
this.seed = seed;
}
public float Get(float x)
{
throw new NotImplementedException();
}
public float Get(float x, float y)
{
throw new NotImplementedException();
}
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Text;
using CubicNoise.Builders;
using CubicNoise.Contracts;
namespace CubicNoise.Noisers
{
public class CubicNoiseIntParameters : IParameterName
{
public static readonly IParameterName OCTAVE = new CubicNoiseIntParameters();
public static readonly IParameterName PERIOD_X = new CubicNoiseIntParameters();
public static readonly IParameterName PERIOD_Y = new CubicNoiseIntParameters();
private CubicNoiseIntParameters()
{
}
}
}

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Text;
using CubicNoise.Builders;
using CubicNoise.Contracts;
namespace CubicNoise.Noisers
{
public sealed class CubicNoiseParameterKinds
{
private CubicNoiseParameterKinds()
{
}
public static CubicNoiseParameterKinds Int { get; } = new CubicNoiseParameterKinds();
public IParameterName Use(CubicNoiseIntParameters name) => name;
}
}

View File

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Text;
using CubicNoise.Contracts;
namespace CubicNoise.Noisers
{
public sealed class RandomNumberEngine : INoiseEngine
{
private Random rng;
public RandomNumberEngine(int seed)
{
this.rng = new Random(seed);
}
public float Get(float x)
{
return this.rng.Next();
}
public float Get(float x, float y)
{
return this.rng.Next();
}
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Text;
using CubicNoise;
using CubicNoise.Builders;
using CubicNoise.Noisers;
using NUnit.Framework;
namespace CubicNoiseTests
{
public class NoiseBuilderTests
{
[Test]
public void SimpleBuild()
{
var engine = NoiseEngine.CreateEngine(NoiseBuilder.New().Type(NoiseTypes.CUBIC_NOISE).Seed("test seed").SetParam(Parameters.Cubic(CubicNoiseParameterKinds.Int.Use(CubicNoiseIntParameters.OCTAVE))))
}
}
}