From ed29cc128829c1a69525dc23ac346c910e416ca8 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Sun, 11 Jun 2023 13:22:13 +0200 Subject: [PATCH] Properly center StreamContainer at center of camera. --- scenes/Game.cs | 26 +++++++++++++++++++------ scenes/Game.tscn | 50 ++++++++++++++++++++++++------------------------ 2 files changed, 45 insertions(+), 31 deletions(-) diff --git a/scenes/Game.cs b/scenes/Game.cs index 0bccc78..a133fc2 100644 --- a/scenes/Game.cs +++ b/scenes/Game.cs @@ -149,14 +149,28 @@ public class Game : Spatial public void UpdateCurrentTile() { - Transform playerTransform = _player.Transform; - Vector3 playerCoord = playerTransform.origin; - _currentTile = _hexGrid.GetHexAt(new Vector2(playerCoord.x, playerCoord.z)); + // cast a ray from the camera to center + Vector3 cameraNormal = _camera.ProjectRayNormal(_camera.GetViewport().Size * 0.5f); + Vector3 cameraPosition = _camera.ProjectRayOrigin(_camera.GetViewport().Size * 0.5f); + Vector3 cameraDir = cameraNormal - cameraPosition; + + Vector3 centerCoord; + + if (Mathf.Abs(cameraDir.y) > Globals.EpsPosition) + { + centerCoord = cameraPosition + cameraNormal * (-cameraPosition.y / cameraNormal.y); + } + else + { + centerCoord = _camera.GlobalTranslation; + centerCoord.y = 0; + } + - _tileLabel.Text = playerTransform.ToString(); + _currentTile = _hexGrid.GetHexAt(new Vector2(centerCoord.x, centerCoord.z)); + + _tileLabel.Text = centerCoord.ToString(); _tileOffsetLabel.Text = _currentTile.OffsetCoords.ToString(); - - _player.Transform = playerTransform; } diff --git a/scenes/Game.tscn b/scenes/Game.tscn index 94c43a4..d64d36c 100644 --- a/scenes/Game.tscn +++ b/scenes/Game.tscn @@ -201,31 +201,9 @@ margin_bottom = 258.0 alignment = 2 script = ExtResource( 12 ) -[node name="WorldTextureRect" type="TextureRect" parent="Generator Container/WorldGeneratorContainer"] -margin_right = 100.0 -margin_bottom = 100.0 -rect_min_size = Vector2( 100, 100 ) -stretch_mode = 1 -flip_v = true - -[node name="HeightTextureRect" type="TextureRect" parent="Generator Container/WorldGeneratorContainer"] -margin_top = 104.0 -margin_right = 100.0 -margin_bottom = 204.0 -rect_min_size = Vector2( 100, 100 ) -stretch_mode = 1 -flip_v = true - -[node name="WorldGenerateButton" type="Button" parent="Generator Container/WorldGeneratorContainer"] -margin_top = 208.0 -margin_right = 100.0 -margin_bottom = 228.0 -text = "Generate" - [node name="HBoxContainer" type="HBoxContainer" parent="Generator Container/WorldGeneratorContainer"] -margin_top = 232.0 margin_right = 100.0 -margin_bottom = 248.0 +margin_bottom = 16.0 [node name="WorldSizeSlider" type="HSlider" parent="Generator Container/WorldGeneratorContainer/HBoxContainer"] margin_right = 88.0 @@ -242,10 +220,32 @@ margin_right = 100.0 margin_bottom = 15.0 text = "4" +[node name="WorldGenerateButton" type="Button" parent="Generator Container/WorldGeneratorContainer"] +margin_top = 20.0 +margin_right = 100.0 +margin_bottom = 40.0 +text = "Generate" + +[node name="WorldTextureRect" type="TextureRect" parent="Generator Container/WorldGeneratorContainer"] +margin_top = 44.0 +margin_right = 100.0 +margin_bottom = 144.0 +rect_min_size = Vector2( 100, 100 ) +stretch_mode = 1 +flip_v = true + +[node name="HeightTextureRect" type="TextureRect" parent="Generator Container/WorldGeneratorContainer"] +margin_top = 148.0 +margin_right = 100.0 +margin_bottom = 248.0 +rect_min_size = Vector2( 100, 100 ) +stretch_mode = 1 +flip_v = true + [node name="StreamContainer" type="Spatial" parent="."] transform = Transform( 1, 0, 0, 0, 1, 2.98023e-08, 0, -2.98023e-08, 1, 0, 0, -4.76837e-07 ) script = ExtResource( 1 ) -Dimensions = Vector2( 30, 30 ) +Dimensions = Vector2( 35, 30 ) World = NodePath("../TileWorld") [node name="ActiveTiles" type="Spatial" parent="StreamContainer"] @@ -264,7 +264,7 @@ transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0 ) shape = SubResource( 9 ) [node name="Camera" type="Camera" parent="."] -transform = Transform( 1, 0, 0, 0, 0.511698, 0.859165, 0, -0.859165, 0.511698, 1.7053e-13, 10.6428, 6.65615 ) +transform = Transform( 1, 0, 0, 0, 0.60042, 0.799685, 0, -0.799685, 0.60042, -4.76837e-07, 8.3759, 5.70105 ) current = true fov = 60.0 script = ExtResource( 10 )