From 041d5805502572f451a1b784d3f945ef7f3666c8 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Wed, 28 Dec 2022 16:22:53 +0100 Subject: [PATCH] Noise based map generation, movement component uses height. --- GodotComponentTest.csproj | 4 +- HexCell.cs | 19 +++-- HexGrid.cs | 11 +-- components/MovableComponent.cs | 79 +++++++++---------- components/WorldInfoComponent.cs | 21 ++++++ entities/Player.cs | 30 +++++++- materials/UndefinedTile.tres | 4 + project.godot | 8 +- scenes/AdaptiveWorldStream.cs | 53 ++++++++++--- scenes/AdaptiveWorldStream.tscn | 23 ++++-- scenes/HexTile3D.cs | 47 ++++++------ scenes/HexTile3D.tscn | 2 +- scenes/StreamContainer.cs | 53 +++++++------ scenes/TileWorld.cs | 125 +++++++++++++++++++++++++++++++ tests/HexCellTests.cs | 18 ++--- tests/HexGridTests.cs | 6 +- utils/SpringDamper.cs | 64 ++++++++-------- 17 files changed, 401 insertions(+), 166 deletions(-) create mode 100644 components/WorldInfoComponent.cs create mode 100644 materials/UndefinedTile.tres create mode 100644 scenes/TileWorld.cs diff --git a/GodotComponentTest.csproj b/GodotComponentTest.csproj index 2fd8e7d..845d179 100644 --- a/GodotComponentTest.csproj +++ b/GodotComponentTest.csproj @@ -5,7 +5,9 @@ - true + true + + $(DefaultItemExcludes);script_templates/* diff --git a/HexCell.cs b/HexCell.cs index c043917..eb7d7e9 100644 --- a/HexCell.cs +++ b/HexCell.cs @@ -15,8 +15,11 @@ public class HexCell : Resource public static readonly Array DIR_ALL = new Array() { DIR_N, DIR_NE, DIR_SE, DIR_S, DIR_SW, DIR_NW }; - - public HexCell() { } + + public HexCell() + { + } + public HexCell(float cubeX, float cubeY, float cubeZ) { CubeCoords = RoundCoords(new Vector3(cubeX, cubeY, cubeZ)); @@ -43,18 +46,17 @@ public class HexCell : Resource } private Vector3 _cubeCoords; + public Vector3 CubeCoords { - get - { - return _cubeCoords; - } + get { return _cubeCoords; } set { if (Mathf.Abs(value.x + value.y + value.z) > 0.0001) { GD.Print("Warning: Invalid cube coordinates for hex (x + y + z != 0): ", value.ToString()); } + _cubeCoords = RoundCoords(value); } } @@ -63,10 +65,7 @@ public class HexCell : Resource { get => new Vector2(CubeCoords.x, CubeCoords.y); - set - { - CubeCoords = AxialToCubeCoords(value); - } + set { CubeCoords = AxialToCubeCoords(value); } } public Vector2 OffsetCoords diff --git a/HexGrid.cs b/HexGrid.cs index eadbf76..bac7f24 100644 --- a/HexGrid.cs +++ b/HexGrid.cs @@ -13,22 +13,20 @@ public class HexGrid : Resource { get { return _hexSize; } } + public Vector2 HexScale { - get - { - return _hexScale; - } + get { return _hexScale; } set { _hexScale = value; _hexSize = _baseHexSize * _hexScale; _hexTransform = new Transform2D( - new Vector2(_hexSize.x * 3/4, -_hexSize.y / 2), + new Vector2(_hexSize.x * 3 / 4, -_hexSize.y / 2), new Vector2(0, -_hexSize.y), new Vector2(0, 0) ); - + _hexTransformInv = _hexTransform.AffineInverse(); } } @@ -62,5 +60,4 @@ public class HexGrid : Resource HexCell result = new HexCell(_hexTransformInv * planeCoord); return result; } - } \ No newline at end of file diff --git a/components/MovableComponent.cs b/components/MovableComponent.cs index 9de46a2..4934c09 100644 --- a/components/MovableComponent.cs +++ b/components/MovableComponent.cs @@ -10,7 +10,7 @@ public class MovableComponent : Node public float targetAngle = 0; public float currentAngle = 0; public float currentAngularVelocity = 0; - + [Export] public float maxSpeed = 3; private SpringDamper _positionSpringDamper; @@ -32,49 +32,50 @@ public class MovableComponent : Node // Called every frame. 'delta' is the elapsed time since the previous frame. public override void _Process(float delta) { - Vector3 targetDir = Vector3.Zero; - Vector3 targetError = targetPosition - currentPosition; + Vector3 targetDir = Vector3.Zero; + Vector3 targetError = targetPosition - currentPosition; - if (targetError.LengthSquared() > 0.01) - { - targetDir = targetError.Normalized(); - targetAngle = new Vector3(0, 0, 1).SignedAngleTo(targetDir, Vector3.Up); + if (targetError.LengthSquared() > 0.01) + { + targetDir = targetError.Normalized(); + targetAngle = new Vector3(0, 0, 1).SignedAngleTo(targetDir, Vector3.Up); - if (currentAngle < Mathf.Pi && targetAngle > Mathf.Pi) - { - - } + if (currentAngle < Mathf.Pi && targetAngle > Mathf.Pi) + { + } - if (targetAngle - currentAngle > Mathf.Pi) - { - currentAngle += 2 * Mathf.Pi; - } - else if (targetAngle - currentAngle < -Mathf.Pi) - { - currentAngle -= 2 * Mathf.Pi; - } - } - - if (Mathf.Abs(currentAngularVelocity) > 0.1 || Mathf.Abs(targetAngle - currentAngle) > 0.01) - { - var springDamperResult = _angleSpringDamper.Calc(currentAngle, currentAngularVelocity, targetAngle, delta); + if (targetAngle - currentAngle > Mathf.Pi) + { + currentAngle += 2 * Mathf.Pi; + } + else if (targetAngle - currentAngle < -Mathf.Pi) + { + currentAngle -= 2 * Mathf.Pi; + } + } - currentAngle = springDamperResult.Item1; - currentAngularVelocity = springDamperResult.Item2; - - EmitSignal("OrientationUpdated", this.currentAngle); - } - - if (( Mathf.Abs(currentAngularVelocity) < 5 && Mathf.Abs(targetAngle - currentAngle) < 0.3) - && (targetPosition - currentPosition).LengthSquared() > 0.01) - { - var springDamperResult = - _positionSpringDamper.CalcClampedSpeed(currentPosition, currentVelocity, targetPosition, delta, maxSpeed); + if (Mathf.Abs(currentAngularVelocity) > 0.1 || Mathf.Abs(targetAngle - currentAngle) > 0.01) + { + var springDamperResult = + _angleSpringDamper.Calc(currentAngle, currentAngularVelocity, targetAngle, delta); - currentPosition = springDamperResult.Item1; - currentVelocity = springDamperResult.Item2; + currentAngle = springDamperResult.Item1; + currentAngularVelocity = springDamperResult.Item2; - EmitSignal("PositionUpdated", currentPosition); - } + EmitSignal("OrientationUpdated", this.currentAngle); + } + + if ((Mathf.Abs(currentAngularVelocity) < 5 && Mathf.Abs(targetAngle - currentAngle) < 0.3) + && (targetPosition - currentPosition).LengthSquared() > 0.01) + { + var springDamperResult = + _positionSpringDamper.CalcClampedSpeed(currentPosition, currentVelocity, targetPosition, delta, + maxSpeed); + + currentPosition = springDamperResult.Item1; + currentVelocity = springDamperResult.Item2; + + EmitSignal("PositionUpdated", currentPosition); + } } } \ No newline at end of file diff --git a/components/WorldInfoComponent.cs b/components/WorldInfoComponent.cs new file mode 100644 index 0000000..7cfa266 --- /dev/null +++ b/components/WorldInfoComponent.cs @@ -0,0 +1,21 @@ +using Godot; +using System; + +public class WorldInfoComponent : Node +{ + [Export] public NodePath World; + + private HexTile3D _currentHexTile3D; + public TileWorld TileWorld; + + // Called when the node enters the scene tree for the first time. + public override void _Ready() + { + TileWorld = GetNode(World); + } + + public void SetWorld(TileWorld tileWorld) + { + TileWorld = tileWorld; + } +} \ No newline at end of file diff --git a/entities/Player.cs b/entities/Player.cs index 567b95f..cc6645b 100644 --- a/entities/Player.cs +++ b/entities/Player.cs @@ -3,8 +3,11 @@ using System; public class Player : KinematicBody { + // other members private MovableComponent _movable; private Spatial _geometry; + private WorldInfoComponent _worldInfo; + private Vector2 _offsetCoord = Vector2.Zero; // Called when the node enters the scene tree for the first time. public override void _Ready() @@ -17,17 +20,36 @@ public class Player : KinematicBody } _geometry = (Spatial)FindNode("Geometry", false); + + _worldInfo = (WorldInfoComponent)FindNode("WorldInfo", false); } - private void OnPositionUpdated(Vector3 newPosition) + public void OnPositionUpdated(Vector3 newPosition) { + if (_worldInfo != null) + { + Vector2 new_offset_coord = _worldInfo.TileWorld.WorldToOffsetCoords(newPosition); + if (_offsetCoord != new_offset_coord) + { + GD.Print("New offset coord " + new_offset_coord); + _offsetCoord = new_offset_coord; + + } + + if (_movable != null) + { + _movable.targetPosition.y = _worldInfo.TileWorld.GetHeightAtOffset(new_offset_coord); + + } + } + Transform transform = Transform; transform.origin = newPosition; Transform = transform; } - + private void OnOrientationUpdated(float newOrientation) { - _geometry.Transform = new Transform (new Quat(Vector3.Up, newOrientation), Vector3.Zero); + _geometry.Transform = new Transform(new Quat(Vector3.Up, newOrientation), Vector3.Zero); } -} +} \ No newline at end of file diff --git a/materials/UndefinedTile.tres b/materials/UndefinedTile.tres new file mode 100644 index 0000000..2466182 --- /dev/null +++ b/materials/UndefinedTile.tres @@ -0,0 +1,4 @@ +[gd_resource type="SpatialMaterial" format=2] + +[resource] +albedo_color = Color( 1, 0, 1, 1 ) diff --git a/project.godot b/project.godot index 2c5ac05..eb2ae4a 100644 --- a/project.godot +++ b/project.godot @@ -9,17 +9,17 @@ config_version=4 _global_script_classes=[ { -"base": "Node", +"base": "Reference", "class": "ClickableComponent", "language": "GDScript", "path": "res://components/ClickableComponent.gd" }, { -"base": "KinematicBody2D", +"base": "Reference", "class": "CollisionLine", "language": "GDScript", "path": "res://utils/CollisionLine.gd" }, { -"base": "Node", +"base": "Reference", "class": "ColorComponent", "language": "GDScript", "path": "res://components/ColorComponent.gd" @@ -54,7 +54,7 @@ _global_script_classes=[ { "language": "GDScript", "path": "res://utils/SpringDamper.gd" }, { -"base": "Sprite", +"base": "Reference", "class": "TintedSpriteComponent", "language": "GDScript", "path": "res://components/TintedSpriteComponent.gd" diff --git a/scenes/AdaptiveWorldStream.cs b/scenes/AdaptiveWorldStream.cs index 1dfd815..78f12f5 100644 --- a/scenes/AdaptiveWorldStream.cs +++ b/scenes/AdaptiveWorldStream.cs @@ -1,6 +1,8 @@ using Godot; using System; +using System.Diagnostics; using System.Linq; +using GoDotLog; using Dictionary = Godot.Collections.Dictionary; using Array = Godot.Collections.Array; @@ -15,6 +17,7 @@ public class AdaptiveWorldStream : Spatial private Label _mouseTileLabel; private Label _numCoordsAddedLabel; private Label _numCoordsRemovedLabel; + private TextureRect _worldTextureRect; // scene nodes private Spatial _tileHighlight; @@ -23,6 +26,7 @@ public class AdaptiveWorldStream : Spatial private Area _streamContainerArea; private Spatial _streamContainerActiveTiles; private Player _player; + private TileWorld _tileWorld; // Resources private PackedScene _tileHighlightScene; @@ -45,6 +49,7 @@ public class AdaptiveWorldStream : Spatial _mouseTileLabel = GetNode