Initial implementation
This commit is contained in:
parent
fa838c1a7f
commit
93632f45fb
49
CubicNoise/Builders/NoiseBuilder.cs
Normal file
49
CubicNoise/Builders/NoiseBuilder.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
19
CubicNoise/Builders/Parameters.cs
Normal file
19
CubicNoise/Builders/Parameters.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
13
CubicNoise/Contracts/INoiseEngine.cs
Normal file
13
CubicNoise/Contracts/INoiseEngine.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
6
CubicNoise/Contracts/IParameterName.cs
Normal file
6
CubicNoise/Contracts/IParameterName.cs
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
namespace CubicNoise.Contracts
|
||||||
|
{
|
||||||
|
public interface IParameterName
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
12
CubicNoise/Extensions.cs
Normal file
12
CubicNoise/Extensions.cs
Normal 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
32
CubicNoise/NoiseEngine.cs
Normal 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
12
CubicNoise/NoiseTypes.cs
Normal 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,
|
||||||
|
}
|
||||||
|
}
|
29
CubicNoise/Noisers/CubicNoiseEngine.cs
Normal file
29
CubicNoise/Noisers/CubicNoiseEngine.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
19
CubicNoise/Noisers/CubicNoiseIntParameters.cs
Normal file
19
CubicNoise/Noisers/CubicNoiseIntParameters.cs
Normal 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()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
20
CubicNoise/Noisers/CubicNoiseParameterKinds.cs
Normal file
20
CubicNoise/Noisers/CubicNoiseParameterKinds.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
27
CubicNoise/Noisers/RandomNumberEngine.cs
Normal file
27
CubicNoise/Noisers/RandomNumberEngine.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
19
CubicNoiseTests/NoiseBuilderTests.cs
Normal file
19
CubicNoiseTests/NoiseBuilderTests.cs
Normal 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))))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user