Minor improvement of the NavigationTest scene.

WorldChunkRefactoring
Martin Felis 2023-08-12 18:16:45 +02:00
parent 0ee11e358c
commit d963c90912
6 changed files with 127 additions and 32 deletions

View File

@ -243,6 +243,7 @@ public class HexGrid : Resource
FindPathCheckedCells++; FindPathCheckedCells++;
HexCell currentHex = new HexCell(frontier.Dequeue()); HexCell currentHex = new HexCell(frontier.Dequeue());
Vector2 currentAxial = currentHex.AxialCoords; Vector2 currentAxial = currentHex.AxialCoords;
if (currentHex == goalHex) if (currentHex == goalHex)
{ {
break; break;
@ -282,6 +283,7 @@ public class HexGrid : Resource
if (!cameFrom.ContainsKey(goalHex.AxialCoords)) if (!cameFrom.ContainsKey(goalHex.AxialCoords))
{ {
GD.Print("Failed to find path from " + startHex + " to " + goalHex);
return new List<HexCell>(); return new List<HexCell>();
} }

View File

@ -344,7 +344,6 @@ public class Game : Spatial
_worldTextureRect.Texture = newWorldTexture; _worldTextureRect.Texture = newWorldTexture;
_tileMaterial.SetShaderParam("MapAlbedoTexture", newWorldTexture); _tileMaterial.SetShaderParam("MapAlbedoTexture", newWorldTexture);
_tileMaterial.SetShaderParam("TextureSize", (int)_tileWorld.ColormapImage.GetSize().x); _tileMaterial.SetShaderParam("TextureSize", (int)_tileWorld.ColormapImage.GetSize().x);
GD.Print("Texture size: " + (int)_tileWorld.ColormapImage.GetSize().x);
ImageTexture newHeightTexture = new ImageTexture(); ImageTexture newHeightTexture = new ImageTexture();
newHeightTexture.CreateFromImage(_tileWorld.HeightmapImage, newHeightTexture.CreateFromImage(_tileWorld.HeightmapImage,

View File

@ -43,6 +43,7 @@ public class TileWorld : Spatial
public float HeightScale = 2.0f; public float HeightScale = 2.0f;
public Image HeightmapImage; public Image HeightmapImage;
public Image ColormapImage; public Image ColormapImage;
public Image NavigationmapImage;
public int Seed = 0; public int Seed = 0;
public Spatial Entities; public Spatial Entities;
public HexGrid HexGrid; public HexGrid HexGrid;
@ -124,15 +125,12 @@ public class TileWorld : Spatial
HeightmapImage.FillRect(new Rect2(0, 0, size, size), Colors.ForestGreen); HeightmapImage.FillRect(new Rect2(0, 0, size, size), Colors.ForestGreen);
_heightmapOffscreenTextureRect.SetSize(sizeVector); _heightmapOffscreenTextureRect.SetSize(sizeVector);
_heightmapOffscreenViewport.Size = 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) public void Generate(int size)
{ {
@ -314,6 +312,14 @@ public class TileWorld : Spatial
return (color.r == 0 && color.g == 0 && color.b > 0.01); 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() private void PopulateEnvironment()
{ {
Random environmentRandom = new Random(Seed); Random environmentRandom = new Random(Seed);
@ -336,7 +342,7 @@ public class TileWorld : Spatial
if (rockAsset != null) if (rockAsset != null)
{ {
_environmentNode.AddChild(rockAsset); _environmentNode.AddChild(rockAsset);
HexGrid.AddObstacle(cell); MarkCellUnwalkable(cell);
} }
} }
else if (IsColorEqualApprox(colorValue, GrassColor)) else if (IsColorEqualApprox(colorValue, GrassColor))
@ -351,7 +357,7 @@ public class TileWorld : Spatial
if (treeAsset != null) if (treeAsset != null)
{ {
Entities.AddChild(treeAsset); Entities.AddChild(treeAsset);
HexGrid.AddObstacle(cell); MarkCellUnwalkable(cell);
} }
else if (environmentRandom.NextDouble() < 0.01) else if (environmentRandom.NextDouble() < 0.01)
{ {
@ -361,12 +367,12 @@ public class TileWorld : Spatial
assetTransform.origin.y += 1.2f; assetTransform.origin.y += 1.2f;
chestAsset.Transform = assetTransform; chestAsset.Transform = assetTransform;
Entities.AddChild(chestAsset); Entities.AddChild(chestAsset);
HexGrid.AddObstacle(cell); MarkCellUnwalkable(cell);
} }
} }
else if (IsColorWater(colorValue)) else if (IsColorWater(colorValue))
{ {
HexGrid.AddObstacle(cell); MarkCellUnwalkable(cell);
} }
} }
} }
@ -473,8 +479,6 @@ public class TileWorld : Spatial
ColormapImage.Lock(); ColormapImage.Lock();
ColormapImage.SetPixel((int)textureCoord.x, (int)textureCoord.y, color); ColormapImage.SetPixel((int)textureCoord.x, (int)textureCoord.y, color);
ColormapImage.Unlock(); ColormapImage.Unlock();
OnMapGenerationComplete();
} }
public Vector2 WorldToOffsetCoords(Vector3 worldCoord) public Vector2 WorldToOffsetCoords(Vector3 worldCoord)

View File

@ -16,27 +16,34 @@ public class EditorUI : Control
private Button _grassButton; private Button _grassButton;
private Button _sandButton; private Button _sandButton;
private Button _waterButton; private Button _waterButton;
private Button _obstacleButton;
private Button _navigateButton;
private ShaderMaterial _tileMaterial;
private CheckBox _gameGeometryCheckBox; private CheckBox _gameGeometryCheckBox;
private CheckBox _physicsGeometryCheckBox; private CheckBox _physicsGeometryCheckBox;
private CheckBox _navigationGeometryCheckBox;
private TileWorld _tileWorld; private TileWorld _tileWorld;
private StreamContainer _streamContainer; private StreamContainer _streamContainer;
private enum TileType public enum InputMode
{ {
None, None,
Grass, Grass,
Sand, 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. // Called when the node enters the scene tree for the first time.
public override void _Ready() public override void _Ready()
{ {
_tileWorld = (TileWorld) GetNode(World); _tileWorld = (TileWorld) GetNode(World);
_streamContainer = (StreamContainer)GetNode(StreamContainer); _streamContainer = (StreamContainer)GetNode(StreamContainer);
_tileMaterial = GD.Load<ShaderMaterial>("materials/HexTileTextureLookup.tres");
// signals // signals
_resetButton = (Button) FindNode("ResetButton"); _resetButton = (Button) FindNode("ResetButton");
@ -51,11 +58,20 @@ public class EditorUI : Control
_waterButton = (Button) FindNode("WaterButton"); _waterButton = (Button) FindNode("WaterButton");
_waterButton.Connect("pressed", this, nameof(OnWaterButton)); _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 = (CheckBox)FindNode("GameGeometryCheckBox");
_gameGeometryCheckBox.Connect("toggled", this, nameof(OnGameGeometryCheckBoxToggled)); _gameGeometryCheckBox.Connect("toggled", this, nameof(OnGameGeometryCheckBoxToggled));
_physicsGeometryCheckBox = (CheckBox)FindNode("PhysicsGeometryCheckBox"); _physicsGeometryCheckBox = (CheckBox)FindNode("PhysicsGeometryCheckBox");
_physicsGeometryCheckBox.Connect("toggled", this, nameof(OnPhysicsGeometryCheckBoxToggled)); _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"); GD.Print("Resetting Map");
_tileWorld.Seed = _tileWorld.Seed + 1; _tileWorld.Seed = _tileWorld.Seed + 1;
_tileWorld.Generate(12); _tileWorld.Generate(24);
} }
public void OnGrassButton() public void OnGrassButton()
{ {
_currentTileType = TileType.Grass; CurrentInputMode = InputMode.Grass;
} }
public void OnSandButton() public void OnSandButton()
{ {
_currentTileType = TileType.Sand; CurrentInputMode = InputMode.Sand;
} }
public void OnWaterButton() 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) 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); ImageTexture newWorldTexture = new ImageTexture();
break; newWorldTexture.CreateFromImage(_tileWorld.NavigationmapImage,
case TileType.Water:_tileWorld.SetTileColorAtOffset(currentTileOffset, Colors.Blue); (uint)(Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat));
break; _tileMaterial.SetShaderParam("MapAlbedoTexture", newWorldTexture);
case TileType.Sand:_tileWorld.SetTileColorAtOffset(currentTileOffset, Colors.Yellow); _tileMaterial.SetShaderParam("TextureSize", (int)_tileWorld.NavigationmapImage.GetSize().x);
break; }
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();
}
} }

View File

@ -58,8 +58,8 @@ public class NavigationTests : Spatial
foreach (Spatial entity in entities) foreach (Spatial entity in entities)
{ {
Vector2 entityPlaneCoords = new Vector2(entity.GlobalTranslation.x, entity.GlobalTranslation.z); Vector2 entityPlaneCoords = new Vector2(entity.GlobalTranslation.x, entity.GlobalTranslation.z);
HexCell entityCell = _hexGrid.GetHexAt(entityPlaneCoords); HexCell entityCell = _tileWorld.HexGrid.GetHexAt(entityPlaneCoords);
_tileWorld.HexGrid.AddObstacle(entityCell); _tileWorld.MarkCellUnwalkable(entityCell);
Vector2 cellPlaneCoords = _hexGrid.GetHexCenterFromOffset(entityCell.OffsetCoords); Vector2 cellPlaneCoords = _hexGrid.GetHexCenterFromOffset(entityCell.OffsetCoords);
Vector3 entityGlobalTranslation = entity.GlobalTranslation; Vector3 entityGlobalTranslation = entity.GlobalTranslation;
entityGlobalTranslation.x = cellPlaneCoords.x; entityGlobalTranslation.x = cellPlaneCoords.x;
@ -85,6 +85,8 @@ public class NavigationTests : Spatial
(uint)(Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat)); (uint)(Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat));
_tileMaterial.SetShaderParam("MapAlbedoTexture", newWorldTexture); _tileMaterial.SetShaderParam("MapAlbedoTexture", newWorldTexture);
_tileMaterial.SetShaderParam("TextureSize", (int)_tileWorld.ColormapImage.GetSize().x); _tileMaterial.SetShaderParam("TextureSize", (int)_tileWorld.ColormapImage.GetSize().x);
CorrectEntityGridPositions();
} }
@ -125,6 +127,11 @@ public class NavigationTests : Spatial
if (_editorUi != null) if (_editorUi != null)
{ {
_editorUi.OnTileClicked(tile.OffsetCoords); _editorUi.OnTileClicked(tile.OffsetCoords);
if (_editorUi.CurrentInputMode == EditorUI.InputMode.Navigate)
{
_playerNavigationComponent.FindPath(_player, _player.GlobalTranslation, tile.GlobalTranslation);
}
} }
} }

View File

@ -4,7 +4,7 @@
[sub_resource type="ButtonGroup" id=4] [sub_resource type="ButtonGroup" id=4]
resource_local_to_scene = false resource_local_to_scene = false
resource_name = "TileTypeButtonGroup" resource_name = "InputTypeButtonGroup"
[node name="EditorUI" type="Control"] [node name="EditorUI" type="Control"]
anchor_right = 1.0 anchor_right = 1.0
@ -56,6 +56,22 @@ toggle_mode = true
group = SubResource( 4 ) group = SubResource( 4 )
text = "Sand" 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="."] [node name="ViewFlagsContainer" type="HBoxContainer" parent="."]
anchor_top = 1.0 anchor_top = 1.0
anchor_bottom = 1.0 anchor_bottom = 1.0
@ -79,3 +95,9 @@ margin_left = 104.0
margin_right = 180.0 margin_right = 180.0
margin_bottom = 40.0 margin_bottom = 40.0
text = "Physics" text = "Physics"
[node name="NavigationGeometryCheckBox" type="CheckBox" parent="ViewFlagsContainer"]
margin_left = 184.0
margin_right = 279.0
margin_bottom = 40.0
text = "Navigation"