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