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-11-18 22:11:34 +01:00
|
|
|
using Priority_Queue;
|
2023-10-05 18:17:48 +02:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public class World : Spatial {
|
|
|
|
public enum GenerationState {
|
2023-12-14 17:27:07 +01:00
|
|
|
Empty,
|
2023-10-23 21:22:53 +02:00
|
|
|
Heightmap,
|
|
|
|
TileType,
|
|
|
|
Objects,
|
|
|
|
Done
|
|
|
|
}
|
2023-10-05 18:17:48 +02:00
|
|
|
|
|
|
|
// constants
|
2023-12-14 17:27:07 +01:00
|
|
|
public int ChunkSize = 14;
|
2023-10-30 22:20:32 +01:00
|
|
|
public const int NumChunkRows = 3;
|
|
|
|
public const int NumChunkColumns = NumChunkRows;
|
2023-12-29 09:26:43 +01:00
|
|
|
|
2023-12-29 17:55:43 +01:00
|
|
|
|
2023-12-29 09:26:43 +01:00
|
|
|
private readonly System.Collections.Generic.Dictionary<int, HexTile3D.TileTypeInfo> _tileTypeInfos = new();
|
2023-11-05 21:46:28 +01:00
|
|
|
|
2023-11-01 16:02:39 +01:00
|
|
|
private readonly Godot.Collections.Dictionary<Vector2, WorldChunk> _cachedWorldChunks;
|
2023-11-15 20:57:25 +01:00
|
|
|
private readonly List<Vector2> _addedChunkIndices = new();
|
2023-11-18 22:11:34 +01:00
|
|
|
private readonly List<WorldChunk> _deactivatedWorldChunks = new();
|
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-11-18 22:11:34 +01:00
|
|
|
private readonly List<Spatial> _removedSpatialNodes = new();
|
2023-10-05 18:17:48 +02:00
|
|
|
|
|
|
|
// delegate void OnCoordClicked(Vector2 world_pos);
|
|
|
|
|
|
|
|
// other members
|
2023-11-10 11:11:08 +01:00
|
|
|
private Vector2 _chunkIndexNorthEast;
|
|
|
|
private Vector2 _chunkIndexSouthWest;
|
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-15 20:57:25 +01:00
|
|
|
private MultiMeshInstance _tileMultiMeshInstance;
|
|
|
|
private int _usedTileMeshInstances;
|
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;
|
|
|
|
|
2023-12-14 17:27:07 +01:00
|
|
|
public GenerationState State = GenerationState.Empty;
|
2023-11-01 17:59:43 +01:00
|
|
|
public Vector2 WorldTextureCoordinateOffset;
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-11-10 11:11:08 +01:00
|
|
|
// 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-11-10 20:22:55 +01:00
|
|
|
[Signal]
|
|
|
|
public delegate void EntityClicked(Entity entity);
|
|
|
|
|
2023-11-15 20:57:25 +01:00
|
|
|
// signals
|
|
|
|
[Signal]
|
|
|
|
private delegate void TileClicked(HexTile3D tile3d);
|
|
|
|
|
|
|
|
[Signal]
|
|
|
|
private delegate void TileHovered(HexTile3D tile3d);
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public World() {
|
2023-10-05 18:17:48 +02:00
|
|
|
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.
|
2023-11-18 22:11:34 +01:00
|
|
|
public override void _Ready() {
|
2023-10-05 18:17:48 +02:00
|
|
|
Chunks = (Spatial)FindNode("Chunks");
|
|
|
|
Debug.Assert(Chunks != null);
|
2023-10-23 21:22:53 +02:00
|
|
|
|
2023-11-15 20:57:25 +01:00
|
|
|
_tileMultiMeshInstance = (MultiMeshInstance)FindNode("TileMultiMeshInstance");
|
|
|
|
Debug.Assert(_tileMultiMeshInstance != null);
|
2023-11-01 16:02:39 +01:00
|
|
|
|
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>();
|
2023-11-18 22:11:34 +01:00
|
|
|
foreach (Spatial asset in GetNode<Node>("Assets/Rocks").GetChildren()) {
|
|
|
|
_rockAssets.Add(asset);
|
|
|
|
}
|
2023-11-05 21:46:28 +01:00
|
|
|
|
|
|
|
_grassAssets = new Array<Spatial>();
|
2023-11-18 22:11:34 +01:00
|
|
|
foreach (Spatial asset in GetNode<Node>("Assets/Grass").GetChildren()) {
|
|
|
|
_grassAssets.Add(asset);
|
|
|
|
}
|
2023-11-05 21:46:28 +01:00
|
|
|
|
|
|
|
_treeAssets = new Array<Spatial>();
|
2023-11-18 22:11:34 +01:00
|
|
|
foreach (Spatial asset in GetNode<Node>("Assets/Trees").GetChildren()) {
|
|
|
|
_treeAssets.Add(asset);
|
|
|
|
}
|
2023-12-29 09:26:43 +01:00
|
|
|
|
|
|
|
_tileTypeInfos[0] = new HexTile3D.TileTypeInfo("Undefined", Colors.Black, 0xffff);
|
|
|
|
LoadTileTypeInfo("DeepWater", (int)Entity.EntityMaskEnum.Water);
|
|
|
|
LoadTileTypeInfo("Water", (int)Entity.EntityMaskEnum.Water);
|
|
|
|
LoadTileTypeInfo("LightWater", (int)Entity.EntityMaskEnum.Water);
|
|
|
|
LoadTileTypeInfo("Sand", (int)Entity.EntityMaskEnum.Ground);
|
|
|
|
LoadTileTypeInfo("Grass", (int)Entity.EntityMaskEnum.Ground);
|
|
|
|
LoadTileTypeInfo("Forest", (int)Entity.EntityMaskEnum.Ground);
|
|
|
|
LoadTileTypeInfo("Rock", (int)Entity.EntityMaskEnum.Ground);
|
|
|
|
LoadTileTypeInfo("Sand", (int)Entity.EntityMaskEnum.Ground);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void LoadTileTypeInfo(string tileTypeName, ushort tileTypeMask) {
|
|
|
|
ShaderMaterial worldTileTypeShaderMaterial =
|
|
|
|
GD.Load<ShaderMaterial>("res://materials/WorldTileTypeMaterial.tres");
|
|
|
|
|
|
|
|
Color tileTypeColor = (Color)worldTileTypeShaderMaterial.GetShaderParam(tileTypeName + "Color");
|
2023-12-29 17:55:43 +01:00
|
|
|
_tileTypeInfos[tileTypeColor.ToRgba32()] =
|
|
|
|
new HexTile3D.TileTypeInfo(tileTypeName, tileTypeColor, tileTypeMask);
|
2023-10-05 18:17:48 +02:00
|
|
|
}
|
|
|
|
|
2023-11-18 22:11:34 +01: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-12-14 17:27:07 +01:00
|
|
|
public void Reset() {
|
|
|
|
foreach (Spatial chunkChild in Chunks.GetChildren()) {
|
|
|
|
chunkChild.QueueFree();
|
|
|
|
}
|
|
|
|
|
|
|
|
// foreach (WorldChunk chunk in _cachedWorldChunks.Values) {
|
|
|
|
// chunk.QueueFree();
|
|
|
|
// }
|
|
|
|
|
|
|
|
_cachedWorldChunks.Clear();
|
|
|
|
_addedChunkIndices.Clear();
|
|
|
|
|
|
|
|
_tileMultiMeshInstance.Multimesh.InstanceCount =
|
|
|
|
ChunkSize * ChunkSize * NumChunkColumns * NumChunkRows;
|
|
|
|
_usedTileMeshInstances = 0;
|
|
|
|
|
|
|
|
State = GenerationState.Empty;
|
|
|
|
}
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public WorldChunk GetOrCreateWorldChunk(Vector2 chunkIndex, Color debugColor) {
|
2023-11-15 20:57:25 +01:00
|
|
|
WorldChunk chunk;
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
if (IsChunkCached(chunkIndex)) {
|
2023-11-15 20:57:25 +01:00
|
|
|
return _cachedWorldChunks[chunkIndex];
|
|
|
|
}
|
2023-11-18 22:11:34 +01:00
|
|
|
|
|
|
|
if (_deactivatedWorldChunks.Count > 0) {
|
|
|
|
chunk = _deactivatedWorldChunks.First();
|
|
|
|
_deactivatedWorldChunks.RemoveAt(0);
|
|
|
|
} else {
|
2023-11-15 20:57:25 +01:00
|
|
|
chunk = CreateWorldChunk(chunkIndex, debugColor);
|
2023-10-05 18:17:48 +02:00
|
|
|
}
|
|
|
|
|
2023-11-15 20:57:25 +01:00
|
|
|
_cachedWorldChunks[chunkIndex] = chunk;
|
|
|
|
|
|
|
|
return chunk;
|
2023-10-05 18:17:48 +02:00
|
|
|
}
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
private bool IsChunkCached(Vector2 chunkIndex) {
|
2023-11-15 20:57:25 +01:00
|
|
|
return _cachedWorldChunks.ContainsKey(chunkIndex);
|
2023-10-08 21:38:49 +02:00
|
|
|
}
|
|
|
|
|
2023-10-05 18:17:48 +02:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
private WorldChunk CreateWorldChunk(Vector2 chunkIndex, Color debugColor) {
|
2023-11-15 20:57:25 +01:00
|
|
|
WorldChunk result = (WorldChunk)_worldChunkScene.Instance();
|
2023-12-14 17:27:07 +01:00
|
|
|
result.SetSize(ChunkSize);
|
2023-11-15 20:57:25 +01:00
|
|
|
Chunks.AddChild(result);
|
|
|
|
result.Connect("TileClicked", this, nameof(OnTileClicked));
|
|
|
|
result.Connect("TileHovered", this, nameof(OnTileHovered));
|
2023-10-05 18:17:48 +02:00
|
|
|
|
2023-11-15 20:57:25 +01:00
|
|
|
result.SetSize(ChunkSize);
|
|
|
|
result.InitializeTileInstances(chunkIndex, _tileMultiMeshInstance, _usedTileMeshInstances);
|
|
|
|
_usedTileMeshInstances += result.Tiles.GetChildCount();
|
2023-10-05 18:17:48 +02:00
|
|
|
|
2023-11-15 20:57:25 +01:00
|
|
|
result.SetChunkIndex(chunkIndex, HexGrid);
|
|
|
|
result.UpdateTileTransforms();
|
2023-10-05 18:17:48 +02:00
|
|
|
|
|
|
|
result.DebugColor = debugColor;
|
|
|
|
result.DebugColor.a = 0.6f;
|
|
|
|
|
2023-10-08 21:38:49 +02:00
|
|
|
_cachedWorldChunks.Add(chunkIndex, result);
|
2023-10-05 18:17:48 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
private bool IsColorEqualApprox(Color colorA, Color colorB) {
|
2023-11-15 20:57:25 +01:00
|
|
|
Vector3 colorDifference = new(colorA.r - colorB.r, colorA.g - colorB.g, colorA.b - colorB.b);
|
2023-11-05 21:46:28 +01:00
|
|
|
return colorDifference.LengthSquared() < 0.1 * 0.1;
|
|
|
|
}
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
private Spatial SelectAsset(Vector2 textureCoord, Array<Spatial> assets, Random randomGenerator,
|
|
|
|
double probability) {
|
|
|
|
if (randomGenerator.NextDouble() < 1.0 - probability) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-11-05 21:46:28 +01:00
|
|
|
|
2023-11-15 20:57:25 +01:00
|
|
|
int assetIndex = randomGenerator.Next(assets.Count);
|
|
|
|
Spatial assetInstance = (Spatial)assets[assetIndex].Duplicate();
|
|
|
|
Transform assetTransform = Transform.Identity;
|
|
|
|
assetTransform.origin = HexGrid.GetHexCenterVec3FromOffset(textureCoord);
|
2023-11-05 21:46:28 +01:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
private void PopulateChunk(WorldChunk chunk) {
|
2023-11-15 20:57:25 +01:00
|
|
|
Random environmentRandom = new(Seed);
|
2023-11-05 21:46:28 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
chunk.CreateUnlockedTileTypeImage();
|
|
|
|
|
|
|
|
foreach (int textureCoordU in Enumerable.Range(0, chunk.Size)) {
|
|
|
|
foreach (int textureCoordV in Enumerable.Range(0, chunk.Size)) {
|
|
|
|
Vector2 textureCoord = new(textureCoordU, textureCoordV);
|
|
|
|
Vector2 offsetCoord = chunk.ChunkIndex * ChunkSize + textureCoord;
|
|
|
|
|
2023-12-29 09:26:43 +01:00
|
|
|
HexTile3D.TileTypeInfo tileTypeInfo = GetTileTypeInfoAtOffset(offsetCoord);
|
|
|
|
|
|
|
|
if (tileTypeInfo.Name == "Rock") {
|
2023-11-18 22:11:34 +01:00
|
|
|
Spatial rockAsset = SelectAsset(textureCoord, _rockAssets, environmentRandom, 0.15);
|
|
|
|
if (rockAsset != null) {
|
|
|
|
chunk.Entities.AddChild(rockAsset);
|
|
|
|
MarkCellUnwalkable(HexGrid.GetHexAtOffset(offsetCoord));
|
|
|
|
}
|
2023-12-29 09:26:43 +01:00
|
|
|
} else if (tileTypeInfo.Name == "Grass" || tileTypeInfo.Name == "DarkGrass") {
|
2023-11-18 22:11:34 +01:00
|
|
|
Spatial grassAsset = SelectAsset(textureCoord, _grassAssets, environmentRandom, 0.15);
|
|
|
|
if (grassAsset != null) {
|
|
|
|
chunk.Entities.AddChild(grassAsset);
|
|
|
|
}
|
|
|
|
|
|
|
|
Tree treeAsset = SelectAsset(textureCoord, _treeAssets, environmentRandom, 0.05) as Tree;
|
|
|
|
if (treeAsset != null) {
|
|
|
|
chunk.Entities.AddChild(treeAsset);
|
|
|
|
treeAsset.Connect("EntityClicked", this, nameof(OnEntityClicked));
|
|
|
|
treeAsset.Connect("TreeChopped", this, nameof(OnBlockingSpatialRemoved));
|
|
|
|
MarkCellUnwalkable(HexGrid.GetHexAtOffset(offsetCoord));
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: MarkCellUnwalkable(cell);
|
|
|
|
// else if (environmentRandom.NextDouble() < 0.01)
|
2023-12-29 17:55:43 +01:00
|
|
|
} else if (tileTypeInfo.Name == "Water" || tileTypeInfo.Name == "LightWater" ||
|
|
|
|
tileTypeInfo.Name == "DeepWater") {
|
|
|
|
Spatial rockAsset = SelectAsset(textureCoord, _rockAssets, environmentRandom, 0.01);
|
|
|
|
if (rockAsset != null) {
|
|
|
|
chunk.Entities.AddChild(rockAsset);
|
|
|
|
MarkCellUnwalkable(HexGrid.GetHexAtOffset(offsetCoord));
|
|
|
|
}
|
2023-11-05 21:46:28 +01:00
|
|
|
// {
|
|
|
|
// 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);
|
|
|
|
// }
|
2023-11-18 22:11:34 +01:00
|
|
|
}
|
2023-11-05 21:46:28 +01:00
|
|
|
// else if (IsColorWater(colorValue))
|
|
|
|
// {
|
|
|
|
// MarkCellUnwalkable(cell);
|
|
|
|
// }
|
2023-11-18 22:11:34 +01:00
|
|
|
}
|
2023-11-05 21:46:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public Vector2 WorldToOffsetCoords(Vector3 fromPositionWorld) {
|
2023-11-10 11:11:08 +01:00
|
|
|
return HexGrid.GetHexAt(new Vector2(fromPositionWorld.x, fromPositionWorld.z)).OffsetCoords;
|
|
|
|
}
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public Vector3 GetHexCenterFromOffset(Vector2 fromPositionOffset) {
|
2023-11-10 11:11:08 +01:00
|
|
|
return HexGrid.GetHexCenterVec3FromOffset(fromPositionOffset);
|
|
|
|
}
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public void UpdateCenterChunkFromPlaneCoord(Vector2 planeCoord) {
|
2023-12-14 17:27:07 +01:00
|
|
|
if (State != GenerationState.Done && State != GenerationState.Empty) {
|
2023-10-23 21:22:53 +02:00
|
|
|
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
|
2023-11-15 20:57:25 +01:00
|
|
|
CenterChunkIndex = GetChunkTupleFromPlaneCoord(planeCoord);
|
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>();
|
2023-11-15 20:57:25 +01:00
|
|
|
_activeChunkIndices.Add(CenterChunkIndex + new Vector2(-1, -1));
|
|
|
|
_activeChunkIndices.Add(CenterChunkIndex + new Vector2(0, -1));
|
|
|
|
_activeChunkIndices.Add(CenterChunkIndex + new Vector2(1, -1));
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-11-15 20:57:25 +01:00
|
|
|
_activeChunkIndices.Add(CenterChunkIndex + new Vector2(-1, 0));
|
|
|
|
_activeChunkIndices.Add(CenterChunkIndex);
|
|
|
|
_activeChunkIndices.Add(CenterChunkIndex + new Vector2(+1, 0));
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-11-15 20:57:25 +01:00
|
|
|
_activeChunkIndices.Add(CenterChunkIndex + new Vector2(-1, +1));
|
|
|
|
_activeChunkIndices.Add(CenterChunkIndex + new Vector2(0, +1));
|
|
|
|
_activeChunkIndices.Add(CenterChunkIndex + new Vector2(+1, +1));
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-11-15 20:57:25 +01:00
|
|
|
// clear unused chunks
|
2023-11-18 22:11:34 +01:00
|
|
|
_deactivatedWorldChunks.Clear();
|
2023-11-15 20:57:25 +01:00
|
|
|
_addedChunkIndices.Clear();
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
foreach (Vector2 oldChunkIndex in oldCachedChunks.Keys) {
|
|
|
|
if (!_activeChunkIndices.Contains(oldChunkIndex)) {
|
2023-11-15 20:57:25 +01:00
|
|
|
DeactivateChunk(oldCachedChunks[oldChunkIndex]);
|
2023-11-18 22:11:34 +01:00
|
|
|
}
|
|
|
|
}
|
2023-10-08 21:38:49 +02:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
foreach (Vector2 activeChunkIndex in _activeChunkIndices) {
|
2023-11-15 20:57:25 +01:00
|
|
|
WorldChunk chunk = GetOrCreateWorldChunk(activeChunkIndex,
|
|
|
|
new Color(GD.Randf(), GD.Randf(), GD.Randf()));
|
|
|
|
}
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-11-15 20:57:25 +01:00
|
|
|
Debug.Assert(_activeChunkIndices.Count == NumChunkRows * NumChunkColumns);
|
2023-10-08 21:38:49 +02:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
foreach (Vector2 chunkKey in _activeChunkIndices) {
|
|
|
|
if (!oldCachedChunks.ContainsKey(chunkKey)) {
|
2023-11-15 20:57:25 +01:00
|
|
|
ActivateChunk(_cachedWorldChunks[chunkKey], chunkKey);
|
2023-11-01 16:02:39 +01:00
|
|
|
State = GenerationState.Heightmap;
|
2023-10-08 21:38:49 +02:00
|
|
|
}
|
2023-11-18 22:11:34 +01:00
|
|
|
}
|
2023-12-29 09:26:43 +01:00
|
|
|
|
2023-12-28 18:14:07 +01:00
|
|
|
WorldChunk currentChunk = GetOrCreateWorldChunk(CenterChunkIndex,
|
|
|
|
new Color(GD.Randf(), GD.Randf(), GD.Randf()));
|
|
|
|
|
|
|
|
_centerChunkRect = new Rect2(
|
|
|
|
new Vector2(currentChunk.Transform.origin.x, currentChunk.Transform.origin.z)
|
|
|
|
+ currentChunk.PlaneRect.Position,
|
|
|
|
currentChunk.PlaneRect.Size);
|
|
|
|
|
|
|
|
UpdateChunkBounds();
|
|
|
|
|
|
|
|
UpdateNavigationBounds();
|
2023-11-01 16:02:39 +01:00
|
|
|
}
|
2023-10-23 21:22:53 +02:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
private void ActivateChunk(WorldChunk chunk, Vector2 chunkIndex) {
|
2023-11-15 20:57:25 +01:00
|
|
|
chunk.SetChunkIndex(chunkIndex, HexGrid);
|
|
|
|
chunk.UpdateTileTransforms();
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-11-15 20:57:25 +01:00
|
|
|
_addedChunkIndices.Add(chunk.ChunkIndex);
|
|
|
|
GenerateChunkNoiseMap(chunk);
|
|
|
|
}
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
private void DeactivateChunk(WorldChunk chunk) {
|
2023-11-15 20:57:25 +01:00
|
|
|
_cachedWorldChunks.Remove(chunk.ChunkIndex);
|
|
|
|
chunk.ClearContent();
|
2023-11-18 22:11:34 +01:00
|
|
|
_deactivatedWorldChunks.Add(chunk);
|
2023-11-15 20:57:25 +01:00
|
|
|
}
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
private void GenerateChunkNoiseMap(WorldChunk chunk) {
|
2023-11-15 20:57:25 +01:00
|
|
|
Vector2 chunkIndex = chunk.ChunkIndex;
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-11-15 20:57:25 +01:00
|
|
|
ImageTexture noiseImageTexture = new();
|
|
|
|
noiseImageTexture.CreateFromImage(_noiseGenerator.GetImage(ChunkSize, ChunkSize, chunkIndex * ChunkSize),
|
|
|
|
0);
|
2023-11-01 16:02:39 +01:00
|
|
|
|
|
|
|
chunk.SetNoisemap(noiseImageTexture);
|
2023-10-08 21:38:49 +02:00
|
|
|
}
|
|
|
|
|
2023-11-18 22:11:34 +01: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-11-18 22:11:34 +01:00
|
|
|
foreach (WorldChunk chunk in Chunks.GetChildren()) {
|
|
|
|
if (chunk.ChunkIndex == new Vector2(cachedChunkKey.x, cachedChunkKey.y)) {
|
2023-10-08 21:38:49 +02:00
|
|
|
chunk.QueueFree();
|
2023-11-18 22:11:34 +01:00
|
|
|
}
|
|
|
|
}
|
2023-10-08 21:38:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
private Vector2 GetChunkTupleFromPlaneCoord(Vector2 planeCoord) {
|
|
|
|
HexCell hexCell = HexGrid.GetHexAt(planeCoord);
|
|
|
|
return GetChunkIndexFromOffsetCoord(hexCell.OffsetCoords);
|
2023-10-05 18:17:48 +02:00
|
|
|
}
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
private Vector2 GetChunkIndexFromOffsetCoord(Vector2 offsetCoord) {
|
|
|
|
return (offsetCoord / ChunkSize).Floor();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetCenterPlaneCoord(Vector2 centerPlaneCoord) {
|
2023-12-14 17:27:07 +01:00
|
|
|
if (State != GenerationState.Done) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
if (!_centerChunkRect.HasPoint(centerPlaneCoord)) {
|
2023-11-10 11:11:08 +01:00
|
|
|
UpdateCenterChunkFromPlaneCoord(centerPlaneCoord);
|
|
|
|
}
|
2023-11-01 16:02:39 +01:00
|
|
|
}
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
private void UpdateWorldViewTexture() {
|
2023-11-15 20:57:25 +01:00
|
|
|
int worldChunkSize = ChunkSize;
|
|
|
|
int numWorldChunkRows = NumChunkRows;
|
|
|
|
int numWorldChunkColumns = NumChunkColumns;
|
2023-11-01 16:02:39 +01:00
|
|
|
|
|
|
|
_heightmapImage.Create(worldChunkSize * numWorldChunkColumns, worldChunkSize * numWorldChunkRows, false,
|
|
|
|
Image.Format.Rgba8);
|
|
|
|
_tileTypeMapImage.Create(worldChunkSize * numWorldChunkColumns, worldChunkSize * numWorldChunkRows, false,
|
|
|
|
Image.Format.Rgba8);
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
foreach (Vector2 chunkIndex in _activeChunkIndices) {
|
2023-11-15 20:57:25 +01:00
|
|
|
WorldChunk worldChunk = GetOrCreateWorldChunk(chunkIndex, Colors.White);
|
2023-11-01 16:02:39 +01:00
|
|
|
|
|
|
|
_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();
|
2023-12-29 09:26:43 +01:00
|
|
|
_viewTileTypeTexture.CreateFromImage(_tileTypeMapImage, (uint)Texture.FlagsEnum.ConvertToLinear);
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-11-10 11:11:08 +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-18 22:11:34 +01:00
|
|
|
private void UpdateChunkBounds() {
|
2023-11-10 11:11:08 +01:00
|
|
|
_chunkIndexSouthWest = Vector2.Inf;
|
|
|
|
_chunkIndexNorthEast = -Vector2.Inf;
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
foreach (Vector2 chunkIndex in _activeChunkIndices) {
|
|
|
|
if (chunkIndex.x <= _chunkIndexSouthWest.x && chunkIndex.y <= _chunkIndexSouthWest.y) {
|
2023-11-10 11:11:08 +01:00
|
|
|
_chunkIndexSouthWest = chunkIndex;
|
2023-11-18 22:11:34 +01:00
|
|
|
} else if (chunkIndex.x >= _chunkIndexNorthEast.x && chunkIndex.y >= _chunkIndexNorthEast.y) {
|
2023-11-10 11:11:08 +01:00
|
|
|
_chunkIndexNorthEast = chunkIndex;
|
2023-11-18 22:11:34 +01:00
|
|
|
}
|
2023-11-10 11:11:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
private void UpdateNavigationBounds() {
|
2023-11-15 20:57:25 +01:00
|
|
|
HexCell cellSouthWest = HexGrid.GetHexAtOffset(_chunkIndexSouthWest * ChunkSize);
|
2023-11-18 22:11:34 +01:00
|
|
|
HexGrid.SetBoundsOffset(cellSouthWest, ChunkSize * new Vector2(NumChunkColumns, NumChunkRows));
|
|
|
|
}
|
2023-11-10 11:11:08 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public void MarkCellUnwalkable(HexCell cell) {
|
|
|
|
HexGrid.AddObstacle(cell);
|
|
|
|
}
|
2023-11-10 11:11:08 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public float GetHexCost(Entity entity, HexCell cell) {
|
|
|
|
float nextHexCost = HexGrid.GetHexCost(cell);
|
2023-12-29 09:26:43 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
if (nextHexCost != 0) {
|
|
|
|
Vector2 nextOffset = cell.OffsetCoords;
|
2023-12-29 09:26:43 +01:00
|
|
|
Color tileTypeColor = GetTileTypeColorAtOffset(nextOffset);
|
2023-12-14 17:27:07 +01:00
|
|
|
|
2023-12-29 09:26:43 +01:00
|
|
|
if (_tileTypeInfos.TryGetValue(tileTypeColor.ToRgba32(), out HexTile3D.TileTypeInfo info)) {
|
|
|
|
int tileTypeMask = info.TileTypeMask;
|
|
|
|
if ((entity.EntityMask ^ tileTypeMask) != 0) {
|
|
|
|
nextHexCost = 0;
|
|
|
|
}
|
2023-12-29 17:55:43 +01:00
|
|
|
} else {
|
|
|
|
GD.Print("Could not find tile type info for color " + tileTypeColor);
|
2023-12-29 17:43:04 +01:00
|
|
|
}
|
2023-12-29 09:26:43 +01:00
|
|
|
}
|
2023-12-14 17:27:07 +01:00
|
|
|
|
2023-12-29 09:26:43 +01:00
|
|
|
return nextHexCost;
|
|
|
|
}
|
2023-11-18 22:11:34 +01:00
|
|
|
|
2023-12-29 09:26:43 +01:00
|
|
|
private HexTile3D.TileTypeInfo GetTileTypeInfoAtOffset(Vector2 offsetCoord) {
|
|
|
|
Color tileTypeColor = GetTileTypeColorAtOffset(offsetCoord);
|
|
|
|
if (_tileTypeInfos.TryGetValue(tileTypeColor.ToRgba32(), out HexTile3D.TileTypeInfo info)) {
|
|
|
|
return info;
|
2023-11-18 22:11:34 +01:00
|
|
|
}
|
|
|
|
|
2023-12-29 09:26:43 +01:00
|
|
|
return _tileTypeInfos[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
public Color GetTileTypeColorAtOffset(Vector2 offsetCoord) {
|
|
|
|
Vector2 chunkIndex = GetChunkIndexFromOffsetCoord(offsetCoord);
|
|
|
|
|
|
|
|
if (!_cachedWorldChunks.ContainsKey(chunkIndex)) {
|
|
|
|
return Colors.Black;
|
|
|
|
}
|
|
|
|
|
|
|
|
WorldChunk chunk = _cachedWorldChunks[chunkIndex];
|
|
|
|
Vector2 textureCoordinate = offsetCoord - Vector2.One * ChunkSize * chunkIndex;
|
|
|
|
|
|
|
|
Color tileTypeColor = chunk.TileTypeImage.GetPixel((int)textureCoordinate.x, (int)textureCoordinate.y);
|
|
|
|
return tileTypeColor;
|
2023-11-18 22:11:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public float GetMoveCost(Entity entity, HexCell currentHex, HexCell nextHex) {
|
|
|
|
if (GetHexCost(entity, nextHex) == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return HexGrid.GetMoveCost(currentHex.AxialCoords,
|
|
|
|
new HexCell(nextHex.AxialCoords - currentHex.AxialCoords).CubeCoords);
|
2023-11-10 11:11:08 +01:00
|
|
|
}
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public List<HexCell> FindPath(Entity entity, HexCell startHex, HexCell goalHex) {
|
|
|
|
if (State != GenerationState.Done) {
|
|
|
|
return new List<HexCell>();
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2 goalAxialCoords = goalHex.AxialCoords;
|
|
|
|
|
|
|
|
SimplePriorityQueue<Vector2, float> frontier = new();
|
|
|
|
frontier.Enqueue(startHex.AxialCoords, 0);
|
|
|
|
System.Collections.Generic.Dictionary<Vector2, Vector2> cameFrom = new();
|
|
|
|
System.Collections.Generic.Dictionary<Vector2, float> costSoFar = new();
|
|
|
|
|
|
|
|
cameFrom.Add(startHex.AxialCoords, startHex.AxialCoords);
|
|
|
|
costSoFar.Add(startHex.AxialCoords, 0);
|
|
|
|
|
|
|
|
int FindPathCheckedCellCount = 0;
|
|
|
|
|
|
|
|
while (frontier.Any()) {
|
|
|
|
FindPathCheckedCellCount++;
|
|
|
|
HexCell currentHex = new(frontier.Dequeue());
|
|
|
|
Vector2 currentAxial = currentHex.AxialCoords;
|
|
|
|
|
|
|
|
if (currentHex == goalHex) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (HexCell nextHex in currentHex.GetAllAdjacent()) {
|
|
|
|
Vector2 nextAxial = nextHex.AxialCoords;
|
|
|
|
|
|
|
|
float moveCost = GetMoveCost(entity, currentHex, nextHex);
|
|
|
|
|
|
|
|
if (nextHex == goalHex && moveCost == 0 && GetHexCost(entity, nextHex) == 0) {
|
|
|
|
// Goal ist an obstacle
|
|
|
|
cameFrom[nextHex.AxialCoords] = currentHex.AxialCoords;
|
|
|
|
frontier.Clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (moveCost == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
moveCost += costSoFar[currentHex.AxialCoords];
|
|
|
|
if (!costSoFar.ContainsKey(nextHex.AxialCoords) || moveCost < costSoFar[nextHex.AxialCoords]) {
|
|
|
|
costSoFar[nextHex.AxialCoords] = moveCost;
|
|
|
|
float priority = moveCost + nextHex.DistanceTo(goalHex);
|
|
|
|
|
|
|
|
frontier.Enqueue(nextHex.AxialCoords, priority);
|
|
|
|
cameFrom[nextHex.AxialCoords] = currentHex.AxialCoords;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GD.Print("Checked Cell Count: " + FindPathCheckedCellCount);
|
|
|
|
|
|
|
|
List<HexCell> result = new();
|
|
|
|
if (!cameFrom.ContainsKey(goalHex.AxialCoords)) {
|
|
|
|
GD.Print("Failed to find path from " + startHex + " to " + goalHex);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HexGrid.GetHexCost(goalAxialCoords) != 0) {
|
|
|
|
result.Add(goalHex);
|
|
|
|
}
|
|
|
|
|
|
|
|
HexCell pathHex = goalHex;
|
|
|
|
while (pathHex != startHex) {
|
|
|
|
pathHex = new HexCell(cameFrom[pathHex.AxialCoords]);
|
|
|
|
result.Insert(0, pathHex);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-12-14 17:27:07 +01:00
|
|
|
public bool CheckSweptTriangleCellCollision(Entity entity, Vector3 startWorld, Vector3 endWorld, float radius) {
|
|
|
|
Vector2 startPlane = new(startWorld.x, startWorld.z);
|
|
|
|
Vector2 endPlane = new(endWorld.x, endWorld.z);
|
|
|
|
Vector2 directionPlane = (endPlane - startPlane).Normalized();
|
|
|
|
Vector2 sidePlane = directionPlane.Rotated(Mathf.Pi * 0.5f);
|
|
|
|
|
|
|
|
List<HexCell> cells =
|
|
|
|
HexGrid.GetCellsForLine(startPlane + directionPlane * radius, endPlane + directionPlane * radius);
|
|
|
|
foreach (HexCell cell in cells) {
|
|
|
|
if (GetHexCost(entity, cell) == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cells = HexGrid.GetCellsForLine(startPlane + sidePlane * radius, endPlane + sidePlane * radius);
|
|
|
|
foreach (HexCell cell in cells) {
|
|
|
|
if (GetHexCost(entity, cell) == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cells = HexGrid.GetCellsForLine(startPlane - sidePlane * radius, endPlane - sidePlane * radius);
|
|
|
|
foreach (HexCell cell in cells) {
|
|
|
|
if (GetHexCost(entity, cell) == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<NavigationPoint> SmoothPath(Entity entity, List<NavigationPoint> navigationPoints) {
|
|
|
|
if (navigationPoints.Count <= 2) {
|
|
|
|
return navigationPoints;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector3 bodyGlobalTranslation = entity.GlobalTranslation;
|
|
|
|
List<NavigationPoint> smoothedPath = new();
|
|
|
|
|
|
|
|
int startIndex = 0;
|
|
|
|
int endIndex = navigationPoints.Count > 1 ? 1 : 0;
|
|
|
|
smoothedPath.Add(navigationPoints[startIndex]);
|
|
|
|
Vector3 startPoint = navigationPoints[startIndex].WorldPosition;
|
|
|
|
|
|
|
|
while (endIndex != navigationPoints.Count) {
|
|
|
|
Vector3 endPoint = navigationPoints[endIndex].WorldPosition;
|
|
|
|
|
|
|
|
if (CheckSweptTriangleCellCollision(entity, startPoint, endPoint, 0.27f)) {
|
|
|
|
if (endIndex - startIndex == 1) {
|
|
|
|
GD.Print("Aborting SmoothPath: input path passes through collision geometry.");
|
|
|
|
entity.GlobalTranslation = bodyGlobalTranslation;
|
|
|
|
return smoothedPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
smoothedPath.Add(navigationPoints[endIndex - 1]);
|
|
|
|
startIndex = endIndex - 1;
|
|
|
|
startPoint = navigationPoints[startIndex].WorldPosition;
|
|
|
|
entity.GlobalTranslation = startPoint;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (endIndex == navigationPoints.Count - 1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
endIndex += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
smoothedPath.Add(navigationPoints[endIndex]);
|
|
|
|
entity.GlobalTranslation = bodyGlobalTranslation;
|
|
|
|
|
|
|
|
return smoothedPath;
|
|
|
|
}
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public override void _Process(float delta) {
|
2023-11-15 20:57:25 +01:00
|
|
|
GenerationState 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-18 22:11:34 +01:00
|
|
|
if (oldState != GenerationState.Done && State == GenerationState.Done) {
|
2023-11-01 17:43:47 +01:00
|
|
|
UpdateWorldViewTexture();
|
2023-11-18 22:11:34 +01:00
|
|
|
}
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
while (_removedSpatialNodes.Count > 0) {
|
|
|
|
GD.Print("Queueing deletion of " + _removedSpatialNodes[0]);
|
|
|
|
_removedSpatialNodes[0].QueueFree();
|
|
|
|
_removedSpatialNodes.RemoveAt(0);
|
|
|
|
}
|
|
|
|
}
|
2023-11-15 20:57:25 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
private void UpdateGenerationState() {
|
|
|
|
if (State == GenerationState.Heightmap) {
|
2023-11-15 20:57:25 +01:00
|
|
|
int numChunksGeneratingHeightmap = 0;
|
2023-11-18 22:11:34 +01:00
|
|
|
foreach (Vector2 chunkIndex in _addedChunkIndices) {
|
2023-11-15 20:57:25 +01:00
|
|
|
WorldChunk chunk = _cachedWorldChunks[chunkIndex];
|
2023-11-18 22:11:34 +01:00
|
|
|
if (chunk.HeightMapFrameCount > 0) {
|
|
|
|
numChunksGeneratingHeightmap++;
|
|
|
|
}
|
2023-11-01 16:02:39 +01:00
|
|
|
}
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
if (numChunksGeneratingHeightmap == 0) {
|
2023-11-01 16:02:39 +01:00
|
|
|
// assign height map images
|
2023-11-18 22:11:34 +01:00
|
|
|
foreach (Vector2 chunkIndex in _addedChunkIndices) {
|
2023-11-15 20:57:25 +01:00
|
|
|
WorldChunk chunk = _cachedWorldChunks[chunkIndex];
|
2023-11-01 16:02:39 +01:00
|
|
|
chunk.SetHeightmap(chunk.HeightmapOffscreenViewport.GetTexture());
|
|
|
|
}
|
2023-11-01 17:43:47 +01:00
|
|
|
|
|
|
|
State = GenerationState.TileType;
|
2023-11-01 16:02:39 +01:00
|
|
|
}
|
2023-11-18 22:11:34 +01:00
|
|
|
} else if (State == GenerationState.TileType) {
|
2023-11-15 20:57:25 +01:00
|
|
|
int numChunksGeneratingTileType = 0;
|
2023-11-18 22:11:34 +01:00
|
|
|
foreach (Vector2 chunkIndex in _addedChunkIndices) {
|
2023-11-15 20:57:25 +01:00
|
|
|
WorldChunk chunk = _cachedWorldChunks[chunkIndex];
|
2023-11-18 22:11:34 +01:00
|
|
|
if (chunk.TileTypeMapFrameCount > 0) {
|
|
|
|
numChunksGeneratingTileType++;
|
|
|
|
}
|
2023-11-01 16:02:39 +01:00
|
|
|
}
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
if (numChunksGeneratingTileType == 0) {
|
2023-11-15 20:57:25 +01:00
|
|
|
State = GenerationState.Objects;
|
|
|
|
}
|
2023-11-18 22:11:34 +01:00
|
|
|
} else if (State == GenerationState.Objects) {
|
2023-11-01 16:02:39 +01:00
|
|
|
// generate objects
|
2023-11-18 22:11:34 +01:00
|
|
|
foreach (Vector2 chunkIndex in _addedChunkIndices) {
|
2023-11-15 20:57:25 +01:00
|
|
|
PopulateChunk(_cachedWorldChunks[chunkIndex]);
|
2023-11-18 22:11:34 +01:00
|
|
|
}
|
2023-11-05 21:46:28 +01:00
|
|
|
|
2023-11-15 20:57:25 +01:00
|
|
|
_addedChunkIndices.Clear();
|
|
|
|
|
2023-11-01 16:02:39 +01:00
|
|
|
State = GenerationState.Done;
|
|
|
|
}
|
|
|
|
}
|
2023-11-10 20:22:55 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
private void OnEntityClicked(Entity entity) {
|
2023-11-10 20:22:55 +01:00
|
|
|
EmitSignal("EntityClicked", entity);
|
|
|
|
}
|
2023-11-15 20:57:25 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public void OnTileClicked(HexTile3D tile) {
|
2023-12-29 09:26:43 +01:00
|
|
|
if (State != GenerationState.Done) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-15 20:57:25 +01:00
|
|
|
EmitSignal("TileClicked", tile);
|
|
|
|
}
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public void OnTileHovered(HexTile3D tile) {
|
2023-12-29 09:26:43 +01:00
|
|
|
if (State != GenerationState.Done) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-15 20:57:25 +01:00
|
|
|
EmitSignal("TileHovered", tile);
|
2023-12-29 09:26:43 +01:00
|
|
|
HexTile3D.TileTypeInfo tileTypeInfo = GetTileTypeInfoAtOffset(tile.OffsetCoords);
|
2023-11-15 20:57:25 +01:00
|
|
|
}
|
2023-11-18 22:11:34 +01:00
|
|
|
|
|
|
|
public void OnBlockingSpatialRemoved(Spatial spatialNode) {
|
|
|
|
if (spatialNode.IsQueuedForDeletion()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
HexGrid.RemoveObstacle(HexGrid.GetHexAt(new Vector2(spatialNode.GlobalTranslation.x,
|
|
|
|
spatialNode.GlobalTranslation.z)));
|
|
|
|
_removedSpatialNodes.Add(spatialNode);
|
|
|
|
}
|
2023-10-05 18:17:48 +02:00
|
|
|
}
|