bogies and exported properties

This commit is contained in:
ktyl 2022-08-16 01:02:38 +01:00 committed by Cat Flynn
parent c0fe68a8ac
commit fb9c9d631d
9 changed files with 186 additions and 23 deletions

View File

@ -1,4 +1,5 @@
{
"editor.formatOnSave": true,
"editor.formatOnType": true
"editor.formatOnType": true,
"vscode-neovim.neovimExecutablePaths.linux": "/usr/bin/nvim"
}

9
scenes/Bogie.tscn Normal file
View File

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

View File

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

25
scenes/Rail.tscn Normal file
View File

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

View File

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

View File

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

21
scenes/TrainCar.tscn Normal file
View File

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

View File

@ -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<CSGPolygon>(_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)

72
scripts/TrainCar.cs Normal file
View File

@ -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<CSGBox>(_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<Spatial>(_bogiePaths[i]);
}
var axis = Vector3.Forward * Wheelbase;
_bogies[0].Translation = axis * 0.5f;
_bogies[1].Translation = axis * -0.5f;
}
}