Add manual helm, grounded interiors, and taller ship classes
This commit is contained in:
@@ -14,14 +14,15 @@ namespace Soundings
|
||||
private Material water;
|
||||
private Mesh waterMesh;
|
||||
private Shader surface, reflection;
|
||||
private float backplateRatio, vesselLength = 71;
|
||||
private float backplateRatio, vesselLength = 72, vesselTop = 16.4f;
|
||||
private int lastHull = -2, lastDecks;
|
||||
private string currentBackplate;
|
||||
private ulong lastLayout;
|
||||
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 passage;
|
||||
private float seaTime, seaSeverity;
|
||||
private Vector3 hullMotion, hullMotionVelocity;
|
||||
private bool sailing;
|
||||
private Bounds hullBounds;
|
||||
private DeckPassengers decks;
|
||||
@@ -33,11 +34,13 @@ namespace Soundings
|
||||
private CameraMode cameraMode;
|
||||
private float freeYaw, freePitch, freeFov = 47f;
|
||||
private Vector3 freePosition;
|
||||
private float walkHeight;
|
||||
private ShipWalkSpace walkSpace;
|
||||
|
||||
private enum CameraMode { Orbit, Flight, DeckWalk, Bridge, Service }
|
||||
private enum CameraMode { Orbit, DeckWalk, Bridge, Service }
|
||||
|
||||
public RenderTexture Frame { get; private set; }
|
||||
internal Vector3 FirstPersonPosition => freePosition;
|
||||
internal bool FirstPersonPositionIsClear => walkSpace != null && walkSpace.CanStand(freePosition);
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
@@ -65,6 +68,7 @@ namespace Soundings
|
||||
ocean.Quad(new Vector3(-2000, 0, -2000), new Vector3(-2000, 0, 2000),
|
||||
new Vector3(2000, 0, 2000), new Vector3(2000, 0, -2000));
|
||||
waterMesh = ocean.Bake();
|
||||
waterMesh.bounds = new Bounds(Vector3.zero, Vector3.one * 4000);
|
||||
waterObject.GetComponent<MeshFilter>().sharedMesh = waterMesh;
|
||||
waterObject.GetComponent<MeshRenderer>().sharedMaterial = water;
|
||||
}
|
||||
@@ -79,6 +83,16 @@ namespace Soundings
|
||||
targetPitch = Mathf.Clamp(targetPitch + drag.y * .13f, 12f, deckIndex >= 0 ? 78f : 48f);
|
||||
}
|
||||
|
||||
public void RotateView(float degrees)
|
||||
{
|
||||
if (Mathf.Abs(degrees) < .0001f)
|
||||
return;
|
||||
if (cameraMode == CameraMode.Orbit)
|
||||
Orbit(new Vector2(degrees / .18f, 0));
|
||||
else
|
||||
freeYaw = Mathf.Repeat(freeYaw + degrees, 360);
|
||||
}
|
||||
|
||||
public void Zoom(float delta)
|
||||
{
|
||||
if (cameraMode != CameraMode.Orbit)
|
||||
@@ -101,12 +115,18 @@ namespace Soundings
|
||||
ExploreServiceArea(serviceArea);
|
||||
return;
|
||||
}
|
||||
if (cameraMode == CameraMode.DeckWalk)
|
||||
{
|
||||
ExploreDeck(deckIndex);
|
||||
return;
|
||||
}
|
||||
ResetOrbit(immediate);
|
||||
}
|
||||
|
||||
private void ResetOrbit(bool immediate)
|
||||
{
|
||||
cameraMode = CameraMode.Orbit;
|
||||
walkSpace = null;
|
||||
viewCamera.orthographic = true;
|
||||
targetYaw = deckIndex >= 0 ? 17f : DefaultYaw;
|
||||
targetPitch = deckIndex >= 0 ? 55f : DefaultPitch;
|
||||
@@ -124,61 +144,23 @@ namespace Soundings
|
||||
if (!sailing || drag.sqrMagnitude < .0001f)
|
||||
return;
|
||||
if (cameraMode == CameraMode.Orbit)
|
||||
EnterFlight();
|
||||
{
|
||||
Orbit(drag);
|
||||
return;
|
||||
}
|
||||
freeYaw = Mathf.Repeat(freeYaw + drag.x * .22f, 360f);
|
||||
freePitch = Mathf.Clamp(freePitch + drag.y * .19f, -80f, 84f);
|
||||
}
|
||||
|
||||
public void Fly(Vector3 input, float delta, bool boost)
|
||||
public void Walk(Vector2 input, float delta, bool boost)
|
||||
{
|
||||
if (!sailing || input.sqrMagnitude < .0001f || delta <= 0)
|
||||
if (!sailing || walkSpace == null || input.sqrMagnitude < .0001f || delta <= 0)
|
||||
return;
|
||||
if (cameraMode == CameraMode.Orbit)
|
||||
EnterFlight();
|
||||
input = Vector3.ClampMagnitude(input, 1f);
|
||||
bool interior = cameraMode is CameraMode.DeckWalk or CameraMode.Bridge or CameraMode.Service;
|
||||
Quaternion heading = Quaternion.Euler(interior ? 0 : freePitch, freeYaw, 0);
|
||||
Vector3 movement = heading * new Vector3(input.x, 0, input.z);
|
||||
if (!interior)
|
||||
movement += Vector3.up * input.y;
|
||||
float speed = interior ? (boost ? 6.4f : 3.3f) : (boost ? 52f : 20f);
|
||||
freePosition += movement * (speed * Mathf.Min(.10f, delta));
|
||||
if (cameraMode == CameraMode.DeckWalk)
|
||||
{
|
||||
freePosition.x = Mathf.Clamp(freePosition.x, -vesselLength * .38f, vesselLength * .31f);
|
||||
freePosition.z = Mathf.Clamp(freePosition.z, -vesselBeam * .77f, vesselBeam * .77f);
|
||||
freePosition.y = walkHeight;
|
||||
}
|
||||
else if (cameraMode == CameraMode.Bridge)
|
||||
{
|
||||
freePosition = decks.ClampBridgePosition(freePosition);
|
||||
}
|
||||
else if (cameraMode == CameraMode.Service)
|
||||
{
|
||||
freePosition.x = Mathf.Clamp(freePosition.x, -vesselLength * .358f, vesselLength * .358f);
|
||||
freePosition.z = Mathf.Clamp(freePosition.z, -vesselBeam * .79f, vesselBeam * .79f);
|
||||
freePosition.y = walkHeight;
|
||||
}
|
||||
else
|
||||
{
|
||||
Vector3 origin = vessel != null ? vessel.position : Vector3.zero;
|
||||
freePosition.x = Mathf.Clamp(freePosition.x, origin.x - 235, origin.x + 235);
|
||||
freePosition.y = Mathf.Clamp(freePosition.y, 1.4f, 150f);
|
||||
freePosition.z = Mathf.Clamp(freePosition.z, origin.z - 180, origin.z + 180);
|
||||
}
|
||||
}
|
||||
|
||||
private void EnterFlight()
|
||||
{
|
||||
cameraMode = CameraMode.Flight;
|
||||
freeFov = 47f;
|
||||
Vector3 angles = viewCamera.transform.eulerAngles;
|
||||
freeYaw = angles.y;
|
||||
freePitch = Mathf.DeltaAngle(0f, angles.x);
|
||||
float distance = viewCamera.orthographicSize / Mathf.Tan(freeFov * .5f * Mathf.Deg2Rad);
|
||||
freePosition = OrbitFocus() - viewCamera.transform.forward * distance;
|
||||
freePosition.y = Mathf.Max(1.4f, freePosition.y);
|
||||
viewCamera.orthographic = false;
|
||||
input = Vector2.ClampMagnitude(input, 1f);
|
||||
Quaternion heading = Quaternion.Euler(0, freeYaw, 0);
|
||||
Vector3 movement = heading * new Vector3(input.x, 0, input.y);
|
||||
float speed = boost ? 2.85f : 1.65f;
|
||||
freePosition = walkSpace.Move(freePosition, movement * (speed * Mathf.Min(.10f, delta)));
|
||||
}
|
||||
|
||||
public void ShowDeck(int index)
|
||||
@@ -199,14 +181,8 @@ namespace Soundings
|
||||
if (!sailing || displayedShip == null || index < 0 || index >= displayedShip.Decks.Count)
|
||||
return;
|
||||
ShowDeck(index);
|
||||
cameraMode = CameraMode.DeckWalk;
|
||||
freeFov = 62f;
|
||||
freePitch = 1.8f;
|
||||
freeYaw = 90f;
|
||||
freePosition = decks.WalkOrigin(index);
|
||||
walkHeight = freePosition.y;
|
||||
viewCamera.orthographic = false;
|
||||
ComposeCamera();
|
||||
decks.Explore(index);
|
||||
EnterWalk(CameraMode.DeckWalk, decks.WalkSpace(index), decks.WalkOrigin(index), 62f, 1.8f, 90f);
|
||||
}
|
||||
|
||||
public void ViewBridge()
|
||||
@@ -216,34 +192,33 @@ namespace Soundings
|
||||
if (serviceDeck != null)
|
||||
serviceDeck.Root.SetActive(false);
|
||||
deckIndex = -1;
|
||||
cameraMode = CameraMode.Bridge;
|
||||
exteriorRoot.gameObject.SetActive(false);
|
||||
decks.SelectBridge();
|
||||
freeFov = 68f;
|
||||
freePitch = 1.3f;
|
||||
freeYaw = 90f;
|
||||
freePosition = decks.BridgeOrigin();
|
||||
viewCamera.orthographic = false;
|
||||
ComposeCamera();
|
||||
EnterWalk(CameraMode.Bridge, decks.BridgeWalkSpace, decks.BridgeOrigin(), 68f, 1.3f, 90f);
|
||||
}
|
||||
|
||||
public void ExploreServiceArea(int area)
|
||||
{
|
||||
if (!sailing || vessel == null || area < 0 || area > 5)
|
||||
if (!sailing || vessel == null || area < 0 || area >= ServiceDeckModel.Areas.Length)
|
||||
return;
|
||||
if (serviceDeck == null)
|
||||
serviceDeck = new ServiceDeckModel(vessel, vesselLength, vesselBeam, surface);
|
||||
deckIndex = -1;
|
||||
serviceArea = area;
|
||||
cameraMode = CameraMode.Service;
|
||||
exteriorRoot.gameObject.SetActive(false);
|
||||
decks.HideRooms();
|
||||
serviceDeck.Root.SetActive(true);
|
||||
freeFov = 66f;
|
||||
freePitch = 2f;
|
||||
freeYaw = serviceDeck.Heading(area);
|
||||
freePosition = serviceDeck.Entry(area);
|
||||
walkHeight = freePosition.y;
|
||||
EnterWalk(CameraMode.Service, serviceDeck.WalkSpace, serviceDeck.Entry(area), 66f, 2f, serviceDeck.Heading(area));
|
||||
}
|
||||
|
||||
private void EnterWalk(CameraMode mode, ShipWalkSpace space, Vector3 entry, float fieldOfView, float lookDown, float heading)
|
||||
{
|
||||
cameraMode = mode;
|
||||
walkSpace = space;
|
||||
freeFov = fieldOfView;
|
||||
freePitch = lookDown;
|
||||
freeYaw = heading;
|
||||
freePosition = walkSpace.Entry(entry);
|
||||
viewCamera.orthographic = false;
|
||||
ComposeCamera();
|
||||
}
|
||||
@@ -254,6 +229,8 @@ namespace Soundings
|
||||
decks.Tick(session, delta);
|
||||
}
|
||||
|
||||
internal void SetRudder(float degrees) => decks?.SetRudder(degrees);
|
||||
|
||||
public bool IsShipAt(Vector2 viewportPoint)
|
||||
{
|
||||
return vessel != null && hullBounds.IntersectRay(viewCamera.ViewportPointToRay(viewportPoint));
|
||||
@@ -262,36 +239,66 @@ namespace Soundings
|
||||
public void BeginPassage(WeatherCondition weather = WeatherCondition.Calm)
|
||||
{
|
||||
sailing = true;
|
||||
water.SetFloat("_WeatherSeverity", (int)weather / 2f);
|
||||
seaSeverity = (int)weather / 2f;
|
||||
seaTime = 0;
|
||||
hullMotion = hullMotionVelocity = Vector3.zero;
|
||||
water.SetFloat("_WeatherSeverity", seaSeverity);
|
||||
cameraMode = CameraMode.Orbit;
|
||||
viewCamera.orthographic = true;
|
||||
targetYaw = 22f;
|
||||
targetPitch = 16f;
|
||||
targetZoom = 1.02f;
|
||||
SetPassage(0);
|
||||
SetPassage(Vector2.zero, 0, 0, 0);
|
||||
}
|
||||
|
||||
public void SetPassage(float progress)
|
||||
public void SetPassage(Vector2 routePosition, float heading, float speed, float delta)
|
||||
{
|
||||
passage = progress;
|
||||
float travel = Mathf.Lerp(-3.6f, 9.25f, progress);
|
||||
seaTime += delta * (1 + speed * .033f);
|
||||
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);
|
||||
if (vessel != null && delta > 0)
|
||||
{
|
||||
// Use wave heights along the waterline to drive a damped heave, pitch and roll.
|
||||
Vector2 bow = new Vector2(cosine, -sine) * (vesselLength * .36f);
|
||||
Vector2 port = new Vector2(sine, cosine) * (vesselBeam * .86f);
|
||||
float fore = WaveHeight(center + bow), aft = WaveHeight(center - bow);
|
||||
float left = WaveHeight(center + port), right = WaveHeight(center - port);
|
||||
var target = new Vector3((fore + aft + left + right) * .25f,
|
||||
Mathf.Atan2(right - left, 2 * port.magnitude) * Mathf.Rad2Deg,
|
||||
Mathf.Atan2(fore - aft, 2 * bow.magnitude) * Mathf.Rad2Deg);
|
||||
float response = .48f + vesselLength * .005f;
|
||||
hullMotion = Vector3.SmoothDamp(hullMotion, target, ref hullMotionVelocity, response, Mathf.Infinity, delta);
|
||||
}
|
||||
if (vessel != null)
|
||||
vessel.localPosition = new Vector3(travel, Mathf.Sin(progress * 19f) * .073f, 0);
|
||||
vessel.SetLocalPositionAndRotation(new Vector3(center.x, hullMotion.x, center.y),
|
||||
Quaternion.Euler(hullMotion.y, heading, hullMotion.z));
|
||||
Vector4 hull = water.GetVector("_HullSpan");
|
||||
hull.z = travel;
|
||||
hull.z = center.x;
|
||||
hull.w = center.y;
|
||||
water.SetVector("_HullSpan", hull);
|
||||
water.SetFloat("_Underway", Mathf.SmoothStep(.3f, 1, Mathf.Clamp01(progress * 5)));
|
||||
water.SetVector("_HullCourse", new Vector4(cosine, sine, Mathf.Clamp01(speed / 19f), 0));
|
||||
water.SetFloat("_SeaTime", seaTime);
|
||||
water.SetFloat("_Underway", 1);
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
public void EndPassage()
|
||||
{
|
||||
sailing = false;
|
||||
passage = 0;
|
||||
if (vessel != null)
|
||||
vessel.localPosition = Vector3.zero;
|
||||
vessel.SetLocalPositionAndRotation(Vector3.zero, Quaternion.identity);
|
||||
Vector4 hull = water.GetVector("_HullSpan");
|
||||
hull.z = 0;
|
||||
hull.z = hull.w = 0;
|
||||
water.SetVector("_HullSpan", hull);
|
||||
water.SetVector("_HullCourse", new Vector4(1, 0, 0, 0));
|
||||
water.SetFloat("_Underway", 0);
|
||||
water.SetFloat("_WeatherSeverity", 0);
|
||||
ShowDeck(-1);
|
||||
@@ -320,10 +327,10 @@ namespace Soundings
|
||||
|
||||
private Vector3 OrbitFocus()
|
||||
{
|
||||
float level = deckIndex >= 0 && decks != null ? decks.FloorHeight(deckIndex) + .5f : 4.25f;
|
||||
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)
|
||||
center.x += Mathf.Lerp(-2, 6.5f, passage);
|
||||
if (sailing && vessel != null)
|
||||
center = vessel.TransformPoint(center);
|
||||
return center;
|
||||
}
|
||||
|
||||
@@ -341,9 +348,8 @@ namespace Soundings
|
||||
viewCamera.orthographic = false;
|
||||
viewCamera.nearClipPlane = .095f;
|
||||
viewCamera.fieldOfView = freeFov;
|
||||
Vector3 placement = cameraMode is CameraMode.Bridge or CameraMode.DeckWalk or CameraMode.Service
|
||||
? vessel.TransformPoint(freePosition) : freePosition;
|
||||
viewCamera.transform.SetPositionAndRotation(placement, Quaternion.Euler(freePitch, freeYaw, 0));
|
||||
viewCamera.transform.SetPositionAndRotation(vessel.TransformPoint(freePosition),
|
||||
vessel.rotation * Quaternion.Euler(freePitch, freeYaw, 0));
|
||||
}
|
||||
FitViewport();
|
||||
}
|
||||
@@ -388,6 +394,13 @@ namespace Soundings
|
||||
return;
|
||||
float aspect = (float)Frame.width / Frame.height;
|
||||
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);
|
||||
water.SetVector("_ViewOrigin", view.position);
|
||||
water.SetVector("_ViewForward", view.forward);
|
||||
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)
|
||||
return;
|
||||
float cropX = Mathf.Min(1, aspect / backplateRatio) * .95f;
|
||||
@@ -434,10 +447,11 @@ namespace Soundings
|
||||
exteriorRoot = new GameObject("Exterior").transform;
|
||||
exteriorRoot.SetParent(vessel, false);
|
||||
|
||||
vesselLength = 71 + hullIndex * 12;
|
||||
float beam = vesselBeam = 6.65f + hullIndex * .87f;
|
||||
vesselLength = ship.Hull.Length;
|
||||
float beam = vesselBeam = ship.Hull.Beam * .5f;
|
||||
vesselTop = VesselExterior.DeckBase + deckCount * VesselExterior.DeckSpacing + 5.95f;
|
||||
water.SetVector("_HullSpan", new Vector4(vesselLength, beam, 0, 0));
|
||||
hullBounds = new Bounds(new Vector3(0, 5.5f, 0), new Vector3(vesselLength, 12.3f + deckCount * 1.7f, beam * 2.1f));
|
||||
hullBounds = new Bounds(new Vector3(0, (vesselTop - 2.65f) * .5f, 0), new Vector3(vesselLength, vesselTop + 2.65f, beam * 2.1f));
|
||||
FitViewport();
|
||||
|
||||
var exterior = new VesselExterior(ship, vesselLength, beam);
|
||||
|
||||
Reference in New Issue
Block a user