Make voyages active with dockside orders, crew calls, and ship PA
This commit is contained in:
@@ -3,7 +3,7 @@ using UnityEngine;
|
||||
|
||||
namespace Soundings
|
||||
{
|
||||
public sealed class Harbor : MonoBehaviour
|
||||
public sealed partial class Harbor : MonoBehaviour
|
||||
{
|
||||
private readonly Dictionary<VesselExterior.Paint, Material> materials = new Dictionary<VesselExterior.Paint, Material>();
|
||||
private readonly Dictionary<VesselExterior.Paint, Material> reflections = new Dictionary<VesselExterior.Paint, Material>();
|
||||
@@ -11,8 +11,10 @@ namespace Soundings
|
||||
private readonly List<Mesh> shipMeshes = new List<Mesh>();
|
||||
private Camera viewCamera;
|
||||
private Transform vessel;
|
||||
private Transform waterSurface;
|
||||
private Material water;
|
||||
private Mesh waterMesh;
|
||||
private PassageWake wake;
|
||||
private Shader surface, reflection;
|
||||
private float backplateRatio, vesselLength = 72, vesselTop = 16.4f;
|
||||
private int lastHull = -2, lastDecks;
|
||||
@@ -21,7 +23,8 @@ namespace Soundings
|
||||
private const float DefaultYaw = 37f, DefaultPitch = 17.5f;
|
||||
private float yaw = DefaultYaw, pitch = DefaultPitch, zoom = 1f;
|
||||
private float targetYaw = DefaultYaw, targetPitch = DefaultPitch, targetZoom = 1f;
|
||||
private float seaTime, seaSeverity;
|
||||
private float seaTime, seaSeverity, passageDistance;
|
||||
private Vector2 seaCenter;
|
||||
private Vector3 hullMotion, hullMotionVelocity;
|
||||
private bool sailing;
|
||||
private Bounds hullBounds;
|
||||
@@ -39,6 +42,8 @@ namespace Soundings
|
||||
private enum CameraMode { Orbit, DeckWalk, Bridge, Service }
|
||||
|
||||
public RenderTexture Frame { get; private set; }
|
||||
internal Vector3 PassageWorldPosition => vessel != null ? vessel.position : Vector3.zero;
|
||||
internal bool PassageUsesPerspective => sailing && cameraMode == CameraMode.Orbit && !viewCamera.orthographic;
|
||||
internal Vector3 FirstPersonPosition => freePosition;
|
||||
internal bool FirstPersonPositionIsClear => walkSpace != null && walkSpace.CanStand(freePosition);
|
||||
|
||||
@@ -47,6 +52,8 @@ namespace Soundings
|
||||
surface = Resources.Load<Shader>("HarborSurface");
|
||||
reflection = Resources.Load<Shader>("HarborReflection");
|
||||
water = new Material(Resources.Load<Shader>("HarborWater"));
|
||||
var oceanDetail = Resources.Load<Texture2D>("Art/OceanDetail");
|
||||
water.SetTexture("_OceanDetail", oceanDetail);
|
||||
|
||||
var cameraObject = new GameObject("Harbor camera", typeof(Camera));
|
||||
cameraObject.transform.SetParent(transform, false);
|
||||
@@ -55,7 +62,7 @@ namespace Soundings
|
||||
viewCamera.clearFlags = CameraClearFlags.SolidColor;
|
||||
viewCamera.backgroundColor = new Color(.09f, .17f, .23f);
|
||||
viewCamera.nearClipPlane = .25f;
|
||||
viewCamera.farClipPlane = 1050;
|
||||
viewCamera.farClipPlane = 1300;
|
||||
viewCamera.cullingMask = 1 << 8;
|
||||
viewCamera.enabled = false;
|
||||
|
||||
@@ -64,6 +71,7 @@ namespace Soundings
|
||||
var waterObject = new GameObject("Harbor backdrop and water", typeof(MeshFilter), typeof(MeshRenderer));
|
||||
waterObject.layer = 8;
|
||||
waterObject.transform.SetParent(transform, false);
|
||||
waterSurface = waterObject.transform;
|
||||
var ocean = new HarborMesh();
|
||||
ocean.Quad(new Vector3(-2000, 0, -2000), new Vector3(-2000, 0, 2000),
|
||||
new Vector3(2000, 0, 2000), new Vector3(2000, 0, -2000));
|
||||
@@ -71,6 +79,7 @@ namespace Soundings
|
||||
waterMesh.bounds = new Bounds(Vector3.zero, Vector3.one * 4000);
|
||||
waterObject.GetComponent<MeshFilter>().sharedMesh = waterMesh;
|
||||
waterObject.GetComponent<MeshRenderer>().sharedMaterial = water;
|
||||
wake = new PassageWake(transform, oceanDetail);
|
||||
}
|
||||
|
||||
public void SetVisible(bool visible) => viewCamera.enabled = visible;
|
||||
@@ -79,8 +88,12 @@ namespace Soundings
|
||||
{
|
||||
if (cameraMode != CameraMode.Orbit)
|
||||
return;
|
||||
targetYaw = sailing ? (targetYaw - drag.x * .18f) : Mathf.Clamp(targetYaw - drag.x * .18f, 3f, 72f);
|
||||
targetPitch = Mathf.Clamp(targetPitch + drag.y * .13f, 12f, deckIndex >= 0 ? 78f : 48f);
|
||||
targetYaw -= drag.x * .18f;
|
||||
if (sailing)
|
||||
targetYaw = Mathf.Repeat(targetYaw + 180f, 360f) - 180f;
|
||||
else
|
||||
targetYaw = Mathf.Clamp(targetYaw, 3f, 72f);
|
||||
targetPitch = Mathf.Clamp(targetPitch + drag.y * .13f, sailing ? 4f : 12f, deckIndex >= 0 ? 79f : 69f);
|
||||
}
|
||||
|
||||
public void RotateView(float degrees)
|
||||
@@ -100,7 +113,7 @@ namespace Soundings
|
||||
freeFov = Mathf.Clamp(freeFov * Mathf.Exp(delta * .035f), 25f, 78f);
|
||||
return;
|
||||
}
|
||||
targetZoom = Mathf.Clamp(targetZoom * Mathf.Exp(delta * .050f), sailing ? .17f : .64f, 1.51f);
|
||||
targetZoom = Mathf.Clamp(targetZoom * Mathf.Exp(delta * .050f), sailing ? .62f : .64f, sailing ? 1.82f : 1.51f);
|
||||
}
|
||||
|
||||
public void ResetView(bool immediate = false)
|
||||
@@ -127,9 +140,8 @@ namespace Soundings
|
||||
{
|
||||
cameraMode = CameraMode.Orbit;
|
||||
walkSpace = null;
|
||||
viewCamera.orthographic = true;
|
||||
targetYaw = deckIndex >= 0 ? 17f : DefaultYaw;
|
||||
targetPitch = deckIndex >= 0 ? 55f : DefaultPitch;
|
||||
targetYaw = deckIndex >= 0 ? 17f : sailing ? 24f : DefaultYaw;
|
||||
targetPitch = deckIndex >= 0 ? 57f : sailing ? 13f : DefaultPitch;
|
||||
targetZoom = deckIndex >= 0 ? .92f : 1f;
|
||||
if (!immediate)
|
||||
return;
|
||||
@@ -236,28 +248,31 @@ namespace Soundings
|
||||
return vessel != null && hullBounds.IntersectRay(viewCamera.ViewportPointToRay(viewportPoint));
|
||||
}
|
||||
|
||||
public void BeginPassage(WeatherCondition weather = WeatherCondition.Calm)
|
||||
public void BeginPassage(WeatherCondition weather = WeatherCondition.Calm, float duration = 180f)
|
||||
{
|
||||
sailing = true;
|
||||
seaSeverity = (int)weather / 2f;
|
||||
seaTime = 0;
|
||||
float nominalKnots = 17f + 2f * (displayedShip?.HullIndex ?? 0);
|
||||
float weatherSlowdown = weather == WeatherCondition.Storm ? 1.32f : weather == WeatherCondition.Swell ? 1.14f : 1f;
|
||||
passageDistance = nominalKnots * .514444f * duration / weatherSlowdown;
|
||||
hullMotion = hullMotionVelocity = Vector3.zero;
|
||||
water.SetFloat("_WeatherSeverity", seaSeverity);
|
||||
cameraMode = CameraMode.Orbit;
|
||||
viewCamera.orthographic = true;
|
||||
targetYaw = 22f;
|
||||
targetPitch = 16f;
|
||||
targetZoom = 1.02f;
|
||||
Shader.SetGlobalFloat("_SoundingsSeaWeather", seaSeverity);
|
||||
Shader.SetGlobalFloat("_SoundingsSeaSailing", 1);
|
||||
wake.Reset();
|
||||
SetPassage(Vector2.zero, 0, 0, 0);
|
||||
ResetOrbit(true);
|
||||
}
|
||||
|
||||
public void SetPassage(Vector2 routePosition, float heading, float speed, float delta)
|
||||
{
|
||||
seaTime += delta * (1 + speed * .033f);
|
||||
seaTime += delta;
|
||||
float headingRadians = heading * Mathf.Deg2Rad;
|
||||
float sine = Mathf.Sin(headingRadians);
|
||||
float cosine = Mathf.Cos(headingRadians);
|
||||
var center = new Vector2(routePosition.x * 13f - 3.6f, -routePosition.y * 13f);
|
||||
var center = new Vector2(routePosition.x, -routePosition.y) * passageDistance;
|
||||
seaCenter = center;
|
||||
if (vessel != null && delta > 0)
|
||||
{
|
||||
// Use wave heights along the waterline to drive a damped heave, pitch and roll.
|
||||
@@ -281,18 +296,26 @@ namespace Soundings
|
||||
water.SetVector("_HullCourse", new Vector4(cosine, sine, Mathf.Clamp01(speed / 19f), 0));
|
||||
water.SetFloat("_SeaTime", seaTime);
|
||||
water.SetFloat("_Underway", 1);
|
||||
waterSurface.localPosition = new Vector3(center.x, 0, center.y);
|
||||
Shader.SetGlobalFloat("_SoundingsSeaTime", seaTime);
|
||||
wake.Update(center, new Vector2(cosine, -sine), speed, vesselLength, vesselBeam, seaTime, seaSeverity);
|
||||
}
|
||||
|
||||
private float WaveHeight(Vector2 point)
|
||||
{
|
||||
float amplitude = Mathf.Lerp(.12f, 1.45f, seaSeverity * seaSeverity);
|
||||
return amplitude * (.72f * Mathf.Sin(.052f * point.x + .115f * point.y - .93f * seaTime)
|
||||
+ .28f * Mathf.Sin(-.073f * point.x + .17f * point.y - 1.22f * seaTime + 1.30f));
|
||||
float amplitude = Mathf.Lerp(.35f, 1.46f, seaSeverity * seaSeverity);
|
||||
return amplitude * (.65f * Mathf.Sin(.052f * point.x + .115f * point.y - .93f * seaTime)
|
||||
+ .25f * Mathf.Sin(-.073f * point.x + .17f * point.y - 1.22f * seaTime + 1.30f)
|
||||
+ .10f * Mathf.Sin(.217f * point.x - .136f * point.y - 1.64f * seaTime + 2.9f));
|
||||
}
|
||||
|
||||
public void EndPassage()
|
||||
{
|
||||
sailing = false;
|
||||
seaCenter = Vector2.zero;
|
||||
waterSurface.localPosition = Vector3.zero;
|
||||
wake.Reset();
|
||||
Shader.SetGlobalFloat("_SoundingsSeaSailing", 0);
|
||||
if (vessel != null)
|
||||
vessel.SetLocalPositionAndRotation(Vector3.zero, Quaternion.identity);
|
||||
Vector4 hull = water.GetVector("_HullSpan");
|
||||
@@ -315,11 +338,11 @@ namespace Soundings
|
||||
ComposeCamera();
|
||||
return;
|
||||
}
|
||||
float easing = 1 - Mathf.Exp(-Time.unscaledDeltaTime * 11f);
|
||||
float easing = 1 - Mathf.Exp(-Time.unscaledDeltaTime * (sailing ? 12f : 11f));
|
||||
if (!sailing && Mathf.Abs(yaw - targetYaw) < .005f && Mathf.Abs(pitch - targetPitch) < .005f &&
|
||||
Mathf.Abs(zoom - targetZoom) < .0001f)
|
||||
return;
|
||||
yaw = Mathf.Lerp(yaw, targetYaw, easing);
|
||||
yaw = Mathf.Repeat(yaw + Mathf.DeltaAngle(yaw, targetYaw) * easing + 180f, 360f) - 180f;
|
||||
pitch = Mathf.Lerp(pitch, targetPitch, easing);
|
||||
zoom = Mathf.Lerp(zoom, targetZoom, easing);
|
||||
ComposeCamera();
|
||||
@@ -329,8 +352,11 @@ namespace Soundings
|
||||
{
|
||||
float level = deckIndex >= 0 && decks != null ? decks.FloorHeight(deckIndex) + .5f : vesselTop * .39f;
|
||||
var center = new Vector3(deckIndex >= 0 ? -2f : 2.1f, level, 0);
|
||||
if (sailing && vessel != null)
|
||||
center = vessel.TransformPoint(center);
|
||||
if (sailing)
|
||||
{
|
||||
center.x += seaCenter.x;
|
||||
center.z = seaCenter.y;
|
||||
}
|
||||
return center;
|
||||
}
|
||||
|
||||
@@ -338,10 +364,13 @@ namespace Soundings
|
||||
{
|
||||
if (cameraMode == CameraMode.Orbit)
|
||||
{
|
||||
viewCamera.orthographic = true;
|
||||
viewCamera.orthographic = !sailing;
|
||||
viewCamera.nearClipPlane = .25f;
|
||||
viewCamera.fieldOfView = deckIndex >= 0 ? 45f : 38f;
|
||||
float aspect = Frame == null ? 16f / 9f : (float)Frame.width / Frame.height;
|
||||
float distance = sailing ? vesselLength * Mathf.Max(1.29f, 2.16f / aspect) * zoom : 244f;
|
||||
Quaternion orbit = Quaternion.Euler(pitch, -yaw, 0);
|
||||
viewCamera.transform.SetPositionAndRotation(OrbitFocus() + orbit * (Vector3.back * 244f), orbit);
|
||||
viewCamera.transform.SetPositionAndRotation(OrbitFocus() + orbit * (Vector3.back * distance), orbit);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -393,6 +422,7 @@ namespace Soundings
|
||||
if (Frame == null)
|
||||
return;
|
||||
float aspect = (float)Frame.width / Frame.height;
|
||||
viewCamera.aspect = aspect;
|
||||
viewCamera.orthographicSize = Mathf.Max(17.6f, vesselLength * .71f / aspect) * zoom;
|
||||
Transform view = viewCamera.transform;
|
||||
float viewHeight = viewCamera.orthographic ? viewCamera.orthographicSize : Mathf.Tan(viewCamera.fieldOfView * Mathf.Deg2Rad * .5f);
|
||||
@@ -401,7 +431,7 @@ namespace Soundings
|
||||
water.SetVector("_ViewRight", view.right);
|
||||
water.SetVector("_ViewUp", view.up);
|
||||
water.SetVector("_ViewSize", new Vector4(viewHeight * aspect, viewHeight, viewCamera.orthographic ? 1 : 0, 0));
|
||||
if (backplateRatio <= 0)
|
||||
if (backplateRatio <= 0 || sailing)
|
||||
return;
|
||||
float cropX = Mathf.Min(1, aspect / backplateRatio) * .95f;
|
||||
float cropY = Mathf.Min(1, backplateRatio / aspect) * .95f;
|
||||
@@ -549,6 +579,7 @@ namespace Soundings
|
||||
{
|
||||
ReleaseFrame();
|
||||
ReleaseShip();
|
||||
wake?.Dispose();
|
||||
foreach (Material material in materials.Values)
|
||||
Destroy(material);
|
||||
foreach (Material material in reflections.Values)
|
||||
|
||||
Reference in New Issue
Block a user