GodotComponentTest/scenes/tests/NavigationTests.cs

131 lines
4.8 KiB
C#

using System.Diagnostics;
using Godot;
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 World _world;
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<ShaderMaterial>("materials/HexTileTextureLookup.tres");
_mouseHighlight = GetNode<Spatial>("MouseHighlight");
_editorUi = GetNode<EditorUI>("EditorUI");
_world = GetNode<World>("World");
_world.Connect("WorldGenerated", this, nameof(OnWorldGenerated));
_streamContainer = GetNode<StreamContainer>("StreamContainer");
_streamContainer.SetCenterTile(_currentTile);
_player = GetNode<Player>("Player");
_playerNavigationComponent = _player.GetNode<NavigationComponent>("Navigation");
// input handling
// _groundLayer.Connect("input_event", this, nameof(OnGroundLayerInputEvent));
_world.Connect("TileClicked", this, nameof(OnTileClicked));
_world.Connect("TileHovered", this, nameof(OnTileHovered));
CorrectEntityGridPositions();
}
public void CorrectEntityGridPositions() {
Spatial entitiesNode = GetNode<Spatial>("Entities");
if (entitiesNode == null) {
return;
}
Array entities = entitiesNode.GetChildren();
foreach (Spatial entity in entities) {
Vector2 entityPlaneCoords = new(entity.GlobalTranslation.x, entity.GlobalTranslation.z);
HexCell entityCell = _world.HexGrid.GetHexAt(entityPlaneCoords);
_world.MarkCellUnwalkable(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 * _world.Size / 2).Round();
Vector3 worldCenterTileCoords = _world.GetTileWorldCenterFromOffset(centerTileCoord);
worldCenterTileCoords.y = _world.GetHeightAtOffset(centerTileCoord);
Transform playerTransform = Transform.Identity;
playerTransform.origin = worldCenterTileCoords;
_player.Transform = playerTransform;
ImageTexture newWorldTexture = new();
newWorldTexture.CreateFromImage(_world.ColormapImage,
(uint)(Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat));
_tileMaterial.SetShaderParam("MapAlbedoTexture", newWorldTexture);
_tileMaterial.SetShaderParam("TextureSize", (int)_world.ColormapImage.GetSize().x);
CorrectEntityGridPositions();
}
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 = _world.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);
if (_editorUi.CurrentInputMode == EditorUI.InputMode.Navigate) {
_playerNavigationComponent.FindPath(_player, _player.GlobalTranslation, tile.GlobalTranslation);
}
}
}
public void OnTileHovered(HexTile3D tile) {
UpdateCurrentTile(tile.Cell);
Debug.Assert(_playerNavigationComponent != null);
_playerNavigationComponent.FindPath(_player, _player.GlobalTranslation, tile.GlobalTranslation);
}
}