Files
grass-cutters/Assets/Scripts/Player/ViewModel.cs
NiemandKG 36e4bb2a10 Round 6 fixes: held tools, headlamp glare, ladder openings, level horizon, roof hulls, rain hits, idle motion, belt packs, meadow variety
- Held tools sit lower and further right; the hammer faces forward and strikes about the camera's right
- Headlamp leaves held items, treats and vending machines alone (rendering layer NoLamp); other lights keep lighting them
- Hay grounded on the floor in world space (it floated)
- Deck fence opens at each ladder, rail colliders split around it; ladders moved to 2 m off the edge middle, clear of the intakes; climbing goes over a rail only if one is in the way
- Crew stand to the soil's up (Planet.Level): flat round the base, the ball's up past it, so the horizon stays level on a small planet
- Convex roof hulls for the crew cabin, garage, pitched roof and windmill tower; cabin box lowered to the eaves
- Rain and acid rain hits differ by surface: soil, grass, wood, metal
- Idle motion: dishes pan, sways sway, sensors turn, eyes blink, windmill turns in the breeze, reels and parked rotors tick over
- Belt: take tools off at the supply wall; hold E at a vending machine for a pack of 3 on the belt, LMB has one
- New meadow pieces: boulders, slab stones, crystals, mushrooms, stump, bulb plants
- -demofix checks for all of the above; loc rows in all 11 tables

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 22:52:05 +06:00

310 lines
17 KiB
C#

using System.Collections.Generic;
using UnityEngine;
namespace GrassCutter
{
/// <summary>
/// First-person tools, rendered by the overlay camera. All animation is procedural and runs off the tool's
/// own stroke, so the blade and the sound land together: scissors and shears snip, the sickle and the scythes sweep
/// from right to left around the body, the hammer strikes when something is built.
/// </summary>
public class ViewModel : MonoBehaviour
{
enum Motion { Snip, Sweep, Strike }
/// <summary>How a tool is carried, in the camera's frame. No hands: the tool floats where the hand would hold it.</summary>
class Hold
{
public ToolId id;
public Motion motion;
public Vector3 pos, euler, usePos, useEuler;
public float scale;
// a sweep turns the whole rig around this point, by this much either way of the middle of the stroke
public Vector3 pivot;
public float arc, middle;
// the model turned about its own handle, so the business end faces where it should
public float spin;
}
class Rig
{
public Hold hold;
public Transform root, bladeA, bladeB;
public Transform mesh;
public Quaternion meshRot;
public Vector3 handle;
}
/// <summary>Every tool sits this much lower and further right than its pose says: out of the middle of the view.</summary>
static readonly Vector3 Shift = new Vector3(0.045f, -0.05f, 0f);
/// <summary>Turns a tool about its handle (the longest side of its mesh), on top of its pose's own spin.</summary>
public static void Spin(ToolId id, float degrees)
{
var vm = FindAnyObjectByType<ViewModel>();
if (vm && vm._rigs.TryGetValue(id, out var r) && r.mesh)
r.mesh.localRotation = r.meshRot * Quaternion.AngleAxis(r.hold.spin + degrees, r.handle);
}
static readonly Hold[] Holds =
{
// scissors and shears are held like a knife, tips up, and tipped toward the grass to cut
new Hold { id = ToolId.NailScissors, motion = Motion.Snip, scale = 0.5f,
pos = new Vector3(0.21f, -0.13f, 0.4f), euler = new Vector3(-42f, -16f, -8f),
usePos = new Vector3(0.2f, -0.17f, 0.46f), useEuler = new Vector3(-28f, -14f, -6f) },
new Hold { id = ToolId.GardenShears, motion = Motion.Snip, scale = 0.75f,
pos = new Vector3(0.25f, -0.14f, 0.46f), euler = new Vector3(-36f, -18f, -6f),
usePos = new Vector3(0.22f, -0.2f, 0.5f), useEuler = new Vector3(-24f, -14f, -6f) },
// the sickle's crescent is tipped back toward the eye, so it reads as a crescent and not as a line
new Hold { id = ToolId.Sickle, motion = Motion.Sweep, scale = 0.34f,
pos = new Vector3(0.27f, -0.17f, 0.46f), euler = new Vector3(-35f, -18f, -6f),
usePos = new Vector3(0.22f, -0.19f, 0.5f), useEuler = new Vector3(-20f, -14f, -6f),
pivot = new Vector3(0.35f, -0.5f, 0f), arc = 32f, middle = 4f },
// the snath runs out ahead, the blade lies out on the grass with its tip to the left
new Hold { id = ToolId.Scythe, motion = Motion.Sweep, scale = 0.46f,
pos = new Vector3(0.32f, -0.1f, 0.36f), euler = new Vector3(9f, -18f, 0f),
usePos = new Vector3(0.32f, -0.11f, 0.36f), useEuler = new Vector3(11f, -18f, 0f),
pivot = new Vector3(0.1f, -0.6f, -0.1f), arc = 28f, middle = 5f },
new Hold { id = ToolId.LongScythe, motion = Motion.Sweep, scale = 0.5f,
pos = new Vector3(0.32f, -0.1f, 0.36f), euler = new Vector3(9f, -20f, 0f),
usePos = new Vector3(0.32f, -0.11f, 0.36f), useEuler = new Vector3(11f, -20f, 0f),
pivot = new Vector3(0.1f, -0.6f, -0.1f), arc = 28f, middle = 5f },
new Hold { id = ToolId.WideScythe, motion = Motion.Sweep, scale = 0.54f,
pos = new Vector3(0.32f, -0.1f, 0.36f), euler = new Vector3(9f, -22f, 0f),
usePos = new Vector3(0.32f, -0.11f, 0.36f), useEuler = new Vector3(11f, -22f, 0f),
pivot = new Vector3(0.1f, -0.6f, -0.1f), arc = 28f, middle = 5f },
// the hammer stands up beside the view, face forward, ready to come down on what's being built
new Hold { id = ToolId.Hammer, motion = Motion.Strike, scale = 0.42f, spin = 270f,
pos = new Vector3(0.27f, -0.2f, 0.42f), euler = new Vector3(-68f, -10f, -8f),
usePos = new Vector3(0.27f, -0.2f, 0.42f), useEuler = new Vector3(-68f, -10f, -8f) },
};
const float Spread = 18f;
public const float RestOpen = 0.35f;
/// <summary>The test bot, looking at the player from outside.</summary>
public static bool Hidden;
readonly Dictionary<ToolId, Rig> _rigs = new Dictionary<ToolId, Rig>();
PlayerAgent _p;
ToolId _shown = (ToolId)255;
float _switch = 1f;
float _use, _strike = 9f;
Vector2 _sway;
float _bob, _hide;
float _land, _fall, _still;
bool _grounded = true;
void Start()
{
BuildRigs();
var phone = GetComponent<Phone>();
if (!phone) gameObject.AddComponent<Phone>();
if (!GetComponent<TreatHand>()) gameObject.AddComponent<TreatHand>();
}
void BuildRigs()
{
var bank = AssetBank.I;
foreach (var h in Holds)
{
var r = new Rig { hold = h, root = new GameObject(h.id.ToString()).transform };
r.root.SetParent(transform, false);
_rigs[h.id] = r;
var model = Add(bank.Model(GameDefs.Tool(h.id).model), r.root, Vector3.zero, Quaternion.identity, Vector3.one * h.scale, PrimitiveType.Cylinder, new Vector3(0.08f, 0.2f, 0.08f), Quaternion.Euler(90, 0, 0));
if (model)
foreach (var t in model.GetComponentsInChildren<Transform>(true))
{
if (t.name == "BladeA") r.bladeA = t;
else if (t.name == "BladeB") r.bladeB = t;
}
var mf = model ? model.GetComponentInChildren<MeshFilter>() : null;
if (mf && mf.sharedMesh)
{
var size = mf.sharedMesh.bounds.size;
r.mesh = mf.transform;
r.meshRot = mf.transform.localRotation;
r.handle = size.x >= size.y && size.x >= size.z ? Vector3.right : size.y >= size.z ? Vector3.up : Vector3.forward;
if (h.spin != 0f) r.mesh.localRotation = r.meshRot * Quaternion.AngleAxis(h.spin, r.handle);
}
SetLayer(r.root, Layers.ViewModel);
DayNight.KeepOutOfLamp(r.root.gameObject);
r.root.gameObject.SetActive(false);
}
}
static Transform Add(GameObject model, Transform parent, Vector3 pos, Quaternion rot, Vector3 scale, PrimitiveType fallback, Vector3 fallbackScale, Quaternion? fallbackRot = null)
{
GameObject go;
if (model) go = Instantiate(model);
else
{
go = GameObject.CreatePrimitive(fallback);
Destroy(go.GetComponent<Collider>());
go.transform.localScale = fallbackScale;
if (fallbackRot.HasValue) rot = fallbackRot.Value * rot;
var mat = AssetBank.I.Mat("M_GloveOrange") ?? AssetBank.I.Mat("M_HullWhite");
if (mat) go.GetComponent<Renderer>().sharedMaterial = mat;
}
go.transform.SetParent(parent, false);
go.transform.localPosition = pos;
go.transform.localRotation = model ? rot * model.transform.localRotation : rot;
if (model) go.transform.localScale = Vector3.Scale(model.transform.localScale, scale);
foreach (var c in go.GetComponentsInChildren<Collider>(true)) Destroy(c);
foreach (var r in go.GetComponentsInChildren<Renderer>(true))
{
r.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
r.receiveShadows = false;
}
return model ? go.transform : null;
}
static void SetLayer(Transform t, int layer)
{
foreach (var c in t.GetComponentsInChildren<Transform>(true)) c.gameObject.layer = layer;
}
public void Bind(PlayerAgent p)
{
if (_p && _p.Tools != null) _p.Tools.Used -= OnUsed;
_p = p;
if (_p) _p.Tools.Used += OnUsed;
if (_p) GetComponent<Phone>()?.Bind(_p);
GetComponent<TreatHand>()?.Bind(_p);
}
void OnUsed(ToolId t, bool pulse)
{
if (t == ToolId.Hammer) _strike = 0f;
}
/// <summary>A stroke goes from +1 on the right to -1 on the left, fast through the middle, then comes back at leisure.</summary>
public static float Sweep(float phase)
{
// a beat drawn back at the right, the cut, carried a little past the left by its own weight, then home
const float Gather = 0.08f, Cut = 0.4f, Settle = 0.5f;
if (phase < Gather) return 1f + 0.06f * Mathf.Sin(phase / Gather * Mathf.PI);
if (phase < Cut) return Mathf.Lerp(1f, -1.1f, Easing.Apply(Ease.InOutCubic, (phase - Gather) / (Cut - Gather)));
if (phase < Settle) return Mathf.Lerp(-1.1f, -1f, Mathf.SmoothStep(0f, 1f, (phase - Cut) / (Settle - Cut)));
return Mathf.Lerp(-1f, 1f, Mathf.SmoothStep(0f, 1f, (phase - Settle) / (1f - Settle)));
}
/// <summary>0 through the cut, rising to 1 halfway back: the blade is lifted clear of the grass on its way home.</summary>
static float Return(float phase) => phase < 0.5f ? 0f : Mathf.Sin((phase - 0.5f) / 0.5f * Mathf.PI);
/// <summary>How far open: shut hard right after the stroke turns over, which is when the snip sounds.</summary>
public static float Snip(float phase) => phase < 0.12f ? 1f - phase / 0.12f : Mathf.SmoothStep(0f, 1f, (phase - 0.12f) / 0.88f);
/// <summary>Scissors or shears open by this much of their full spread.</summary>
public static void Open(Transform a, Transform b, float open)
{
a.localRotation = BaseRot.Of(a) * Quaternion.AngleAxis(open * Spread, Vector3.forward);
b.localRotation = BaseRot.Of(b) * Quaternion.AngleAxis(-open * Spread, Vector3.forward);
}
void LateUpdate()
{
if (!_p || !_p.IsSpawned)
{
foreach (var r in _rigs.Values) r.root.gameObject.SetActive(false);
return;
}
var tools = _p.Tools;
var tool = tools.Current;
if (tool != _shown)
{
_switch -= Time.deltaTime * 9f;
if (_switch <= 0f || _shown == (ToolId)255)
{
foreach (var kv in _rigs) kv.Value.root.gameObject.SetActive(kv.Key == tool);
_shown = tool;
_switch = 0f;
Sfx.Play("plastic_grab", 0.25f, 1.3f);
}
}
else _switch = Mathf.MoveTowards(_switch, 1f, Time.deltaTime * 5f);
bool hide = _p.Grab.Holding || _p.Driving.Value || Hidden || Phone.Focused || TreatHand.Busy;
_hide = Mathf.MoveTowards(_hide, hide ? 1f : 0f, Time.deltaTime * 6f);
if (!_rigs.TryGetValue(_shown, out var rig)) return;
var h = rig.hold;
var look = Ctl.Look;
_sway = Vector2.Lerp(_sway, new Vector2(Mathf.Clamp(look.x, -6f, 6f), Mathf.Clamp(look.y, -6f, 6f)), Time.deltaTime * 10f);
float flat = new Vector2(_p.Velocity.x, _p.Velocity.z).magnitude;
bool swim = _p.IsSwimming;
if (_p.Grounded || swim) _bob += Time.deltaTime * (swim ? 2.2f : flat * 1.6f);
float bobAmt = swim ? 1.4f : Mathf.Clamp01(flat / 6f) * (_p.Grounded ? 1f : 0.3f);
// a figure of eight: down on each step, side to side once a stride
var bobOff = new Vector3(Mathf.Cos(_bob) * 0.012f, -Mathf.Abs(Mathf.Sin(_bob)) * 0.016f + 0.008f, Mathf.Sin(_bob * 2f) * 0.003f) * bobAmt;
float breathe = Mathf.Sin(Time.time * 1.6f) * 0.004f;
// the tool drops and springs back when the feet hit the ground
if (_p.Grounded && !_grounded) _land = Mathf.Clamp01(_fall / 8f);
_grounded = _p.Grounded;
_fall = _p.Grounded ? 0f : Mathf.Max(_fall, -Vector3.Dot(_p.Velocity, Planet.Up(_p.transform.position)));
_land = Mathf.MoveTowards(_land, 0f, Time.deltaTime * 3.5f);
float dip = Mathf.Sin(_land * Mathf.PI) * _land;
// left alone a while, the player turns the tool over to look at it
_still = flat < 0.3f && !tools.IsUsing && Ctl.Look.sqrMagnitude < 0.01f ? _still + Time.deltaTime : 0f;
float admire = Mathf.SmoothStep(0f, 1f, Mathf.Clamp01((_still - 5f) / 1.5f));
// settle into the working pose and back out of it
_use = Mathf.MoveTowards(_use, tools.IsUsing ? 1f : 0f, Time.deltaTime * 6f);
float use = Mathf.SmoothStep(0f, 1f, _use);
// at a run the tool is carried lower and tucked in
float run = Mathf.Clamp01((flat - 4.5f) / 2.5f) * (1f - use);
Vector3 pos = Vector3.Lerp(h.pos, h.usePos, use) + bobOff + new Vector3(-_sway.x * 0.004f, -_sway.y * 0.004f + breathe, 0f) + new Vector3(0.02f, -0.03f, -0.03f) * run;
Vector3 eul = Vector3.Lerp(h.euler, h.useEuler, use) + new Vector3(_sway.y * 0.6f, _sway.x * 0.8f, -_sway.x * 0.6f) + new Vector3(6f, -6f, 4f) * run;
if (swim) { pos += new Vector3(0f, 0.06f, -0.04f); eul += new Vector3(-10f, 0f, Mathf.Sin(Time.time * 1.3f) * 4f); }
pos += new Vector3(0f, -0.05f, 0.01f) * dip;
eul += new Vector3(7f, 0f, 0f) * dip;
pos += new Vector3(-0.05f, 0.05f, -0.03f) * admire;
eul += new Vector3(-12f + Mathf.Sin(_still * 0.7f) * 4f, -22f, 28f + Mathf.Sin(_still * 0.45f) * 10f) * admire;
float phase = Mathf.Repeat(tools.Stroke, 1f);
if (h.motion == Motion.Snip)
{
float open = Mathf.Lerp(RestOpen, Snip(phase), use);
if (rig.bladeA && rig.bladeB) Open(rig.bladeA, rig.bladeB, open);
// the fist squeezes and bites forward, the wrist turns into each cut, and the tool works along the edge of the grass
float shut = (1f - open) * use;
pos += new Vector3(0f, -0.008f, 0.02f) * shut;
eul += new Vector3(4f * shut, Mathf.Sin(tools.Stroke * 0.4f * Mathf.PI) * 7f * use, -6f * shut);
}
var rot = Quaternion.Euler(eul);
if (h.motion == Motion.Sweep)
{
float s = Sweep(phase) * use, back = Return(phase) * use;
var turn = Quaternion.AngleAxis(s * h.arc + h.middle * use, Vector3.up);
// lifted at both ends of the stroke and clear of the grass on the way back, low and pushed forward through
// the middle, rolling over with the wrists
float through = (1f - s * s) * (1f - back) * use;
pos = h.pivot + turn * (pos - h.pivot) + new Vector3(0f, (s * s - 0.4f) * 0.03f * use + back * 0.035f, through * 0.03f);
rot = turn * rot * Quaternion.Euler(s * s * -4f - back * 8f, 0f, s * 7f + through * -3f);
}
if (h.motion == Motion.Strike)
{
// raised high, down hard, a jolt as it lands, and back up at leisure
_strike += Time.deltaTime;
float a = _strike < 0.06f ? Mathf.Lerp(0f, -26f, Easing.Apply(Ease.OutQuad, _strike / 0.06f))
: _strike < 0.12f ? Mathf.Lerp(-26f, 50f, Mathf.Pow((_strike - 0.06f) / 0.06f, 2f))
: _strike < 0.2f ? 50f + Mathf.Sin((_strike - 0.12f) / 0.08f * Mathf.PI * 3f) * 4f * (1f - (_strike - 0.12f) / 0.08f)
: Mathf.Lerp(50f, 0f, Mathf.SmoothStep(0f, 1f, (_strike - 0.2f) / 0.36f));
rot = Quaternion.AngleAxis(a, Vector3.right) * rot;
pos += new Vector3(0f, -0.04f, 0.06f) * (Mathf.Max(0f, a) / 50f) + new Vector3(0f, 0.03f, -0.03f) * (Mathf.Max(0f, -a) / 26f);
}
// a new tool comes up from below with a turn of the wrist, and settles with a little overshoot
float low = 1f - Easing.Apply(Ease.OutBack, _switch);
float away = Mathf.Max(low, _hide);
pos += new Vector3(0.04f * low, -0.35f * away, 0f);
rot = Quaternion.Euler(25f * away, -10f * low, -35f * low) * rot;
rig.root.localPosition = pos + Shift;
rig.root.localRotation = rot;
rig.root.gameObject.SetActive(_hide < 0.99f);
}
}
}