using System;
using UnityEngine;
namespace GrassCutter
{
///
/// The crew's collision capsule. Unity's CharacterController is welded to the world's Y axis, which is no good on a
/// ball, so this one stands along the local up: it sweeps through solid things (deck, machines, rocks), climbs low
/// steps, and takes the soil straight from .
///
[RequireComponent(typeof(CapsuleCollider))]
public class CrewBody : MonoBehaviour
{
const float Skin = 0.03f, StepUp = 0.4f, FloorDot = 0.6f, StickDown = 0.3f;
const int Solid = (1 << Layers.Default) | (1 << Physics.IgnoreRaycastLayer) | (1 << Layers.Prop) | (1 << Layers.Riding) | (1 << Layers.Vehicle) | (1 << Layers.Player) | (1 << Layers.Build);
public bool isGrounded { get; private set; }
/// Walked into something: what was hit and which way we were going.
public event Action Bumped;
CapsuleCollider _cap;
readonly Collider[] _near = new Collider[16];
void Awake() { _cap = GetComponent(); }
void OnEnable() { if (_cap) _cap.enabled = true; }
void OnDisable() { if (_cap) _cap.enabled = false; isGrounded = false; }
void Ends(Vector3 pos, Vector3 up, out Vector3 low, out Vector3 high)
{
low = pos + up * _cap.radius;
high = pos + up * (_cap.height - _cap.radius);
}
public void Move(Vector3 delta)
{
var pos = transform.position;
// the body stands the way the crew member stands (the soil's up: see Planet.Level)
var up = transform.up;
float rise = Vector3.Dot(delta, up);
var flat = delta - up * rise;
bool was = isGrounded;
isGrounded = false;
Unstick(ref pos, up);
float lifted = was ? Sweep(ref pos, up, StepUp, up) : 0f;
Slide(ref pos, flat, up);
if (rise > 0f && !was) Sweep(ref pos, up, rise, up);
else if (rise > 0f) Sweep(ref pos, up, rise - Mathf.Min(rise, lifted), up);
if (rise <= 0f || lifted > rise)
{
float down = Mathf.Max(0f, lifted - Mathf.Max(0f, rise)) - Mathf.Min(0f, rise);
if (was && rise <= 0f) down += 0.04f;
if (Physics.CapsuleCast(Low(pos, up), High(pos, up), _cap.radius - Skin, -up, out var hit, down + Skin, Solid, QueryTriggerInteraction.Ignore))
{
if (hit.distance > 0f) pos -= up * Mathf.Max(0f, hit.distance - Skin);
if (Vector3.Dot(hit.normal, up) >= FloorDot) isGrounded = true;
}
else pos -= up * down;
}
// the soil itself
float alt = Planet.Altitude(pos);
if (alt <= 0f || (was && rise <= 0f && alt < StickDown && !isGrounded))
{
pos -= Planet.Up(pos) * alt;
isGrounded = true;
}
transform.position = pos;
}
Vector3 Low(Vector3 pos, Vector3 up) { Ends(pos, up, out var l, out _); return l; }
Vector3 High(Vector3 pos, Vector3 up) { Ends(pos, up, out _, out var h); return h; }
/// Move straight along dir until something stops us. Returns how far we got.
float Sweep(ref Vector3 pos, Vector3 dir, float dist, Vector3 up)
{
if (dist <= 0f) return 0f;
float go = dist;
if (Physics.CapsuleCast(Low(pos, up), High(pos, up), _cap.radius - Skin, dir, out var hit, dist + Skin, Solid, QueryTriggerInteraction.Ignore))
go = hit.distance > 0f ? Mathf.Max(0f, hit.distance - Skin) : 0f;
pos += dir * go;
return go;
}
void Slide(ref Vector3 pos, Vector3 move, Vector3 up)
{
for (int i = 0; i < 4 && move.sqrMagnitude > 1e-8f; i++)
{
float dist = move.magnitude;
var dir = move / dist;
if (!Physics.CapsuleCast(Low(pos, up), High(pos, up), _cap.radius - Skin, dir, out var hit, dist + Skin, Solid, QueryTriggerInteraction.Ignore))
{
pos += move;
return;
}
if (hit.distance <= 0f) { Unstick(ref pos, up); continue; }
float go = Mathf.Max(0f, hit.distance - Skin);
pos += dir * go;
Bumped?.Invoke(hit, dir);
move = Vector3.ProjectOnPlane(dir * (dist - go), hit.normal);
// a wall never lifts you: only a walkable slope may
if (Vector3.Dot(hit.normal, up) < FloorDot) move -= up * Mathf.Max(0f, Vector3.Dot(move, up));
}
}
/// Spawned, teleported or shoved into something: step out of it.
void Unstick(ref Vector3 pos, Vector3 up)
{
int n = Physics.OverlapCapsuleNonAlloc(Low(pos, up), High(pos, up), _cap.radius, _near, Solid, QueryTriggerInteraction.Ignore);
var turn = Quaternion.FromToRotation(Vector3.up, up);
for (int i = 0; i < n; i++)
{
var c = _near[i];
if (c == _cap || c.isTrigger) continue;
if (Physics.ComputePenetration(_cap, pos, turn, c, c.transform.position, c.transform.rotation, out var dir, out var dist))
pos += dir * dist;
}
}
}
}