make train go back n forth

This commit is contained in:
ktyl 2022-08-12 00:16:38 +01:00
parent 8d80cdb372
commit 89477e7ce8
7 changed files with 87 additions and 84 deletions

View File

@ -7,12 +7,13 @@
[node name="Main" type="Node2D"]
[node name="Train" parent="." instance=ExtResource( 1 )]
_speed = 5.0
_railwayPath = NodePath("../Railway")
[node name="DirectionalLight" type="DirectionalLight" parent="."]
transform = Transform( 0.515898, 0.606099, -0.605386, -0.393123, 0.795389, 0.461314, 0.76112, -2.26831e-08, 0.648611, 0, 0, 0 )
[node name="OrbitCamera" parent="." instance=ExtResource( 3 )]
_speed = 1.0
_speed = 0.5
[node name="Railway" parent="." instance=ExtResource( 2 )]
script = null

View File

@ -6,4 +6,5 @@
script = ExtResource( 1 )
[node name="Camera" type="Camera" parent="."]
transform = Transform( 0.636065, -0.249781, 0.73009, 0, 0.946159, 0.323703, -0.771636, -0.205896, 0.601818, 8.49012, 5.30069, 6.71091 )
transform = Transform( 1, 0, 0, 0, 0.913325, 0.407231, 0, -0.407231, 0.913325, 0, 24.1191, 58.6188 )
fov = 25.0

View File

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

View File

@ -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

View File

@ -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

View File

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

View File

@ -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<Railway>(_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;
}
}