shmodot/scripts/TrainCar.cs

91 lines
1.8 KiB
C#

using Godot;
using System;
[Tool]
public class TrainCar : Spatial
{
public float Length => Box.Depth;
[Export]
private NodePath _boxNode;
[Export]
private NodePath[] _bogiePaths;
[Export]
public float Wheelbase
{
get => _wheelbase;
set
{
_wheelbase = value;
// don't set the wheelbase except from the editor
if (!Engine.EditorHint) return;
SetBogeyPositions();
}
}
private float _wheelbase;
public Spatial[] Bogies
{
get
{
if (_bogies == null)
{
_bogies = new Spatial[2];
for (int i = 0; i < _bogiePaths.Length; i++)
{
_bogies[i] = GetNode<Spatial>(_bogiePaths[i]);
}
}
return _bogies;
}
}
private Spatial[] _bogies = null;
private CSGBox _box;
private CSGBox Box
{
get
{
if (_box == null)
{
_box = GetNode<CSGBox>(_boxNode);
}
return _box;
}
}
public Railway Railway { get; set; }
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;
}
}