using System.Collections.Generic; using UnityEngine; namespace BigShipTycoon { public sealed partial class Harbor { internal const float PortCinematicDuration = 4.2f; private const float RegionalQuayLength = 174f, QuayWidth = 12f; private readonly List quayMeshes = new(); private readonly List quayFinishes = new(); private GameObject portQuay; private int cinematicPort = -1; private bool portArrival; private float portFrame, quayLength; private Vector3 resumeVesselPosition, resumeWaterPosition; private Quaternion resumeVesselRotation; private Vector4 resumeHull, resumeCourse; internal bool PortCinematicActive => cinematicPort >= 0; private bool MegaPortBerth => displayedShip?.HullIndex == Market.Mega; internal (Vector2 Near, Vector2 Far, Vector4 Matte) PortCinematicAlignment() { Vector3 edge = new(0, 1.4f, -QuayWidth * .5f); Vector3 near = portQuay.transform.TransformPoint(edge - Vector3.right * quayLength * .45f); Vector3 far = portQuay.transform.TransformPoint(edge + Vector3.right * quayLength * .45f); return (viewCamera.WorldToViewportPoint(near), viewCamera.WorldToViewportPoint(far), water.GetVector("_PortMatteCrop")); } internal bool BeginPortCinematic(int port, bool arriving) { if (PortCinematicActive || vessel == null || port < 0 || port >= Market.Ports.Count) return false; ShowDeck(-1); HideRiverBanks(); cinematicPort = port; portArrival = arriving; portFrame = 0; resumeVesselPosition = vessel.localPosition; resumeVesselRotation = vessel.localRotation; resumeWaterPosition = waterSurface.localPosition; resumeHull = water.GetVector("_HullSpan"); resumeCourse = water.GetVector("_HullCourse"); float berthLength = MegaPortBerth ? vesselLength + 44f : displayedShip?.HullIndex == Market.River ? 44f : RegionalQuayLength; if (portQuay == null || !Mathf.Approximately(quayLength, berthLength)) { ReleaseCinematicQuay(); quayLength = berthLength; portQuay = BuildPortQuay(); } float berthCenter = vesselLength * (MegaPortBerth ? .016f : .27f); portQuay.transform.localPosition = new Vector3(berthCenter, 0, vesselBeam + 2.05f + QuayWidth * .5f); Vector3 quay = portQuay.transform.position; water.SetVector("_PortBerth", new Vector4(quay.x, quay.z, quayLength * .5f, QuayWidth * .5f)); portQuay.SetActive(true); ShowPortScenery(port); water.SetFloat("_Underway", 0); water.SetFloat("_PortMotion", 1); water.SetFloat("_WeatherSeverity", 0); Shader.SetGlobalFloat("_TycoonSeaSailing", 0); waterSurface.localPosition = Vector3.zero; wake.Reset(); // Start with the wake the arriving ship already laid beyond the mouth of the berth. if (portArrival) for (int i = 7; i > 0; i--) UpdatePortWake(-i * .047f); PresentPortCinematic(0); return true; } internal void PresentPortCinematic(float elapsed) { if (!PortCinematicActive) return; portFrame = Mathf.Clamp01(elapsed / PortCinematicDuration); var (center, heading, knots) = PortMotion(portFrame); float heave = Mathf.Sin(elapsed * 1.87f + .7f) * .052f; float roll = Mathf.Sin(elapsed * 1.24f + 1.1f) * (displayedShip?.HullIndex == Market.River ? .65f : .29f); vessel.SetLocalPositionAndRotation(new Vector3(center.x, heave, center.y), Quaternion.AngleAxis(heading, Vector3.up) * Quaternion.AngleAxis(roll, Vector3.right)); water.SetVector("_HullSpan", new Vector4(vesselLength, vesselBeam, center.x, center.y)); Vector2 course = PortBow(heading); water.SetVector("_HullCourse", new Vector4(course.x, -course.y, Mathf.Clamp01(knots / 19f), 0)); float wakeTime = 3f + portFrame * PortCinematicDuration; water.SetFloat("_SeaTime", wakeTime); wake.Update(center, course, knots, vesselLength, vesselBeam, wakeTime, 0); ComposePortCinematic(); } internal void EndPortCinematic() { if (!PortCinematicActive) return; cinematicPort = -1; portQuay.SetActive(false); portScenery?.SetVisible(false); portMatteAspect = 0; wake.Reset(); water.SetFloat("_PortMotion", 0); water.SetFloat("_PortMatteActive", 0); water.SetFloat("_Underway", sailing ? 1 : 0); water.SetFloat("_WeatherSeverity", sailing ? seaSeverity : 0); water.SetFloat("_SeaTime", seaTime); Shader.SetGlobalFloat("_TycoonSeaSailing", sailing ? 1 : 0); if (vessel != null) vessel.SetLocalPositionAndRotation(resumeVesselPosition, resumeVesselRotation); waterSurface.localPosition = resumeWaterPosition; water.SetVector("_HullSpan", resumeHull); water.SetVector("_HullCourse", resumeCourse); SetHarbor(Market.Ports[displayedShip?.Port ?? 0].Region); ResetOrbit(true); } private (Vector2 Center, float Heading, float Knots) PortMotion(float t) { float run = PortRun; // The stern of the longer hull sweeps farther out from the pivot during a turn. float turn = MegaPortBerth ? .68f : 1f; if (portArrival) { float slowing = t * (2f - t); float approach = Mathf.SmoothStep(0, 1, t); return (new Vector2((slowing - .88f) * run, -(1f - approach) * (8.5f + vesselBeam * .31f)), -12f * turn * (1f - approach), Mathf.Lerp(11.8f, .35f, Mathf.Clamp01(t))); } float leaving = t * (.21f + t * .79f); float castOff = Mathf.SmoothStep(0, 1, t); return (new Vector2((leaving - .35f) * run, -castOff * (9f + vesselBeam * .31f)), castOff * 10.5f * turn, Mathf.Lerp(1.4f, 12.6f, t)); } private float PortRun => MegaPortBerth ? vesselLength * .18f : Mathf.Min(35f, vesselLength * .38f); private static Vector2 PortBow(float heading) { float radians = heading * Mathf.Deg2Rad; return new Vector2(Mathf.Cos(radians), -Mathf.Sin(radians)); } private void UpdatePortWake(float t) { var (center, heading, knots) = PortMotion(t); wake.Update(center, PortBow(heading), knots, vesselLength, vesselBeam, 3f + t * PortCinematicDuration, 0); } private void ComposePortCinematic() { if (portMatteAspect > 0) { // Painted waterfronts were framed broadside. A fixed lens keeps the painted shore and 3D berth aligned. var fixedFacing = Quaternion.Euler(portArrival ? 10.4f : 9.9f, 0, 0); var fixedFocus = new Vector3(PortRun * (portArrival ? -.36f : .10f), vesselTop * .32f, -.85f); SetPortCamera(fixedFocus, fixedFacing, vesselLength * 1.43f, 39f); return; } // Keep the ship ahead of the lens and the quay in frame as the camera tracks along the water. float glide = Mathf.SmoothStep(0, 1, portFrame); float angle = portArrival ? Mathf.Lerp(-28f, 31f, glide) : Mathf.Lerp(32f, -25f, glide); float elevation = portArrival ? Mathf.Lerp(10.8f, 8.9f, glide) : Mathf.Lerp(8.0f, 12.2f, glide); float distance = vesselLength * (portArrival ? Mathf.Lerp(1.49f, 1.25f, glide) : Mathf.Lerp(1.23f, 1.44f, glide)); Quaternion facing = Quaternion.Euler(elevation, -angle, 0); Vector3 focus = vessel.position; focus.x = focus.x * .64f + vesselLength * (portArrival ? .021f : .048f); focus.y = vesselTop * .32f; focus.z -= .85f; SetPortCamera(focus, facing, distance, Mathf.Lerp(37.5f, 40.5f, portArrival ? 1f - glide : glide)); } private void SetPortCamera(Vector3 focus, Quaternion facing, float distance, float fieldOfView) { distance *= Mathf.Max(1, 1.63f / ViewAspect); viewCamera.orthographic = false; viewCamera.nearClipPlane = OrbitNearClip; viewCamera.fieldOfView = fieldOfView; viewCamera.transform.SetPositionAndRotation(focus + facing * Vector3.back * distance, facing); FitViewport(); } private GameObject BuildPortQuay() { var quay = new GameObject("Cinematic berth"); quay.transform.SetParent(transform, false); var concrete = new HarborMesh(); var caps = new HarborMesh(); var fenders = new HarborMesh(); var steel = new HarborMesh(); var paint = new HarborMesh(); var lantern = new HarborMesh(); float edge = -QuayWidth * .5f; concrete.Box(new Vector3(0, .5f, 0), new Vector3(quayLength, 1.32f, QuayWidth)); caps.Box(new Vector3(0, 1.24f, 0), new Vector3(quayLength + .3f, .19f, QuayWidth + .20f)); caps.Box(new Vector3(0, 1.43f, edge + .10f), new Vector3(quayLength, .24f, .48f)); paint.Box(new Vector3(0, 1.34f, edge + .61f), new Vector3(quayLength - .35f, .026f, .085f)); steel.Box(new Vector3(0, 1.41f, -edge - .37f), new Vector3(quayLength - .3f, .16f, .19f)); const float stationSpacing = 9.52f; int stations = Mathf.FloorToInt(quayLength / (2 * stationSpacing)); for (int station = -stations; station <= stations; station++) { float x = station * stationSpacing; fenders.Box(new Vector3(x, .61f, edge - .079f), new Vector3(.64f, 1.04f, .29f)); if (station < stations) fenders.Box(new Vector3(x + stationSpacing * .5f, 1.33f, 0), new Vector3(.031f, .021f, QuayWidth - 1.6f)); Vector3 foot = new Vector3(x, 1.33f, edge + 1.54f); steel.Rod(foot, foot + Vector3.up * .48f, .14f, 8, true); steel.Rod(foot + new Vector3(-.23f, .42f, 0), foot + new Vector3(.23f, .42f, 0), .10f, 8, true); paint.Rod(foot + Vector3.up * .38f, foot + Vector3.up * .50f, .16f, 8, true); if (station % 3 == 0) { var lamp = new Vector3(x, 1.37f, -edge - .6f); steel.Rod(lamp, lamp + Vector3.up * 4.45f, .066f, 8, true); steel.Rod(lamp + Vector3.up * 4.43f, lamp + new Vector3(0, 4.62f, -.83f), .058f, 8); steel.Box(lamp + new Vector3(0, 4.60f, -.82f), new Vector3(.31f, .13f, .52f)); lantern.Box(lamp + new Vector3(0, 4.53f, -.83f), new Vector3(.23f, .027f, .39f)); } } AddQuayBatch(quay.transform, concrete, "Weathered quay wall", new Color(.43f, .45f, .43f)); AddQuayBatch(quay.transform, caps, "Concrete coping", new Color(.59f, .56f, .48f)); AddQuayBatch(quay.transform, fenders, "Rubber fenders and joints", new Color(.15f, .17f, .16f)); AddQuayBatch(quay.transform, steel, "Mooring and lighting hardware", new Color(.23f, .28f, .29f)); AddQuayBatch(quay.transform, paint, "Berth safety marking", new Color(.74f, .57f, .26f)); AddQuayBatch(quay.transform, lantern, "Quay lamps", new Color(.98f, .83f, .51f), .70f); return quay; } private void AddQuayBatch(Transform quay, HarborMesh geometry, string name, Color color, float emission = 0) { var model = new GameObject(name, typeof(MeshFilter), typeof(MeshRenderer)) { layer = 8 }; model.transform.SetParent(quay, false); Mesh mesh = geometry.Bake(); var material = new Material(surface) { color = color, name = name }; material.SetFloat("_Roughness", .81f); material.SetFloat("_Weather", .54f); material.SetFloat("_Emissive", emission); model.GetComponent().sharedMesh = mesh; model.GetComponent().sharedMaterial = material; quayMeshes.Add(mesh); quayFinishes.Add(material); } private void ReleasePortQuay() { portScenery?.Release(); ReleaseCinematicQuay(); } private void ReleaseCinematicQuay() { if (portQuay != null) { portQuay.SetActive(false); Destroy(portQuay); portQuay = null; } foreach (Mesh mesh in quayMeshes) Destroy(mesh); foreach (Material material in quayFinishes) Destroy(material); quayMeshes.Clear(); quayFinishes.Clear(); } } }