Minor cleanup.

WorldChunkRefactoring
Martin Felis 2023-11-01 17:43:47 +01:00
parent 36be4bc4c8
commit 6ca776014b
1 changed files with 30 additions and 33 deletions

View File

@ -30,11 +30,12 @@ public class World : Spatial
// other members
private Vector2 _centerPlaneCoord;
private readonly Image _heightmapImage = new();
private ImageTexture _heightmapTexture;
private readonly List<Vector2> _removedChunkIndices = new();
private TileInstanceManager _tileInstanceManager;
private readonly Image _heightmapImage = new();
private ImageTexture _heightmapTexture;
private readonly Image _tileTypeMapImage = new();
private ImageTexture _viewTileTypeTexture;
@ -45,7 +46,7 @@ public class World : Spatial
public Color DebugColor;
public HexGrid HexGrid = new();
private OpenSimplexNoise noiseGenerator = new();
private OpenSimplexNoise _noiseGenerator = new();
public int Seed = 0;
public GenerationState State = GenerationState.Done;
@ -73,13 +74,13 @@ public class World : Spatial
public void InitNoiseGenerator()
{
noiseGenerator = new OpenSimplexNoise();
_noiseGenerator = new OpenSimplexNoise();
noiseGenerator.Seed = Seed;
noiseGenerator.Octaves = 1;
noiseGenerator.Period = 10;
noiseGenerator.Persistence = 0.5f;
noiseGenerator.Lacunarity = 2;
_noiseGenerator.Seed = Seed;
_noiseGenerator.Octaves = 1;
_noiseGenerator.Period = 10;
_noiseGenerator.Persistence = 0.5f;
_noiseGenerator.Lacunarity = 2;
}
public WorldChunk GetOrCreateWorldChunk(int xIndex, int yIndex, Color debugColor)
@ -193,7 +194,7 @@ public class World : Spatial
Mathf.RoundToInt(Mathf.Abs(chunkIndex.x + chunkIndex.y)) % 2);
var noiseImageTexture = new ImageTexture();
noiseImageTexture.CreateFromImage(noiseGenerator.GetImage(ChunkSize, ChunkSize, chunkIndex * ChunkSize),
noiseImageTexture.CreateFromImage(_noiseGenerator.GetImage(ChunkSize, ChunkSize, chunkIndex * ChunkSize),
0);
// Debug Texture
@ -286,18 +287,19 @@ public class World : Spatial
public override void _Process(float delta)
{
if (State == GenerationState.Heightmap)
{
// generate heightmap for all new chunks
foreach (var chunkIndex in _addedChunkIndices)
{
}
State = GenerationState.Heightmap;
}
var oldState = State;
UpdateGenerationState();
if (oldState != GenerationState.Done && State == GenerationState.Done)
{
UpdateWorldViewTexture();
EmitSignal("OnTilesChanged", _removedChunkIndices.ToArray(), _addedChunkIndices.ToArray());
}
}
private void UpdateGenerationState()
{
if (State == GenerationState.Heightmap)
{
var numChunksGeneratingHeightmap = 0;
@ -310,39 +312,34 @@ public class World : Spatial
if (numChunksGeneratingHeightmap == 0)
{
// assign height map images
State = GenerationState.TileType;
foreach (var chunkIndex in _addedChunkIndices)
{
var chunk = _cachedWorldChunks[chunkIndex];
chunk.SetHeightmap(chunk.HeightmapOffscreenViewport.GetTexture());
}
State = GenerationState.TileType;
}
}
else if (State == GenerationState.TileType)
{
// assign tile type images
// generate heightmap for all new chunks
var numChunksGeneratingTileType = 0;
foreach (var chunkIndex in _addedChunkIndices)
{
var chunk = _cachedWorldChunks[chunkIndex];
chunk.SetHeightmap(chunk.HeightmapOffscreenViewport.GetTexture());
if (chunk.TileTypeMapFrameCount > 0) numChunksGeneratingTileType++;
}
State = GenerationState.Objects;
if (numChunksGeneratingTileType == 0)
{
State = GenerationState.Objects;
}
}
else if (State == GenerationState.Objects)
{
// generate objects
State = GenerationState.Done;
}
if (oldState != GenerationState.Done && State == GenerationState.Done)
{
UpdateWorldViewTexture();
EmitSignal("OnTilesChanged", _removedChunkIndices.ToArray(), _addedChunkIndices.ToArray());
}
}
// ui elements