From 5209657cefc35901759efe8c22c59fc568639b93 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Sun, 4 Dec 2022 21:54:44 +0100 Subject: [PATCH] Minor tweaks. --- components/MovableComponent.cs | 36 +++++++++++++++++++++++ entities/Player.cs | 2 +- scenes/AdaptiveWorldStream.cs | 51 +++++++++------------------------ scenes/AdaptiveWorldStream.tscn | 20 +------------ 4 files changed, 52 insertions(+), 57 deletions(-) create mode 100644 components/MovableComponent.cs diff --git a/components/MovableComponent.cs b/components/MovableComponent.cs new file mode 100644 index 0000000..c7e61ca --- /dev/null +++ b/components/MovableComponent.cs @@ -0,0 +1,36 @@ +using Godot; +using System; + +public class MovableComponent : Node +{ + public Vector3 targetPosition = Vector3.Zero; + public Vector3 currentPosition = Vector3.Zero; + public Vector3 currentVelocity = Vector3.Zero; + [Export] public float maxSpeed = 3; + + private SpringDamper _springDamper; + + [Signal] + delegate void PositionUpdated(Vector3 newPosition); + + // Called when the node enters the scene tree for the first time. + public override void _Ready() + { + _springDamper = new SpringDamper(4, 0.99f, 0.5f); + } + + // Called every frame. 'delta' is the elapsed time since the previous frame. + public override void _Process(float delta) + { + if ((targetPosition - currentPosition).LengthSquared() > 0.01) + { + var springDamperResult = + _springDamper.CalcClampedSpeed(currentPosition, currentVelocity, targetPosition, delta, maxSpeed); + + currentPosition = springDamperResult.Item1; + currentVelocity = springDamperResult.Item2; + + EmitSignal("PositionUpdated", currentPosition); + } + } +} \ No newline at end of file diff --git a/entities/Player.cs b/entities/Player.cs index a7a0618..7b1188f 100644 --- a/entities/Player.cs +++ b/entities/Player.cs @@ -8,7 +8,7 @@ public class Player : KinematicBody // Called when the node enters the scene tree for the first time. public override void _Ready() { - _movable = GetNode("Movable"); + _movable = (MovableComponent)FindNode("Movable", false); if (_movable != null) { _movable.Connect("PositionUpdated", this, nameof(OnPositionUpdated)); diff --git a/scenes/AdaptiveWorldStream.cs b/scenes/AdaptiveWorldStream.cs index 745196d..1dfd815 100644 --- a/scenes/AdaptiveWorldStream.cs +++ b/scenes/AdaptiveWorldStream.cs @@ -67,12 +67,8 @@ public class AdaptiveWorldStream : Spatial _streamContainerArea.Connect("input_event", this, nameof(OnAreaInputEvent)); _streamContainer.Connect("TileSelected", this, nameof(OnTileSelected)); - // CreateTileGrid(); - - //playerTransform.origin += new Vector3(0, 0, -1) * delta; - Transform playerTransform = _player.Transform; - playerTransform.origin.x = 3; - _player.Transform = playerTransform; + UpdateCurrentTile(); + _streamContainer.SetCenterTile(_currentTile); } public void CreateTileGrid() @@ -96,28 +92,17 @@ public class AdaptiveWorldStream : Spatial } - public void CreateStreamActiveTiles() + public void UpdateCurrentTile() { - foreach (int coord_x in Enumerable.Range((int)_streamContainer.CurrentOffsetCoordRect.Position.x, - (int)_streamContainer.CurrentOffsetCoordRect.Size.x)) - { - foreach (int coord_y in Enumerable.Range((int)_streamContainer.CurrentOffsetCoordRect.Position.y, - (int)_streamContainer.CurrentOffsetCoordRect.Size.y)) - { - Spatial hexTile3d = _streamContainer.GetTile3dAt(new Vector2(coord_x, coord_y)); - // HexCell cell = new HexCell(); - // cell.OffsetCoords = new Vector2(coord_x, coord_y); - // Vector2 cellWorldCenter = _hexGrid.GetHexCenter(cell); - // - // Spatial highlightTile = (Spatial)_tileHighlightScene.Instance(); - // Transform highlightTileTransform = Transform.Identity; - // highlightTileTransform.origin.x = cellWorldCenter.x; - // highlightTileTransform.origin.z = cellWorldCenter.y; - // highlightTile.Transform = highlightTileTransform; - // - // _streamContainerActiveTiles.AddChild(highlightTile); - } - } + Transform playerTransform = _player.Transform; + Vector3 playerCoord = playerTransform.origin; + _currentTile = _hexGrid.GetHexAt(new Vector2(playerCoord.x, playerCoord.z)); + + _tileLabel.Text = playerTransform.ToString(); + _tileOffsetLabel.Text = _currentTile.OffsetCoords.ToString(); + + //playerTransform.origin += new Vector3(-0.1f, 0, 1) * delta; + _player.Transform = playerTransform; } public override void _Process(float delta) @@ -125,16 +110,8 @@ public class AdaptiveWorldStream : Spatial _framesPerSecondLabel.Text = Engine.GetFramesPerSecond().ToString(); _lastTile = _currentTile; - Transform playerTransform = _player.Transform; - Vector3 playerCoord = playerTransform.origin; - _currentTile = _hexGrid.GetHexAt(new Vector2(playerCoord.x, playerCoord.z)); - - _tileLabel.Text = playerTransform.ToString(); - _tileOffsetLabel.Text = _currentTile.OffsetCoords.ToString(); - - //playerTransform.origin += new Vector3(-0.1f, 0, 1) * delta; - _player.Transform = playerTransform; - + UpdateCurrentTile(); + Transform tileHighlightTransform = Transform.Identity; Vector2 currentTileCenter = _hexGrid.GetHexCenter(_currentTile); tileHighlightTransform.origin.x = currentTileCenter.x; diff --git a/scenes/AdaptiveWorldStream.tscn b/scenes/AdaptiveWorldStream.tscn index f0702f6..af57112 100644 --- a/scenes/AdaptiveWorldStream.tscn +++ b/scenes/AdaptiveWorldStream.tscn @@ -1,7 +1,6 @@ -[gd_scene load_steps=14 format=2] +[gd_scene load_steps=13 format=2] [ext_resource path="res://scenes/AdaptiveWorldStream.cs" type="Script" id=1] -[ext_resource path="res://entities/Player3D.tscn" type="PackedScene" id=2] [ext_resource path="res://scenes/World.gd" type="Script" id=3] [ext_resource path="res://scenes/StreamContainer.cs" type="Script" id=4] [ext_resource path="res://entities/Player.cs" type="Script" id=5] @@ -35,21 +34,6 @@ script = ExtResource( 1 ) [node name="MouseTileHighlight" parent="." instance=ExtResource( 7 )] -[node name="OPlayer" parent="." instance=ExtResource( 2 )] -visible = false - -[node name="Camera" parent="OPlayer" index="0"] -visible = false - -[node name="MeshInstance" parent="OPlayer" index="1"] -visible = false - -[node name="Collision" parent="OPlayer" index="2"] -visible = false - -[node name="Movable" parent="OPlayer" index="3"] -visible = false - [node name="World" type="Spatial" parent="."] script = ExtResource( 3 ) @@ -231,5 +215,3 @@ mesh = SubResource( 8 ) [node name="Movable" type="Node" parent="Player"] script = ExtResource( 8 ) - -[editable path="OPlayer"]