using Godot; using System; using System.Diagnostics; using Godot.Collections; public class NavigationTests : Spatial { 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; private NavigationComponent _playerNavigationComponent; public override void _Ready() { _hexGrid = new HexGrid(); _currentTile = new HexCell(); _lastTile = new HexCell(); _tileMaterial = GD.Load("materials/HexTileTextureLookup.tres"); _mouseHighlight = GetNode("MouseHighlight"); _editorUi = GetNode("EditorUI"); _tileWorld = GetNode("TileWorld"); _tileWorld.Connect("WorldGenerated", this, nameof(OnWorldGenerated)); _streamContainer = GetNode("StreamContainer"); _streamContainer.SetCenterTile(_currentTile); _player = GetNode("Player"); _playerNavigationComponent = _player.GetNode("Navigation"); // input handling // _groundLayer.Connect("input_event", this, nameof(OnGroundLayerInputEvent)); _streamContainer.Connect("TileClicked", this, nameof(OnTileClicked)); _streamContainer.Connect("TileHovered", this, nameof(OnTileHovered)); CorrectEntityGridPositions(); } public void CorrectEntityGridPositions() { Spatial entitiesNode = GetNode("Entities"); if (entitiesNode == null) { return; } var entities = entitiesNode.GetChildren(); foreach (Spatial entity in entities) { Vector2 entityPlaneCoords = new Vector2(entity.GlobalTranslation.x, entity.GlobalTranslation.z); HexCell entityCell = _hexGrid.GetHexAt(entityPlaneCoords); _tileWorld.HexGrid.AddObstacle(entityCell); Vector2 cellPlaneCoords = _hexGrid.GetHexCenterFromOffset(entityCell.OffsetCoords); Vector3 entityGlobalTranslation = entity.GlobalTranslation; entityGlobalTranslation.x = cellPlaneCoords.x; entityGlobalTranslation.z = cellPlaneCoords.y; entity.GlobalTranslation = entityGlobalTranslation; } } 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.ColormapImage, (uint)(Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat)); _tileMaterial.SetShaderParam("MapAlbedoTexture", newWorldTexture); _tileMaterial.SetShaderParam("TextureSize", (int)_tileWorld.ColormapImage.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); Debug.Assert(_playerNavigationComponent != null); _playerNavigationComponent.FindPath(_player, _player.GlobalTranslation, tile.GlobalTranslation); } }