2023-10-23 21:22:53 +02:00
|
|
|
using System.Linq;
|
2023-10-05 18:17:48 +02:00
|
|
|
using Godot;
|
2023-10-14 21:54:06 +02:00
|
|
|
using Godot.Collections;
|
2023-10-05 18:17:48 +02:00
|
|
|
|
|
|
|
public class WorldView : Spatial
|
|
|
|
{
|
|
|
|
// ui elements
|
|
|
|
|
|
|
|
// scene nodes
|
|
|
|
|
|
|
|
// resources
|
|
|
|
|
|
|
|
// exports
|
|
|
|
[Export] public NodePath World;
|
|
|
|
[Export] public Vector2 ViewCenterPlaneCoord;
|
2023-10-27 22:58:37 +02:00
|
|
|
[Export] public bool ShowHexTiles = false;
|
2023-10-05 18:17:48 +02:00
|
|
|
|
|
|
|
// signals
|
2023-10-23 21:22:53 +02:00
|
|
|
[Signal]
|
|
|
|
delegate void TileClicked(HexTile3D tile3d);
|
|
|
|
[Signal]
|
|
|
|
delegate void TileHovered(HexTile3D tile3d);
|
2023-10-05 18:17:48 +02:00
|
|
|
|
|
|
|
// other members
|
|
|
|
private World _world;
|
2023-10-23 21:22:53 +02:00
|
|
|
|
|
|
|
private class SceneTileChunk : Spatial
|
|
|
|
{
|
|
|
|
private Vector2 _chunkIndex = Vector2.Inf;
|
|
|
|
public Vector2 ChunkIndex
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return _chunkIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
{
|
|
|
|
Transform chunkTransform = Transform.Identity;
|
|
|
|
Vector2 chunkOriginPlaneCoord = HexGrid.GetHexCenterFromOffset(value * global::World.ChunkSize);
|
|
|
|
chunkTransform.origin = new Vector3(chunkOriginPlaneCoord.x, 0, chunkOriginPlaneCoord.y);
|
|
|
|
Transform = chunkTransform;
|
|
|
|
_chunkIndex = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Array<HexTile3D> TileNodes = new();
|
|
|
|
|
|
|
|
private PackedScene _hexTile3DScene = GD.Load<PackedScene>("res://scenes/HexTile3D.tscn");
|
|
|
|
private HexGrid HexGrid = new();
|
|
|
|
|
|
|
|
public SceneTileChunk(Vector2 chunkIndex, int size)
|
|
|
|
{
|
|
|
|
foreach (int i in Enumerable.Range(0, size))
|
|
|
|
{
|
|
|
|
foreach (int j in Enumerable.Range(0, size))
|
|
|
|
{
|
|
|
|
HexTile3D tile3D = (HexTile3D)_hexTile3DScene.Instance();
|
|
|
|
|
|
|
|
Transform tileTransform = Transform.Identity;
|
|
|
|
Vector2 centerPlaneCoord = HexGrid.GetHexCenterFromOffset(new Vector2(i, j));
|
|
|
|
tileTransform.origin = new Vector3(centerPlaneCoord.x, 0, centerPlaneCoord.y);
|
|
|
|
tile3D.Transform = tileTransform;
|
|
|
|
|
|
|
|
TileNodes.Add(tile3D);
|
|
|
|
AddChild(tile3D);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ChunkIndex = chunkIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Array<SceneTileChunk> _sceneTileChunks = new Array<SceneTileChunk>();
|
2023-10-05 18:17:48 +02:00
|
|
|
|
|
|
|
// Called when the node enters the scene tree for the first time.
|
|
|
|
public override void _Ready()
|
|
|
|
{
|
|
|
|
_world = GetNode<World>(World);
|
2023-10-14 21:54:06 +02:00
|
|
|
|
|
|
|
_world.Connect("OnTilesChanged", this, nameof(HandleWorldTileChange));
|
2023-10-05 18:17:48 +02:00
|
|
|
}
|
|
|
|
|
2023-10-23 21:22:53 +02:00
|
|
|
|
2023-10-05 18:17:48 +02:00
|
|
|
public override void _Process(float delta)
|
|
|
|
{
|
|
|
|
}
|
2023-10-14 21:54:06 +02:00
|
|
|
|
2023-10-23 21:22:53 +02:00
|
|
|
SceneTileChunk CreateSceneTileChunk(Vector2 chunkIndex)
|
|
|
|
{
|
|
|
|
SceneTileChunk sceneTileChunk = new SceneTileChunk(chunkIndex, global::World.ChunkSize);
|
|
|
|
|
|
|
|
foreach (HexTile3D hexTile3D in sceneTileChunk.TileNodes)
|
|
|
|
{
|
|
|
|
hexTile3D.Connect("TileClicked", this, nameof(OnTileClicked));
|
|
|
|
hexTile3D.Connect("TileHovered", this, nameof(OnTileHovered));
|
|
|
|
}
|
|
|
|
|
|
|
|
return sceneTileChunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
SceneTileChunk RemoveChunkFromScene(Vector2 chunkIndex)
|
|
|
|
{
|
|
|
|
foreach (Spatial child in GetChildren())
|
|
|
|
{
|
|
|
|
SceneTileChunk sceneTileChunk = child as SceneTileChunk;
|
|
|
|
if (sceneTileChunk == null)
|
|
|
|
{
|
|
|
|
RemoveChild(child);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sceneTileChunk.ChunkIndex == chunkIndex)
|
|
|
|
{
|
|
|
|
return sceneTileChunk;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-10-14 21:54:06 +02:00
|
|
|
private void HandleWorldTileChange(Array<Vector2> removedChunkIndices, Array<Vector2> addedChunkIndices)
|
|
|
|
{
|
2023-10-23 21:22:53 +02:00
|
|
|
Array<SceneTileChunk> removedChunks = new();
|
|
|
|
foreach (Vector2 chunkIndex in removedChunkIndices)
|
|
|
|
{
|
|
|
|
SceneTileChunk chunk = RemoveChunkFromScene(chunkIndex);
|
|
|
|
if (chunk != null)
|
|
|
|
{
|
|
|
|
removedChunks.Add(chunk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (Vector2 chunkIndex in addedChunkIndices)
|
|
|
|
{
|
|
|
|
SceneTileChunk sceneTileChunk = null;
|
|
|
|
if (removedChunks.Count > 0)
|
|
|
|
{
|
|
|
|
sceneTileChunk = removedChunks[^1];
|
|
|
|
removedChunks.RemoveAt(removedChunks.Count - 1);
|
|
|
|
GD.Print("Reused SceneTileChunk");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sceneTileChunk = CreateSceneTileChunk(chunkIndex);
|
|
|
|
AddChild(sceneTileChunk);
|
|
|
|
GD.Print("Created SceneTileChunk");
|
|
|
|
}
|
|
|
|
|
|
|
|
sceneTileChunk.ChunkIndex = chunkIndex;
|
2023-10-27 22:58:37 +02:00
|
|
|
|
|
|
|
if (ShowHexTiles)
|
|
|
|
{
|
|
|
|
foreach (HexTile3D tile3D in sceneTileChunk.TileNodes)
|
|
|
|
{
|
|
|
|
tile3D.Transform = new Transform(Basis.Identity.Scaled(Vector3.One * 0.95f),
|
|
|
|
tile3D.Transform.origin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-23 21:22:53 +02:00
|
|
|
_sceneTileChunks.Add(sceneTileChunk);
|
|
|
|
}
|
|
|
|
|
2023-10-14 21:54:06 +02:00
|
|
|
GD.Print("Removed Chunks " + removedChunkIndices.Count);
|
|
|
|
GD.Print("Added Chunks " + addedChunkIndices.Count);
|
2023-10-23 21:22:53 +02:00
|
|
|
GD.Print("Removed chunk count: " + removedChunks.Count);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnTileClicked(HexTile3D tile)
|
|
|
|
{
|
|
|
|
EmitSignal("TileClicked", tile);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnTileHovered(HexTile3D tile)
|
|
|
|
{
|
|
|
|
EmitSignal("TileHovered", tile);
|
2023-10-14 21:54:06 +02:00
|
|
|
}
|
2023-10-05 18:17:48 +02:00
|
|
|
}
|