Scale docking momentum by hull, require manual mooring and channel maneuvers in Captain Career, and add rank-specific bridge instruments and voiced training.
404 lines
20 KiB
C#
404 lines
20 KiB
C#
using UnityEngine;
|
||
using UnityEngine.UIElements;
|
||
|
||
namespace BigShipTycoon
|
||
{
|
||
public sealed partial class TycoonApp
|
||
{
|
||
private static readonly string[] DockOrderNames = { "Half astern", "Dead slow astern", "Stop", "Dead slow ahead", "Half ahead" };
|
||
private static readonly Color DockRouteColor = new(.33f, .61f, .66f);
|
||
private static readonly Color DockMarkColor = new(.87f, .73f, .47f);
|
||
private readonly Button[] dockOrderButtons = new Button[DockOrderNames.Length];
|
||
private Button dockBowLineButton, dockSternLineButton, dockAutopilotButton;
|
||
private Label dockSpeedReadout, dockCourseReadout, dockPrecisionReadout, dockInstruction;
|
||
private VisualElement dockPlan;
|
||
private StyleSheet dockManeuverStyle;
|
||
private float dockRudderHold, dockThrusterHold, dockRudderPulse, dockThrusterPulse;
|
||
private float dockRudderUntil, dockThrusterUntil, dockNextPlanPaint;
|
||
|
||
private void DrawDockManeuverControls(VisualElement overlay)
|
||
{
|
||
DockManeuver maneuver = ActiveDockManeuver;
|
||
if (maneuver == null)
|
||
return;
|
||
|
||
dockRudderHold = dockThrusterHold = dockRudderPulse = dockThrusterPulse = 0;
|
||
dockRudderUntil = dockThrusterUntil = dockNextPlanPaint = 0;
|
||
var host = Add(overlay, "dock-console-host");
|
||
host.pickingMode = PickingMode.Ignore;
|
||
dockManeuverStyle ??= Resources.Load<StyleSheet>("DockManeuver");
|
||
if (dockManeuverStyle != null)
|
||
host.styleSheets.Add(dockManeuverStyle);
|
||
|
||
var console = Add(host, "dock-console");
|
||
var header = Add(console, "dock-header");
|
||
var title = Add(header, "dock-title-group");
|
||
int port = maneuver.Arriving ? underway.Destination : underway.Origin;
|
||
AddText(title, Market.Ports[port].Name.ToUpperInvariant(), "voyage-caption");
|
||
AddDisplay(title, maneuver.Arriving ? "Bring the ship alongside" : "Clear the harbor", "dock-title");
|
||
var camera = Add(header, "dock-controls-row");
|
||
camera.Add(Action("Bridge B", ShowDockBridge, PassageViewClass(passageBridge), "dock-bridge"));
|
||
camera.Add(Action("Exterior V", ShowDockExterior, PassageViewClass(!passageBridge), "dock-exterior"));
|
||
if (DockRadarAvailable)
|
||
camera.Add(Action("Radar range R", CycleDockRadarRange, "voyage-view-button", "dock-radar-range"));
|
||
if (DockAdvancedRadarAvailable)
|
||
camera.Add(Action("Radar orientation N", ToggleDockRadarOrientation, "voyage-view-button", "dock-radar-orientation"));
|
||
|
||
var instruments = Add(console, "dock-instruments");
|
||
var chart = Add(instruments, "dock-chart");
|
||
var chartLegend = Add(chart, "dock-chart-legend");
|
||
AddText(chartLegend, "BERTH", "voyage-caption");
|
||
AddText(chartLegend, "CHANNEL EXIT", "voyage-caption");
|
||
dockPlan = Add(chart, "dock-plan");
|
||
dockPlan.pickingMode = PickingMode.Ignore;
|
||
dockPlan.generateVisualContent += ctx => PaintDockPlan(ctx, maneuver);
|
||
dockSpeedReadout = AddText(chart, "", "dock-speed");
|
||
dockCourseReadout = AddText(chart, "", "dock-course");
|
||
dockPrecisionReadout = DockAdvancedRadarAvailable ? AddText(chart, "", "dock-course") : null;
|
||
|
||
var controls = Add(instruments, "dock-controls");
|
||
AddText(controls, "ENGINE ORDER · W / S", "voyage-caption");
|
||
var orders = Add(controls, "dock-controls-row dock-orders");
|
||
for (int index = 0; index < DockOrderNames.Length; index++)
|
||
{
|
||
DockOrder order = (DockOrder)(index - 2);
|
||
Button button = Action(DockOrderNames[index], () =>
|
||
{
|
||
maneuver.SetOrder(order);
|
||
RefreshDockManeuverControls(maneuver);
|
||
},
|
||
"voyage-view-button dock-order", $"dock-order-{(int)order}");
|
||
if (order == DockOrder.Stop)
|
||
button.tooltip = "X sets Stop. The ship keeps its way until drag or reverse thrust slows it.";
|
||
if (index == DockOrderNames.Length - 1)
|
||
button.style.marginRight = 0;
|
||
dockOrderButtons[index] = button;
|
||
orders.Add(button);
|
||
}
|
||
|
||
var steering = Add(controls, "dock-controls-row dock-steering");
|
||
var rudder = Add(steering, "dock-input-group");
|
||
AddText(rudder, "RUDDER · A / D", "voyage-caption");
|
||
var rudderButtons = Add(rudder, "dock-controls-row");
|
||
rudderButtons.Add(DockMomentaryButton("Port", "dock-rudder-port", false, -1));
|
||
rudderButtons.Add(DockMomentaryButton("Starboard", "dock-rudder-starboard", false, 1));
|
||
if (maneuver.HasBowThruster)
|
||
{
|
||
var thruster = Add(steering, "dock-input-group");
|
||
AddText(thruster, "BOW THRUSTER · Z / C", "voyage-caption");
|
||
var thrustButtons = Add(thruster, "dock-controls-row");
|
||
thrustButtons.Add(DockMomentaryButton("Push port", "dock-thruster-port", true, -1));
|
||
thrustButtons.Add(DockMomentaryButton("Push starboard", "dock-thruster-starboard", true, 1));
|
||
}
|
||
|
||
var lines = Add(controls, "dock-controls-row dock-lines");
|
||
dockBowLineButton = Action("", () => ToggleDockLine(maneuver, true), "voyage-view-button dock-line", "dock-bow-line");
|
||
dockSternLineButton = Action("", () => ToggleDockLine(maneuver, false), "voyage-view-button dock-line", "dock-stern-line");
|
||
lines.Add(dockBowLineButton);
|
||
lines.Add(dockSternLineButton);
|
||
dockAutopilotButton = null;
|
||
if (!maneuver.Arriving)
|
||
{
|
||
dockAutopilotButton = Action("Engage autopilot H", EngageDockAutopilot,
|
||
"voyage-view-button dock-handoff", "dock-autopilot");
|
||
lines.Add(dockAutopilotButton);
|
||
}
|
||
dockInstruction = AddText(console, "", "dock-instruction");
|
||
RefreshDockManeuverControls(maneuver);
|
||
}
|
||
|
||
private void UpdateDockManeuverControls(float delta)
|
||
{
|
||
DockManeuver maneuver = ActiveDockManeuver;
|
||
if (maneuver == null)
|
||
return;
|
||
|
||
bool acceptsInput = Application.isFocused && !EditingText();
|
||
if (acceptsInput)
|
||
{
|
||
if (Input.GetKeyDown(KeyCode.W))
|
||
maneuver.SetOrder((DockOrder)Mathf.Min(2, (int)maneuver.Order + 1));
|
||
if (Input.GetKeyDown(KeyCode.S))
|
||
maneuver.SetOrder((DockOrder)Mathf.Max(-2, (int)maneuver.Order - 1));
|
||
if (Input.GetKeyDown(KeyCode.X))
|
||
maneuver.SetOrder(DockOrder.Stop);
|
||
if (Input.GetKeyDown(KeyCode.F))
|
||
maneuver.ToggleLine(true);
|
||
if (Input.GetKeyDown(KeyCode.G))
|
||
maneuver.ToggleLine(false);
|
||
if (Input.GetKeyDown(KeyCode.B))
|
||
ShowDockBridge();
|
||
if (Input.GetKeyDown(KeyCode.V))
|
||
ShowDockExterior();
|
||
if (DockRadarAvailable && Input.GetKeyDown(KeyCode.R))
|
||
CycleDockRadarRange();
|
||
if (DockAdvancedRadarAvailable && Input.GetKeyDown(KeyCode.N))
|
||
ToggleDockRadarOrientation();
|
||
if (maneuver.ReadyForAutopilot && Input.GetKeyDown(KeyCode.H))
|
||
{
|
||
EngageDockAutopilot();
|
||
return;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
dockRudderHold = dockThrusterHold = 0;
|
||
dockRudderUntil = dockThrusterUntil = 0;
|
||
}
|
||
|
||
float rudder = 0, thruster = 0;
|
||
if (acceptsInput)
|
||
{
|
||
float now = Time.unscaledTime;
|
||
rudder = DockInput(MoveAxis(KeyCode.D, KeyCode.A), dockRudderHold, now < dockRudderUntil ? dockRudderPulse : 0);
|
||
if (maneuver.HasBowThruster)
|
||
thruster = DockInput(MoveAxis(KeyCode.C, KeyCode.Z), dockThrusterHold, now < dockThrusterUntil ? dockThrusterPulse : 0);
|
||
}
|
||
maneuver.Advance(delta, rudder, thruster);
|
||
RefreshDockManeuverControls(maneuver);
|
||
}
|
||
|
||
private void RefreshDockManeuverControls(DockManeuver maneuver)
|
||
{
|
||
if (dockInstruction == null)
|
||
return;
|
||
|
||
float heading = Mathf.Repeat(maneuver.Heading, 360);
|
||
int headingRounded = Mathf.RoundToInt(heading) % 360;
|
||
float remaining = maneuver.Arriving ? Vector2.Distance(maneuver.Position, maneuver.Berth)
|
||
: Mathf.Max(0, maneuver.ChannelEnd - maneuver.Position.x);
|
||
string way = Mathf.Abs(maneuver.SpeedKnots) < .05f
|
||
? maneuver.SpeedOverGroundKnots < .05f ? "stopped" : "sideways"
|
||
: maneuver.SpeedKnots > 0 ? "ahead" : "astern";
|
||
SetPassageText(dockSpeedReadout, $"{maneuver.SpeedOverGroundKnots:0.0} kn over ground · {way}");
|
||
SetPassageText(dockCourseReadout, $"Heading {headingRounded:000}° · {remaining:0} m to {(maneuver.Arriving ? "berth" : "exit")}");
|
||
if (dockPrecisionReadout != null)
|
||
SetPassageText(dockPrecisionReadout,
|
||
$"Turn {maneuver.TurnRate:+0.0;-0.0;0.0}°/s · Drift {maneuver.LateralSpeedKnots:+0.0;-0.0;0.0} kn");
|
||
|
||
for (int index = 0; index < dockOrderButtons.Length; index++)
|
||
{
|
||
dockOrderButtons[index].EnableInClassList("voyage-control-active", (int)maneuver.Order == index - 2);
|
||
dockOrderButtons[index].SetEnabled(!maneuver.LinesAttached || index == 2);
|
||
}
|
||
RefreshDockLine(dockBowLineButton, maneuver, true);
|
||
RefreshDockLine(dockSternLineButton, maneuver, false);
|
||
if (dockAutopilotButton != null)
|
||
{
|
||
dockAutopilotButton.SetEnabled(maneuver.ReadyForAutopilot);
|
||
dockAutopilotButton.EnableInClassList("voyage-control-active", maneuver.ReadyForAutopilot);
|
||
}
|
||
|
||
string instruction = DockHelmInstruction(maneuver);
|
||
SetPassageText(dockInstruction, instruction);
|
||
dockInstruction.EnableInClassList("dock-instruction-warning", maneuver.AtChannelEdge);
|
||
if (dockPlan != null && Time.unscaledTime >= dockNextPlanPaint)
|
||
{
|
||
dockNextPlanPaint = Time.unscaledTime + .08f;
|
||
dockPlan.MarkDirtyRepaint();
|
||
}
|
||
}
|
||
|
||
private bool DockRadarAvailable => Ship != null && Market.HullRank(Ship.HullIndex) >= Market.HullRank(Market.Coastal);
|
||
|
||
private bool DockAdvancedRadarAvailable => Ship != null && Market.HullRank(Ship.HullIndex) >= Market.HullRank(Market.Ocean);
|
||
|
||
private void ToggleDockLine(DockManeuver maneuver, bool bow)
|
||
{
|
||
maneuver.ToggleLine(bow);
|
||
RefreshDockManeuverControls(maneuver);
|
||
}
|
||
|
||
private static void RefreshDockLine(Button button, DockManeuver maneuver, bool bow)
|
||
{
|
||
bool secure = bow ? maneuver.BowLine : maneuver.SternLine;
|
||
string line = bow ? "bow line" : "stern line";
|
||
string shortcut = bow ? "F" : "G";
|
||
button.text = maneuver.Arriving
|
||
? secure ? $"{(bow ? "Bow" : "Stern")} line secure" : $"Make fast {line} {shortcut}"
|
||
: secure ? $"Haul in {line} {shortcut}" : $"{(bow ? "Bow" : "Stern")} line aboard";
|
||
button.SetEnabled(maneuver.CanToggleLine(bow));
|
||
button.EnableInClassList("dock-line-complete", maneuver.Arriving ? secure : !secure);
|
||
button.tooltip = maneuver.Arriving ? "Bring this end of the ship close to the berth and stop to secure its line."
|
||
: "Bring this line aboard before leaving the berth.";
|
||
}
|
||
|
||
private static string DockHelmInstruction(DockManeuver maneuver)
|
||
{
|
||
if (maneuver.Arriving)
|
||
{
|
||
if (maneuver.Secured)
|
||
return "Both lines are secure.";
|
||
if (maneuver.CanToggleLine(true) || maneuver.CanToggleLine(false))
|
||
return "Make fast the bow and stern lines when both ends are alongside.";
|
||
if (maneuver.AtBerth)
|
||
return $"Set Stop and slow below {DockManeuver.LinesSpeedLimit:0.00} kn before securing the lines. Use a short astern order to check forward way.";
|
||
}
|
||
if (maneuver.AtChannelEdge)
|
||
return "Shallow water or quay edge. Slow down and steer back into the marked channel.";
|
||
if (maneuver.Arriving)
|
||
{
|
||
float approachLead = Mathf.Max(25f, 4f * maneuver.BerthAlongTolerance);
|
||
if (Vector2.Distance(maneuver.Position, maneuver.Berth) < approachLead)
|
||
return $"Line up alongside at {maneuver.TargetHeading:000}°. Set Stop and let the ship coast, or use astern power to check forward way.";
|
||
return "Follow the marked channel toward the berth. Reduce power early for a slow, parallel approach.";
|
||
}
|
||
if (maneuver.BowLine || maneuver.SternLine)
|
||
return "Haul in both lines, then select Dead slow ahead to leave the berth.";
|
||
if (maneuver.ReadyForAutopilot)
|
||
return "The ship is clear of the channel. Engage autopilot when ready.";
|
||
if (maneuver.ChannelClear)
|
||
return $"Keep heading near {maneuver.TargetHeading:000}°, center the ship, and make {DockManeuver.HandoffMinimumSpeed:0.0}–{DockManeuver.HandoffMaximumSpeed:0.0} kn ahead to engage autopilot.";
|
||
return $"Follow the marked channel toward its exit. Use the rudder to bring the heading back to {maneuver.TargetHeading:000}°.";
|
||
}
|
||
|
||
private static float DockInput(float keyboard, float held, float pulse) => keyboard != 0 ? keyboard : held != 0 ? held : pulse;
|
||
|
||
private Button DockMomentaryButton(string label, string id, bool thruster, float direction)
|
||
{
|
||
Button button = Action(label, null, "voyage-view-button dock-momentary", id);
|
||
int pointer = -1;
|
||
float pressedAt = 0;
|
||
void SetHold(float value)
|
||
{
|
||
if (thruster)
|
||
dockThrusterHold = value;
|
||
else
|
||
dockRudderHold = value;
|
||
button.EnableInClassList("voyage-control-active", value != 0);
|
||
}
|
||
void Pulse()
|
||
{
|
||
if (thruster)
|
||
{
|
||
dockThrusterPulse = direction;
|
||
dockThrusterUntil = Time.unscaledTime + .32f;
|
||
}
|
||
else
|
||
{
|
||
dockRudderPulse = direction;
|
||
dockRudderUntil = Time.unscaledTime + .32f;
|
||
}
|
||
}
|
||
button.tooltip = thruster ? "Hold to push the bow sideways at low speed." : "Hold to turn the rudder while water is flowing past it.";
|
||
button.RegisterCallback<PointerDownEvent>(evt =>
|
||
{
|
||
if (evt.button != 0 || pointer != -1)
|
||
return;
|
||
pointer = evt.pointerId;
|
||
pressedAt = Time.unscaledTime;
|
||
SetHold(direction);
|
||
button.CapturePointer(pointer);
|
||
button.Focus();
|
||
evt.StopImmediatePropagation();
|
||
}, TrickleDown.TrickleDown);
|
||
button.RegisterCallback<PointerUpEvent>(evt =>
|
||
{
|
||
if (evt.pointerId != pointer)
|
||
return;
|
||
if (Time.unscaledTime - pressedAt < .12f)
|
||
Pulse();
|
||
SetHold(0);
|
||
pointer = -1;
|
||
button.ReleasePointer(evt.pointerId);
|
||
evt.StopImmediatePropagation();
|
||
}, TrickleDown.TrickleDown);
|
||
button.RegisterCallback<PointerCancelEvent>(evt =>
|
||
{
|
||
if (evt.pointerId == pointer)
|
||
button.ReleasePointer(pointer);
|
||
});
|
||
button.RegisterCallback<PointerCaptureOutEvent>(_ =>
|
||
{
|
||
pointer = -1;
|
||
SetHold(0);
|
||
});
|
||
button.RegisterCallback<NavigationSubmitEvent>(evt =>
|
||
{
|
||
Pulse();
|
||
evt.StopImmediatePropagation();
|
||
}, TrickleDown.TrickleDown);
|
||
return button;
|
||
}
|
||
|
||
private static void PaintDockPlan(MeshGenerationContext context, DockManeuver maneuver)
|
||
{
|
||
Rect bounds = context.visualElement.contentRect;
|
||
if (bounds.width < 32f || bounds.height < 32f)
|
||
return;
|
||
Painter2D painter = context.painter2D;
|
||
float channelLength = maneuver.ChannelEnd - maneuver.Berth.x;
|
||
float berthSide = maneuver.PierY >= 0 ? 1 : -1;
|
||
float halfWidth = Mathf.Max(maneuver.ChannelHalfWidth, Mathf.Abs(maneuver.PierY)) * 1.13f;
|
||
float left = maneuver.Berth.x - .10f * channelLength;
|
||
float right = maneuver.ChannelEnd + .10f * channelLength;
|
||
Vector2 Point(float along, float lateral) => new(
|
||
Mathf.Lerp(10, bounds.width - 10, Mathf.InverseLerp(left, right, along)),
|
||
Mathf.Lerp(7, bounds.height - 7, Mathf.InverseLerp(-halfWidth, halfWidth, lateral)));
|
||
|
||
float pierEnd = maneuver.Berth.x + maneuver.PierHalfLength;
|
||
Vector2 pierStart = Point(left, maneuver.PierY);
|
||
Vector2 pierTip = Point(pierEnd, berthSide * halfWidth);
|
||
painter.fillColor = new Color(.35f, .32f, .24f);
|
||
painter.BeginPath();
|
||
painter.MoveTo(pierStart);
|
||
painter.LineTo(new Vector2(pierTip.x, pierStart.y));
|
||
painter.LineTo(pierTip);
|
||
painter.LineTo(new Vector2(pierStart.x, pierTip.y));
|
||
painter.ClosePath();
|
||
painter.Fill();
|
||
|
||
painter.strokeColor = new Color(.37f, .58f, .59f, .67f);
|
||
painter.lineWidth = 1;
|
||
painter.BeginPath();
|
||
painter.MoveTo(Point(maneuver.Berth.x, maneuver.Berth.y));
|
||
painter.LineTo(Point(maneuver.Berth.x + channelLength * .30f, 0));
|
||
painter.LineTo(Point(right, 0));
|
||
painter.Stroke();
|
||
|
||
float channelSide = maneuver.ChannelHalfWidth * .82f;
|
||
for (int row = 1; row <= 4; row++)
|
||
{
|
||
float markX = Mathf.Lerp(maneuver.Berth.x, maneuver.ChannelEnd, row * .25f);
|
||
DockMapMark(painter, Point(markX, -channelSide), new Color(.26f, .70f, .53f));
|
||
DockMapMark(painter, Point(markX, channelSide), new Color(.80f, .39f, .31f));
|
||
}
|
||
|
||
painter.strokeColor = DockMarkColor;
|
||
painter.lineWidth = 1.1f;
|
||
painter.BeginPath();
|
||
painter.MoveTo(Point(maneuver.ChannelEnd, -channelSide));
|
||
painter.LineTo(Point(maneuver.ChannelEnd, channelSide));
|
||
painter.Stroke();
|
||
DockMapMark(painter, Point(maneuver.Berth.x, maneuver.Berth.y), DockRouteColor);
|
||
|
||
Vector2 ship = Point(maneuver.Position.x, maneuver.Position.y);
|
||
float radians = maneuver.Heading * Mathf.Deg2Rad;
|
||
Vector2 bow = new(Mathf.Cos(radians), Mathf.Sin(radians));
|
||
Vector2 wing = new(-bow.y, bow.x);
|
||
painter.fillColor = DockMarkColor;
|
||
painter.strokeColor = new Color(.18f, .24f, .24f);
|
||
painter.lineWidth = 1.25f;
|
||
painter.BeginPath();
|
||
painter.MoveTo(ship + bow * 9);
|
||
painter.LineTo(ship - bow * 6 + wing * 4);
|
||
painter.LineTo(ship - bow * 6 - wing * 4);
|
||
painter.ClosePath();
|
||
painter.Fill();
|
||
painter.Stroke();
|
||
}
|
||
|
||
private static void DockMapMark(Painter2D painter, Vector2 point, Color color)
|
||
{
|
||
painter.fillColor = color;
|
||
painter.BeginPath();
|
||
painter.MoveTo(point + Vector2.up * 3);
|
||
painter.LineTo(point + Vector2.right * 3);
|
||
painter.LineTo(point + Vector2.down * 3);
|
||
painter.LineTo(point + Vector2.left * 3);
|
||
painter.ClosePath();
|
||
painter.Fill();
|
||
}
|
||
}
|
||
}
|