GodotComponentTest/scenes/TileInstanceManager.cs

164 lines
4.8 KiB
C#
Raw Normal View History

using System.Linq;
using Godot;
using Godot.Collections;
public class TileInstanceManager : Spatial
{
2023-11-01 17:59:43 +01:00
// other members
[Export] public bool ShowHexTiles;
[Export] public Vector2 ViewCenterPlaneCoord;
private World _world;
private readonly Array<SceneTileChunk> _sceneTileChunks = new();
private ImageTexture _viewTileTypeTexture;
// ui elements
// scene nodes
// resources
// exports
[Export] public NodePath World;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_world = GetNode<World>(World);
_world.Connect("OnTilesChanged", this, nameof(HandleWorldTileChange));
}
public override void _Process(float delta)
{
}
2023-11-01 17:59:43 +01:00
private SceneTileChunk CreateSceneTileChunk(Vector2 chunkIndex)
{
2023-11-01 17:59:43 +01:00
var sceneTileChunk = new SceneTileChunk(chunkIndex);
2023-11-01 17:59:43 +01:00
foreach (var hexTile3D in sceneTileChunk.TileNodes)
{
hexTile3D.Connect("TileClicked", this, nameof(OnTileClicked));
hexTile3D.Connect("TileHovered", this, nameof(OnTileHovered));
}
2023-11-01 17:59:43 +01:00
return sceneTileChunk;
}
2023-11-01 17:59:43 +01:00
private SceneTileChunk FindSceneTileChunkAtIndex(Vector2 chunkIndex)
{
foreach (Spatial child in GetChildren())
{
2023-11-01 17:59:43 +01:00
var sceneTileChunk = child as SceneTileChunk;
if (sceneTileChunk == null) continue;
2023-11-01 17:59:43 +01:00
if (sceneTileChunk.ChunkIndex == chunkIndex) return sceneTileChunk;
}
return null;
}
2023-11-01 17:59:43 +01:00
private void HandleWorldTileChange(Array<Vector2> removedChunkIndices, Array<Vector2> addedChunkIndices)
{
Array<SceneTileChunk> removedChunks = new();
2023-11-01 17:59:43 +01:00
foreach (var chunkIndex in removedChunkIndices)
{
2023-11-01 17:59:43 +01:00
var chunk = FindSceneTileChunkAtIndex(chunkIndex);
if (chunk != null) removedChunks.Add(chunk);
}
2023-11-01 17:59:43 +01:00
foreach (var 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;
if (ShowHexTiles)
2023-11-01 17:59:43 +01:00
foreach (var tile3D in sceneTileChunk.TileNodes)
tile3D.Transform = new Transform(Basis.Identity.Scaled(Vector3.One * 0.95f),
tile3D.Transform.origin);
2023-11-01 17:59:43 +01:00
_sceneTileChunks.Add(sceneTileChunk);
}
2023-11-01 17:59:43 +01:00
GD.Print("Removed Chunks " + removedChunkIndices.Count);
GD.Print("Added Chunks " + addedChunkIndices.Count);
GD.Print("Removed chunk count: " + removedChunks.Count);
}
2023-11-01 17:59:43 +01:00
public void OnTileClicked(HexTile3D tile)
{
EmitSignal("TileClicked", tile);
}
2023-11-01 17:59:43 +01:00
public void OnTileHovered(HexTile3D tile)
{
EmitSignal("TileHovered", tile);
}
2023-11-01 17:59:43 +01:00
// signals
[Signal]
private delegate void TileClicked(HexTile3D tile3d);
[Signal]
private delegate void TileHovered(HexTile3D tile3d);
private class SceneTileChunk : Spatial
{
private Vector2 _chunkIndex = Vector2.Inf;
private readonly PackedScene _hexTile3DScene = GD.Load<PackedScene>("res://scenes/HexTile3D.tscn");
private readonly HexGrid HexGrid = new();
public readonly Array<HexTile3D> TileNodes = new();
public SceneTileChunk(Vector2 chunkIndex)
{
var chunkSize = global::World.ChunkSize;
foreach (var i in Enumerable.Range(0, chunkSize))
foreach (var j in Enumerable.Range(0, chunkSize))
{
var tile3D = (HexTile3D)_hexTile3DScene.Instance();
var tileTransform = Transform.Identity;
var 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;
}
public Vector2 ChunkIndex
{
get => _chunkIndex;
set
{
var chunkTransform = Transform.Identity;
var chunkOriginPlaneCoord = HexGrid.GetHexCenterFromOffset(value * global::World.ChunkSize);
chunkTransform.origin = new Vector3(chunkOriginPlaneCoord.x, 0, chunkOriginPlaneCoord.y);
Transform = chunkTransform;
_chunkIndex = value;
}
}
}
}