83 lines
1.9 KiB
C#
83 lines
1.9 KiB
C#
using Godot;
|
|
using System;
|
|
|
|
[Tool]
|
|
public class Railway : Node
|
|
{
|
|
[Export]
|
|
private NodePath[] _railPaths;
|
|
|
|
|
|
[Export]
|
|
private NodePath _centreLine;
|
|
|
|
[Export]
|
|
private float _railWidth;
|
|
[Export]
|
|
private float _railHeight;
|
|
[Export]
|
|
private float _railGauge = 1.0f;
|
|
|
|
private Path _path = null;
|
|
private Path Path
|
|
{
|
|
get
|
|
{
|
|
if (_path == null)
|
|
{
|
|
_path = GetNode<Path>(_centreLine);
|
|
}
|
|
return _path;
|
|
}
|
|
}
|
|
|
|
public Curve3D Curve => Path.Curve;
|
|
|
|
// the centre line should not have geometry directly associated with it.
|
|
// instead, two additional path/csgpolygon combinations should be managed
|
|
// in reference to the centre line for the left and right rails.
|
|
|
|
// Called when the node enters the scene tree for the first time.
|
|
public override void _Ready()
|
|
{
|
|
}
|
|
|
|
private void SetRailCrossSection()
|
|
{
|
|
// error checkin
|
|
if (_railPaths.Length != 2)
|
|
throw new Exception("need 2 rails");
|
|
|
|
// set rail geometry
|
|
for (int i = 0; i < 2; i++)
|
|
{
|
|
var rail = GetNode<CSGPolygon>(_railPaths[i]);
|
|
|
|
float w = _railWidth;
|
|
float h = _railHeight;
|
|
// horizontal offset of rail from centreline
|
|
float c = (-.5f + i) * _railGauge;
|
|
|
|
var polygon = new Vector2[4];
|
|
polygon[0] = new Vector2(c - w, 0);
|
|
polygon[1] = new Vector2(c - w, h);
|
|
polygon[2] = new Vector2(c + w, h);
|
|
polygon[3] = new Vector2(c + w, 0);
|
|
rail.Polygon = polygon;
|
|
}
|
|
}
|
|
|
|
public override void _Process(float delta)
|
|
{
|
|
if (Engine.EditorHint)
|
|
{
|
|
SetRailCrossSection();
|
|
}
|
|
}
|
|
|
|
public void AddPathFollower(PathFollow pathFollow)
|
|
{
|
|
Path.AddChild(pathFollow);
|
|
}
|
|
}
|