make train go back n forth
This commit is contained in:
parent
cd800be3eb
commit
a890b71df0
|
@ -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 )
|
|
|
@ -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]
|
[ext_resource path="res://scripts/Railway.cs" type="Script" id=1]
|
||||||
|
|
||||||
[sub_resource type="Curve3D" id=1]
|
[sub_resource type="Curve3D" id=1]
|
||||||
|
bake_interval = 0.01
|
||||||
_data = {
|
_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 ),
|
"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 )
|
"tilts": PoolRealArray( 0, 0, 0, 0, 0, 0, 0, 0, 0 )
|
||||||
}
|
}
|
||||||
|
|
||||||
[sub_resource type="SpatialMaterial" id=2]
|
[node name="Railway" type="Spatial"]
|
||||||
flags_unshaded = true
|
|
||||||
vertex_color_use_as_albedo = true
|
|
||||||
|
|
||||||
[node name="Railway" type="Path"]
|
|
||||||
curve = SubResource( 1 )
|
|
||||||
script = ExtResource( 1 )
|
script = ExtResource( 1 )
|
||||||
_color = Color( 0, 1, 0.133333, 1 )
|
|
||||||
|
|
||||||
[node name="ImmediateGeometry" type="ImmediateGeometry" parent="."]
|
[node name="Path" type="Path" parent="."]
|
||||||
material_override = SubResource( 2 )
|
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
|
||||||
|
|
|
@ -2,11 +2,10 @@
|
||||||
|
|
||||||
[ext_resource path="res://scripts/Train.cs" type="Script" id=1]
|
[ext_resource path="res://scripts/Train.cs" type="Script" id=1]
|
||||||
|
|
||||||
[node name="Train" type="Node"]
|
[node name="Train" type="Spatial"]
|
||||||
script = ExtResource( 1 )
|
script = ExtResource( 1 )
|
||||||
|
|
||||||
[node name="CSGBox" type="CSGBox" parent="."]
|
[node name="CSGBox" type="CSGBox" parent="."]
|
||||||
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.5, 0 )
|
|
||||||
width = 1.0
|
width = 1.0
|
||||||
height = 1.0
|
height = 1.0
|
||||||
depth = 4.0
|
depth = 4.0
|
||||||
|
|
|
@ -1,53 +1,34 @@
|
||||||
using Godot;
|
using Godot;
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
[Tool]
|
public class Railway : Node
|
||||||
public class Railway : Path
|
|
||||||
{
|
{
|
||||||
[Export]
|
private Path _path = null;
|
||||||
private Color _color;
|
private Path Path
|
||||||
|
|
||||||
[Export]
|
|
||||||
private float _gauge = 1f;
|
|
||||||
|
|
||||||
private ImmediateGeometry _geo = null;
|
|
||||||
private Vector3[] _points = null;
|
|
||||||
|
|
||||||
public void OnPathCurveChanged()
|
|
||||||
{
|
{
|
||||||
Curve.Tessellate();
|
get
|
||||||
_points = Curve.GetBakedPoints();
|
{
|
||||||
|
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.
|
// Called when the node enters the scene tree for the first time.
|
||||||
public override void _Ready()
|
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)
|
public override void _Process(float delta)
|
||||||
{
|
{
|
||||||
base._Process(delta);
|
}
|
||||||
|
|
||||||
if (_geo == null) return;
|
public void AddPathFollower(PathFollow pathFollow)
|
||||||
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]);
|
Path.AddChild(pathFollow);
|
||||||
}
|
|
||||||
_geo.End();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,52 @@
|
||||||
using Godot;
|
using Godot;
|
||||||
using System;
|
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.
|
// Called when the node enters the scene tree for the first time.
|
||||||
public override void _Ready()
|
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.
|
// Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||||
// public override void _Process(float delta)
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue