2022-12-02 21:09:40 +01:00
|
|
|
using System;
|
|
|
|
using Godot;
|
|
|
|
|
|
|
|
public class HexGrid : Resource
|
|
|
|
{
|
|
|
|
private Vector2 _baseHexSize = new Vector2(1, Mathf.Sqrt(3) / 2);
|
|
|
|
private Vector2 _hexSize = new Vector2(1, Mathf.Sqrt(3) / 2);
|
|
|
|
private Vector2 _hexScale = new Vector2(1, 1);
|
|
|
|
private Godot.Transform2D _hexTransform;
|
|
|
|
private Godot.Transform2D _hexTransformInv;
|
|
|
|
|
|
|
|
public Vector2 HexSize
|
|
|
|
{
|
|
|
|
get { return _hexSize; }
|
|
|
|
}
|
2022-12-28 16:22:53 +01:00
|
|
|
|
2022-12-02 21:09:40 +01:00
|
|
|
public Vector2 HexScale
|
|
|
|
{
|
2022-12-28 16:22:53 +01:00
|
|
|
get { return _hexScale; }
|
2022-12-02 21:09:40 +01:00
|
|
|
set
|
|
|
|
{
|
|
|
|
_hexScale = value;
|
|
|
|
_hexSize = _baseHexSize * _hexScale;
|
|
|
|
_hexTransform = new Transform2D(
|
2022-12-28 16:22:53 +01:00
|
|
|
new Vector2(_hexSize.x * 3 / 4, -_hexSize.y / 2),
|
2022-12-02 21:09:40 +01:00
|
|
|
new Vector2(0, -_hexSize.y),
|
|
|
|
new Vector2(0, 0)
|
|
|
|
);
|
2022-12-28 16:22:53 +01:00
|
|
|
|
2022-12-02 21:09:40 +01:00
|
|
|
_hexTransformInv = _hexTransform.AffineInverse();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public HexGrid()
|
|
|
|
{
|
|
|
|
HexScale = new Vector2(1, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Vector2 GetHexCenter(HexCell cell)
|
|
|
|
{
|
|
|
|
return _hexTransform * cell.AxialCoords;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Vector2 GetHexCenterFromOffset(Vector2 offsetCoord)
|
|
|
|
{
|
|
|
|
HexCell cell = new HexCell();
|
|
|
|
cell.OffsetCoords = offsetCoord;
|
|
|
|
return GetHexCenter(cell);
|
|
|
|
}
|
|
|
|
|
|
|
|
public HexCell GetHexAtOffset(Vector2 offsetCoord)
|
|
|
|
{
|
|
|
|
HexCell cell = new HexCell();
|
|
|
|
cell.OffsetCoords = offsetCoord;
|
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
|
|
|
public HexCell GetHexAt(Vector2 planeCoord)
|
|
|
|
{
|
|
|
|
HexCell result = new HexCell(_hexTransformInv * planeCoord);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|