diff --git a/.vscode/settings.json b/.vscode/settings.json index 96790fb..6be1d0a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,4 +1,5 @@ { "editor.formatOnSave": true, - "editor.formatOnType": true + "editor.formatOnType": true, + "vscode-neovim.neovimExecutablePaths.linux": "/usr/bin/nvim" } \ No newline at end of file diff --git a/scenes/Bogie.tscn b/scenes/Bogie.tscn new file mode 100644 index 0000000..20ca9c0 --- /dev/null +++ b/scenes/Bogie.tscn @@ -0,0 +1,9 @@ +[gd_scene format=2] + +[node name="Bogey" type="Spatial"] + +[node name="CSGBox" type="CSGBox" parent="."] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.2, 0 ) +width = 0.9 +height = 0.4 +depth = 1.1 diff --git a/scenes/Main.tscn b/scenes/Main.tscn index d0886f1..c78681e 100644 --- a/scenes/Main.tscn +++ b/scenes/Main.tscn @@ -7,6 +7,7 @@ [node name="Main" type="Node2D"] [node name="Train" parent="." instance=ExtResource( 1 )] +_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 ) @@ -14,4 +15,3 @@ transform = Transform( 0.515898, 0.606099, -0.605386, -0.393123, 0.795389, 0.461 [node name="OrbitCamera" parent="." instance=ExtResource( 3 )] [node name="Railway" parent="." instance=ExtResource( 2 )] -script = null diff --git a/scenes/Rail.tscn b/scenes/Rail.tscn new file mode 100644 index 0000000..648f3de --- /dev/null +++ b/scenes/Rail.tscn @@ -0,0 +1,25 @@ +[gd_scene load_steps=2 format=2] + +[sub_resource type="Curve3D" id=1] +_data = { +"points": PoolVector3Array( ), +"tilts": PoolRealArray( ) +} + +[node name="Rail" type="Spatial"] + +[node name="Path" type="Path" parent="."] +curve = SubResource( 1 ) + +[node name="CSGPolygon" type="CSGPolygon" parent="."] +polygon = PoolVector2Array( -0.383256, -0.00220253, -0.383256, 1.0066, 0.383256, 1.00219, 0.383256, -0.00220253 ) +mode = 2 +path_node = NodePath("../Path") +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 diff --git a/scenes/Railway.tscn b/scenes/Railway.tscn index b1413fe..15a5b52 100644 --- a/scenes/Railway.tscn +++ b/scenes/Railway.tscn @@ -5,23 +5,23 @@ [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, -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 ), +"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 ) } [node name="Railway" type="Spatial"] script = ExtResource( 1 ) +RailWidth = 0.08 +RailHeight = 0.12 +RailGauge = 1.0 _railPaths = [ NodePath("L"), NodePath("R") ] _centreLine = NodePath("Centreline") -_railWidth = 0.07 -_railHeight = 0.12 -_railGauge = 0.8 [node name="Centreline" type="Path" parent="."] curve = SubResource( 1 ) [node name="L" type="CSGPolygon" parent="."] -polygon = PoolVector2Array( -0.47, 0, -0.47, 0.12, -0.33, 0.12, -0.33, 0 ) +polygon = PoolVector2Array( -0.58, 0, -0.58, 0.12, -0.42, 0.12, -0.42, 0 ) mode = 2 path_node = NodePath("../Centreline") path_interval_type = 0 @@ -34,7 +34,7 @@ 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 ) +polygon = PoolVector2Array( 0.42, 0, 0.42, 0.12, 0.58, 0.12, 0.58, 0 ) mode = 2 path_node = NodePath("../Centreline") path_interval_type = 0 diff --git a/scenes/Train.tscn b/scenes/Train.tscn index 1f28946..af13423 100644 --- a/scenes/Train.tscn +++ b/scenes/Train.tscn @@ -1,11 +1,9 @@ -[gd_scene load_steps=2 format=2] +[gd_scene load_steps=3 format=2] [ext_resource path="res://scripts/Train.cs" type="Script" id=1] +[ext_resource path="res://scenes/TrainCar.tscn" type="PackedScene" id=2] [node name="Train" type="Spatial"] script = ExtResource( 1 ) -[node name="CSGBox" type="CSGBox" parent="."] -width = 1.0 -height = 1.0 -depth = 4.0 +[node name="Train Car" parent="." instance=ExtResource( 2 )] diff --git a/scenes/TrainCar.tscn b/scenes/TrainCar.tscn new file mode 100644 index 0000000..f11e3ec --- /dev/null +++ b/scenes/TrainCar.tscn @@ -0,0 +1,21 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://scripts/TrainCar.cs" type="Script" id=1] +[ext_resource path="res://scenes/Bogie.tscn" type="PackedScene" id=2] + +[node name="Train Car" type="Spatial"] +script = ExtResource( 1 ) +_boxNode = NodePath("CSGBox") +_bogiePaths = [ NodePath("Bogie"), NodePath("Bogie2") ] + +[node name="CSGBox" type="CSGBox" parent="."] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0 ) +width = 1.0 +height = 1.0 +depth = 8.0 + +[node name="Bogie" parent="." instance=ExtResource( 2 )] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -2 ) + +[node name="Bogie2" parent="." instance=ExtResource( 2 )] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 2 ) diff --git a/scripts/Railway.cs b/scripts/Railway.cs index ca86bee..3513380 100644 --- a/scripts/Railway.cs +++ b/scripts/Railway.cs @@ -11,12 +11,51 @@ public class Railway : Node [Export] private NodePath _centreLine; - [Export] + private float _railWidth; - [Export] private float _railHeight; + private float _railGauge; + [Export] - private float _railGauge = 1.0f; + private float RailWidth + { + get => _railWidth; + set + { + _railWidth = value; + if (Engine.EditorHint) + { + SetRailCrossSection(); + } + } + } + [Export] + private float RailHeight + { + get => _railHeight; + set + { + _railHeight = value; + if (Engine.EditorHint) + { + SetRailCrossSection(); + } + } + } + + [Export] + private float RailGauge + { + get => _railGauge; + set + { + _railGauge = value; + if (Engine.EditorHint) + { + SetRailCrossSection(); + } + } + } private Path _path = null; private Path Path @@ -40,6 +79,7 @@ public class Railway : Node // Called when the node enters the scene tree for the first time. public override void _Ready() { + } private void SetRailCrossSection() @@ -53,10 +93,10 @@ public class Railway : Node { var rail = GetNode(_railPaths[i]); - float w = _railWidth; - float h = _railHeight; + float w = RailWidth; + float h = RailHeight; // horizontal offset of rail from centreline - float c = (-.5f + i) * _railGauge; + float c = (-.5f + i) * RailGauge; var polygon = new Vector2[4]; polygon[0] = new Vector2(c - w, 0); @@ -69,10 +109,7 @@ public class Railway : Node public override void _Process(float delta) { - if (Engine.EditorHint) - { - SetRailCrossSection(); - } + } public void AddPathFollower(PathFollow pathFollow) diff --git a/scripts/TrainCar.cs b/scripts/TrainCar.cs new file mode 100644 index 0000000..8d88df1 --- /dev/null +++ b/scripts/TrainCar.cs @@ -0,0 +1,72 @@ +using Godot; +using System; + +[Tool] +public class TrainCar : Spatial +{ + public float Length => Box.Depth; + + [Export] + private NodePath _boxNode; + + [Export] + private NodePath[] _bogiePaths; + + [Export] + private float Wheelbase + { + get => _wheelbase; + set + { + _wheelbase = value; + if (Engine.EditorHint) + { + SetBogeyPositions(); + } + } + } + private float _wheelbase; + + private readonly Spatial[] _bogies = new Spatial[2]; + + private CSGBox _box; + private CSGBox Box + { + get + { + if (_box == null) + { + _box = GetNode(_boxNode); + } + return _box; + } + } + + public override void _Ready() + { + + } + + public override void _Process(float delta) + { + } + + private void SetBogeyPositions() + { + + if (Wheelbase > Length) + throw new Exception("wheelbase cannot be longer than car length"); + + if (_bogiePaths.Length != 2) + throw new Exception("_bogeyPaths should be of length 2"); + + for (int i = 0; i < 2; i++) + { + _bogies[i] = GetNode(_bogiePaths[i]); + } + + var axis = Vector3.Forward * Wheelbase; + _bogies[0].Translation = axis * 0.5f; + _bogies[1].Translation = axis * -0.5f; + } +}