116 lines
3.8 KiB
C#
116 lines
3.8 KiB
C#
|
using Godot;
|
||
|
using System;
|
||
|
|
||
|
public class NavigationTests : Spatial
|
||
|
{
|
||
|
// Declare member variables here. Examples:
|
||
|
// private int a = 2;
|
||
|
// private string b = "text";
|
||
|
|
||
|
private StaticBody _groundLayer;
|
||
|
private HexGrid _hexGrid;
|
||
|
private HexCell _currentTile;
|
||
|
private HexCell _lastTile;
|
||
|
|
||
|
private Spatial _mouseHighlight;
|
||
|
private ShaderMaterial _tileMaterial;
|
||
|
|
||
|
private EditorUI _editorUi;
|
||
|
private TileWorld _tileWorld;
|
||
|
private StreamContainer _streamContainer;
|
||
|
private Player _player;
|
||
|
|
||
|
// Called when the node enters the scene tree for the first time.
|
||
|
public override void _Ready()
|
||
|
{
|
||
|
_hexGrid = new HexGrid();
|
||
|
_currentTile = new HexCell();
|
||
|
_lastTile = new HexCell();
|
||
|
|
||
|
_tileMaterial = GD.Load<ShaderMaterial>("materials/HexTileTextureLookup.tres");
|
||
|
|
||
|
_groundLayer = GetNode<StaticBody>("GroundLayer");
|
||
|
|
||
|
_mouseHighlight = GetNode<Spatial>("MouseHighlight");
|
||
|
|
||
|
_editorUi = GetNode<EditorUI>("EditorUI");
|
||
|
_tileWorld = GetNode<TileWorld>("TileWorld");
|
||
|
_tileWorld.Connect("WorldGenerated", this, nameof(OnWorldGenerated));
|
||
|
_streamContainer = GetNode<StreamContainer>("StreamContainer");
|
||
|
|
||
|
_streamContainer.SetCenterTile(_currentTile);
|
||
|
|
||
|
_player = GetNode<Player>("Player");
|
||
|
|
||
|
// input handling
|
||
|
_groundLayer.Connect("input_event", this, nameof(OnGroundLayerInputEvent));
|
||
|
_streamContainer.Connect("TileClicked", this, nameof(OnTileClicked));
|
||
|
_streamContainer.Connect("TileHovered", this, nameof(OnTileHovered));
|
||
|
}
|
||
|
|
||
|
public void OnWorldGenerated()
|
||
|
{
|
||
|
_streamContainer.OnWorldGenerated();
|
||
|
|
||
|
// Properly place the Player
|
||
|
Vector2 centerTileCoord = (Vector2.One * _tileWorld.Size / 2).Round();
|
||
|
Vector3 worldCenterTileCoords = _tileWorld.GetTileWorldCenterFromOffset(centerTileCoord);
|
||
|
worldCenterTileCoords.y = _tileWorld.GetHeightAtOffset(centerTileCoord);
|
||
|
Transform playerTransform = Transform.Identity;
|
||
|
playerTransform.origin = worldCenterTileCoords;
|
||
|
_player.Transform = playerTransform;
|
||
|
|
||
|
ImageTexture newWorldTexture = new ImageTexture();
|
||
|
newWorldTexture.CreateFromImage(_tileWorld.Colormap,
|
||
|
(uint)(Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat));
|
||
|
_tileMaterial.SetShaderParam("MapAlbedoTexture", newWorldTexture);
|
||
|
_tileMaterial.SetShaderParam("TextureSize", (int)_tileWorld.Colormap.GetSize().x);
|
||
|
}
|
||
|
|
||
|
|
||
|
public void UpdateCurrentTile(HexCell tile)
|
||
|
{
|
||
|
if (_currentTile.AxialCoords == tile.AxialCoords)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
_lastTile = _currentTile;
|
||
|
_currentTile = tile;
|
||
|
|
||
|
GD.Print("Current tile: " + _currentTile.OffsetCoords);
|
||
|
|
||
|
if (_lastTile.OffsetCoords != _currentTile.OffsetCoords && _editorUi != null)
|
||
|
{
|
||
|
_editorUi.currentTileOffset = _currentTile.OffsetCoords;
|
||
|
}
|
||
|
|
||
|
Vector2 planeCoords = _hexGrid.GetHexCenterFromOffset(_currentTile.OffsetCoords);
|
||
|
Transform tileTransform = Transform.Identity;
|
||
|
tileTransform.origin.x = planeCoords.x;
|
||
|
tileTransform.origin.y = _tileWorld.GetHeightAtOffset(_currentTile.OffsetCoords) + 0.1f;
|
||
|
tileTransform.origin.z = planeCoords.y;
|
||
|
|
||
|
_mouseHighlight.Transform = tileTransform;
|
||
|
}
|
||
|
|
||
|
public void OnGroundLayerInputEvent(Node camera, InputEvent inputEvent, Vector3 position, Vector3 normal,
|
||
|
int shapeIndex)
|
||
|
{
|
||
|
UpdateCurrentTile(_hexGrid.GetHexAt(new Vector2(position.x, position.z)));
|
||
|
}
|
||
|
|
||
|
public void OnTileClicked(HexTile3D tile)
|
||
|
{
|
||
|
if (_editorUi != null)
|
||
|
{
|
||
|
_editorUi.OnTileClicked(tile.OffsetCoords);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void OnTileHovered(HexTile3D tile)
|
||
|
{
|
||
|
UpdateCurrentTile(tile.Cell);
|
||
|
}
|
||
|
}
|