389 lines
10 KiB
C#
389 lines
10 KiB
C#
using Godot;
|
|
using System;
|
|
|
|
public partial class World : Node2D
|
|
{
|
|
public enum GeneratorAlgorithm
|
|
{
|
|
RANDOM,
|
|
FAST_NOISE_LITE,
|
|
COMPLEX
|
|
}
|
|
|
|
|
|
Vector2I darkGras = new Vector2I(0, 0);
|
|
Vector2I brightGras = new Vector2I(1, 0);
|
|
Vector2I flower = new Vector2I(2, 0);
|
|
TileMapLayer tileMapLayer;
|
|
|
|
Vector2 screenSize;
|
|
float worldWidth;
|
|
float worldHeight;
|
|
|
|
private Timer generationTimeout;
|
|
|
|
[Export] GeneratorAlgorithm algorithm = GeneratorAlgorithm.RANDOM;
|
|
|
|
public override void _Ready()
|
|
{
|
|
screenSize = GetViewportRect().Size;
|
|
worldWidth = screenSize.X;
|
|
worldHeight = screenSize.Y;
|
|
|
|
tileMapLayer = GetNode<TileMapLayer>("TileMapLayer");
|
|
|
|
generationTimeout = GetNode<Timer>("GenerationTimeout");
|
|
|
|
GD.Randomize();
|
|
GenerateWorld();
|
|
}
|
|
|
|
public override void _Process(double delta)
|
|
{
|
|
GD.Print(algorithm);
|
|
|
|
}
|
|
|
|
private void GenerateWorld()
|
|
{
|
|
if (generationTimeout.IsStopped())
|
|
{
|
|
switch (algorithm)
|
|
{
|
|
case GeneratorAlgorithm.RANDOM:
|
|
GenerateWorldRandom();
|
|
break;
|
|
case GeneratorAlgorithm.FAST_NOISE_LITE:
|
|
GenerateWorldSimplexNoise();
|
|
break;
|
|
case GeneratorAlgorithm.COMPLEX:
|
|
GenerateWorldComplex();
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
generationTimeout.Start();
|
|
}
|
|
|
|
private void GenerateWorldRandom()
|
|
{
|
|
for (int y = 0; y < worldHeight; y++)
|
|
{
|
|
for (int x = 0; x < worldWidth; x++)
|
|
{
|
|
float rnd = GD.Randf();
|
|
|
|
if (rnd < .7f)
|
|
tileMapLayer.SetCell(new Vector2I(x, y), 0, darkGras);
|
|
else if (rnd < .9f)
|
|
tileMapLayer.SetCell(new Vector2I(x, y), 0, brightGras);
|
|
else
|
|
tileMapLayer.SetCell(new Vector2I(x, y), 0, flower);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void GenerateWorldSimplexNoise()
|
|
{
|
|
var noise = new FastNoiseLite();
|
|
|
|
noise.Seed = GD.RandRange(int.MinValue, int.MaxValue);
|
|
noise.FractalOctaves = 2;
|
|
|
|
for (int y = 0; y < worldHeight; y++)
|
|
{
|
|
for (int x = 0; x < worldWidth; x++)
|
|
{
|
|
var rnd = noise.GetNoise2D(x, y);
|
|
|
|
// var xRnd = noise.GetNoise1D(x);
|
|
// var yRnd = noise.GetNoise1D(y);
|
|
|
|
if (rnd < .3f)
|
|
tileMapLayer.SetCell(new Vector2I(x, y), 0, darkGras);
|
|
else if (rnd < .6f)
|
|
tileMapLayer.SetCell(new Vector2I(x, y), 0, brightGras);
|
|
else
|
|
tileMapLayer.SetCell(new Vector2I(x, y), 0, flower);
|
|
|
|
|
|
// if (yRnd < .3f)
|
|
// tileMapLayer.SetCell(new Vector2I(x, y), 0, darkGras);
|
|
// else if (xRnd < .6f)
|
|
// tileMapLayer.SetCell(new Vector2I(x, y), 0, brightGras);
|
|
// else
|
|
// tileMapLayer.SetCell(new Vector2I(x, y), 0, flower);
|
|
}
|
|
}
|
|
}
|
|
|
|
private int _noiseSeed;
|
|
[Export] public int NoiseSeed
|
|
{
|
|
get => _noiseSeed;
|
|
set
|
|
{
|
|
_noiseSeed = value;
|
|
GenerateWorld();
|
|
}
|
|
}
|
|
|
|
private FastNoiseLite.NoiseTypeEnum _noiseType;
|
|
[Export]
|
|
public FastNoiseLite.NoiseTypeEnum NoiseType
|
|
{
|
|
get => _noiseType;
|
|
set
|
|
{
|
|
_noiseType = value;
|
|
GenerateWorld();
|
|
}
|
|
}
|
|
|
|
[Export] public float NoiseFrequency
|
|
{
|
|
get => noiseFrequency;
|
|
set
|
|
{
|
|
noiseFrequency = value;
|
|
GenerateWorld();
|
|
}
|
|
}
|
|
|
|
[Export] public Vector3 NoiseOffset
|
|
{
|
|
get => noiseOffset;
|
|
set
|
|
{
|
|
noiseOffset = value;
|
|
GenerateWorld();
|
|
}
|
|
}
|
|
|
|
[Export] public FastNoiseLite.FractalTypeEnum NoiseFractalType
|
|
{
|
|
get => noiseFractalType;
|
|
set
|
|
{
|
|
noiseFractalType = value;
|
|
GenerateWorld();
|
|
}
|
|
}
|
|
|
|
[Export] public int NoiseFractalOctaves
|
|
{
|
|
get => noiseFractalOctaves;
|
|
set
|
|
{
|
|
noiseFractalOctaves = value;
|
|
GenerateWorld();
|
|
}
|
|
}
|
|
|
|
[Export] public float NoiseFractalLacunarity
|
|
{
|
|
get => noiseFractalLacunarity;
|
|
set
|
|
{
|
|
noiseFractalLacunarity = value;
|
|
GenerateWorld();
|
|
}
|
|
}
|
|
|
|
[Export] public float NoiseFractalGain
|
|
{
|
|
get => noiseFractalGain;
|
|
set
|
|
{
|
|
noiseFractalGain = value;
|
|
GenerateWorld();
|
|
}
|
|
}
|
|
|
|
[Export] public float NoiseFractalWeightedStrength
|
|
{
|
|
get => noiseFractalWeightedStrength;
|
|
set
|
|
{
|
|
noiseFractalWeightedStrength = value;
|
|
GenerateWorld();
|
|
}
|
|
}
|
|
|
|
[Export] public float NoiseFractalPingPongStrength
|
|
{
|
|
get => noiseFractalPingPongStrength;
|
|
set
|
|
{
|
|
noiseFractalPingPongStrength = value;
|
|
GenerateWorld();
|
|
}
|
|
}
|
|
|
|
[Export] public float NoiseCellularJitter
|
|
{
|
|
get => noiseCellularJitter;
|
|
set
|
|
{
|
|
noiseCellularJitter = value;
|
|
GenerateWorld();
|
|
}
|
|
}
|
|
|
|
[Export] public FastNoiseLite.CellularReturnTypeEnum NoiseCellularReturnType
|
|
{
|
|
get => noiseCellularReturnType;
|
|
set
|
|
{
|
|
noiseCellularReturnType = value;
|
|
GenerateWorld();
|
|
}
|
|
}
|
|
|
|
[Export] public bool NoiseDomainWarpEnabled
|
|
{
|
|
get => noiseDomainWarpEnabled;
|
|
set
|
|
{
|
|
noiseDomainWarpEnabled = value;
|
|
GenerateWorld();
|
|
}
|
|
}
|
|
|
|
[Export] public FastNoiseLite.DomainWarpTypeEnum NoiseDomainWarpType
|
|
{
|
|
get => noiseDomainWarpType;
|
|
set
|
|
{
|
|
noiseDomainWarpType = value;
|
|
GenerateWorld();
|
|
}
|
|
}
|
|
|
|
[Export] public float NoiseDomainWarpAmplitude
|
|
{
|
|
get => noiseDomainWarpAmplitude;
|
|
set
|
|
{
|
|
noiseDomainWarpAmplitude = value;
|
|
GenerateWorld();
|
|
}
|
|
}
|
|
|
|
[Export] public float NoiseDomainWarpFrequency
|
|
{
|
|
get => noiseDomainWarpFrequency;
|
|
set
|
|
{
|
|
noiseDomainWarpFrequency = value;
|
|
GenerateWorld();
|
|
}
|
|
}
|
|
|
|
[Export] public FastNoiseLite.DomainWarpFractalTypeEnum NoiseDomainWarpFractalType
|
|
{
|
|
get => noiseDomainWarpFractalType;
|
|
set
|
|
{
|
|
noiseDomainWarpFractalType = value;
|
|
GenerateWorld();
|
|
}
|
|
}
|
|
|
|
[Export] public int NoiseDomainWarpFractalOctaves
|
|
{
|
|
get => noiseDomainWarpFractalOctaves;
|
|
set
|
|
{
|
|
noiseDomainWarpFractalOctaves = value;
|
|
GenerateWorld();
|
|
}
|
|
}
|
|
|
|
[Export] public float NoiseDomainWarpFractalLacunarity
|
|
{
|
|
get => noiseDomainWarpFractalLacunarity;
|
|
set
|
|
{
|
|
noiseDomainWarpFractalLacunarity = value;
|
|
GenerateWorld();
|
|
}
|
|
}
|
|
|
|
[Export] public float NoiseDomainWarpFractalGain
|
|
{
|
|
get => noiseDomainWarpFractalGain;
|
|
set
|
|
{
|
|
noiseDomainWarpFractalGain = value;
|
|
GenerateWorld();
|
|
}
|
|
}
|
|
|
|
public float noiseFrequency;
|
|
public Vector3 noiseOffset;
|
|
public FastNoiseLite.FractalTypeEnum noiseFractalType;
|
|
public int noiseFractalOctaves;
|
|
public float noiseFractalLacunarity;
|
|
public float noiseFractalGain;
|
|
public float noiseFractalWeightedStrength;
|
|
public float noiseFractalPingPongStrength;
|
|
public float noiseCellularJitter;
|
|
public FastNoiseLite.CellularReturnTypeEnum noiseCellularReturnType;
|
|
public bool noiseDomainWarpEnabled;
|
|
public FastNoiseLite.DomainWarpTypeEnum noiseDomainWarpType;
|
|
public float noiseDomainWarpAmplitude;
|
|
public float noiseDomainWarpFrequency;
|
|
public FastNoiseLite.DomainWarpFractalTypeEnum noiseDomainWarpFractalType;
|
|
public int noiseDomainWarpFractalOctaves;
|
|
public float noiseDomainWarpFractalLacunarity;
|
|
public float noiseDomainWarpFractalGain;
|
|
|
|
private void GenerateWorldComplex()
|
|
{
|
|
var noise = new FastNoiseLite();
|
|
if (NoiseSeed == 0)
|
|
noise.Seed = GD.RandRange(int.MinValue, int.MaxValue);
|
|
else
|
|
noise.Seed = NoiseSeed;
|
|
GD.Print(noise.Seed);
|
|
|
|
noise.NoiseType = _noiseType;
|
|
noise.Frequency = noiseFrequency;
|
|
noise.Offset = noiseOffset;
|
|
noise.FractalType = noiseFractalType;
|
|
noise.FractalOctaves = noiseFractalOctaves;
|
|
noise.FractalLacunarity = noiseFractalLacunarity;
|
|
noise.FractalGain = noiseFractalGain;
|
|
noise.FractalWeightedStrength = noiseFractalWeightedStrength;
|
|
noise.FractalPingPongStrength = noiseFractalPingPongStrength;
|
|
noise.CellularJitter = noiseCellularJitter;
|
|
noise.CellularReturnType = noiseCellularReturnType;
|
|
noise.DomainWarpEnabled = noiseDomainWarpEnabled;
|
|
noise.DomainWarpType = noiseDomainWarpType;
|
|
noise.DomainWarpAmplitude = noiseDomainWarpAmplitude;
|
|
noise.DomainWarpFrequency = noiseDomainWarpFrequency;
|
|
noise.DomainWarpFractalType = noiseDomainWarpFractalType;
|
|
noise.DomainWarpFractalOctaves = noiseDomainWarpFractalOctaves;
|
|
noise.DomainWarpFractalLacunarity = noiseDomainWarpFractalLacunarity;
|
|
noise.DomainWarpFractalGain = noiseDomainWarpFractalGain;
|
|
|
|
|
|
|
|
for (int y = 0; y < worldHeight; y++)
|
|
{
|
|
for (int x = 0; x < worldWidth; x++)
|
|
{
|
|
var rnd = noise.GetNoise2D(x, y);
|
|
|
|
if (rnd < .3f)
|
|
tileMapLayer.SetCell(new Vector2I(x, y), 0, darkGras);
|
|
else if (rnd < .6f)
|
|
tileMapLayer.SetCell(new Vector2I(x, y), 0, brightGras);
|
|
else
|
|
tileMapLayer.SetCell(new Vector2I(x, y), 0, flower);
|
|
}
|
|
}
|
|
}
|
|
} |