Minor tweaks.

WorldChunkRefactoring
Martin Felis 2022-12-04 21:54:44 +01:00
parent e6b4cecae6
commit 5209657cef
4 changed files with 52 additions and 57 deletions

View File

@ -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);
}
}
}

View File

@ -8,7 +8,7 @@ public class Player : KinematicBody
// 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()
{ {
_movable = GetNode<MovableComponent>("Movable"); _movable = (MovableComponent)FindNode("Movable", false);
if (_movable != null) if (_movable != null)
{ {
_movable.Connect("PositionUpdated", this, nameof(OnPositionUpdated)); _movable.Connect("PositionUpdated", this, nameof(OnPositionUpdated));

View File

@ -67,12 +67,8 @@ public class AdaptiveWorldStream : Spatial
_streamContainerArea.Connect("input_event", this, nameof(OnAreaInputEvent)); _streamContainerArea.Connect("input_event", this, nameof(OnAreaInputEvent));
_streamContainer.Connect("TileSelected", this, nameof(OnTileSelected)); _streamContainer.Connect("TileSelected", this, nameof(OnTileSelected));
// CreateTileGrid(); UpdateCurrentTile();
_streamContainer.SetCenterTile(_currentTile);
//playerTransform.origin += new Vector3(0, 0, -1) * delta;
Transform playerTransform = _player.Transform;
playerTransform.origin.x = 3;
_player.Transform = playerTransform;
} }
public void CreateTileGrid() 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, Transform playerTransform = _player.Transform;
(int)_streamContainer.CurrentOffsetCoordRect.Size.x)) Vector3 playerCoord = playerTransform.origin;
{ _currentTile = _hexGrid.GetHexAt(new Vector2(playerCoord.x, playerCoord.z));
foreach (int coord_y in Enumerable.Range((int)_streamContainer.CurrentOffsetCoordRect.Position.y,
(int)_streamContainer.CurrentOffsetCoordRect.Size.y)) _tileLabel.Text = playerTransform.ToString();
{ _tileOffsetLabel.Text = _currentTile.OffsetCoords.ToString();
Spatial hexTile3d = _streamContainer.GetTile3dAt(new Vector2(coord_x, coord_y));
// HexCell cell = new HexCell(); //playerTransform.origin += new Vector3(-0.1f, 0, 1) * delta;
// cell.OffsetCoords = new Vector2(coord_x, coord_y); _player.Transform = playerTransform;
// 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);
}
}
} }
public override void _Process(float delta) public override void _Process(float delta)
@ -125,16 +110,8 @@ public class AdaptiveWorldStream : Spatial
_framesPerSecondLabel.Text = Engine.GetFramesPerSecond().ToString(); _framesPerSecondLabel.Text = Engine.GetFramesPerSecond().ToString();
_lastTile = _currentTile; _lastTile = _currentTile;
Transform playerTransform = _player.Transform; UpdateCurrentTile();
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;
Transform tileHighlightTransform = Transform.Identity; Transform tileHighlightTransform = Transform.Identity;
Vector2 currentTileCenter = _hexGrid.GetHexCenter(_currentTile); Vector2 currentTileCenter = _hexGrid.GetHexCenter(_currentTile);
tileHighlightTransform.origin.x = currentTileCenter.x; tileHighlightTransform.origin.x = currentTileCenter.x;

View File

@ -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://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/World.gd" type="Script" id=3]
[ext_resource path="res://scenes/StreamContainer.cs" type="Script" id=4] [ext_resource path="res://scenes/StreamContainer.cs" type="Script" id=4]
[ext_resource path="res://entities/Player.cs" type="Script" id=5] [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="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="."] [node name="World" type="Spatial" parent="."]
script = ExtResource( 3 ) script = ExtResource( 3 )
@ -231,5 +215,3 @@ mesh = SubResource( 8 )
[node name="Movable" type="Node" parent="Player"] [node name="Movable" type="Node" parent="Player"]
script = ExtResource( 8 ) script = ExtResource( 8 )
[editable path="OPlayer"]