GodotComponentTest/scenes/tests/NavigationTests.cs

119 lines
4.4 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 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");
_player = GetNode<Player>("Player");
_playerNavigationComponent = _player.GetNode<NavigationComponent>("Navigation");
// connect signals
_world.Connect("TileClicked", this, nameof(OnTileClicked));
_world.Connect("TileHovered", this, nameof(OnTileHovered));
_world.Connect("OnWorldViewTileTypeImageChanged", this, nameof(OnWorldViewTileTypeImageChanged));
_world.Connect("OnHeightmapImageChanged", this, nameof(OnHeightmapImageChanged));
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;
}
}
private void OnHeightmapImageChanged(Image heightmapImage) {
ImageTexture newHeightmapTexture = new();
newHeightmapTexture.CreateFromImage(heightmapImage,
(uint)(Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat));
}
private void OnWorldViewTileTypeImageChanged(Image viewTileTypeImage) {
ImageTexture newWorldTexture = new();
newWorldTexture.CreateFromImage(viewTileTypeImage,
(uint)(Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat));
_tileMaterial.SetShaderParam("MapAlbedoTexture", newWorldTexture);
_tileMaterial.SetShaderParam("TextureSize", (int)newWorldTexture.GetSize().x);
_tileMaterial.SetShaderParam("CoordinateOffsetU", (int)_world.WorldTextureCoordinateOffset.x);
_tileMaterial.SetShaderParam("CoordinateOffsetV", (int)_world.WorldTextureCoordinateOffset.y);
}
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);
}
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);
}
}