Refactoring of the Class and new generation Algorithm. Grid generation.
This commit is contained in:
@@ -1,85 +1,130 @@
|
||||
using Godot;
|
||||
using System;
|
||||
using Godot.Collections;
|
||||
|
||||
public partial class World : Node2D
|
||||
{
|
||||
public enum GeneratorAlgorithm
|
||||
{
|
||||
RANDOM,
|
||||
FAST_NOISE_LITE,
|
||||
COMPLEX
|
||||
Random,
|
||||
Simple,
|
||||
Complex
|
||||
}
|
||||
|
||||
|
||||
Vector2I darkGras = new Vector2I(0, 0);
|
||||
Vector2I brightGras = new Vector2I(1, 0);
|
||||
Vector2I flower = new Vector2I(2, 0);
|
||||
TileMapLayer tileMapLayer;
|
||||
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;
|
||||
Vector2 _screenSize;
|
||||
float _worldWidth;
|
||||
float _worldHeight;
|
||||
private Vector2I _tileSize;
|
||||
private Timer _generationTimeout;
|
||||
|
||||
private Timer generationTimeout;
|
||||
[Export] public GeneratorAlgorithm Algorithm = GeneratorAlgorithm.Random;
|
||||
|
||||
[Export] GeneratorAlgorithm algorithm = GeneratorAlgorithm.RANDOM;
|
||||
private void GenerateGrid()
|
||||
{
|
||||
Dictionary grid = new Dictionary();
|
||||
for (int x = 0; x < _worldHeight; x++)
|
||||
{
|
||||
for (int y = 0; y < _worldWidth; y++)
|
||||
{
|
||||
// grid[new Vector2(x, y)] = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Vector2 gridToWorld(Vector2 pos) => pos * _tileSize.X;
|
||||
// private Vector2 worldToGrid(Vector2 pos) => Godot.Mathf.Floor(pos / _tileSize.X);
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
screenSize = GetViewportRect().Size;
|
||||
worldWidth = screenSize.X;
|
||||
worldHeight = screenSize.Y;
|
||||
|
||||
tileMapLayer = GetNode<TileMapLayer>("TileMapLayer");
|
||||
GD.Print("World Node Ready");
|
||||
|
||||
generationTimeout = GetNode<Timer>("GenerationTimeout");
|
||||
|
||||
GD.Print("Getting Screen Size");
|
||||
_screenSize = GetViewportRect().Size;
|
||||
GD.Print("Setting World Size");
|
||||
_worldWidth = _screenSize.X;
|
||||
_worldHeight = _screenSize.Y;
|
||||
|
||||
GD.Print("Getting Tilemap Layer and Setting Tile Size");
|
||||
_tileMapLayer = GetNode<TileMapLayer>("TileMapLayer");
|
||||
_tileSize = _tileMapLayer.TileSet.TileSize;
|
||||
|
||||
GD.Print("Getting Timer");
|
||||
_generationTimeout = GetNode<Timer>("GenerationTimeout");
|
||||
|
||||
// Grid generation
|
||||
GD.Print("Setting Grid Properties");
|
||||
Node2D grid = GetNode<Node2D>("Grid");
|
||||
grid.Set("width", _worldHeight / _tileSize.X);
|
||||
grid.Set("height", _worldHeight / _tileSize.Y);
|
||||
grid.Set("cell_size", _tileSize);
|
||||
GD.Print("Calling generateGrid");
|
||||
grid.Call("generateGrid");
|
||||
|
||||
GD.Print("Randomizing Seed");
|
||||
GD.Randomize();
|
||||
GenerateWorld();
|
||||
|
||||
// World generation
|
||||
GD.Print("Generating World");
|
||||
if (_generationTimeout.IsNodeReady())
|
||||
GenerateWorld();
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
GD.Print(algorithm);
|
||||
|
||||
}
|
||||
// public override void _Process(double delta)
|
||||
// {
|
||||
// GD.Print(algorithm);
|
||||
//
|
||||
// }
|
||||
|
||||
private void GenerateWorld()
|
||||
{
|
||||
if (generationTimeout.IsStopped())
|
||||
try
|
||||
{
|
||||
switch (algorithm)
|
||||
if (_generationTimeout.IsStopped())
|
||||
{
|
||||
case GeneratorAlgorithm.RANDOM:
|
||||
GenerateWorldRandom();
|
||||
break;
|
||||
case GeneratorAlgorithm.FAST_NOISE_LITE:
|
||||
GenerateWorldSimplexNoise();
|
||||
break;
|
||||
case GeneratorAlgorithm.COMPLEX:
|
||||
GenerateWorldComplex();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
switch (Algorithm)
|
||||
{
|
||||
case GeneratorAlgorithm.Random:
|
||||
GenerateWorldRandom();
|
||||
break;
|
||||
case GeneratorAlgorithm.Simple:
|
||||
GenerateWorldSimplexNoise();
|
||||
break;
|
||||
case GeneratorAlgorithm.Complex:
|
||||
GenerateWorldComplex();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
_generationTimeout.Start();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
GD.PrintErr(e);
|
||||
}
|
||||
generationTimeout.Start();
|
||||
}
|
||||
|
||||
private void GenerateWorldRandom()
|
||||
{
|
||||
for (int y = 0; y < worldHeight; y++)
|
||||
for (int y = 0; y < _worldHeight; y++)
|
||||
{
|
||||
for (int x = 0; x < worldWidth; x++)
|
||||
for (int x = 0; x < _worldWidth; x++)
|
||||
{
|
||||
float rnd = GD.Randf();
|
||||
|
||||
if (rnd < .7f)
|
||||
tileMapLayer.SetCell(new Vector2I(x, y), 0, darkGras);
|
||||
_tileMapLayer.SetCell(new Vector2I(x, y), 0, _darkGras);
|
||||
else if (rnd < .9f)
|
||||
tileMapLayer.SetCell(new Vector2I(x, y), 0, brightGras);
|
||||
_tileMapLayer.SetCell(new Vector2I(x, y), 0, _brightGras);
|
||||
else
|
||||
tileMapLayer.SetCell(new Vector2I(x, y), 0, flower);
|
||||
_tileMapLayer.SetCell(new Vector2I(x, y), 0, _flower);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -91,9 +136,9 @@ public partial class World : Node2D
|
||||
noise.Seed = GD.RandRange(int.MinValue, int.MaxValue);
|
||||
noise.FractalOctaves = 2;
|
||||
|
||||
for (int y = 0; y < worldHeight; y++)
|
||||
for (int y = 0; y < _worldHeight; y++)
|
||||
{
|
||||
for (int x = 0; x < worldWidth; x++)
|
||||
for (int x = 0; x < _worldWidth; x++)
|
||||
{
|
||||
var rnd = noise.GetNoise2D(x, y);
|
||||
|
||||
@@ -101,11 +146,11 @@ public partial class World : Node2D
|
||||
// var yRnd = noise.GetNoise1D(y);
|
||||
|
||||
if (rnd < .3f)
|
||||
tileMapLayer.SetCell(new Vector2I(x, y), 0, darkGras);
|
||||
_tileMapLayer.SetCell(new Vector2I(x, y), 0, _darkGras);
|
||||
else if (rnd < .6f)
|
||||
tileMapLayer.SetCell(new Vector2I(x, y), 0, brightGras);
|
||||
_tileMapLayer.SetCell(new Vector2I(x, y), 0, _brightGras);
|
||||
else
|
||||
tileMapLayer.SetCell(new Vector2I(x, y), 0, flower);
|
||||
_tileMapLayer.SetCell(new Vector2I(x, y), 0, _flower);
|
||||
|
||||
|
||||
// if (yRnd < .3f)
|
||||
@@ -118,18 +163,8 @@ public partial class World : Node2D
|
||||
}
|
||||
}
|
||||
|
||||
private int _noiseSeed;
|
||||
[Export] public int NoiseSeed
|
||||
{
|
||||
get => _noiseSeed;
|
||||
set
|
||||
{
|
||||
_noiseSeed = value;
|
||||
GenerateWorld();
|
||||
}
|
||||
}
|
||||
|
||||
private FastNoiseLite.NoiseTypeEnum _noiseType;
|
||||
[ExportGroup("General")]
|
||||
[Export]
|
||||
public FastNoiseLite.NoiseTypeEnum NoiseType
|
||||
{
|
||||
@@ -140,205 +175,235 @@ public partial class World : Node2D
|
||||
GenerateWorld();
|
||||
}
|
||||
}
|
||||
|
||||
[Export] public float NoiseFrequency
|
||||
[Export] public int NoiseSeed
|
||||
{
|
||||
get => noiseFrequency;
|
||||
set
|
||||
get => _noiseSeed;
|
||||
set
|
||||
{
|
||||
noiseFrequency = value;
|
||||
_noiseSeed = value;
|
||||
GenerateWorld();
|
||||
}
|
||||
}
|
||||
|
||||
[Export] public Vector3 NoiseOffset
|
||||
{
|
||||
get => noiseOffset;
|
||||
get => _noiseOffset;
|
||||
set
|
||||
{
|
||||
noiseOffset = value;
|
||||
_noiseOffset = value;
|
||||
GenerateWorld();
|
||||
}
|
||||
}
|
||||
[ExportGroup("Perlin and Simplex")]
|
||||
[Export] public float NoiseFrequency
|
||||
{
|
||||
get => _noiseFrequency;
|
||||
set
|
||||
{
|
||||
_noiseFrequency = value;
|
||||
GenerateWorld();
|
||||
}
|
||||
}
|
||||
|
||||
[Export] public FastNoiseLite.FractalTypeEnum NoiseFractalType
|
||||
{
|
||||
get => noiseFractalType;
|
||||
get => _noiseFractalType;
|
||||
set
|
||||
{
|
||||
noiseFractalType = value;
|
||||
_noiseFractalType = value;
|
||||
GenerateWorld();
|
||||
}
|
||||
}
|
||||
|
||||
[Export] public int NoiseFractalOctaves
|
||||
{
|
||||
get => noiseFractalOctaves;
|
||||
get => _noiseFractalOctaves;
|
||||
set
|
||||
{
|
||||
noiseFractalOctaves = value;
|
||||
_noiseFractalOctaves = value;
|
||||
GenerateWorld();
|
||||
}
|
||||
}
|
||||
|
||||
[Export] public float NoiseFractalLacunarity
|
||||
{
|
||||
get => noiseFractalLacunarity;
|
||||
get => _noiseFractalLacunarity;
|
||||
set
|
||||
{
|
||||
noiseFractalLacunarity = value;
|
||||
_noiseFractalLacunarity = value;
|
||||
GenerateWorld();
|
||||
}
|
||||
}
|
||||
|
||||
[Export] public float NoiseFractalGain
|
||||
{
|
||||
get => noiseFractalGain;
|
||||
get => _noiseFractalGain;
|
||||
set
|
||||
{
|
||||
noiseFractalGain = value;
|
||||
_noiseFractalGain = value;
|
||||
GenerateWorld();
|
||||
}
|
||||
}
|
||||
|
||||
[Export] public float NoiseFractalWeightedStrength
|
||||
{
|
||||
get => noiseFractalWeightedStrength;
|
||||
get => _noiseFractalWeightedStrength;
|
||||
set
|
||||
{
|
||||
noiseFractalWeightedStrength = value;
|
||||
_noiseFractalWeightedStrength = value;
|
||||
GenerateWorld();
|
||||
}
|
||||
}
|
||||
|
||||
[Export] public float NoiseFractalPingPongStrength
|
||||
{
|
||||
get => noiseFractalPingPongStrength;
|
||||
get => _noiseFractalPingPongStrength;
|
||||
set
|
||||
{
|
||||
noiseFractalPingPongStrength = value;
|
||||
_noiseFractalPingPongStrength = value;
|
||||
GenerateWorld();
|
||||
}
|
||||
}
|
||||
|
||||
[ExportGroup("Cellular")]
|
||||
[Export] public float NoiseCellularJitter
|
||||
{
|
||||
get => _noiseCellularJitter;
|
||||
set
|
||||
{
|
||||
_noiseCellularJitter = value;
|
||||
GenerateWorld();
|
||||
}
|
||||
}
|
||||
|
||||
[Export] public float NoiseCellularJitter
|
||||
[Export]
|
||||
public FastNoiseLite.CellularDistanceFunctionEnum NoiseCellularDistanceFunction
|
||||
{
|
||||
get => noiseCellularJitter;
|
||||
set
|
||||
get => _noiseCellularDistanceFunction;
|
||||
set
|
||||
{
|
||||
noiseCellularJitter = value;
|
||||
_noiseCellularDistanceFunction = value;
|
||||
GenerateWorld();
|
||||
}
|
||||
}
|
||||
|
||||
[Export] public FastNoiseLite.CellularReturnTypeEnum NoiseCellularReturnType
|
||||
{
|
||||
get => noiseCellularReturnType;
|
||||
get => _noiseCellularReturnType;
|
||||
set
|
||||
{
|
||||
noiseCellularReturnType = value;
|
||||
_noiseCellularReturnType = value;
|
||||
GenerateWorld();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[ExportGroup("Domain Warp")]
|
||||
[Export] public bool NoiseDomainWarpEnabled
|
||||
{
|
||||
get => noiseDomainWarpEnabled;
|
||||
get => _noiseDomainWarpEnabled;
|
||||
set
|
||||
{
|
||||
noiseDomainWarpEnabled = value;
|
||||
_noiseDomainWarpEnabled = value;
|
||||
GenerateWorld();
|
||||
}
|
||||
}
|
||||
|
||||
[Export] public FastNoiseLite.DomainWarpTypeEnum NoiseDomainWarpType
|
||||
{
|
||||
get => noiseDomainWarpType;
|
||||
get => _noiseDomainWarpType;
|
||||
set
|
||||
{
|
||||
noiseDomainWarpType = value;
|
||||
_noiseDomainWarpType = value;
|
||||
GenerateWorld();
|
||||
}
|
||||
}
|
||||
|
||||
[Export] public float NoiseDomainWarpAmplitude
|
||||
{
|
||||
get => noiseDomainWarpAmplitude;
|
||||
get => _noiseDomainWarpAmplitude;
|
||||
set
|
||||
{
|
||||
noiseDomainWarpAmplitude = value;
|
||||
_noiseDomainWarpAmplitude = value;
|
||||
GenerateWorld();
|
||||
}
|
||||
}
|
||||
|
||||
[Export] public float NoiseDomainWarpFrequency
|
||||
{
|
||||
get => noiseDomainWarpFrequency;
|
||||
get => _noiseDomainWarpFrequency;
|
||||
set
|
||||
{
|
||||
noiseDomainWarpFrequency = value;
|
||||
_noiseDomainWarpFrequency = value;
|
||||
GenerateWorld();
|
||||
}
|
||||
}
|
||||
|
||||
[Export] public FastNoiseLite.DomainWarpFractalTypeEnum NoiseDomainWarpFractalType
|
||||
{
|
||||
get => noiseDomainWarpFractalType;
|
||||
get => _noiseDomainWarpFractalType;
|
||||
set
|
||||
{
|
||||
noiseDomainWarpFractalType = value;
|
||||
_noiseDomainWarpFractalType = value;
|
||||
GenerateWorld();
|
||||
}
|
||||
}
|
||||
|
||||
[Export] public int NoiseDomainWarpFractalOctaves
|
||||
{
|
||||
get => noiseDomainWarpFractalOctaves;
|
||||
get => _noiseDomainWarpFractalOctaves;
|
||||
set
|
||||
{
|
||||
noiseDomainWarpFractalOctaves = value;
|
||||
_noiseDomainWarpFractalOctaves = value;
|
||||
GenerateWorld();
|
||||
}
|
||||
}
|
||||
|
||||
[Export] public float NoiseDomainWarpFractalLacunarity
|
||||
{
|
||||
get => noiseDomainWarpFractalLacunarity;
|
||||
get => _noiseDomainWarpFractalLacunarity;
|
||||
set
|
||||
{
|
||||
noiseDomainWarpFractalLacunarity = value;
|
||||
_noiseDomainWarpFractalLacunarity = value;
|
||||
GenerateWorld();
|
||||
}
|
||||
}
|
||||
|
||||
[Export] public float NoiseDomainWarpFractalGain
|
||||
{
|
||||
get => noiseDomainWarpFractalGain;
|
||||
get => _noiseDomainWarpFractalGain;
|
||||
set
|
||||
{
|
||||
noiseDomainWarpFractalGain = value;
|
||||
_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;
|
||||
|
||||
// General
|
||||
private int _noiseSeed;
|
||||
private FastNoiseLite.NoiseTypeEnum _noiseType;
|
||||
|
||||
// Simplex and Perlin
|
||||
private float _noiseFrequency = 0.01f;
|
||||
private Vector3 _noiseOffset = new(0.0f, 0.0f, 0.0f);
|
||||
private FastNoiseLite.FractalTypeEnum _noiseFractalType = FastNoiseLite.FractalTypeEnum.Fbm;
|
||||
private int _noiseFractalOctaves = 5;
|
||||
private float _noiseFractalLacunarity = 2.0f;
|
||||
private float _noiseFractalGain = 0.5f;
|
||||
private float _noiseFractalWeightedStrength = 0.0f;
|
||||
private float _noiseFractalPingPongStrength = 2.0f;
|
||||
|
||||
// Cellular
|
||||
private float _noiseCellularJitter = 1.0f;
|
||||
private FastNoiseLite.CellularDistanceFunctionEnum _noiseCellularDistanceFunction;
|
||||
private FastNoiseLite.CellularReturnTypeEnum _noiseCellularReturnType = FastNoiseLite.CellularReturnTypeEnum.Distance;
|
||||
|
||||
// Domain Warp
|
||||
private bool _noiseDomainWarpEnabled = false;
|
||||
private FastNoiseLite.DomainWarpTypeEnum _noiseDomainWarpType = FastNoiseLite.DomainWarpTypeEnum.SimplexReduced;
|
||||
private float _noiseDomainWarpAmplitude = 30.0f;
|
||||
private float _noiseDomainWarpFrequency = 0.05f;
|
||||
private FastNoiseLite.DomainWarpFractalTypeEnum _noiseDomainWarpFractalType =
|
||||
FastNoiseLite.DomainWarpFractalTypeEnum.Progressive;
|
||||
private int _noiseDomainWarpFractalOctaves = 5;
|
||||
private float _noiseDomainWarpFractalLacunarity = 6.0f;
|
||||
private float _noiseDomainWarpFractalGain = 0.5f;
|
||||
|
||||
private void GenerateWorldComplex()
|
||||
{
|
||||
@@ -350,39 +415,39 @@ public partial class World : Node2D
|
||||
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;
|
||||
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 y = 0; y < _worldHeight; y++)
|
||||
{
|
||||
for (int x = 0; x < worldWidth; x++)
|
||||
for (int x = 0; x < _worldWidth; x++)
|
||||
{
|
||||
var rnd = noise.GetNoise2D(x, y);
|
||||
|
||||
if (rnd < .3f)
|
||||
tileMapLayer.SetCell(new Vector2I(x, y), 0, darkGras);
|
||||
_tileMapLayer.SetCell(new Vector2I(x, y), 0, _darkGras);
|
||||
else if (rnd < .6f)
|
||||
tileMapLayer.SetCell(new Vector2I(x, y), 0, brightGras);
|
||||
_tileMapLayer.SetCell(new Vector2I(x, y), 0, _brightGras);
|
||||
else
|
||||
tileMapLayer.SetCell(new Vector2I(x, y), 0, flower);
|
||||
_tileMapLayer.SetCell(new Vector2I(x, y), 0, _flower);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user