GodotComponentTest/scenes/StreamContainer.cs

144 lines
5.0 KiB
C#
Raw Normal View History

2022-12-03 21:58:28 +01:00
using System;
2022-12-02 21:09:40 +01:00
using Godot;
2022-12-03 20:34:01 +01:00
using System.Collections.Generic;
using System.Linq;
//using Dictionary = Godot.Collections.Dictionary;
2022-12-02 21:09:40 +01:00
public class StreamContainer : Spatial
{
// scene nodes
private MeshInstance _bounds;
private Spatial _activeTiles;
2022-12-03 20:34:01 +01:00
2022-12-02 21:09:40 +01:00
// resources
private PackedScene _hexTileScene = GD.Load<PackedScene>("res://scenes/HexTile3D.tscn");
2022-12-03 20:34:01 +01:00
2022-12-03 00:43:19 +01:00
// exports
2022-12-03 20:34:01 +01:00
[Export] public Vector2 Dimensions = new Vector2(8, 4);
2022-12-03 00:43:19 +01:00
// other members
private Rect2 _worldRect;
2022-12-03 20:34:01 +01:00
private Rect2 _currentOffsetCoordRect;
private Rect2 _oldOffsetCoordRect;
2022-12-03 00:43:19 +01:00
private HexGrid _hexGrid;
2022-12-03 21:58:28 +01:00
private Random _tileTypeRandom;
2022-12-03 00:43:19 +01:00
2022-12-03 21:58:28 +01:00
private Dictionary<Vector2, HexTile3D> _coordToTile = new Dictionary<Vector2, HexTile3D>();
2022-12-03 20:34:01 +01:00
public List<Vector2> RemovedCoords = new List<Vector2>();
public List<Vector2> AddedCoords = new List<Vector2>();
public Rect2 CurrentOffsetCoordRect
2022-12-03 00:43:19 +01:00
{
2022-12-03 20:34:01 +01:00
get { return _currentOffsetCoordRect; }
2022-12-03 00:43:19 +01:00
}
2022-12-03 20:34:01 +01:00
2022-12-02 21:09:40 +01:00
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_bounds = GetNode<MeshInstance>("Bounds");
_activeTiles = GetNode<Spatial>("ActiveTiles");
2022-12-03 21:58:28 +01:00
_tileTypeRandom = new Random();
2022-12-03 20:34:01 +01:00
2022-12-03 00:43:19 +01:00
_hexGrid = new HexGrid();
2022-12-02 21:09:40 +01:00
Transform boundsTransform = Transform.Identity;
2022-12-03 20:34:01 +01:00
boundsTransform = boundsTransform.Scaled(new Vector3(Dimensions.x, 1, Dimensions.y));
2022-12-03 00:43:19 +01:00
_bounds.Transform = boundsTransform;
}
2022-12-03 20:34:01 +01:00
2022-12-03 00:43:19 +01:00
public void UpdateRects(Vector2 centerPlane)
{
2022-12-03 20:34:01 +01:00
_oldOffsetCoordRect = _currentOffsetCoordRect;
Vector2 bottomLeftCoord = centerPlane - new Vector2(Dimensions.x / 2, Dimensions.y / 2);
Vector2 topRightCoord = centerPlane + new Vector2(Dimensions.x / 2, Dimensions.y / 2);
_worldRect = new Rect2(bottomLeftCoord, Dimensions);
// GD.Print("World rect now: " + _worldRect.ToString() + " center: " + _worldRect.GetCenter());
2022-12-03 00:43:19 +01:00
// y axis needs to be inverted as HexGrid's offset coordinates use the opposite axis direction
HexCell bottomLeftCell = _hexGrid.GetHexAt(new Vector2(bottomLeftCoord.x, topRightCoord.y));
HexCell topRightCell = _hexGrid.GetHexAt(new Vector2(topRightCoord.x, bottomLeftCoord.y));
2022-12-03 20:34:01 +01:00
_currentOffsetCoordRect = new Rect2(bottomLeftCell.OffsetCoords,
2022-12-03 00:43:19 +01:00
topRightCell.OffsetCoords - bottomLeftCell.OffsetCoords + Vector2.One);
2022-12-03 20:34:01 +01:00
// GD.Print("Offset rect now: " + _currentOffsetCoordRect.ToString() + " center: " +
// _currentOffsetCoordRect.GetCenter());
2022-12-03 00:43:19 +01:00
Transform boundsTransform = _bounds.Transform;
boundsTransform.origin.x = centerPlane.x;
boundsTransform.origin.z = centerPlane.y;
2022-12-02 21:09:40 +01:00
_bounds.Transform = boundsTransform;
2022-12-03 20:34:01 +01:00
// GD.Print("Bounds Transform: " + boundsTransform.ToString());
// GD.Print("Bounds Global : " + _bounds.GlobalTransform.ToString());
if (!_currentOffsetCoordRect.Equals(_oldOffsetCoordRect))
{
UpdateTileCache();
}
2022-12-03 00:43:19 +01:00
}
2022-12-03 20:34:01 +01:00
public void UpdateTileCache()
{
RemovedCoords.Clear();
AddedCoords.Clear();
2022-12-03 00:43:19 +01:00
2022-12-03 20:34:01 +01:00
Rect2 expandedRect = _currentOffsetCoordRect.Merge(_oldOffsetCoordRect);
foreach (int coord_x in Enumerable.Range(Mathf.FloorToInt(expandedRect.Position.x), Mathf.CeilToInt(expandedRect.Size.x)))
{
foreach (int coord_y in Enumerable.Range(Mathf.FloorToInt(expandedRect.Position.y),
Mathf.CeilToInt(expandedRect.Size.y)))
{
Vector2 coord = new Vector2(coord_x, coord_y);
bool isInCurrent = _currentOffsetCoordRect.HasPoint(coord);
bool isInOld = _oldOffsetCoordRect.HasPoint(coord);
if (isInCurrent && !isInOld)
{
AddedCoords.Add(coord);
GetTile3dAt(coord);
}
else if (isInOld && !isInCurrent)
{
RemovedCoords.Add(coord);
if (_coordToTile.Keys.Contains(coord))
{
Spatial tile3d = _coordToTile[coord];
_activeTiles.RemoveChild(tile3d);
tile3d.QueueFree();
2022-12-03 21:58:28 +01:00
_coordToTile.Remove(coord);
2022-12-03 20:34:01 +01:00
}
}
}
}
}
public Spatial GetTile3dAt(Vector2 offsetCoords)
{
if (!_coordToTile.Keys.Contains(offsetCoords))
{
2022-12-03 21:58:28 +01:00
HexTile3D tile3d = (HexTile3D)_hexTileScene.Instance();
tile3d.OffsetCoords = offsetCoords;
2022-12-03 20:34:01 +01:00
_activeTiles.AddChild(tile3d);
2022-12-03 21:58:28 +01:00
Transform tileTransform = tile3d.Transform;
tileTransform.origin.y = GD.Randf() * 0.2f;
tile3d.Transform = tileTransform;
2022-12-03 21:58:28 +01:00
tile3d.Type = HexTile3D.ValidTileTypes[_tileTypeRandom.Next(HexTile3D.ValidTileTypes.Length)];
2022-12-03 20:34:01 +01:00
_coordToTile[offsetCoords] = tile3d;
}
return _coordToTile[offsetCoords];
}
2022-12-03 00:43:19 +01:00
public void SetCenterTile(HexCell cell)
{
UpdateRects(_hexGrid.GetHexCenter(cell));
2022-12-02 21:09:40 +01:00
}
2022-12-03 20:34:01 +01:00
}