From d963c90912eaf0a6c4a09e7b2198766c50a83c99 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Sat, 12 Aug 2023 18:16:45 +0200 Subject: [PATCH] Minor improvement of the NavigationTest scene. --- HexGrid.cs | 2 + scenes/Game.cs | 1 - scenes/TileWorld.cs | 30 ++++++----- scenes/tests/EditorUI.cs | 91 +++++++++++++++++++++++++++------ scenes/tests/NavigationTests.cs | 11 +++- ui/EditorUI.tscn | 24 ++++++++- 6 files changed, 127 insertions(+), 32 deletions(-) diff --git a/HexGrid.cs b/HexGrid.cs index 816765b..8f2cd7c 100644 --- a/HexGrid.cs +++ b/HexGrid.cs @@ -243,6 +243,7 @@ public class HexGrid : Resource FindPathCheckedCells++; HexCell currentHex = new HexCell(frontier.Dequeue()); Vector2 currentAxial = currentHex.AxialCoords; + if (currentHex == goalHex) { break; @@ -282,6 +283,7 @@ public class HexGrid : Resource if (!cameFrom.ContainsKey(goalHex.AxialCoords)) { + GD.Print("Failed to find path from " + startHex + " to " + goalHex); return new List(); } diff --git a/scenes/Game.cs b/scenes/Game.cs index 9148e37..dc0db1b 100644 --- a/scenes/Game.cs +++ b/scenes/Game.cs @@ -344,7 +344,6 @@ public class Game : Spatial _worldTextureRect.Texture = newWorldTexture; _tileMaterial.SetShaderParam("MapAlbedoTexture", newWorldTexture); _tileMaterial.SetShaderParam("TextureSize", (int)_tileWorld.ColormapImage.GetSize().x); - GD.Print("Texture size: " + (int)_tileWorld.ColormapImage.GetSize().x); ImageTexture newHeightTexture = new ImageTexture(); newHeightTexture.CreateFromImage(_tileWorld.HeightmapImage, diff --git a/scenes/TileWorld.cs b/scenes/TileWorld.cs index 74fc180..ad68efd 100644 --- a/scenes/TileWorld.cs +++ b/scenes/TileWorld.cs @@ -43,6 +43,7 @@ public class TileWorld : Spatial public float HeightScale = 2.0f; public Image HeightmapImage; public Image ColormapImage; + public Image NavigationmapImage; public int Seed = 0; public Spatial Entities; public HexGrid HexGrid; @@ -124,15 +125,12 @@ public class TileWorld : Spatial HeightmapImage.FillRect(new Rect2(0, 0, size, size), Colors.ForestGreen); _heightmapOffscreenTextureRect.SetSize(sizeVector); _heightmapOffscreenViewport.Size = sizeVector; + + NavigationmapImage = new Image(); + NavigationmapImage.Create(size, size, false, Image.Format.Rgb8); + NavigationmapImage.FillRect(new Rect2(0, 0, size, size), new Color(1, 1, 1)); } - public void PrintTextureSizes() - { - GD.Print("Color Viewport: " + _worldOffscreenViewport.Size); - GD.Print("Color TextureRect: " + _worldOffscreenTextureRect.Texture.GetSize()); - GD.Print("Heightmap Viewport: " + _heightmapOffscreenViewport.Size); - GD.Print("Heightmap TextureRect: " + _heightmapOffscreenTextureRect.Texture.GetSize()); - } public void Generate(int size) { @@ -314,6 +312,14 @@ public class TileWorld : Spatial return (color.r == 0 && color.g == 0 && color.b > 0.01); } + public void MarkCellUnwalkable(HexCell cell) + { + HexGrid.AddObstacle(cell); + NavigationmapImage.Lock(); + NavigationmapImage.SetPixelv(OffsetToTextureCoord(cell.OffsetCoords), Colors.Red); + NavigationmapImage.Unlock(); + } + private void PopulateEnvironment() { Random environmentRandom = new Random(Seed); @@ -336,7 +342,7 @@ public class TileWorld : Spatial if (rockAsset != null) { _environmentNode.AddChild(rockAsset); - HexGrid.AddObstacle(cell); + MarkCellUnwalkable(cell); } } else if (IsColorEqualApprox(colorValue, GrassColor)) @@ -351,7 +357,7 @@ public class TileWorld : Spatial if (treeAsset != null) { Entities.AddChild(treeAsset); - HexGrid.AddObstacle(cell); + MarkCellUnwalkable(cell); } else if (environmentRandom.NextDouble() < 0.01) { @@ -361,12 +367,12 @@ public class TileWorld : Spatial assetTransform.origin.y += 1.2f; chestAsset.Transform = assetTransform; Entities.AddChild(chestAsset); - HexGrid.AddObstacle(cell); + MarkCellUnwalkable(cell); } } else if (IsColorWater(colorValue)) { - HexGrid.AddObstacle(cell); + MarkCellUnwalkable(cell); } } } @@ -473,8 +479,6 @@ public class TileWorld : Spatial ColormapImage.Lock(); ColormapImage.SetPixel((int)textureCoord.x, (int)textureCoord.y, color); ColormapImage.Unlock(); - - OnMapGenerationComplete(); } public Vector2 WorldToOffsetCoords(Vector3 worldCoord) diff --git a/scenes/tests/EditorUI.cs b/scenes/tests/EditorUI.cs index 2db282b..42aba9d 100644 --- a/scenes/tests/EditorUI.cs +++ b/scenes/tests/EditorUI.cs @@ -16,27 +16,34 @@ public class EditorUI : Control private Button _grassButton; private Button _sandButton; private Button _waterButton; + private Button _obstacleButton; + private Button _navigateButton; + private ShaderMaterial _tileMaterial; private CheckBox _gameGeometryCheckBox; private CheckBox _physicsGeometryCheckBox; + private CheckBox _navigationGeometryCheckBox; private TileWorld _tileWorld; private StreamContainer _streamContainer; - private enum TileType + public enum InputMode { None, Grass, Sand, - Water + Water, + Obstacle, + Navigate } - private TileType _currentTileType = TileType.None; + public InputMode CurrentInputMode = InputMode.None; // Called when the node enters the scene tree for the first time. public override void _Ready() { _tileWorld = (TileWorld) GetNode(World); _streamContainer = (StreamContainer)GetNode(StreamContainer); + _tileMaterial = GD.Load("materials/HexTileTextureLookup.tres"); // signals _resetButton = (Button) FindNode("ResetButton"); @@ -51,11 +58,20 @@ public class EditorUI : Control _waterButton = (Button) FindNode("WaterButton"); _waterButton.Connect("pressed", this, nameof(OnWaterButton)); + _obstacleButton = (Button) FindNode("ObstacleButton"); + _obstacleButton.Connect("pressed", this, nameof(OnObstacleButton)); + + _navigateButton = (Button) FindNode("NavigateButton"); + _navigateButton.Connect("pressed", this, nameof(OnNavigateButton)); + _gameGeometryCheckBox = (CheckBox)FindNode("GameGeometryCheckBox"); _gameGeometryCheckBox.Connect("toggled", this, nameof(OnGameGeometryCheckBoxToggled)); _physicsGeometryCheckBox = (CheckBox)FindNode("PhysicsGeometryCheckBox"); _physicsGeometryCheckBox.Connect("toggled", this, nameof(OnPhysicsGeometryCheckBoxToggled)); + + _navigationGeometryCheckBox = (CheckBox)FindNode("NavigationGeometryCheckBox"); + _navigationGeometryCheckBox.Connect("toggled", this, nameof(OnNavigationGeometryCheckBoxToggled)); } @@ -63,24 +79,37 @@ public class EditorUI : Control { GD.Print("Resetting Map"); _tileWorld.Seed = _tileWorld.Seed + 1; - _tileWorld.Generate(12); + _tileWorld.Generate(24); } public void OnGrassButton() { - _currentTileType = TileType.Grass; + CurrentInputMode = InputMode.Grass; } public void OnSandButton() { - _currentTileType = TileType.Sand; + CurrentInputMode = InputMode.Sand; } public void OnWaterButton() { - _currentTileType = TileType.Water; + CurrentInputMode = InputMode.Water; } + + + public void OnObstacleButton() + { + CurrentInputMode = InputMode.Obstacle; + } + + + public void OnNavigateButton() + { + CurrentInputMode = InputMode.Navigate; + } + public void OnGameGeometryCheckBoxToggled(bool pressed) { @@ -107,16 +136,48 @@ public class EditorUI : Control } } - public void OnTileClicked(Vector2 offsetCoord) + + public void OnNavigationGeometryCheckBoxToggled(bool pressed) { - switch (_currentTileType) + UpdateTileMaterial(); + } + + public void UpdateTileMaterial() + { + if (_navigationGeometryCheckBox.Pressed) { - case TileType.Grass:_tileWorld.SetTileColorAtOffset(currentTileOffset, Colors.Green); - break; - case TileType.Water:_tileWorld.SetTileColorAtOffset(currentTileOffset, Colors.Blue); - break; - case TileType.Sand:_tileWorld.SetTileColorAtOffset(currentTileOffset, Colors.Yellow); - break; + ImageTexture newWorldTexture = new ImageTexture(); + newWorldTexture.CreateFromImage(_tileWorld.NavigationmapImage, + (uint)(Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat)); + _tileMaterial.SetShaderParam("MapAlbedoTexture", newWorldTexture); + _tileMaterial.SetShaderParam("TextureSize", (int)_tileWorld.NavigationmapImage.GetSize().x); + } + else + { + 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 OnTileClicked(Vector2 offsetCoord) + { + switch (CurrentInputMode) + { + case InputMode.Grass:_tileWorld.SetTileColorAtOffset(currentTileOffset, Colors.Green); + break; + case InputMode.Water:_tileWorld.SetTileColorAtOffset(currentTileOffset, Colors.Blue); + break; + case InputMode.Sand:_tileWorld.SetTileColorAtOffset(currentTileOffset, Colors.Yellow); + break; + case InputMode.Obstacle: + _tileWorld.MarkCellUnwalkable(HexCell.FromOffsetCoords(offsetCoord)); + break; + } + + UpdateTileMaterial(); + } } diff --git a/scenes/tests/NavigationTests.cs b/scenes/tests/NavigationTests.cs index d4a7b9c..0c8796b 100644 --- a/scenes/tests/NavigationTests.cs +++ b/scenes/tests/NavigationTests.cs @@ -58,8 +58,8 @@ public class NavigationTests : Spatial foreach (Spatial entity in entities) { Vector2 entityPlaneCoords = new Vector2(entity.GlobalTranslation.x, entity.GlobalTranslation.z); - HexCell entityCell = _hexGrid.GetHexAt(entityPlaneCoords); - _tileWorld.HexGrid.AddObstacle(entityCell); + HexCell entityCell = _tileWorld.HexGrid.GetHexAt(entityPlaneCoords); + _tileWorld.MarkCellUnwalkable(entityCell); Vector2 cellPlaneCoords = _hexGrid.GetHexCenterFromOffset(entityCell.OffsetCoords); Vector3 entityGlobalTranslation = entity.GlobalTranslation; entityGlobalTranslation.x = cellPlaneCoords.x; @@ -85,6 +85,8 @@ public class NavigationTests : Spatial (uint)(Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat)); _tileMaterial.SetShaderParam("MapAlbedoTexture", newWorldTexture); _tileMaterial.SetShaderParam("TextureSize", (int)_tileWorld.ColormapImage.GetSize().x); + + CorrectEntityGridPositions(); } @@ -125,6 +127,11 @@ public class NavigationTests : Spatial if (_editorUi != null) { _editorUi.OnTileClicked(tile.OffsetCoords); + + if (_editorUi.CurrentInputMode == EditorUI.InputMode.Navigate) + { + _playerNavigationComponent.FindPath(_player, _player.GlobalTranslation, tile.GlobalTranslation); + } } } diff --git a/ui/EditorUI.tscn b/ui/EditorUI.tscn index ae27c19..935af8c 100644 --- a/ui/EditorUI.tscn +++ b/ui/EditorUI.tscn @@ -4,7 +4,7 @@ [sub_resource type="ButtonGroup" id=4] resource_local_to_scene = false -resource_name = "TileTypeButtonGroup" +resource_name = "InputTypeButtonGroup" [node name="EditorUI" type="Control"] anchor_right = 1.0 @@ -56,6 +56,22 @@ toggle_mode = true group = SubResource( 4 ) text = "Sand" +[node name="ObstacleButton" type="Button" parent="HBoxContainer"] +margin_left = 244.0 +margin_right = 313.0 +margin_bottom = 25.0 +toggle_mode = true +group = SubResource( 4 ) +text = "Obstacle" + +[node name="NavigateButton" type="Button" parent="HBoxContainer"] +margin_left = 317.0 +margin_right = 384.0 +margin_bottom = 25.0 +toggle_mode = true +group = SubResource( 4 ) +text = "Navigate" + [node name="ViewFlagsContainer" type="HBoxContainer" parent="."] anchor_top = 1.0 anchor_bottom = 1.0 @@ -79,3 +95,9 @@ margin_left = 104.0 margin_right = 180.0 margin_bottom = 40.0 text = "Physics" + +[node name="NavigationGeometryCheckBox" type="CheckBox" parent="ViewFlagsContainer"] +margin_left = 184.0 +margin_right = 279.0 +margin_bottom = 40.0 +text = "Navigation"