From c0fe68a8ace0d4d381c0dd74a5d2ef05058f687e Mon Sep 17 00:00:00 2001 From: ktyl Date: Sun, 14 Aug 2022 23:50:09 +0100 Subject: [PATCH] 2 tracks --- scenes/Railway.tscn | 35 +++++++++++++++++++++++-------- scripts/Railway.cs | 50 ++++++++++++++++++++++++++++++++++++++++++++- scripts/Tracks.cs | 22 -------------------- 3 files changed, 75 insertions(+), 32 deletions(-) delete mode 100644 scripts/Tracks.cs diff --git a/scenes/Railway.tscn b/scenes/Railway.tscn index 77709bd..b1413fe 100644 --- a/scenes/Railway.tscn +++ b/scenes/Railway.tscn @@ -5,24 +5,41 @@ [sub_resource type="Curve3D" id=1] bake_interval = 0.01 _data = { -"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 ), +"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, -15.8992, 0.000904083, 5.83998, -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.16602, 4.07454e-10, 3.45615, 1.16602, -4.07454e-10, -3.45615, -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 ) } [node name="Railway" type="Spatial"] script = ExtResource( 1 ) +_railPaths = [ NodePath("L"), NodePath("R") ] +_centreLine = NodePath("Centreline") +_railWidth = 0.07 +_railHeight = 0.12 +_railGauge = 0.8 -[node name="Path" type="Path" parent="."] +[node name="Centreline" type="Path" parent="."] curve = SubResource( 1 ) -[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 ) +[node name="L" type="CSGPolygon" parent="."] +polygon = PoolVector2Array( -0.47, 0, -0.47, 0.12, -0.33, 0.12, -0.33, 0 ) mode = 2 -path_node = NodePath("../Path") -path_interval_type = 1 -path_interval = 0.01 -path_simplify_angle = 0.2 +path_node = NodePath("../Centreline") +path_interval_type = 0 +path_interval = 1.0 +path_simplify_angle = 0.0 +path_rotation = 2 +path_local = false +path_continuous_u = true +path_u_distance = 1.0 +path_joined = false + +[node name="R" type="CSGPolygon" parent="."] +polygon = PoolVector2Array( 0.33, 0, 0.33, 0.12, 0.47, 0.12, 0.47, 0 ) +mode = 2 +path_node = NodePath("../Centreline") +path_interval_type = 0 +path_interval = 1.0 +path_simplify_angle = 0.0 path_rotation = 2 path_local = false path_continuous_u = true diff --git a/scripts/Railway.cs b/scripts/Railway.cs index 3125d3e..ca86bee 100644 --- a/scripts/Railway.cs +++ b/scripts/Railway.cs @@ -1,8 +1,23 @@ using Godot; using System; +[Tool] public class Railway : Node { + [Export] + private NodePath[] _railPaths; + + + [Export] + private NodePath _centreLine; + + [Export] + private float _railWidth; + [Export] + private float _railHeight; + [Export] + private float _railGauge = 1.0f; + private Path _path = null; private Path Path { @@ -10,7 +25,7 @@ public class Railway : Node { if (_path == null) { - _path = GetNode("Path"); + _path = GetNode(_centreLine); } return _path; } @@ -18,13 +33,46 @@ public class Railway : Node public Curve3D Curve => Path.Curve; + // the centre line should not have geometry directly associated with it. + // instead, two additional path/csgpolygon combinations should be managed + // in reference to the centre line for the left and right rails. + // Called when the node enters the scene tree for the first time. public override void _Ready() { } + private void SetRailCrossSection() + { + // error checkin + if (_railPaths.Length != 2) + throw new Exception("need 2 rails"); + + // set rail geometry + for (int i = 0; i < 2; i++) + { + var rail = GetNode(_railPaths[i]); + + float w = _railWidth; + float h = _railHeight; + // horizontal offset of rail from centreline + float c = (-.5f + i) * _railGauge; + + var polygon = new Vector2[4]; + polygon[0] = new Vector2(c - w, 0); + polygon[1] = new Vector2(c - w, h); + polygon[2] = new Vector2(c + w, h); + polygon[3] = new Vector2(c + w, 0); + rail.Polygon = polygon; + } + } + public override void _Process(float delta) { + if (Engine.EditorHint) + { + SetRailCrossSection(); + } } public void AddPathFollower(PathFollow pathFollow) diff --git a/scripts/Tracks.cs b/scripts/Tracks.cs deleted file mode 100644 index 27593a2..0000000 --- a/scripts/Tracks.cs +++ /dev/null @@ -1,22 +0,0 @@ -using Godot; -using System; - -[Tool] -public class Tracks : ImmediateGeometry -{ - private SpatialMaterial _m; - - public override void _Ready() - { - // set up a material for imgui to use - _m = new SpatialMaterial(); - _m.VertexColorUseAsAlbedo = true; - _m.FlagsUnshaded = true; - this.MaterialOverride = _m; - } - - public override void _Process(float delta) - { - GD.Print("hi!"); - } -}