bogies and exported properties

This commit is contained in:
ktyl 2022-08-16 01:02:38 +01:00 committed by Cat Flynn
parent 64a77d01ae
commit be9d2a944b
9 changed files with 187 additions and 21 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

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

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

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

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;
}
}