Scale docking momentum by hull, require manual mooring and channel maneuvers in Captain Career, and add rank-specific bridge instruments and voiced training.
340 lines
16 KiB
C#
340 lines
16 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace BigShipTycoon
|
|
{
|
|
internal enum DockInstrumentKind { Compass, Radar, Conning, Machinery, Approach, Repeater }
|
|
|
|
internal sealed class DockInstrumentRaster
|
|
{
|
|
internal const int Width = 384, Height = 192;
|
|
private static readonly Color32 Ink = new(5, 15, 20, 255);
|
|
private static readonly Color32 Subpanel = new(9, 28, 34, 255);
|
|
private static readonly Color32 Grid = new(33, 65, 68, 255);
|
|
private static readonly Color32 Muted = new(127, 176, 181, 255);
|
|
private static readonly Color32 Cyan = new(112, 224, 235, 255);
|
|
private static readonly Color32 Paper = new(235, 249, 243, 255);
|
|
private static readonly Color32 Green = new(111, 225, 160, 255);
|
|
private static readonly Color32 Gold = new(255, 200, 114, 255);
|
|
private static readonly Color32 Rose = new(247, 133, 115, 255);
|
|
|
|
internal Color32[] Pixels { get; } = new Color32[Width * Height];
|
|
|
|
internal void Render(DockManeuver dock, DockInstrumentKind kind, float range, bool northUp, float sweep)
|
|
{
|
|
Array.Fill(Pixels, Ink);
|
|
if (dock == null)
|
|
return;
|
|
|
|
switch (kind)
|
|
{
|
|
case DockInstrumentKind.Radar: Radar(dock, Mathf.Max(20, range), northUp, sweep); break;
|
|
case DockInstrumentKind.Conning: Conning(dock); break;
|
|
case DockInstrumentKind.Machinery: Machinery(dock); break;
|
|
case DockInstrumentKind.Approach: Approach(dock); break;
|
|
case DockInstrumentKind.Repeater: Repeater(dock); break;
|
|
default: Compass(dock); break;
|
|
}
|
|
}
|
|
|
|
private void Compass(DockManeuver dock)
|
|
{
|
|
Header("COMPASS", dock);
|
|
Text("HEADING", 18, 141, 2, Muted);
|
|
Text(Heading(dock.Heading), 17, 99, 5, Paper);
|
|
Text("DEG", 115, 100, 2, Cyan);
|
|
HeadingTape(dock.Heading, 163, 111, 200);
|
|
Card(13, 14, 162, 64, "SPEED KN", Signed(dock.SpeedKnots), Cyan, 4);
|
|
Card(190, 14, 181, 64, "RUDDER", Direction(dock.Rudder), Gold, 3);
|
|
}
|
|
|
|
private void Conning(DockManeuver dock)
|
|
{
|
|
Header("CONNING", dock);
|
|
Card(13, 87, 109, 67, "HDG DEG", Heading(dock.Heading), Paper, 4);
|
|
Card(137, 87, 109, 67, "SPEED KN", Signed(dock.SpeedKnots), Cyan, 4);
|
|
Card(261, 87, 110, 67, "SOG KN", $"{dock.SpeedOverGroundKnots:0.0}", Cyan, 4);
|
|
Text("RUDDER", 14, 57, 2, Muted);
|
|
Text(Direction(dock.Rudder), 15, 34, 2, Gold);
|
|
Gauge(132, 45, 231, dock.Rudder / 35f, Gold);
|
|
}
|
|
|
|
private void Machinery(DockManeuver dock)
|
|
{
|
|
Header("MACHINERY", dock);
|
|
Card(13, 88, 358, 66, "ENGINE ORDER", Order(dock.Order), Paper, 3);
|
|
if (dock.HasBowThruster)
|
|
{
|
|
Text("BOW THRUST", 14, 63, 2, Muted);
|
|
Text(Mathf.Abs(dock.BowThruster) < .005f ? "IDLE"
|
|
: $"{Mathf.RoundToInt(Mathf.Abs(dock.BowThruster) * 100)}% {(dock.BowThruster < 0 ? "P" : "S")}",
|
|
14, 40, 2, Cyan);
|
|
Gauge(165, 52, 199, dock.BowThruster, Cyan);
|
|
Text($"DRIFT {Signed(dock.LateralSpeedKnots)} KN", 14, 14, 2, Green);
|
|
}
|
|
else
|
|
{
|
|
Text("RUDDER", 14, 57, 2, Muted);
|
|
Text(Direction(dock.Rudder), 14, 34, 2, Gold);
|
|
Gauge(148, 46, 216, dock.Rudder / 35f, Gold);
|
|
}
|
|
}
|
|
|
|
private void Approach(DockManeuver dock)
|
|
{
|
|
Header("APPROACH", dock);
|
|
Card(13, 95, 157, 57, "TURN DEG/S", Signed(dock.TurnRate), Gold, 4);
|
|
Card(189, 95, 182, 57, "DISPLACEMENT T", $"{dock.DisplacementTonnes:0}", Paper, 3);
|
|
Vector2 target = dock.Arriving ? dock.Berth : new Vector2(dock.ChannelEnd, 0);
|
|
Text(dock.Arriving ? "BERTH OFFSETS" : "CHANNEL OFFSETS", 14, 66, 2, Muted);
|
|
Text($"ALONG {Signed(target.x - dock.Position.x)} M", 14, 43, 2, Cyan);
|
|
Text($"CROSS {Signed(target.y - dock.Position.y)} M", 14, 19, 2, Cyan);
|
|
Text("COAST EST", 232, 46, 2, Muted);
|
|
Text($"{dock.NeutralCoastMetres:0} M", 232, 16, 3, Green);
|
|
}
|
|
|
|
private void Repeater(DockManeuver dock)
|
|
{
|
|
Header("HELM REPEATER", dock);
|
|
Card(13, 85, 160, 69, "HEADING DEG", Heading(dock.Heading), Paper, 5);
|
|
Card(188, 85, 183, 69, "SPEED KN", Signed(dock.SpeedKnots), Cyan, 5);
|
|
Text(Order(dock.Order), 14, 47, 3, Muted);
|
|
Text($"RUDDER {Direction(dock.Rudder)}", 14, 16, 2, Gold);
|
|
}
|
|
|
|
private void Radar(DockManeuver dock, float range, bool northUp, float sweep)
|
|
{
|
|
Header("HARBOR RADAR", dock);
|
|
Vector2 center = new(107, 80);
|
|
const int radius = 69;
|
|
float heading = dock.Heading * Mathf.Deg2Rad;
|
|
float turn = northUp ? 0 : heading;
|
|
float cosine = Mathf.Cos(turn), sine = Mathf.Sin(turn);
|
|
float scale = radius / range;
|
|
|
|
Zone(15, 7, 182, 148, Subpanel);
|
|
Circle(107, 80, 23, Grid);
|
|
Circle(107, 80, 46, Grid);
|
|
Circle(107, 80, radius, Muted);
|
|
Line(38, 80, 176, 80, Grid);
|
|
Line(107, 11, 107, 149, Grid);
|
|
|
|
float sweepAngle = Mathf.Repeat(sweep, 1f) * Mathf.PI * 2f;
|
|
for (int ray = 20; ray >= 0; ray--)
|
|
{
|
|
float angle = sweepAngle - ray * .012f;
|
|
Vector2 end = center + radius * new Vector2(Mathf.Sin(angle), Mathf.Cos(angle));
|
|
Color32 tint = ray == 0 ? Green : new Color32(22, (byte)(89 - ray * 2.5f), 57, 255);
|
|
Line(107, 80, Mathf.RoundToInt(end.x), Mathf.RoundToInt(end.y), tint);
|
|
}
|
|
|
|
Vector2 Project(float along, float across)
|
|
{
|
|
Vector2 relative = new Vector2(along, across) - dock.Position;
|
|
return new Vector2(relative.y * cosine - relative.x * sine, relative.x * cosine + relative.y * sine) * scale;
|
|
}
|
|
|
|
void Trace(float ax, float ay, float bx, float by, Color32 color)
|
|
{
|
|
Vector2 a = Project(ax, ay), b = Project(bx, by);
|
|
int samples = Mathf.Max(1, Mathf.CeilToInt(Vector2.Distance(a, b) * 1.48f));
|
|
for (int sample = 0; sample <= samples; sample++)
|
|
{
|
|
Vector2 point = Vector2.LerpUnclamped(a, b, (float)sample / samples);
|
|
if (point.sqrMagnitude > radius * radius)
|
|
continue;
|
|
point += center;
|
|
Dot(Mathf.RoundToInt(point.x), Mathf.RoundToInt(point.y), color);
|
|
}
|
|
}
|
|
|
|
float pierMin = dock.Berth.x - dock.PierHalfLength;
|
|
float pierMax = dock.Berth.x + dock.PierHalfLength;
|
|
float farPierY = dock.PierY + 12;
|
|
Trace(pierMin, dock.PierY, pierMax, dock.PierY, Gold);
|
|
Trace(pierMin, farPierY, pierMax, farPierY, Muted);
|
|
Trace(pierMin, dock.PierY, pierMin, farPierY, Muted);
|
|
Trace(pierMax, dock.PierY, pierMax, farPierY, Muted);
|
|
Trace(dock.ChannelEnd, -dock.ChannelHalfWidth, dock.ChannelEnd, dock.ChannelHalfWidth, Grid);
|
|
float length = (dock.ChannelEnd - 65f) / .42f;
|
|
float signal = Mathf.Clamp(length * .024f, .84f, 2.25f);
|
|
for (int mark = 0; mark < 4; mark++)
|
|
{
|
|
float gate = dock.ChannelEnd * Mathf.Lerp(.26f, 1, mark / 3f);
|
|
float outer = dock.ChannelHalfWidth + signal * .46f;
|
|
float pierSide = gate < dock.PierHalfLength ? Mathf.Min(outer, dock.PierY - signal * .74f) : outer;
|
|
RadarEcho(Project(gate, pierSide), center, radius, Green);
|
|
RadarEcho(Project(gate, -outer), center, radius, Rose);
|
|
}
|
|
|
|
float ownAngle = heading - turn;
|
|
Vector2 bow = new(Mathf.Sin(ownAngle), Mathf.Cos(ownAngle));
|
|
Vector2 side = new(bow.y, -bow.x);
|
|
Vector2 tip = center + bow * 8f;
|
|
Vector2 left = center - bow * 6f - side * 4f;
|
|
Vector2 right = center - bow * 6f + side * 4f;
|
|
Line(tip, left, Paper);
|
|
Line(tip, right, Paper);
|
|
Line(left, right, Paper);
|
|
|
|
Vector2 north = center + (radius - 7) * new Vector2(-sine, cosine);
|
|
Text("N", Mathf.RoundToInt(north.x) - 2, Mathf.RoundToInt(north.y) - 3, 1, Paper);
|
|
Text(northUp ? "NORTH UP" : "HEAD UP", 218, 135, 2, Green);
|
|
Text("RANGE", 218, 105, 2, Muted);
|
|
Text($"{range:0} M", 218, 80, 3, Paper);
|
|
Text($"HDG {Heading(dock.Heading)}", 218, 52, 2, Cyan);
|
|
Text($"SOG {dock.SpeedOverGroundKnots:0.0} KN", 218, 25, 2, Cyan);
|
|
}
|
|
|
|
private void RadarEcho(Vector2 projected, Vector2 center, int radius, Color32 color)
|
|
{
|
|
if (projected.sqrMagnitude >= (radius - 4) * (radius - 4))
|
|
return;
|
|
Vector2 at = projected + center;
|
|
int x = Mathf.RoundToInt(at.x), y = Mathf.RoundToInt(at.y);
|
|
Zone(x - 1, y - 1, 3, 3, color);
|
|
}
|
|
|
|
private void Header(string title, DockManeuver dock)
|
|
{
|
|
Zone(0, 166, Width, 26, Subpanel);
|
|
Zone(0, 165, Width, 1, Grid);
|
|
Text(title, 14, 172, 2, Cyan);
|
|
string status = dock.Secured ? "SECURED" : dock.LinesAttached ? "LINES ON"
|
|
: dock.AtChannelEdge ? "CHANNEL EDGE" : dock.ReadyForAutopilot ? "CLEAR" : "IN CHANNEL";
|
|
Text(status, Width - 12 - TextWidth(status, 1), 176, 1, dock.AtChannelEdge ? Gold : Green);
|
|
}
|
|
|
|
private void Card(int x, int y, int width, int height, string name, string value, Color32 color, int scale)
|
|
{
|
|
Zone(x, y, width, height, Subpanel);
|
|
Zone(x, y, width, 1, Grid);
|
|
Text(name, x + 9, y + height - 19, 2, Muted);
|
|
Text(value, x + 9, y + 8, scale, color);
|
|
}
|
|
|
|
private void HeadingTape(float heading, int x, int y, int width)
|
|
{
|
|
Zone(x, y, width, 32, Subpanel);
|
|
Line(x, y + 6, x + width, y + 6, Grid);
|
|
int near = Mathf.RoundToInt(heading / 10f) * 10;
|
|
for (int tick = near - 30; tick <= near + 30; tick += 10)
|
|
{
|
|
int at = x + width / 2 + Mathf.RoundToInt((tick - heading) * 3);
|
|
if (at < x + 12 || at > x + width - 12)
|
|
continue;
|
|
Line(at, y + 6, at, y + 13, Muted);
|
|
Text(Heading(tick), at - 8, y + 18, 1, Muted);
|
|
}
|
|
int center = x + width / 2;
|
|
Line(center, y + 3, center, y + 14, Gold);
|
|
}
|
|
|
|
private void Gauge(int x, int y, int width, float input, Color32 color)
|
|
{
|
|
int center = x + width / 2;
|
|
int tip = center + Mathf.RoundToInt(Mathf.Clamp(input, -1, 1) * width * .5f);
|
|
Zone(x, y - 3, width, 7, Grid);
|
|
Zone(Mathf.Min(tip, center), y - 4, Mathf.Abs(tip - center), 9, color);
|
|
Line(center, y - 7, center, y + 7, Paper);
|
|
Text("P", x, y - 25, 2, Muted);
|
|
Text("S", x + width - 10, y - 25, 2, Muted);
|
|
}
|
|
|
|
private static string Direction(float angle) => Mathf.Abs(angle) < .5f
|
|
? "CENTER" : $"{Mathf.Abs(angle):0} DEG {(angle < 0 ? "P" : "S")}";
|
|
private static string Heading(float degrees) => $"{Mathf.RoundToInt(Mathf.Repeat(degrees, 360)) % 360:000}";
|
|
private static string Signed(float value) => Mathf.Abs(value) < .05f ? "0.0" : $"{value:+0.0;-0.0;0.0}";
|
|
private static string Order(DockOrder order) => order switch
|
|
{
|
|
DockOrder.HalfAstern => "HALF ASTERN",
|
|
DockOrder.DeadSlowAstern => "DEAD SLOW ASTERN",
|
|
DockOrder.DeadSlowAhead => "DEAD SLOW AHEAD",
|
|
DockOrder.HalfAhead => "HALF AHEAD",
|
|
_ => "STOP"
|
|
};
|
|
|
|
private void Circle(int x, int y, int radius, Color32 color)
|
|
{
|
|
int previousX = x, previousY = y + radius;
|
|
for (int point = 1; point <= 90; point++)
|
|
{
|
|
float radians = point * Mathf.PI * 2f / 90f;
|
|
int nextX = x + Mathf.RoundToInt(radius * Mathf.Sin(radians));
|
|
int nextY = y + Mathf.RoundToInt(radius * Mathf.Cos(radians));
|
|
Line(previousX, previousY, nextX, nextY, color);
|
|
(previousX, previousY) = (nextX, nextY);
|
|
}
|
|
}
|
|
|
|
private void Line(Vector2 a, Vector2 b, Color32 color) =>
|
|
Line(Mathf.RoundToInt(a.x), Mathf.RoundToInt(a.y), Mathf.RoundToInt(b.x), Mathf.RoundToInt(b.y), color);
|
|
|
|
private void Line(int x, int y, int toX, int toY, Color32 color)
|
|
{
|
|
int dx = Mathf.Abs(toX - x), dy = -Mathf.Abs(toY - y);
|
|
int sx = x < toX ? 1 : -1, sy = y < toY ? 1 : -1;
|
|
int error = dx + dy;
|
|
while (true)
|
|
{
|
|
Dot(x, y, color);
|
|
if (x == toX && y == toY)
|
|
return;
|
|
int twice = error * 2;
|
|
if (twice >= dy) { error += dy; x += sx; }
|
|
if (twice <= dx) { error += dx; y += sy; }
|
|
}
|
|
}
|
|
|
|
private void Dot(int x, int y, Color32 color)
|
|
{
|
|
if ((uint)x < Width && (uint)y < Height)
|
|
Pixels[y * Width + x] = color;
|
|
}
|
|
|
|
private void Zone(int x, int y, int width, int height, Color32 color)
|
|
{
|
|
int left = Mathf.Max(0, x), right = Mathf.Min(Width, x + width);
|
|
int bottom = Mathf.Max(0, y), top = Mathf.Min(Height, y + height);
|
|
for (int row = bottom; row < top; row++)
|
|
for (int column = left; column < right; column++)
|
|
Pixels[row * Width + column] = color;
|
|
}
|
|
|
|
private static int TextWidth(string value, int scale) => value.Length * 6 * scale - scale;
|
|
|
|
private void Text(string value, int x, int y, int scale, Color32 color)
|
|
{
|
|
foreach (char letter in value)
|
|
{
|
|
int glyph = Alphabet.IndexOf(letter);
|
|
if (glyph >= 0)
|
|
for (int row = 0; row < 7; row++)
|
|
for (int column = 0; column < 5; column++)
|
|
if ((Letters[glyph, row] & 1 << (4 - column)) != 0)
|
|
Zone(x + column * scale, y + (6 - row) * scale, scale, scale, color);
|
|
x += 6 * scale;
|
|
}
|
|
}
|
|
|
|
private const string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-./:%";
|
|
private static readonly byte[,] Letters =
|
|
{
|
|
{14,17,17,31,17,17,17}, {30,17,17,30,17,17,30}, {15,16,16,16,16,16,15},
|
|
{30,17,17,17,17,17,30}, {31,16,16,30,16,16,31}, {31,16,16,30,16,16,16},
|
|
{15,16,16,23,17,17,15}, {17,17,17,31,17,17,17}, {31,4,4,4,4,4,31},
|
|
{7,2,2,2,18,18,12}, {17,18,20,24,20,18,17}, {16,16,16,16,16,16,31},
|
|
{17,27,21,21,17,17,17}, {17,25,21,19,17,17,17}, {14,17,17,17,17,17,14},
|
|
{30,17,17,30,16,16,16}, {14,17,17,17,21,18,13}, {30,17,17,30,20,18,17},
|
|
{15,16,16,14,1,1,30}, {31,4,4,4,4,4,4}, {17,17,17,17,17,17,14},
|
|
{17,17,17,17,17,10,4}, {17,17,17,21,21,27,17}, {17,17,10,4,10,17,17},
|
|
{17,17,10,4,4,4,4}, {31,1,2,4,8,16,31},
|
|
{14,17,19,21,25,17,14}, {4,12,4,4,4,4,14}, {14,17,1,2,4,8,31},
|
|
{30,1,1,14,1,1,30}, {2,6,10,18,31,2,2}, {31,16,16,30,1,1,30},
|
|
{14,16,16,30,17,17,14}, {31,1,2,4,8,8,8}, {14,17,17,14,17,17,14},
|
|
{14,17,17,15,1,1,14}, {0,4,4,31,4,4,0}, {0,0,0,31,0,0,0},
|
|
{0,0,0,0,0,6,6}, {1,2,2,4,8,8,16}, {0,6,6,0,6,6,0}, {25,26,2,4,8,11,19}
|
|
};
|
|
}
|
|
}
|