Files
cruiseSimulator/Assets/BigShipTycoon/Runtime/HarborRiver.cs
josh 290e4cb45d Add riverboat captain career and distinct ship classes
Add class-specific vessel geometry and sea motion, a guided helm qualification path, dockside sailing orders, and local excursions at all 24 ports.
2026-11-29 20:41:00 -05:00

204 lines
10 KiB
C#

using System.Collections.Generic;
using UnityEngine;
namespace BigShipTycoon
{
public sealed partial class Harbor
{
private const float RiverReachLength = 240f;
private static readonly float[] RiverTerrainDistances = { 2.2f, 13f, 29f, 60f, 110f, 220f, 460f, 930f };
private GameObject riverBanks;
private readonly Transform[] riverReaches = new Transform[5];
private readonly List<Mesh> riverMeshes = new();
private readonly List<Material> riverMaterials = new();
private int riverReach = int.MinValue;
internal bool RiverBanksVisible => riverBanks != null && riverBanks.activeSelf;
private void PresentRiverBanks(Vector2 shipPosition)
{
if (!sailing || displayedShip?.HullIndex != Market.River || PortCinematicActive)
{
HideRiverBanks();
return;
}
if (riverBanks == null)
BuildRiverBanks();
int reach = Mathf.FloorToInt(shipPosition.x / RiverReachLength);
if (reach != riverReach)
{
riverReach = reach;
for (int index = 0; index < riverReaches.Length; index++)
riverReaches[index].localPosition = Vector3.right * ((reach + index - 2) * RiverReachLength);
}
riverBanks.SetActive(true);
}
private void HideRiverBanks()
{
if (riverBanks != null && riverBanks.activeSelf)
riverBanks.SetActive(false);
}
private void ReleaseRiverBanks()
{
foreach (Mesh mesh in riverMeshes)
Destroy(mesh);
foreach (Material material in riverMaterials)
Destroy(material);
if (riverBanks != null)
Destroy(riverBanks);
}
private void BuildRiverBanks()
{
riverBanks = new GameObject("River channel and banks");
riverBanks.transform.SetParent(transform, false);
for (int i = 0; i < riverReaches.Length; i++)
{
riverReaches[i] = new GameObject("River reach").transform;
riverReaches[i].SetParent(riverBanks.transform, false);
}
var grass = new HarborMesh();
var bank = new HarborMesh();
var foliage = new HarborMesh();
var canopyTips = new HarborMesh();
var undergrowth = new HarborMesh();
var timber = new HarborMesh();
var building = new HarborMesh();
var green = new HarborMesh();
var red = new HarborMesh();
for (int side = -1; side <= 1; side += 2)
{
const int divisions = 28;
for (int i = 0; i < divisions; i++)
{
float x = i * RiverReachLength / divisions, next = (i + 1) * RiverReachLength / divisions;
float edge = BankWidth(x, side), nextEdge = BankWidth(next, side);
Surface(bank, new(x, -.37f, edge * side), new(next, -.37f, nextEdge * side),
new(next, 1.18f, (nextEdge + 2.2f) * side), new(x, 1.18f, (edge + 2.2f) * side), side);
for (int strip = 1; strip < RiverTerrainDistances.Length; strip++)
{
float near = RiverTerrainDistances[strip - 1], far = RiverTerrainDistances[strip];
Surface(grass, RiverGround(x, edge, near, side), RiverGround(next, nextEdge, near, side),
RiverGround(next, nextEdge, far, side), RiverGround(x, edge, far, side), side);
}
float shrub = .62f + .78f * Variation(i * 19 + side * 7);
float brushX = x + 1.2f + Variation(i * 7 - side) * 4.4f;
undergrowth.Ellipsoid(new Vector3(brushX, 1.46f, (edge + 2.9f) * side), new Vector3(1.9f, .52f, .82f) * shrub, 5, 9);
undergrowth.Ellipsoid(new Vector3(brushX + 1.16f, 1.56f, (edge + 3.4f) * side), new Vector3(1.23f, .83f, 1.03f) * shrub, 5, 8);
for (int reed = 0; reed < 4; reed++)
{
float jitter = Variation(i * 51 + reed * 11 + side * 6);
Vector3 root = new(brushX + reed * .44f, .45f, (edge + .98f + jitter * .34f) * side);
Vector3 leaf = new(.16f + jitter * .13f, .91f + jitter * .52f, side * -.32f);
canopyTips.Quad(root - Vector3.right * .08f, root + Vector3.right * .10f,
root + leaf + Vector3.right * .022f, root + leaf - Vector3.right * .036f);
}
}
for (int i = 0; i < 25; i++)
{
float x = i * 9.3f + 2.1f * Variation(35 + i * 21 + side * 12);
float edge = BankWidth(x, side);
for (int line = 0; line < 2; line++)
{
float offset = 6.1f + 14.5f * line + 8.8f * Variation(i * 14 + line + side * 3);
Vector3 foot = RiverGround(x, edge, offset, side);
float height = 1.9f + 3.9f * Variation(i * 12 + line * 17 + side);
float canopy = 1.3f + 1.53f * Variation(i * 18 + line * 3);
Vector3 fork = foot + new Vector3(.32f * side, height * .77f, .27f);
Vector3 crown = foot + new Vector3(.45f * side, height + .24f, .07f);
timber.Rod(foot, fork, .122f + canopy * .022f, 6, true);
timber.Rod(fork, crown, .087f, 5);
timber.Rod(fork, crown + new Vector3(-.61f, -.38f, .29f) * canopy, .061f, 5);
foliage.Ellipsoid(crown, new Vector3(1.12f, .73f, .88f) * canopy, 6, 9);
foliage.Ellipsoid(crown + new Vector3(-.72f, -.12f, .37f) * canopy, new Vector3(.71f, .51f, .64f) * canopy, 5, 9);
canopyTips.Ellipsoid(crown + new Vector3(.48f, .27f, -.43f) * canopy, new Vector3(.77f, .52f, .65f) * canopy, 5, 9);
}
}
for (int i = 0; i < 3; i++)
{
float x = 31f + i * 80f + side * 9f;
Vector3 pylon = new(x, -.08f, side * 24);
HarborMesh signal = side < 0 ? green : red;
timber.Rod(pylon, pylon + Vector3.up * 2.7f, .105f, 7, true);
signal.Box(pylon + Vector3.up * 2.19f, new Vector3(.54f, .68f, .38f));
signal.Radome(pylon + Vector3.up * 2.65f, .15f);
}
float dockX = side < 0 ? 69f : 154f;
float dockEdge = BankWidth(dockX, side);
var wharf = new Vector3(dockX, .67f, (dockEdge - 3f) * side);
timber.Box(wharf, new Vector3(17, .32f, 8.4f));
for (int i = -2; i <= 2; i++)
{
Vector3 pile = wharf + new Vector3(i * 3.4f, -.8f, -side * 3.1f);
timber.Rod(pile, pile + Vector3.up * 1.5f, .15f, 7, true);
building.Box(pile + Vector3.up * 1.5f, new Vector3(.31f, .075f, .31f));
}
var shed = new Vector3(dockX + 2.2f, 2.22f, (dockEdge + 8.1f) * side);
building.Box(shed, new Vector3(10.8f, 2.48f, 5.5f));
timber.Box(shed + Vector3.up * 1.25f, new Vector3(11.25f, .19f, 6.0f));
for (int i = -2; i <= 2; i++)
green.Box(shed + new Vector3(i * 1.8f, .35f, -side * 2.77f), new Vector3(.96f, .81f, .048f));
}
RiverBatch(grass, "Wetland grass", new Color(.30f, .39f, .22f), 8);
RiverBatch(bank, "Exposed banks", new Color(.33f, .29f, .22f), 1);
RiverBatch(foliage, "Canopy", new Color(.19f, .35f, .23f), 4);
RiverBatch(canopyTips, "Young leaves and reeds", new Color(.31f, .44f, .219f), 4);
RiverBatch(undergrowth, "Reeds and shrubs", new Color(.38f, .49f, .28f), 4);
RiverBatch(timber, "Timber docks and trees", new Color(.47f, .35f, .22f), 5);
RiverBatch(building, "River boathouses", new Color(.78f, .76f, .59f), 2);
RiverBatch(green, "Green marks and shutters", new Color(.20f, .57f, .37f), 0);
RiverBatch(red, "Red channel markers", new Color(.77f, .22f, .19f), 0);
}
private void RiverBatch(HarborMesh batch, string name, Color tone, int detail)
{
var mesh = batch.Bake();
var material = new Material(Resources.Load<Shader>("PortScenerySurface")) { name = name, color = tone };
material.SetFloat("_Detail", detail);
riverMeshes.Add(mesh);
riverMaterials.Add(material);
foreach (Transform reach in riverReaches)
{
var model = new GameObject(name, typeof(MeshFilter), typeof(MeshRenderer)) { layer = 8 };
model.transform.SetParent(reach, false);
model.GetComponent<MeshFilter>().sharedMesh = mesh;
model.GetComponent<MeshRenderer>().sharedMaterial = material;
}
}
private static void Surface(HarborMesh mesh, Vector3 a, Vector3 b, Vector3 c, Vector3 d, int side)
{
if (side < 0)
mesh.Quad(a, b, c, d);
else
mesh.Quad(d, c, b, a);
}
private static float BankWidth(float x, int side)
{
float cycle = x * (2 * Mathf.PI / RiverReachLength);
return 40f + 4.8f * Mathf.Sin(cycle + side * .52f) + 2.2f * Mathf.Cos(cycle * 2 + side * .91f);
}
private static Vector3 RiverGround(float x, float width, float inland, int side)
{
float phase = x * (2 * Mathf.PI / RiverReachLength);
float inlandRise = Mathf.Clamp01((inland - 2.2f) / 23f);
float height = 1.18f + inlandRise * (1.25f + .70f * Mathf.Sin(phase * 2 + inland * .061f + side)
+ .44f * Mathf.Cos(phase * 5 - inland * .11f));
return new Vector3(x, height, (width + inland) * side);
}
private static float Variation(int seed) => Mathf.Repeat(Mathf.Sin(seed * 37.17f) * 1627.31f, 1f);
}
}