2023-10-05 18:17:48 +02:00
|
|
|
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-11-15 20:57:25 +01:00
|
|
|
using Godot.Collections;
|
2023-10-05 18:17:48 +02:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public class WorldChunk : Spatial {
|
2023-11-15 20:57:25 +01:00
|
|
|
private readonly PackedScene _hexTile3DScene = GD.Load<PackedScene>("res://scenes/HexTile3D.tscn");
|
|
|
|
private MultiMeshInstance _multiMeshInstance;
|
|
|
|
private readonly Array<int> _tileInstanceIndices = new();
|
|
|
|
|
2023-11-05 21:46:28 +01:00
|
|
|
private readonly SpatialMaterial _rectMaterial = new();
|
2023-11-01 16:02:39 +01:00
|
|
|
private Sprite _heightmapSprite;
|
|
|
|
private TextureRect _heightmapTextureRect;
|
|
|
|
private Sprite _noiseMask;
|
2023-10-23 21:22:53 +02:00
|
|
|
|
2023-11-01 16:02:39 +01:00
|
|
|
private Sprite _noiseSprite;
|
|
|
|
private bool _showTextureOverlay;
|
2023-11-15 20:57:25 +01:00
|
|
|
|
2023-11-01 16:02:39 +01:00
|
|
|
[Export] public Vector2 ChunkIndex;
|
2023-11-15 20:57:25 +01:00
|
|
|
|
2023-11-01 16:02:39 +01:00
|
|
|
public Color DebugColor = Colors.White;
|
2023-11-15 20:57:25 +01:00
|
|
|
public Spatial Entities;
|
|
|
|
public Spatial Tiles;
|
|
|
|
private readonly HexGrid _hexGrid = new();
|
|
|
|
private readonly bool _showHexTiles;
|
|
|
|
|
2023-11-01 16:02:39 +01:00
|
|
|
[Export] public Texture HeightMap;
|
|
|
|
public int HeightMapFrameCount;
|
2023-10-23 21:22:53 +02:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public Image TileTypeImage;
|
2023-11-01 16:02:39 +01:00
|
|
|
public Viewport HeightmapOffscreenViewport;
|
2023-10-05 18:17:48 +02:00
|
|
|
[Export] public Texture NavigationMap;
|
|
|
|
|
2023-11-01 16:02:39 +01:00
|
|
|
public bool NoiseTextureCheckerboardOverlay = true;
|
2023-10-27 22:56:49 +02:00
|
|
|
|
2023-10-05 18:17:48 +02:00
|
|
|
// signals
|
2023-11-15 20:57:25 +01:00
|
|
|
[Signal]
|
|
|
|
private delegate void TileClicked(HexTile3D tile3d);
|
|
|
|
|
|
|
|
[Signal]
|
|
|
|
private delegate void TileHovered(HexTile3D tile3d);
|
2023-10-05 18:17:48 +02:00
|
|
|
|
|
|
|
// other members
|
|
|
|
public Rect2 PlaneRect;
|
2023-11-15 20:57:25 +01:00
|
|
|
|
2023-11-01 16:02:39 +01:00
|
|
|
// ui elements
|
|
|
|
|
|
|
|
// scene nodes
|
|
|
|
private MeshInstance PlaneRectMesh;
|
2023-12-14 17:27:07 +01:00
|
|
|
public int Size = 32;
|
2023-11-01 16:02:39 +01:00
|
|
|
|
|
|
|
// resources
|
|
|
|
|
|
|
|
// exports
|
|
|
|
[Export] public Texture TileTypeMap;
|
|
|
|
public int TileTypeMapFrameCount;
|
2023-10-23 21:22:53 +02:00
|
|
|
public Viewport TileTypeOffscreenViewport;
|
2023-10-05 18:17:48 +02:00
|
|
|
|
2023-11-01 16:02:39 +01:00
|
|
|
[Export]
|
2023-11-18 22:11:34 +01:00
|
|
|
public bool ShowTextureOverlay {
|
2023-11-01 16:02:39 +01:00
|
|
|
get => _showTextureOverlay;
|
2023-10-23 21:22:53 +02:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
set {
|
|
|
|
if (PlaneRectMesh != null) {
|
|
|
|
PlaneRectMesh.Visible = value;
|
|
|
|
}
|
2023-10-23 21:22:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-23 21:22:53 +02:00
|
|
|
PlaneRectMesh = (MeshInstance)FindNode("PlaneRectMesh");
|
2023-10-05 18:17:48 +02:00
|
|
|
Debug.Assert(PlaneRectMesh != null);
|
2023-11-18 22:11:34 +01:00
|
|
|
if (PlaneRectMesh.Visible) {
|
|
|
|
_showTextureOverlay = true;
|
|
|
|
}
|
2023-10-23 21:22:53 +02:00
|
|
|
|
2023-11-15 20:57:25 +01:00
|
|
|
Transform planeRectTransform = Transform.Identity;
|
2023-10-23 21:22:53 +02:00
|
|
|
planeRectTransform =
|
|
|
|
planeRectTransform.Scaled(new Vector3(PlaneRect.Size.x, 0.125f, PlaneRect.Size.y));
|
2023-10-05 18:17:48 +02:00
|
|
|
planeRectTransform.origin.x = PlaneRect.GetCenter().x;
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-10-23 21:22:53 +02:00
|
|
|
planeRectTransform.origin.z = PlaneRect.GetCenter().y;
|
|
|
|
|
2023-10-05 18:17:48 +02:00
|
|
|
PlaneRectMesh.Transform = planeRectTransform;
|
|
|
|
|
2023-10-23 21:22:53 +02:00
|
|
|
// PlaneRectMesh.MaterialOverride = new SpatialMaterial();
|
|
|
|
// ((SpatialMaterial)PlaneRectMesh.MaterialOverride).AlbedoColor = DebugColor;
|
|
|
|
// ((SpatialMaterial)PlaneRectMesh.MaterialOverride).FlagsTransparent = true;
|
|
|
|
|
2023-11-01 16:02:39 +01:00
|
|
|
HeightmapOffscreenViewport = (Viewport)FindNode("HeightmapOffscreenViewport");
|
|
|
|
HeightmapOffscreenViewport.Size = Vector2.One * Size;
|
|
|
|
Debug.Assert(HeightmapOffscreenViewport != null);
|
|
|
|
_noiseSprite = (Sprite)FindNode("NoiseSprite");
|
|
|
|
_noiseMask = (Sprite)FindNode("NoiseMask");
|
|
|
|
_heightmapSprite = (Sprite)FindNode("HeightmapSprite");
|
|
|
|
|
2023-10-23 21:22:53 +02:00
|
|
|
TileTypeOffscreenViewport = (Viewport)FindNode("TileTypeOffscreenViewport");
|
|
|
|
TileTypeOffscreenViewport.Size = Vector2.One * Size;
|
|
|
|
Debug.Assert(TileTypeOffscreenViewport != null);
|
|
|
|
|
2023-11-05 21:46:28 +01:00
|
|
|
Entities = (Spatial)FindNode("Entities");
|
|
|
|
Debug.Assert(Entities != null);
|
|
|
|
|
2023-11-15 20:57:25 +01:00
|
|
|
Tiles = (Spatial)FindNode("Tiles");
|
|
|
|
Debug.Assert(Tiles != null);
|
2023-11-01 16:02:39 +01:00
|
|
|
}
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public void SetSize(int size) {
|
2023-11-01 16:02:39 +01:00
|
|
|
Size = size;
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
if (TileTypeOffscreenViewport != null) {
|
2023-11-01 16:02:39 +01:00
|
|
|
TileTypeOffscreenViewport.Size = Vector2.One * size;
|
|
|
|
HeightmapOffscreenViewport.Size = Vector2.One * size;
|
|
|
|
_noiseMask.Transform = Transform2D.Identity.Scaled(Vector2.One * size / _noiseMask.Texture.GetSize().x);
|
|
|
|
_noiseSprite.Transform = Transform2D.Identity.Scaled(Vector2.One * size / _noiseSprite.Texture.GetSize().x);
|
|
|
|
_heightmapSprite.Transform =
|
|
|
|
Transform2D.Identity.Scaled(Vector2.One * size / _heightmapSprite.Texture.GetSize().x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public void SetChunkIndex(Vector2 chunkIndex, HexGrid hexGrid) {
|
2023-11-15 20:57:25 +01:00
|
|
|
ChunkIndex = chunkIndex;
|
2023-12-14 17:27:07 +01:00
|
|
|
float chunkSize = Size;
|
2023-11-15 20:57:25 +01:00
|
|
|
|
|
|
|
Vector2 planeCoordSouthWest = hexGrid.GetHexCenterFromOffset(chunkIndex * chunkSize);
|
|
|
|
|
|
|
|
Transform = new Transform(Basis.Identity, new Vector3(planeCoordSouthWest.x, 0, planeCoordSouthWest.y));
|
|
|
|
|
|
|
|
Vector2 localPlaneCoordSouthWest = new Vector2(-hexGrid.HexSize.x, hexGrid.HexSize.y) * 0.5f;
|
|
|
|
Vector2 localPlaneCoordNorthEast = hexGrid.GetHexCenterFromOffset(Vector2.One * chunkSize) +
|
|
|
|
new Vector2(hexGrid.HexSize.x, -hexGrid.HexSize.y) * 0.5f;
|
|
|
|
|
|
|
|
PlaneRect = new Rect2(
|
|
|
|
new Vector2(localPlaneCoordSouthWest.x, localPlaneCoordNorthEast.y),
|
|
|
|
new Vector2(localPlaneCoordNorthEast.x - localPlaneCoordSouthWest.x,
|
|
|
|
localPlaneCoordSouthWest.y - localPlaneCoordNorthEast.y)
|
2023-11-18 22:11:34 +01:00
|
|
|
);
|
2023-11-15 20:57:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void InitializeTileInstances(Vector2 chunkIndex, MultiMeshInstance multiMeshInstance,
|
2023-11-18 22:11:34 +01:00
|
|
|
int tileInstanceIndexStart) {
|
2023-11-15 20:57:25 +01:00
|
|
|
_multiMeshInstance = multiMeshInstance;
|
|
|
|
_tileInstanceIndices.Clear();
|
|
|
|
|
2023-12-14 17:27:07 +01:00
|
|
|
int chunkSize = Size;
|
2023-11-15 20:57:25 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
foreach (Spatial node in Tiles.GetChildren()) {
|
2023-11-15 20:57:25 +01:00
|
|
|
node.QueueFree();
|
2023-11-18 22:11:34 +01:00
|
|
|
}
|
2023-11-15 20:57:25 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
foreach (int i in Enumerable.Range(0, chunkSize)) {
|
|
|
|
foreach (int j in Enumerable.Range(0, chunkSize)) {
|
|
|
|
HexTile3D tile3D = (HexTile3D)_hexTile3DScene.Instance();
|
|
|
|
tile3D.Connect("TileClicked", this, nameof(OnTileClicked));
|
|
|
|
tile3D.Connect("TileHovered", this, nameof(OnTileHovered));
|
2023-11-15 20:57:25 +01:00
|
|
|
|
2023-12-14 17:27:07 +01:00
|
|
|
tile3D.Cell.OffsetCoords = new Vector2(chunkIndex * chunkSize + new Vector2(i, j));
|
2023-11-18 22:11:34 +01:00
|
|
|
_tileInstanceIndices.Add(tileInstanceIndexStart + _tileInstanceIndices.Count);
|
2023-11-15 20:57:25 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
Transform tileTransform = Transform.Identity;
|
|
|
|
Vector2 centerPlaneCoord = _hexGrid.GetHexCenterFromOffset(new Vector2(i, j));
|
|
|
|
tileTransform.origin = new Vector3(centerPlaneCoord.x, 0, centerPlaneCoord.y);
|
|
|
|
tile3D.Transform = tileTransform;
|
2023-11-15 20:57:25 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
Tiles.AddChild(tile3D);
|
|
|
|
}
|
2023-11-15 20:57:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_multiMeshInstance.Multimesh.VisibleInstanceCount = _multiMeshInstance.Multimesh.InstanceCount;
|
2023-12-14 17:27:07 +01:00
|
|
|
|
|
|
|
GD.Print("Chunk: " + chunkIndex + " Last index: " + _tileInstanceIndices.Last());
|
2023-11-15 20:57:25 +01:00
|
|
|
}
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public void ClearContent() {
|
|
|
|
foreach (Spatial child in Entities.GetChildren()) {
|
2023-11-15 20:57:25 +01:00
|
|
|
child.QueueFree();
|
2023-11-18 22:11:34 +01:00
|
|
|
}
|
2023-11-15 20:57:25 +01:00
|
|
|
}
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public void UpdateTileTransforms() {
|
2023-11-15 20:57:25 +01:00
|
|
|
Transform chunkTransform = Transform.Identity;
|
2023-12-14 17:27:07 +01:00
|
|
|
Vector2 chunkOriginPlaneCoord = _hexGrid.GetHexCenterFromOffset(ChunkIndex * Size);
|
2023-11-15 20:57:25 +01:00
|
|
|
chunkTransform.origin = new Vector3(chunkOriginPlaneCoord.x, 0, chunkOriginPlaneCoord.y);
|
|
|
|
Transform = chunkTransform;
|
|
|
|
|
|
|
|
Basis tileOrientation = new(Vector3.Up, 90f * Mathf.Pi / 180f);
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
foreach (int i in Enumerable.Range(0, _tileInstanceIndices.Count)) {
|
2023-12-14 17:27:07 +01:00
|
|
|
int column = i % Size;
|
|
|
|
int row = i / Size;
|
2023-11-15 20:57:25 +01:00
|
|
|
|
|
|
|
Vector2 tilePlaneCoord =
|
|
|
|
_hexGrid.GetHexCenterFromOffset(new Vector2(column, row));
|
|
|
|
|
|
|
|
Transform hexTransform = new(tileOrientation,
|
|
|
|
chunkTransform.origin + new Vector3(tilePlaneCoord.x, 0, tilePlaneCoord.y));
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
if (_showHexTiles) {
|
2023-11-15 20:57:25 +01:00
|
|
|
hexTransform = new Transform(tileOrientation.Scaled(Vector3.One * 0.95f),
|
|
|
|
hexTransform.origin);
|
2023-11-18 22:11:34 +01:00
|
|
|
}
|
2023-11-15 20:57:25 +01:00
|
|
|
|
|
|
|
_multiMeshInstance.Multimesh.SetInstanceTransform(_tileInstanceIndices[i], hexTransform);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-01 16:02:39 +01:00
|
|
|
// other members
|
2023-11-18 22:11:34 +01:00
|
|
|
public void SaveToFile(string chunkName) {
|
2023-11-15 20:57:25 +01:00
|
|
|
Image image = new();
|
2023-11-01 16:02:39 +01:00
|
|
|
|
|
|
|
image.CreateFromData(Size, Size, false, Image.Format.Rgba8, TileTypeMap.GetData().GetData());
|
|
|
|
image.SavePng(chunkName + "_tileType.png");
|
|
|
|
|
|
|
|
image.CreateFromData(Size, Size, false, Image.Format.Rgba8, NavigationMap.GetData().GetData());
|
|
|
|
image.SavePng(chunkName + "_navigationMap.png");
|
|
|
|
|
|
|
|
image.CreateFromData(Size, Size, false, Image.Format.Rgba8, HeightMap.GetData().GetData());
|
|
|
|
image.SavePng(chunkName + "_heightMap.png");
|
|
|
|
}
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public void LoadFromFile(string chunkName) { }
|
2023-11-01 16:02:39 +01:00
|
|
|
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public void SetNoisemap(Texture texture) {
|
2023-11-01 16:02:39 +01:00
|
|
|
_noiseSprite.Texture = texture;
|
|
|
|
_noiseSprite.Transform =
|
|
|
|
Transform2D.Identity.Scaled(HeightmapOffscreenViewport.Size / _noiseSprite.Texture.GetSize().x);
|
|
|
|
HeightmapOffscreenViewport.RenderTargetUpdateMode = Viewport.UpdateMode.Once;
|
|
|
|
HeightMapFrameCount = 1;
|
2023-10-23 21:22:53 +02:00
|
|
|
}
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public void SetHeightmap(Texture texture) {
|
2023-11-01 16:02:39 +01:00
|
|
|
_heightmapSprite.Texture = texture;
|
|
|
|
_heightmapSprite.Transform =
|
|
|
|
Transform2D.Identity.Scaled(TileTypeOffscreenViewport.Size / _heightmapSprite.Texture.GetSize());
|
|
|
|
|
|
|
|
TileTypeOffscreenViewport.RenderTargetUpdateMode = Viewport.UpdateMode.Once;
|
|
|
|
TileTypeMapFrameCount = 1;
|
2023-10-05 18:17:48 +02:00
|
|
|
}
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public void CreateUnlockedTileTypeImage() {
|
|
|
|
TileTypeImage = TileTypeOffscreenViewport.GetTexture().GetData();
|
|
|
|
TileTypeImage.Lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void _Process(float delta) {
|
2023-10-23 21:22:53 +02:00
|
|
|
Texture tileTypeTexture = TileTypeOffscreenViewport.GetTexture();
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
if (NoiseTextureCheckerboardOverlay) {
|
2023-11-15 20:57:25 +01:00
|
|
|
Image tileTypeImage = tileTypeTexture.GetData();
|
2023-10-23 21:22:53 +02:00
|
|
|
tileTypeImage.Lock();
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
foreach (int i in Enumerable.Range(0, Size)) {
|
|
|
|
foreach (int j in Enumerable.Range(0, Size)) {
|
|
|
|
Vector2 textureCoord = new(i, j);
|
|
|
|
Color baseColor = tileTypeImage.GetPixelv(textureCoord);
|
|
|
|
|
|
|
|
if ((i + j) % 2 == 0) {
|
|
|
|
tileTypeImage.SetPixelv(textureCoord, baseColor);
|
|
|
|
} else {
|
|
|
|
tileTypeImage.SetPixelv(textureCoord, baseColor * 0.6f);
|
|
|
|
}
|
|
|
|
}
|
2023-10-23 21:22:53 +02:00
|
|
|
}
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-10-23 21:22:53 +02:00
|
|
|
tileTypeImage.Unlock();
|
2023-11-15 20:57:25 +01:00
|
|
|
ImageTexture imageTexture = new();
|
2023-11-01 16:02:39 +01:00
|
|
|
imageTexture.CreateFromImage(tileTypeImage, 0);
|
2023-10-23 21:22:53 +02:00
|
|
|
tileTypeTexture = imageTexture;
|
|
|
|
}
|
|
|
|
|
|
|
|
_rectMaterial.AlbedoTexture = tileTypeTexture;
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-10-23 21:22:53 +02:00
|
|
|
_rectMaterial.FlagsTransparent = true;
|
|
|
|
// _rectMaterial.AlbedoTexture = _heightmapRect.Texture;
|
|
|
|
_rectMaterial.Uv1Scale = new Vector3(-3, -2, 1);
|
|
|
|
_rectMaterial.Uv1Offset = Vector3.One * 2;
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-10-23 21:22:53 +02:00
|
|
|
//RectMaterial.Uv1Triplanar = true;
|
|
|
|
PlaneRectMesh.SetSurfaceMaterial(0, _rectMaterial);
|
2023-11-01 16:02:39 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
if (HeightMapFrameCount == 0) {
|
|
|
|
HeightmapOffscreenViewport.RenderTargetUpdateMode = Viewport.UpdateMode.Disabled;
|
|
|
|
}
|
2023-11-01 16:02:39 +01:00
|
|
|
|
|
|
|
HeightMapFrameCount = HeightMapFrameCount > 0 ? HeightMapFrameCount - 1 : 0;
|
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
if (TileTypeMapFrameCount == 0) {
|
|
|
|
TileTypeOffscreenViewport.RenderTargetUpdateMode = Viewport.UpdateMode.Disabled;
|
|
|
|
}
|
2023-11-01 16:02:39 +01:00
|
|
|
|
|
|
|
TileTypeMapFrameCount = TileTypeMapFrameCount > 0 ? TileTypeMapFrameCount - 1 : 0;
|
|
|
|
|
2023-10-23 21:22:53 +02:00
|
|
|
PlaneRectMesh.MaterialOverride = null;
|
|
|
|
}
|
2023-11-15 20:57:25 +01:00
|
|
|
|
2023-11-18 22:11:34 +01:00
|
|
|
public void OnTileClicked(HexTile3D tile) {
|
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-11-15 20:57:25 +01:00
|
|
|
EmitSignal("TileHovered", tile);
|
|
|
|
}
|
2023-10-23 21:22:53 +02:00
|
|
|
}
|