Add manual helm, grounded interiors, and taller ship classes
This commit is contained in:
129
Assets/Soundings/Runtime/ShipWalkSpace.cs
Normal file
129
Assets/Soundings/Runtime/ShipWalkSpace.cs
Normal file
@@ -0,0 +1,129 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Soundings
|
||||
{
|
||||
internal sealed class ShipWalkSpace
|
||||
{
|
||||
private const float BodyRadius = .29f;
|
||||
private const float ContactGap = .006f;
|
||||
private const float MinMotion = .0001f;
|
||||
private readonly Rect limits;
|
||||
private readonly List<Rect> solids = new();
|
||||
|
||||
public ShipWalkSpace(float minX, float maxX, float minZ, float maxZ)
|
||||
{
|
||||
limits = Rect.MinMaxRect(minX + BodyRadius, minZ + BodyRadius, maxX - BodyRadius, maxZ - BodyRadius);
|
||||
}
|
||||
|
||||
// A room's solids and camera both use vessel coordinates, so heave and travel never move one through the other.
|
||||
public void Block(Vector3 center, Vector3 size) => Block(Plan(center), Plan(size));
|
||||
|
||||
public void Block(Vector2 center, Vector2 size)
|
||||
{
|
||||
Vector2 half = size * .5f + Vector2.one * BodyRadius;
|
||||
solids.Add(Rect.MinMaxRect(center.x - half.x, center.y - half.y, center.x + half.x, center.y + half.y));
|
||||
}
|
||||
|
||||
public bool CanStand(Vector3 position) => CanStand(Plan(position));
|
||||
|
||||
public Vector3 Entry(Vector3 requested)
|
||||
{
|
||||
Vector2 origin = Confine(Plan(requested));
|
||||
if (CanStand(origin))
|
||||
return AtHeight(origin, requested.y);
|
||||
|
||||
// Keep entry safe even after a refit puts furniture close to the original spawn.
|
||||
for (float radius = .12f; radius <= 4; radius += .12f)
|
||||
{
|
||||
for (int i = 0; i < 24; i++)
|
||||
{
|
||||
float angle = i * (Mathf.PI / 12f);
|
||||
Vector2 nearby = Confine(origin + radius * new Vector2(Mathf.Cos(angle), Mathf.Sin(angle)));
|
||||
if (CanStand(nearby))
|
||||
return AtHeight(nearby, requested.y);
|
||||
}
|
||||
}
|
||||
return AtHeight(origin, requested.y);
|
||||
}
|
||||
|
||||
public Vector3 Move(Vector3 position, Vector3 motion)
|
||||
{
|
||||
Vector2 origin = Plan(position);
|
||||
if (!CanStand(origin))
|
||||
return Entry(position);
|
||||
|
||||
Vector2 remaining = Plan(motion);
|
||||
for (int contact = 0; contact < 4; contact++)
|
||||
{
|
||||
remaining = Confine(origin + remaining) - origin;
|
||||
if (remaining.sqrMagnitude < MinMotion * MinMotion)
|
||||
break;
|
||||
|
||||
float time = 1;
|
||||
Vector2 normal = Vector2.zero;
|
||||
foreach (Rect solid in solids)
|
||||
{
|
||||
if (Sweep(origin, remaining, solid, out float hit, out Vector2 surface) && hit < time)
|
||||
{
|
||||
time = hit;
|
||||
normal = surface;
|
||||
}
|
||||
}
|
||||
|
||||
if (normal == Vector2.zero)
|
||||
{
|
||||
origin += remaining;
|
||||
break;
|
||||
}
|
||||
|
||||
float shortOfContact = Mathf.Max(0, time - ContactGap / remaining.magnitude);
|
||||
origin += remaining * shortOfContact;
|
||||
remaining *= 1 - time;
|
||||
remaining -= Vector2.Dot(remaining, normal) * normal;
|
||||
}
|
||||
return AtHeight(Confine(origin), position.y);
|
||||
}
|
||||
|
||||
private bool CanStand(Vector2 point)
|
||||
{
|
||||
if (point.x < limits.xMin || point.x > limits.xMax || point.y < limits.yMin || point.y > limits.yMax)
|
||||
return false;
|
||||
foreach (Rect solid in solids)
|
||||
if (solid.xMin < point.x && point.x < solid.xMax && solid.yMin < point.y && point.y < solid.yMax)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool Sweep(Vector2 origin, Vector2 motion, Rect solid, out float time, out Vector2 normal)
|
||||
{
|
||||
time = 0;
|
||||
float exit = 1;
|
||||
normal = Vector2.zero;
|
||||
return SweepAxis(origin.x, motion.x, solid.xMin, solid.xMax, Vector2.right, ref time, ref exit, ref normal)
|
||||
&& SweepAxis(origin.y, motion.y, solid.yMin, solid.yMax, Vector2.up, ref time, ref exit, ref normal)
|
||||
&& normal != Vector2.zero && time < exit;
|
||||
}
|
||||
|
||||
private static bool SweepAxis(float origin, float motion, float lower, float upper, Vector2 axis,
|
||||
ref float enter, ref float exit, ref Vector2 normal)
|
||||
{
|
||||
if (motion == 0)
|
||||
return lower < origin && origin < upper;
|
||||
float first = (lower - origin) / motion;
|
||||
float second = (upper - origin) / motion;
|
||||
float near = Mathf.Min(first, second);
|
||||
if (near >= enter)
|
||||
{
|
||||
enter = near;
|
||||
normal = motion > 0 ? -axis : axis;
|
||||
}
|
||||
exit = Mathf.Min(exit, Mathf.Max(first, second));
|
||||
return enter <= exit;
|
||||
}
|
||||
|
||||
private Vector2 Confine(Vector2 point) => new(Mathf.Clamp(point.x, limits.xMin, limits.xMax), Mathf.Clamp(point.y, limits.yMin, limits.yMax));
|
||||
private static Vector2 Plan(Vector3 point) => new(point.x, point.z);
|
||||
private static Vector3 AtHeight(Vector2 point, float height) => new(point.x, height, point.y);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user