From a890b71df0c5cf03748fec5d19c0c9cf98820436 Mon Sep 17 00:00:00 2001 From: ktyl Date: Fri, 12 Aug 2022 00:16:38 +0100 Subject: [PATCH] make train go back n forth --- scenes/RailNode.tscn | 22 ----------------- scenes/Railway.tscn | 33 +++++++++++++++---------- scenes/Train.tscn | 3 +-- scripts/Railway.cs | 57 +++++++++++++++----------------------------- scripts/Train.cs | 48 ++++++++++++++++++++++++++++++++----- 5 files changed, 82 insertions(+), 81 deletions(-) delete mode 100644 scenes/RailNode.tscn diff --git a/scenes/RailNode.tscn b/scenes/RailNode.tscn deleted file mode 100644 index 6d7e870..0000000 --- a/scenes/RailNode.tscn +++ /dev/null @@ -1,22 +0,0 @@ -[gd_scene load_steps=2 format=2] - -[sub_resource type="CSharpScript" id=1] -script/source = "using Godot; -using System; - -public class RailNode : Spatial -{ - public override void _Ready() - { - - } - -// public override void _Process(float delta) -// { -// -// } -} -" - -[node name="Spatial" type="Spatial"] -script = SubResource( 1 ) diff --git a/scenes/Railway.tscn b/scenes/Railway.tscn index 730def8..77709bd 100644 --- a/scenes/Railway.tscn +++ b/scenes/Railway.tscn @@ -1,23 +1,30 @@ -[gd_scene load_steps=4 format=2] +[gd_scene load_steps=3 format=2] [ext_resource path="res://scripts/Railway.cs" type="Script" id=1] [sub_resource type="Curve3D" id=1] +bake_interval = 0.01 _data = { -"points": PoolVector3Array( 0, 0, 0, 0, 0, 0, 8.10688, -1.81506, 7.57791, 0, 0, 0, 0, 0, 0, -4.46766, -3.67929, 0.175438, 0, 0, 0, 0, 0, 0, -6.79476, 2.00362, -6.79993, 0, 0, 0, 0, 0, 0, 5.56111, 6.33345, -1.84912, 0, 0, 0, 0, 0, 0, 11.2517, 3.50708, 4.91382, 0, 0, 0, 0, 0, 0, 14.9168, -0.251479, 11.0718 ), -"tilts": PoolRealArray( 0, 0, 0, 0, 0, 0 ) +"points": PoolVector3Array( -2.68413, 0, -1.21546, 2.68413, 0, 1.21546, -20.005, 0, -8.27961, -0.202576, 0, -2.12705, 0.202576, 0, 2.12705, -15.5484, 0.00448608, -2.35426, -1.06352, 0, -2.88671, 1.06352, 0, 2.88671, -16.1055, 0.000946045, 5.85007, -3.24122, 0, 0.202576, 3.24122, 0, -0.202576, -8.81272, 0, 10.4587, -0.468547, 0, 2.85805, 0.468547, 0, -2.85805, -3.58364, -0.000244141, 4.56199, -1.17756, 0, 3.41513, 1.17756, 0, -3.41513, -5.10296, -0.000259399, -3.5917, -2.78542, 0, -1.56996, 2.78542, 0, 1.56996, 3.2912, -0.000259399, -6.30449, -3.70976, 0, -1.39605, 3.70976, 0, 1.39605, 9.68509, -0.000259399, -0.0972629, 0, 0, 0, 0, 0, 0, 19.9658, -0.000259399, -1.11014 ), +"tilts": PoolRealArray( 0, 0, 0, 0, 0, 0, 0, 0, 0 ) } -[sub_resource type="SpatialMaterial" id=2] -flags_unshaded = true -vertex_color_use_as_albedo = true - -[node name="Railway" type="Path"] -curve = SubResource( 1 ) +[node name="Railway" type="Spatial"] script = ExtResource( 1 ) -_color = Color( 0, 1, 0.133333, 1 ) -[node name="ImmediateGeometry" type="ImmediateGeometry" parent="."] -material_override = SubResource( 2 ) +[node name="Path" type="Path" parent="."] +curve = SubResource( 1 ) -[connection signal="curve_changed" from="." to="." method="OnPathCurveChanged"] +[node name="CSGPolygon" type="CSGPolygon" parent="."] +snap = 0.01 +polygon = PoolVector2Array( -0.0540804, -0.000681907, -0.0495726, 0.0487114, 0.0444932, 0.0509578, 0.0464191, 0.000881314 ) +mode = 2 +path_node = NodePath("../Path") +path_interval_type = 1 +path_interval = 0.01 +path_simplify_angle = 0.2 +path_rotation = 2 +path_local = false +path_continuous_u = true +path_u_distance = 1.0 +path_joined = false diff --git a/scenes/Train.tscn b/scenes/Train.tscn index 265da53..1f28946 100644 --- a/scenes/Train.tscn +++ b/scenes/Train.tscn @@ -2,11 +2,10 @@ [ext_resource path="res://scripts/Train.cs" type="Script" id=1] -[node name="Train" type="Node"] +[node name="Train" type="Spatial"] script = ExtResource( 1 ) [node name="CSGBox" type="CSGBox" parent="."] -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.5, 0 ) width = 1.0 height = 1.0 depth = 4.0 diff --git a/scripts/Railway.cs b/scripts/Railway.cs index c52a75e..3125d3e 100644 --- a/scripts/Railway.cs +++ b/scripts/Railway.cs @@ -1,53 +1,34 @@ using Godot; using System; -[Tool] -public class Railway : Path +public class Railway : Node { - [Export] - private Color _color; - - [Export] - private float _gauge = 1f; - - private ImmediateGeometry _geo = null; - private Vector3[] _points = null; - - public void OnPathCurveChanged() + private Path _path = null; + private Path Path { - Curve.Tessellate(); - _points = Curve.GetBakedPoints(); + get + { + if (_path == null) + { + _path = GetNode("Path"); + } + return _path; + } } - + + public Curve3D Curve => Path.Curve; + // Called when the node enters the scene tree for the first time. public override void _Ready() { - base._Ready(); - - _geo = GetNode("ImmediateGeometry"); - - var m = new SpatialMaterial(); - m.VertexColorUseAsAlbedo = true; - m.FlagsUnshaded = true; - _geo.MaterialOverride = m; - - OnPathCurveChanged(); } public override void _Process(float delta) { - base._Process(delta); - - if (_geo == null) return; - if (_points == null || _points.Length == 0) return; - - _geo.Clear(); - _geo.Begin(Mesh.PrimitiveType.LineStrip); - _geo.SetColor(new Color(1, 0, 0)); - for(int i = 0; i < _points.Length; i++) - { - _geo.AddVertex(_points[i]); - } - _geo.End(); + } + + public void AddPathFollower(PathFollow pathFollow) + { + Path.AddChild(pathFollow); } } diff --git a/scripts/Train.cs b/scripts/Train.cs index dd405a1..2451bb3 100644 --- a/scripts/Train.cs +++ b/scripts/Train.cs @@ -1,16 +1,52 @@ using Godot; using System; -public class Train : Node +public class Train : Spatial { + [Export] + private float _speed = 1.0f; + + [Export] + private NodePath _railwayPath; + + private Railway _railway; + private readonly PathFollow _pathFollow = new PathFollow + { + RotationMode = PathFollow.RotationModeEnum.Oriented + }; + private float _distance = 0; + // Called when the node enters the scene tree for the first time. public override void _Ready() { + _railway = GetNode(_railwayPath) as Railway; + _railway.AddPathFollower(_pathFollow); } -// // Called every frame. 'delta' is the elapsed time since the previous frame. -// public override void _Process(float delta) -// { -// -// } + // Called every frame. 'delta' is the elapsed time since the previous frame. + public override void _Process(float delta) + { + var length = _railway.Curve.GetBakedLength(); + + // keep track of total distance + _distance += delta * _speed; + // wrap at 2x total distance + _distance %= length * 2; + + var distance = PingPong(_distance, length); + + _pathFollow.Offset = distance; + Translation = _pathFollow.Translation; + Rotation = _pathFollow.Rotation; + } + + // poor mans branching ping pong + private float PingPong(float t, float length) + { + var tml = t % length; + + return t > length + ? length - tml + : tml; + } }