mirror of
https://github.com/MaSzyna-EU07/StarterNG.git
synced 2026-10-16 23:49:19 +02:00
Hand the driver over when a locomotive is moved to the head
The locomotive put first takes the driver, in cab A; the one it pushed behind is left unstaffed. Stepping a card left or right is now an ordinary move, so it also recouples the train and frees the coupler at its back.
This commit is contained in:
@@ -87,6 +87,42 @@ public class ConsistCrewTests
|
|||||||
Assert.Equal(eDriverType.Nobody, second.Cars[0].DriverType);
|
Assert.Equal(eDriverType.Nobody, second.Cars[0].DriverType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void A_locomotive_moved_to_the_head_takes_the_driver_from_the_one_it_put_behind()
|
||||||
|
{
|
||||||
|
var first = Add("ep07-001", 0);
|
||||||
|
var second = Add("ep07-002", 1);
|
||||||
|
|
||||||
|
_consist.Move(1, 0);
|
||||||
|
|
||||||
|
Assert.Equal(eDriverType.Headdriver, second.Cars[0].DriverType);
|
||||||
|
Assert.Equal(eDriverType.Nobody, first.Cars[0].DriverType);
|
||||||
|
Assert.Equal(eDriverType.Nobody, first.Driver);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Stepping_a_locomotive_left_onto_the_head_hands_over_the_same_way()
|
||||||
|
{
|
||||||
|
var first = Add("ep07-001", 0);
|
||||||
|
var second = Add("ep07-002", 1);
|
||||||
|
|
||||||
|
_consist.MoveLeft(second);
|
||||||
|
|
||||||
|
Assert.Equal(eDriverType.Headdriver, second.Cars[0].DriverType);
|
||||||
|
Assert.Equal(eDriverType.Nobody, first.Cars[0].DriverType);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void A_wagon_moved_to_the_head_leaves_the_driver_where_he_is()
|
||||||
|
{
|
||||||
|
var loco = Add("ep07-001", 0);
|
||||||
|
Add("b111-01", 1);
|
||||||
|
|
||||||
|
_consist.Move(1, 0);
|
||||||
|
|
||||||
|
Assert.Equal(eDriverType.Headdriver, loco.Cars[0].DriverType);
|
||||||
|
}
|
||||||
|
|
||||||
private ConsistItem Add(string skin, int at)
|
private ConsistItem Add(string skin, int at)
|
||||||
{
|
{
|
||||||
var item = Item(skin);
|
var item = Item(skin);
|
||||||
|
|||||||
@@ -289,36 +289,51 @@ public sealed class Consist : IReadOnlyList<ConsistItem>
|
|||||||
{
|
{
|
||||||
int i = _items.IndexOf(item);
|
int i = _items.IndexOf(item);
|
||||||
if (i > 0)
|
if (i > 0)
|
||||||
{
|
Move(i, i - 1);
|
||||||
(_items[i - 1], _items[i]) = (_items[i], _items[i - 1]);
|
|
||||||
Raise();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void MoveRight(ConsistItem item)
|
public void MoveRight(ConsistItem item)
|
||||||
{
|
{
|
||||||
int i = _items.IndexOf(item);
|
int i = _items.IndexOf(item);
|
||||||
if (i >= 0 && i < _items.Count - 1)
|
if (i >= 0 && i < _items.Count - 1)
|
||||||
{
|
Move(i, i + 1);
|
||||||
(_items[i + 1], _items[i]) = (_items[i], _items[i + 1]);
|
|
||||||
Raise();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Move(int from, int to)
|
public void Move(int from, int to)
|
||||||
{
|
{
|
||||||
if (from < 0 || from >= _items.Count) return;
|
if (from < 0 || from >= _items.Count) return;
|
||||||
|
|
||||||
|
var previousHead = _items[0];
|
||||||
var item = _items[from];
|
var item = _items[from];
|
||||||
_items.RemoveAt(from);
|
_items.RemoveAt(from);
|
||||||
to = Math.Clamp(to, 0, _items.Count);
|
to = Math.Clamp(to, 0, _items.Count);
|
||||||
_items.Insert(to, item);
|
_items.Insert(to, item);
|
||||||
|
|
||||||
|
HandOverLead(previousHead);
|
||||||
Selected = item;
|
Selected = item;
|
||||||
AutoConnectAll();
|
AutoConnectAll();
|
||||||
Raise();
|
Raise();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A locomotive that comes to the head of the train takes over the driver of
|
||||||
|
/// the one it put behind, which is left unstaffed: the train is driven from its
|
||||||
|
/// front, from cab A.
|
||||||
|
/// </summary>
|
||||||
|
private void HandOverLead(ConsistItem previousHead)
|
||||||
|
{
|
||||||
|
var head = _items[0];
|
||||||
|
if (ReferenceEquals(head, previousHead) || !IsPowered(head) ||
|
||||||
|
previousHead.Driver is not (eDriverType.Headdriver or eDriverType.Reardriver))
|
||||||
|
return;
|
||||||
|
|
||||||
|
previousHead.Driver = eDriverType.Nobody;
|
||||||
|
foreach (var car in previousHead.Cars)
|
||||||
|
car.DriverType = eDriverType.Nobody;
|
||||||
|
|
||||||
|
Crew(head, eDriverType.Headdriver);
|
||||||
|
}
|
||||||
|
|
||||||
public void Remove(ConsistItem item)
|
public void Remove(ConsistItem item)
|
||||||
{
|
{
|
||||||
int gap = _items.IndexOf(item);
|
int gap = _items.IndexOf(item);
|
||||||
|
|||||||
Reference in New Issue
Block a user