2023-10-05 18:17:48 +02:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Diagnostics;
|
2023-10-23 21:22:53 +02:00
|
|
|
using System.Linq;
|
2023-11-01 16:02:39 +01:00
|
|
|
using Godot;
|
2023-10-14 21:54:06 +02:00
|
|
|
using Godot.Collections;
|
2023-10-05 18:17:48 +02:00
|
|
|
|
|
|
|
public class World : Spatial
|
|
|
|
{
|
2023-10-23 21:22:53 +02:00
|
|
|
public enum GenerationState
|
|
|
|
{
|
|
|
|
Undefined,
|
|
|
|
Heightmap,
|
|
|
|
TileType,
|
|
|
|
Objects,
|
|
|
|
Done
|
|
|
|
}
|
2023-10-05 18:17:48 +02:00
|
|
|
|
|
|
|
// constants
|
2023-11-05 21:46:28 +01:00
|
|
|
public const int ChunkSize = 10;
|
2023-10-30 22:20:32 +01:00
|
|
|
public const int NumChunkRows = 3;
|
|
|
|
public const int NumChunkColumns = NumChunkRows;
|
2023-11-05 21:46:28 +01:00
|
|
|
private static readonly Color RockColor = new(0.5f, 0.5f, 0.4f);
|
|
|
|
private static readonly Color GrassColor = new(0, 0.4f, 0);
|
|
|
|
private static readonly Color DarkGrassColor = new(0.05882353f, 0.5411765f, 0.05882353f);
|
|
|
|
private static readonly Color LightWaterColor = new(0.05882353f, 0.05882353f, 0.8627451f);
|
|
|
|
|
2023-11-01 16:02:39 +01:00
|
|
|
private readonly List<Vector2> _addedChunkIndices = new();
|
|
|
|
private readonly Godot.Collections.Dictionary<Vector2, WorldChunk> _cachedWorldChunks;
|
2023-11-01 17:59:43 +01:00
|
|
|
private readonly Image _heightmapImage = new();
|
|
|
|
private readonly List<Vector2> _removedChunkIndices = new();
|
|
|
|
private readonly Image _tileTypeMapImage = new();
|
|
|
|
|
|
|
|
// referenced scenes
|
|
|
|
private readonly PackedScene _worldChunkScene = GD.Load<PackedScene>("res://scenes/WorldChunk.tscn");
|
|
|
|
|
|
|
|
private List<Vector2> _activeChunkIndices = new();
|
2023-11-01 16:02:39 +01:00
|
|
|
private Rect2 _centerChunkRect;
|
2023-10-05 18:17:48 +02:00
|
|
|
|
|
|
|
// delegate void OnCoordClicked(Vector2 world_pos);
|
|
|
|
|
|
|
|
// other members
|
|
|
|
private Vector2 _centerPlaneCoord;
|
2023-11-05 21:46:28 +01:00
|
|
|
private Array<Spatial> _grassAssets;
|
2023-11-01 17:59:43 +01:00
|
|
|
private ImageTexture _heightmapTexture;
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-11-01 17:59:43 +01:00
|
|
|
private OpenSimplexNoise _noiseGenerator = new();
|
2023-11-05 21:46:28 +01:00
|
|
|
private Array<Spatial> _rockAssets;
|
2023-11-01 16:02:39 +01:00
|
|
|
private TileInstanceManager _tileInstanceManager;
|
2023-11-05 21:46:28 +01:00
|
|
|
private Array<Spatial> _treeAssets;
|
2023-11-01 16:02:39 +01:00
|
|
|
private ImageTexture _viewTileTypeTexture;
|
|
|
|
public Vector2 CenterChunkIndex = Vector2.Zero;
|
|
|
|
public Spatial Chunks;
|
|
|
|
public Color DebugColor;
|
|
|
|
public HexGrid HexGrid = new();
|
|
|
|
public int Seed = 0;
|
|
|
|
|
|
|
|
public GenerationState State = GenerationState.Done;
|
2023-11-01 17:59:43 +01:00
|
|
|
public Vector2 WorldTextureCoordinateOffset;
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-10-05 18:17:48 +02:00
|
|
|
public World()
|
|
|
|
{
|
|
|
|
Debug.Assert(ChunkSize % 2 == 0);
|
|
|
|
|
2023-10-14 21:54:06 +02:00
|
|
|
_cachedWorldChunks = new Godot.Collections.Dictionary<Vector2, WorldChunk>();
|
2023-10-05 18:17:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called when the node enters the scene tree for the first time.
|
|
|
|
public override void _Ready()
|
|
|
|
{
|
|
|
|
Chunks = (Spatial)FindNode("Chunks");
|
|
|
|
Debug.Assert(Chunks != null);
|
2023-10-23 21:22:53 +02:00
|
|
|
|
2023-11-01 16:02:39 +01:00
|
|
|
_tileInstanceManager = (TileInstanceManager)FindNode("TileInstanceManager");
|
|
|
|
Debug.Assert(_tileInstanceManager != null);
|
|
|
|
|
2023-10-23 21:22:53 +02:00
|
|
|
InitNoiseGenerator();
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-11-05 21:46:28 +01:00
|
|
|
GetNode<Spatial>("Assets").Visible = false;
|
|
|
|
|
|
|
|
_rockAssets = new Array<Spatial>();
|
|
|
|
foreach (Spatial asset in GetNode<Node>("Assets/Rocks").GetChildren()) _rockAssets.Add(asset);
|
|
|
|
|
|
|
|
_grassAssets = new Array<Spatial>();
|
|
|
|
foreach (Spatial asset in GetNode<Node>("Assets/Grass").GetChildren()) _grassAssets.Add(asset);
|
|
|
|
|
|
|
|
_treeAssets = new Array<Spatial>();
|
|
|
|
foreach (Spatial asset in GetNode<Node>("Assets/Trees").GetChildren()) _treeAssets.Add(asset);
|
|
|
|
|
2023-10-05 18:17:48 +02:00
|
|
|
SetCenterPlaneCoord(Vector2.Zero);
|
|
|
|
}
|
|
|
|
|
2023-10-23 21:22:53 +02:00
|
|
|
public void InitNoiseGenerator()
|
|
|
|
{
|
2023-11-01 17:43:47 +01:00
|
|
|
_noiseGenerator = new OpenSimplexNoise();
|
2023-10-23 21:22:53 +02:00
|
|
|
|
2023-11-01 17:43:47 +01:00
|
|
|
_noiseGenerator.Seed = Seed;
|
|
|
|
_noiseGenerator.Octaves = 1;
|
|
|
|
_noiseGenerator.Period = 10;
|
|
|
|
_noiseGenerator.Persistence = 0.5f;
|
|
|
|
_noiseGenerator.Lacunarity = 2;
|
2023-10-23 21:22:53 +02:00
|
|
|
}
|
|
|
|
|
2023-10-05 18:17:48 +02:00
|
|
|
public WorldChunk GetOrCreateWorldChunk(int xIndex, int yIndex, Color debugColor)
|
|
|
|
{
|
2023-11-01 17:59:43 +01:00
|
|
|
if (IsChunkCached(xIndex, yIndex))
|
2023-10-05 18:17:48 +02:00
|
|
|
{
|
2023-11-01 16:02:39 +01:00
|
|
|
var cachedChunk = _cachedWorldChunks[new Vector2(xIndex, yIndex)];
|
2023-10-05 18:17:48 +02:00
|
|
|
return cachedChunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CreateWorldChunk(xIndex, yIndex, debugColor);
|
|
|
|
}
|
|
|
|
|
2023-11-01 17:59:43 +01:00
|
|
|
private bool IsChunkCached(int xIndex, int yIndex)
|
2023-10-08 21:38:49 +02:00
|
|
|
{
|
2023-10-14 21:54:06 +02:00
|
|
|
return _cachedWorldChunks.ContainsKey(new Vector2(xIndex, yIndex));
|
2023-10-08 21:38:49 +02:00
|
|
|
}
|
|
|
|
|
2023-10-05 18:17:48 +02:00
|
|
|
private WorldChunk CreateWorldChunk(int xIndex, int yIndex, Color debugColor)
|
|
|
|
{
|
2023-11-01 16:02:39 +01:00
|
|
|
var result = (WorldChunk)_worldChunkScene.Instance();
|
2023-10-23 21:22:53 +02:00
|
|
|
result.SetSize(ChunkSize);
|
2023-10-05 18:17:48 +02:00
|
|
|
|
2023-11-01 16:02:39 +01:00
|
|
|
var offsetCoordSouthWest = new Vector2(xIndex, yIndex) * ChunkSize;
|
|
|
|
var offsetCoordNorthEast = offsetCoordSouthWest + new Vector2(1, 1) * (ChunkSize - 1);
|
2023-10-05 18:17:48 +02:00
|
|
|
|
2023-11-01 16:02:39 +01:00
|
|
|
var planeCoordSouthWest = HexGrid.GetHexCenterFromOffset(offsetCoordSouthWest) +
|
|
|
|
new Vector2(-HexGrid.HexSize.x, HexGrid.HexSize.y) * 0.5f;
|
|
|
|
var planeCoordNorthEast = HexGrid.GetHexCenterFromOffset(offsetCoordNorthEast) +
|
|
|
|
new Vector2(HexGrid.HexSize.x, -HexGrid.HexSize.y) * 0.5f;
|
2023-10-05 18:17:48 +02:00
|
|
|
|
2023-11-01 16:02:39 +01:00
|
|
|
result.ChunkIndex = new Vector2(xIndex, yIndex);
|
2023-10-05 18:17:48 +02:00
|
|
|
result.PlaneRect = new Rect2(
|
|
|
|
new Vector2(planeCoordSouthWest.x, planeCoordNorthEast.y),
|
|
|
|
new Vector2(planeCoordNorthEast.x - planeCoordSouthWest.x, planeCoordSouthWest.y - planeCoordNorthEast.y));
|
|
|
|
|
|
|
|
result.DebugColor = debugColor;
|
|
|
|
result.DebugColor.a = 0.6f;
|
|
|
|
|
|
|
|
Chunks.AddChild(result);
|
2023-11-01 16:02:39 +01:00
|
|
|
var chunkIndex = new Vector2(xIndex, yIndex);
|
2023-10-08 21:38:49 +02:00
|
|
|
_cachedWorldChunks.Add(chunkIndex, result);
|
2023-10-05 18:17:48 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-11-05 21:46:28 +01:00
|
|
|
|
|
|
|
private bool IsColorEqualApprox(Color colorA, Color colorB)
|
|
|
|
{
|
|
|
|
var colorDifference = new Vector3(colorA.r - colorB.r, colorA.g - colorB.g, colorA.b - colorB.b);
|
|
|
|
return colorDifference.LengthSquared() < 0.1 * 0.1;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Spatial SelectAsset(Vector2 offsetCoord, Array<Spatial> assets, Random randomGenerator, double probability)
|
|
|
|
{
|
|
|
|
if (randomGenerator.NextDouble() < 1.0 - probability) return null;
|
|
|
|
|
|
|
|
var assetIndex = randomGenerator.Next(assets.Count);
|
|
|
|
var assetInstance = (Spatial)assets[assetIndex].Duplicate();
|
|
|
|
var assetTransform = Transform.Identity;
|
|
|
|
assetTransform.origin = HexGrid.GetHexCenterVec3FromOffset(offsetCoord);
|
|
|
|
// TODO: assetTransform.origin.y = GetHeightAtOffset(offsetCoord);
|
|
|
|
assetTransform.origin.y = 0;
|
|
|
|
assetTransform.basis =
|
|
|
|
assetTransform.basis.Rotated(Vector3.Up, (float)(randomGenerator.NextDouble() * Mathf.Pi * 2));
|
|
|
|
assetInstance.Transform = assetTransform;
|
|
|
|
|
|
|
|
return assetInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void PopulateChunk(WorldChunk chunk)
|
|
|
|
{
|
|
|
|
var environmentRandom = new Random(Seed);
|
|
|
|
|
|
|
|
var tileTypeImage = chunk.TileTypeOffscreenViewport.GetTexture().GetData();
|
|
|
|
tileTypeImage.Lock();
|
|
|
|
|
|
|
|
foreach (var textureCoordU in Enumerable.Range(0, chunk.Size))
|
|
|
|
foreach (var textureCoordV in Enumerable.Range(0, chunk.Size))
|
|
|
|
{
|
|
|
|
var colorValue = tileTypeImage.GetPixel(textureCoordU, textureCoordV);
|
|
|
|
var textureCoord = new Vector2(textureCoordU, textureCoordV);
|
|
|
|
var offsetCoord = chunk.ChunkIndex * ChunkSize + textureCoord;
|
|
|
|
|
|
|
|
if (IsColorEqualApprox(colorValue, RockColor))
|
|
|
|
{
|
|
|
|
var rockAsset = SelectAsset(offsetCoord, _rockAssets, environmentRandom, 0.15);
|
|
|
|
if (rockAsset != null) chunk.Entities.AddChild(rockAsset);
|
|
|
|
// TODO: MarkCellUnwalkable(cell);
|
|
|
|
}
|
|
|
|
else if (IsColorEqualApprox(colorValue, GrassColor) || IsColorEqualApprox(colorValue, DarkGrassColor))
|
|
|
|
{
|
|
|
|
var grassAsset = SelectAsset(offsetCoord, _grassAssets, environmentRandom, 0.15);
|
|
|
|
if (grassAsset != null) chunk.Entities.AddChild(grassAsset);
|
|
|
|
|
|
|
|
var treeAsset = SelectAsset(offsetCoord, _treeAssets, environmentRandom, 0.05);
|
|
|
|
if (treeAsset != null) chunk.Entities.AddChild(treeAsset);
|
|
|
|
// TODO: MarkCellUnwalkable(cell);
|
|
|
|
// else if (environmentRandom.NextDouble() < 0.01)
|
|
|
|
// {
|
|
|
|
// var chestAsset = (Chest)_chestScene.Instance();
|
|
|
|
// var assetTransform = Transform.Identity;
|
|
|
|
// assetTransform.origin = GetHexCenterFromOffset(offsetCoord);
|
|
|
|
// assetTransform.origin.y = GetHeightAtOffset(offsetCoord);
|
|
|
|
// chestAsset.Transform = assetTransform;
|
|
|
|
// Entities.AddChild(chestAsset);
|
|
|
|
// MarkCellUnwalkable(cell);
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
// else if (IsColorWater(colorValue))
|
|
|
|
// {
|
|
|
|
// MarkCellUnwalkable(cell);
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
tileTypeImage.Unlock();
|
|
|
|
}
|
|
|
|
|
2023-10-05 18:17:48 +02:00
|
|
|
public void UpdateCenterChunkFromPlaneCoord(Vector2 planeCoord)
|
|
|
|
{
|
2023-10-23 21:22:53 +02:00
|
|
|
if (State != GenerationState.Done)
|
|
|
|
{
|
|
|
|
GD.PrintErr("Cannot update chunk to new planeCoord " + planeCoord + ": Chunk generation not yet finished!");
|
|
|
|
return;
|
|
|
|
}
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-10-08 21:38:49 +02:00
|
|
|
// mark all chunks as retired
|
2023-10-14 21:54:06 +02:00
|
|
|
Godot.Collections.Dictionary<Vector2, WorldChunk> oldCachedChunks = new(_cachedWorldChunks);
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-10-08 21:38:49 +02:00
|
|
|
// set new center chunk
|
|
|
|
var chunkIndex = GetChunkTupleFromPlaneCoord(planeCoord);
|
2023-10-30 22:20:32 +01:00
|
|
|
CenterChunkIndex = new Vector2(chunkIndex.Item1, chunkIndex.Item2);
|
2023-11-01 16:02:39 +01:00
|
|
|
|
|
|
|
var currentChunk = GetOrCreateWorldChunk(chunkIndex.Item1, chunkIndex.Item2,
|
2023-10-05 18:17:48 +02:00
|
|
|
new Color(GD.Randf(), GD.Randf(), GD.Randf()));
|
2023-10-23 21:22:53 +02:00
|
|
|
_centerChunkRect = currentChunk.PlaneRect;
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-10-08 21:38:49 +02:00
|
|
|
// load or create adjacent chunks
|
2023-10-14 21:54:06 +02:00
|
|
|
_activeChunkIndices = new List<Vector2>();
|
|
|
|
_activeChunkIndices.Add(new Vector2(chunkIndex.Item1 - 1, chunkIndex.Item2 - 1));
|
|
|
|
_activeChunkIndices.Add(new Vector2(chunkIndex.Item1, chunkIndex.Item2 - 1));
|
|
|
|
_activeChunkIndices.Add(new Vector2(chunkIndex.Item1 + 1, chunkIndex.Item2 - 1));
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-10-14 21:54:06 +02:00
|
|
|
_activeChunkIndices.Add(new Vector2(chunkIndex.Item1 - 1, chunkIndex.Item2));
|
|
|
|
_activeChunkIndices.Add(new Vector2(chunkIndex.Item1, chunkIndex.Item2));
|
|
|
|
_activeChunkIndices.Add(new Vector2(chunkIndex.Item1 + 1, chunkIndex.Item2));
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-10-14 21:54:06 +02:00
|
|
|
_activeChunkIndices.Add(new Vector2(chunkIndex.Item1 - 1, chunkIndex.Item2 + 1));
|
|
|
|
_activeChunkIndices.Add(new Vector2(chunkIndex.Item1, chunkIndex.Item2 + 1));
|
|
|
|
_activeChunkIndices.Add(new Vector2(chunkIndex.Item1 + 1, chunkIndex.Item2 + 1));
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-10-30 22:20:32 +01:00
|
|
|
Debug.Assert(_activeChunkIndices.Count == NumChunkRows * NumChunkColumns);
|
2023-11-01 16:02:39 +01:00
|
|
|
|
|
|
|
foreach (var activeChunkIndex in _activeChunkIndices)
|
|
|
|
GetOrCreateWorldChunk((int)activeChunkIndex.x, (int)activeChunkIndex.y,
|
|
|
|
new Color(GD.Randf(), GD.Randf(), GD.Randf()));
|
2023-10-08 21:38:49 +02:00
|
|
|
|
|
|
|
// unload retired chunks
|
|
|
|
_removedChunkIndices.Clear();
|
|
|
|
_addedChunkIndices.Clear();
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-10-08 21:38:49 +02:00
|
|
|
foreach (var cachedChunkKey in oldCachedChunks.Keys)
|
|
|
|
if (!_activeChunkIndices.Contains(cachedChunkKey))
|
|
|
|
RemoveChunk(cachedChunkKey);
|
|
|
|
|
|
|
|
foreach (var chunkKey in _activeChunkIndices)
|
|
|
|
if (!oldCachedChunks.ContainsKey(chunkKey))
|
|
|
|
{
|
2023-11-01 16:02:39 +01:00
|
|
|
_addedChunkIndices.Add(chunkKey);
|
|
|
|
|
|
|
|
var chunk = _cachedWorldChunks[chunkKey];
|
|
|
|
GenerateChunkNoiseMap(chunk);
|
|
|
|
|
|
|
|
State = GenerationState.Heightmap;
|
2023-10-08 21:38:49 +02:00
|
|
|
}
|
2023-11-01 16:02:39 +01:00
|
|
|
}
|
2023-10-23 21:22:53 +02:00
|
|
|
|
2023-11-01 16:02:39 +01:00
|
|
|
private void GenerateChunkNoiseMap(WorldChunk chunk)
|
|
|
|
{
|
|
|
|
var chunkIndex = chunk.ChunkIndex;
|
|
|
|
|
|
|
|
var debugChunkColor = new Color(Mathf.Abs(chunkIndex.x) / 5, Mathf.Abs(chunkIndex.y) / 5,
|
|
|
|
Mathf.RoundToInt(Mathf.Abs(chunkIndex.x + chunkIndex.y)) % 2);
|
|
|
|
|
|
|
|
var noiseImageTexture = new ImageTexture();
|
2023-11-01 17:43:47 +01:00
|
|
|
noiseImageTexture.CreateFromImage(_noiseGenerator.GetImage(ChunkSize, ChunkSize, chunkIndex * ChunkSize),
|
2023-11-01 16:02:39 +01:00
|
|
|
0);
|
|
|
|
|
|
|
|
// Debug Texture
|
|
|
|
var simpleImage = new Image();
|
|
|
|
simpleImage.Create(ChunkSize, ChunkSize, false, Image.Format.Rgb8);
|
|
|
|
simpleImage.Lock();
|
|
|
|
|
|
|
|
foreach (var i in Enumerable.Range(0, ChunkSize))
|
|
|
|
foreach (var j in Enumerable.Range(0, ChunkSize))
|
|
|
|
if ((i + j) % 2 == 0)
|
|
|
|
simpleImage.SetPixelv(new Vector2(i, j), Colors.Aqua);
|
|
|
|
else
|
|
|
|
simpleImage.SetPixelv(new Vector2(i, j), debugChunkColor);
|
|
|
|
|
|
|
|
simpleImage.Unlock();
|
|
|
|
// noiseImageTexture.CreateFromImage(simpleImage, 0);
|
|
|
|
|
|
|
|
chunk.SetNoisemap(noiseImageTexture);
|
2023-10-08 21:38:49 +02:00
|
|
|
}
|
|
|
|
|
2023-10-14 21:54:06 +02:00
|
|
|
private void RemoveChunk(Vector2 cachedChunkKey)
|
2023-10-08 21:38:49 +02:00
|
|
|
{
|
|
|
|
_cachedWorldChunks.Remove(cachedChunkKey);
|
|
|
|
_removedChunkIndices.Add(cachedChunkKey);
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-10-08 21:38:49 +02:00
|
|
|
foreach (WorldChunk chunk in Chunks.GetChildren())
|
2023-11-01 16:02:39 +01:00
|
|
|
if (chunk.ChunkIndex == new Vector2(cachedChunkKey.x, cachedChunkKey.y))
|
2023-10-08 21:38:49 +02:00
|
|
|
chunk.QueueFree();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private Tuple<int, int> GetChunkTupleFromPlaneCoord(Vector2 planeCoord)
|
|
|
|
{
|
2023-11-01 16:02:39 +01:00
|
|
|
var centerOffsetCoord = HexGrid.GetHexAt(planeCoord);
|
|
|
|
var chunkIndexFloat = (centerOffsetCoord.OffsetCoords / ChunkSize).Floor();
|
|
|
|
var chunkIndex = new Tuple<int, int>((int)chunkIndexFloat.x, (int)chunkIndexFloat.y);
|
2023-10-08 21:38:49 +02:00
|
|
|
return chunkIndex;
|
2023-10-05 18:17:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetCenterPlaneCoord(Vector2 centerPlaneCoord)
|
|
|
|
{
|
2023-11-01 16:02:39 +01:00
|
|
|
if (!_centerChunkRect.HasPoint(centerPlaneCoord)) UpdateCenterChunkFromPlaneCoord(centerPlaneCoord);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void UpdateWorldViewTexture()
|
|
|
|
{
|
|
|
|
var worldChunkSize = ChunkSize;
|
|
|
|
var numWorldChunkRows = NumChunkRows;
|
|
|
|
var numWorldChunkColumns = NumChunkColumns;
|
|
|
|
|
|
|
|
_heightmapImage.Create(worldChunkSize * numWorldChunkColumns, worldChunkSize * numWorldChunkRows, false,
|
|
|
|
Image.Format.Rgba8);
|
|
|
|
_tileTypeMapImage.Create(worldChunkSize * numWorldChunkColumns, worldChunkSize * numWorldChunkRows, false,
|
|
|
|
Image.Format.Rgba8);
|
|
|
|
|
|
|
|
var chunkIndexSouthWest = Vector2.Inf;
|
|
|
|
var chunkIndexNorthEast = -Vector2.Inf;
|
|
|
|
|
|
|
|
foreach (var chunkIndex in _activeChunkIndices)
|
2023-10-05 18:17:48 +02:00
|
|
|
{
|
2023-11-01 16:02:39 +01:00
|
|
|
var worldChunk = GetOrCreateWorldChunk((int)chunkIndex.x, (int)chunkIndex.y, Colors.White);
|
|
|
|
|
|
|
|
if (chunkIndex.x <= chunkIndexSouthWest.x && chunkIndex.y <= chunkIndexSouthWest.y)
|
|
|
|
chunkIndexSouthWest = chunkIndex;
|
|
|
|
else if (chunkIndex.x >= chunkIndexNorthEast.x && chunkIndex.y >= chunkIndexNorthEast.y)
|
|
|
|
chunkIndexNorthEast = chunkIndex;
|
|
|
|
|
|
|
|
_heightmapImage.BlendRect(
|
|
|
|
worldChunk.HeightmapOffscreenViewport.GetTexture().GetData(),
|
|
|
|
new Rect2(Vector2.Zero, Vector2.One * worldChunkSize),
|
|
|
|
(chunkIndex - CenterChunkIndex + Vector2.One) * worldChunkSize);
|
|
|
|
|
|
|
|
_tileTypeMapImage.BlendRect(
|
|
|
|
worldChunk.TileTypeOffscreenViewport.GetTexture().GetData(),
|
|
|
|
new Rect2(Vector2.Zero, Vector2.One * worldChunkSize),
|
|
|
|
(chunkIndex - CenterChunkIndex + Vector2.One) * worldChunkSize);
|
2023-10-05 18:17:48 +02:00
|
|
|
}
|
2023-11-01 16:02:39 +01:00
|
|
|
|
|
|
|
_heightmapTexture = new ImageTexture();
|
|
|
|
_heightmapTexture.CreateFromImage(_heightmapImage);
|
|
|
|
|
|
|
|
_viewTileTypeTexture = new ImageTexture();
|
|
|
|
_viewTileTypeTexture.CreateFromImage(_tileTypeMapImage);
|
|
|
|
|
2023-11-01 17:59:43 +01:00
|
|
|
WorldTextureCoordinateOffset = chunkIndexSouthWest * worldChunkSize;
|
2023-11-01 16:02:39 +01:00
|
|
|
|
|
|
|
EmitSignal("OnWorldViewTileTypeImageChanged", _tileTypeMapImage);
|
|
|
|
EmitSignal("OnHeightmapImageChanged", _heightmapImage);
|
2023-10-05 18:17:48 +02:00
|
|
|
}
|
|
|
|
|
2023-11-01 16:02:39 +01:00
|
|
|
public override void _Process(float delta)
|
|
|
|
{
|
2023-11-01 17:43:47 +01:00
|
|
|
var oldState = State;
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-11-01 17:43:47 +01:00
|
|
|
UpdateGenerationState();
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-11-01 17:43:47 +01:00
|
|
|
if (oldState != GenerationState.Done && State == GenerationState.Done)
|
|
|
|
{
|
|
|
|
UpdateWorldViewTexture();
|
2023-11-04 19:15:26 +01:00
|
|
|
|
2023-11-01 17:43:47 +01:00
|
|
|
EmitSignal("OnTilesChanged", _removedChunkIndices.ToArray(), _addedChunkIndices.ToArray());
|
|
|
|
}
|
|
|
|
}
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-11-01 17:43:47 +01:00
|
|
|
private void UpdateGenerationState()
|
|
|
|
{
|
2023-11-01 16:02:39 +01:00
|
|
|
if (State == GenerationState.Heightmap)
|
|
|
|
{
|
|
|
|
var numChunksGeneratingHeightmap = 0;
|
|
|
|
foreach (var chunkIndex in _addedChunkIndices)
|
|
|
|
{
|
|
|
|
var chunk = _cachedWorldChunks[chunkIndex];
|
|
|
|
if (chunk.HeightMapFrameCount > 0) numChunksGeneratingHeightmap++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numChunksGeneratingHeightmap == 0)
|
|
|
|
{
|
|
|
|
// assign height map images
|
|
|
|
foreach (var chunkIndex in _addedChunkIndices)
|
|
|
|
{
|
|
|
|
var chunk = _cachedWorldChunks[chunkIndex];
|
|
|
|
chunk.SetHeightmap(chunk.HeightmapOffscreenViewport.GetTexture());
|
|
|
|
}
|
2023-11-01 17:43:47 +01:00
|
|
|
|
|
|
|
State = GenerationState.TileType;
|
2023-11-01 16:02:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (State == GenerationState.TileType)
|
|
|
|
{
|
2023-11-01 17:43:47 +01:00
|
|
|
var numChunksGeneratingTileType = 0;
|
2023-11-01 16:02:39 +01:00
|
|
|
foreach (var chunkIndex in _addedChunkIndices)
|
|
|
|
{
|
|
|
|
var chunk = _cachedWorldChunks[chunkIndex];
|
2023-11-01 17:43:47 +01:00
|
|
|
if (chunk.TileTypeMapFrameCount > 0) numChunksGeneratingTileType++;
|
2023-11-01 16:02:39 +01:00
|
|
|
}
|
|
|
|
|
2023-11-01 17:59:43 +01:00
|
|
|
if (numChunksGeneratingTileType == 0) State = GenerationState.Objects;
|
2023-11-01 16:02:39 +01:00
|
|
|
}
|
|
|
|
else if (State == GenerationState.Objects)
|
|
|
|
{
|
|
|
|
// generate objects
|
2023-11-05 21:46:28 +01:00
|
|
|
foreach (var chunkIndex in _addedChunkIndices) PopulateChunk(_cachedWorldChunks[chunkIndex]);
|
|
|
|
|
2023-11-01 16:02:39 +01:00
|
|
|
State = GenerationState.Done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ui elements
|
|
|
|
|
|
|
|
// scene nodes
|
|
|
|
|
|
|
|
// resources
|
|
|
|
|
|
|
|
// exports
|
|
|
|
// [Export] public Vector2 Size = new Vector2(1, 1);
|
|
|
|
|
|
|
|
// signals
|
|
|
|
[Signal]
|
|
|
|
private delegate void OnTilesChanged(Array<Vector2> removedChunkIndices, Array<Vector2> addedChunkIndices);
|
|
|
|
|
|
|
|
[Signal]
|
|
|
|
private delegate void OnWorldViewTileTypeImageChanged(Image viewTileTypeImage);
|
|
|
|
|
|
|
|
[Signal]
|
|
|
|
private delegate void OnHeightmapImageChanged(Image heightmapImage);
|
2023-10-05 18:17:48 +02:00
|
|
|
}
|