From 44cbc7b5aec2e17d666d64eaef7245add5693ad1 Mon Sep 17 00:00:00 2001 From: ktyl Date: Tue, 16 Aug 2022 01:02:38 +0100 Subject: [PATCH] bogies and exported properties --- .vscode/settings.json | 3 +- scenes/Bogie.tscn | 9 ++++++ scenes/Main.tscn | 3 ++ scenes/Rail.tscn | 25 +++++++++++++++ scenes/Railway.tscn | 10 +++--- scenes/Train.tscn | 8 ++--- scenes/TrainCar.tscn | 21 +++++++++++++ scripts/Railway.cs | 57 ++++++++++++++++++++++++++++------ scripts/TrainCar.cs | 72 +++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 187 insertions(+), 21 deletions(-) create mode 100644 scenes/Bogie.tscn create mode 100644 scenes/Rail.tscn create mode 100644 scenes/TrainCar.tscn create mode 100644 scripts/TrainCar.cs 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 d1a25a5..0d89453 100644 --- a/scenes/Main.tscn +++ b/scenes/Main.tscn @@ -17,3 +17,6 @@ transform = Transform( 0.515898, 0.606099, -0.605386, -0.393123, 0.795389, 0.461 _speed = 0.5 [node name="Railway" parent="." instance=ExtResource( 2 )] +RailWidth = 0.0 +RailHeight = 0.0 +RailGauge = 0.0 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..1430423 100644 --- a/scenes/Railway.tscn +++ b/scenes/Railway.tscn @@ -11,17 +11,17 @@ _data = { [node name="Railway" type="Spatial"] script = ExtResource( 1 ) +RailWidth = 0.05 +RailHeight = 0.1 +RailGauge = 0.8 _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.45, 0, -0.45, 0.1, -0.35, 0.1, -0.35, 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.35, 0, 0.35, 0.1, 0.45, 0.1, 0.45, 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; + } +}