GodotComponentTest/scenes/StreamContainer.cs

69 lines
2.4 KiB
C#

using Godot;
using System;
public class StreamContainer : Spatial
{
// scene nodes
private MeshInstance _bounds;
private Spatial _activeTiles;
// resources
private PackedScene _hexTileScene = GD.Load<PackedScene>("res://scenes/HexTile3D.tscn");
// exports
[Export] public Vector2 _dimensions = new Vector2(8, 4);
// other members
private Rect2 _worldRect;
private Rect2 _offsetCoordRect;
private HexGrid _hexGrid;
public Rect2 OffsetCoordRect
{
get { return _offsetCoordRect; }
}
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_bounds = GetNode<MeshInstance>("Bounds");
_activeTiles = GetNode<Spatial>("ActiveTiles");
_hexGrid = new HexGrid();
Transform boundsTransform = Transform.Identity;
boundsTransform = boundsTransform.Scaled(new Vector3(_dimensions.x, 1, _dimensions.y));
_bounds.Transform = boundsTransform;
}
public void UpdateRects(Vector2 centerPlane)
{
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());
// 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));
_offsetCoordRect = new Rect2(bottomLeftCell.OffsetCoords,
topRightCell.OffsetCoords - bottomLeftCell.OffsetCoords + Vector2.One);
GD.Print("Offset rect now: " + _offsetCoordRect.ToString() + " center: " + _offsetCoordRect.GetCenter());
Transform boundsTransform = _bounds.Transform;
boundsTransform.origin.x = centerPlane.x;
boundsTransform.origin.z = centerPlane.y;
_bounds.Transform = boundsTransform;
GD.Print("Bounds Transform: " + boundsTransform.ToString());
GD.Print("Bounds Global : " + _bounds.GlobalTransform.ToString());
}
public void SetCenterTile(HexCell cell)
{
UpdateRects(_hexGrid.GetHexCenter(cell));
}
}