Files
grass-cutters/Assets/Scripts/Gameplay/VendingMachine.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

320 lines
14 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace GrassCutter
{
/// <summary>
/// The two machines on the deck that take a kilo of grass out of your tool and give you something for it: the green
/// one a can of Grass Beer, the wooden one a Herb Cigar. Everything about it is a show: the grass slurps into the
/// hopper, the sign flares, the machine shudders, the can thumps into the tray (or a knob pulls and the drawer slides
/// out with a ding), and the thing flies up into your hand. The others see the same show at their end.
/// </summary>
public class VendingMachine : MonoBehaviour, IInteractable
{
public TreatId kind;
public Renderer sign, display;
public Transform body, button, tray, drawer, hopper, dropPoint;
public Transform[] knobs;
public static readonly List<VendingMachine> All = new List<VendingMachine>();
bool _busy;
float _flash, _deny, _shudder;
MaterialPropertyBlock _mpb;
Color _signBase, _displayBase;
Vector3 _buttonRest, _drawerRest, _bodyScale, _hopperScale = Vector3.one;
Quaternion _trayRest;
Vector3[] _knobRest;
static readonly int EmissionId = Shader.PropertyToID("_EmissionColor");
static readonly Color BeerGlow = new Color(1f, 0.82f, 0.32f), CigarGlow = new Color(1f, 0.55f, 0.22f), Deny = new Color(1f, 0.15f, 0.1f);
public string Prompt
{
get
{
var line = Loc.F("{0}: 1 kg of grass from your tool. {1}, {2} for 2½ minutes", Treats.Name(kind), Treats.Boost(kind), Treats.Effect(kind));
var me = PlayerAgent.Local;
return me && me.Tools.PackRoom(kind) ? line + "\n" + Loc.F("Hold {0}: a pack of {1} for your belt ({1} kg)", Binds.Cap(Act.Interact), Treats.PackSize) : line;
}
}
public bool CanInteract => TeamState.Instance && !_busy && !TreatHand.Busy;
void OnEnable() => All.Add(this);
void OnDisable() => All.Remove(this);
void Start()
{
// its own lit sign and display: the headlamp on top of them only burnt them out
DayNight.KeepOutOfLamp(gameObject);
_mpb = new MaterialPropertyBlock();
_signBase = kind == TreatId.Beer ? new Color(0.25f, 1f, 0.5f) : CigarGlow;
_displayBase = kind == TreatId.Beer ? new Color(0.45f, 1f, 0.55f) : new Color(1f, 0.72f, 0.4f);
if (button) _buttonRest = button.localPosition;
if (drawer) _drawerRest = drawer.localPosition;
if (tray) _trayRest = tray.localRotation;
if (body) _bodyScale = body.localScale;
if (hopper) _hopperScale = hopper.localScale;
if (knobs != null)
{
_knobRest = new Vector3[knobs.Length];
for (int i = 0; i < knobs.Length; i++) if (knobs[i]) _knobRest[i] = knobs[i].localPosition;
}
}
public void Interact()
{
var me = PlayerAgent.Local;
if (!me || _busy) return;
if (me.Tools.Carried < Treats.CostKg - 0.001f)
{
_deny = 1f;
Sfx.PlayAt("error", transform.position, 0.7f);
Hud.Toast(Loc.T("Bring 1 kg of grass in your tool: mow a little first"), Hud.ToastKind.Warn);
return;
}
StartCoroutine(Choose(me));
}
/// <summary>A tap has it now; held a moment, it's a pack for the belt (if there's room and grass enough).</summary>
IEnumerator Choose(PlayerAgent me)
{
_busy = true;
float held = 0f;
while (Ctl.InteractHeld && held < 0.4f) { held += Time.deltaTime; yield return null; }
_busy = false;
var from = me.Tools.Nozzle;
if (held >= 0.4f && me.Tools.PackRoom(kind))
{
float cost = Treats.CostKg * Treats.PackSize;
if (me.Tools.Carried < cost - 0.001f)
{
_deny = 1f;
Sfx.PlayAt("error", transform.position, 0.7f);
Hud.Toast(Loc.F("A pack takes {0} kg of grass from your tools", Treats.PackSize), Hud.ToastKind.Warn);
yield break;
}
me.Tools.Spend(cost);
me.TreatRpc((byte)kind, transform.position, from);
_pack = true;
Play(true, from);
yield break;
}
me.Tools.Spend(Treats.CostKg);
me.TreatRpc((byte)kind, transform.position, from);
Play(true, from);
}
bool _pack;
/// <summary>Run the machine's show: for the buyer it ends with the treat in their hand, for everyone else it's just the show.</summary>
public void Play(bool mine, Vector3 from)
{
if (_busy) return;
StartCoroutine(Vend(mine, from));
}
public static VendingMachine Nearest(Vector3 at)
{
VendingMachine best = null;
float bd = 25f;
foreach (var m in All)
{
float d = Vector3.Distance(m.transform.position, at);
if (d < bd) { bd = d; best = m; }
}
return best;
}
Vector3 Out => transform.forward;
Vector3 Up => transform.up;
Vector3 Drop => dropPoint ? dropPoint.position : transform.position + Out * 0.35f + Up * 0.25f;
Vector3 Mouth => hopper ? hopper.position : transform.position + Out * 0.45f + Up * 1.4f;
IEnumerator Vend(bool mine, Vector3 from)
{
_busy = true;
bool beer = kind == TreatId.Beer;
// the grass goes in: a green stream from the tool to the hopper, and the hopper gulps it
Sfx.PlayAt("vend_hopper", Mouth, 0.9f);
for (float t = 0f; t < 0.45f; t += Time.deltaTime)
{
Fx.Stream(from, Mouth, 1f);
if (hopper) hopper.localScale = _hopperScale * (1f + Mathf.Sin(t * 30f) * 0.06f);
yield return null;
}
if (hopper) { hopper.localScale = _hopperScale; Tween.Punch(hopper, 0.25f, 0.35f); }
Fx.Sparkles(Mouth, 14, new Color(0.55f, 0.9f, 0.3f));
// pressed: the button goes in, the sign flares
_flash = 1f;
if (button) StartCoroutine(Press(button, _buttonRest, -0.03f, 0.18f));
if (!beer && knobs != null && knobs.Length > 0)
{
int k = Random.Range(0, knobs.Length);
if (knobs[k]) StartCoroutine(Press(knobs[k], _knobRest[k], 0.07f, 0.45f));
Sfx.PlayAt("knob_pull", transform.position + Up * 1f, 0.9f);
}
yield return new WaitForSeconds(0.2f);
Transform item;
if (beer)
{
// the machine thinks about it: a whirr and a shudder, then the can falls into the tray
Sfx.PlayAt("vend_whirr", transform.position + Up, 0.9f);
_shudder = 1f;
yield return new WaitForSeconds(0.75f);
item = Spawn("SM_BeerCan", Drop + Up * 0.45f, Quaternion.LookRotation(Out, Up) * Quaternion.Euler(90f, 0f, 0f));
for (float t = 0f; t < 0.2f; t += Time.deltaTime)
{
if (item) item.position = Vector3.Lerp(Drop + Up * 0.45f, Drop, t * t / 0.04f);
yield return null;
}
if (item) { item.position = Drop; Tween.Punch(item, 0.3f, 0.3f); }
if (tray) StartCoroutine(Flap(tray, _trayRest));
Fx.Steam(Drop + Up * 0.1f, 0.25f);
Fx.Sparkles(Drop + Up * 0.1f, 18, new Color(1f, 0.85f, 0.35f));
if (mine) CameraFx.Shake(0.03f);
}
else
{
// the drawer slides out with a ding, a wisp of smoke, and there's your cigar
Sfx.PlayAt("drawer_bell", transform.position + Up * 0.8f, 0.9f);
if (drawer) StartCoroutine(Slide(drawer, _drawerRest, 0.2f, 0.3f));
yield return new WaitForSeconds(0.32f);
item = Spawn("SM_Cigar", Drop, Quaternion.LookRotation(transform.right, Up));
if (item) Tween.Punch(item, 0.3f, 0.35f);
Fx.Smoke(Drop + Up * 0.2f, 0.25f);
Fx.Sparkles(Drop + Up * 0.1f, 18, new Color(1f, 0.6f, 0.25f));
}
yield return new WaitForSeconds(0.35f);
// it flies up into your hand (or just pops away for the people watching)
var cam = CameraFx.Cam;
if (item && mine && cam)
{
var start = item.position;
var spin = Random.rotation;
for (float t = 0f; t < 0.32f; t += Time.deltaTime)
{
float k = Easing.Apply(Ease.InQuad, t / 0.32f);
var to = cam.transform.position + cam.transform.forward * 0.35f - cam.transform.up * 0.15f;
item.position = Vector3.Lerp(start, to, k) + Up * Mathf.Sin(k * Mathf.PI) * 0.6f;
item.rotation = Quaternion.Slerp(item.rotation, spin, Time.deltaTime * 8f);
yield return null;
}
Destroy(item.gameObject);
if (_pack)
{
_pack = false;
var me = PlayerAgent.Local;
if (me && me.Tools.AddPack(kind, Treats.PackSize))
{
Sfx.Play("plastic_grab", 0.6f, 0.9f);
Hud.Toast(Loc.F("{0} ×{1} on your belt: pick the slot and click to have one", Treats.Name(kind), me.Tools.PackLeft(kind)), Hud.ToastKind.Info);
}
else TreatHand.I?.Play(kind);
}
else TreatHand.I?.Play(kind);
}
else if (item)
{
Fx.Sparkles(item.position, 10, Color.white);
Destroy(item.gameObject, 0.05f);
}
if (!beer && drawer) StartCoroutine(Slide(drawer, _drawerRest, 0f, 0.25f, 0.2f));
yield return new WaitForSeconds(0.3f);
_busy = false;
}
Transform Spawn(string model, Vector3 at, Quaternion rot)
{
var src = AssetBank.I ? AssetBank.I.Model(model) : null;
GameObject go;
if (src) go = Instantiate(src, at, rot * src.transform.localRotation);
else
{
go = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
go.transform.SetPositionAndRotation(at, rot);
go.transform.localScale = model == "SM_Cigar" ? new Vector3(0.025f, 0.07f, 0.025f) : new Vector3(0.066f, 0.06f, 0.066f);
}
foreach (var c in go.GetComponentsInChildren<Collider>()) Destroy(c);
return go.transform;
}
IEnumerator Press(Transform t, Vector3 rest, float depth, float hold)
{
var dir = t.parent ? t.parent.InverseTransformDirection(Out) : Out;
for (float k = 0f; k < 1f; k += Time.deltaTime / 0.08f) { t.localPosition = rest + dir * depth * k; yield return null; }
yield return new WaitForSeconds(hold);
for (float k = 1f; k > 0f; k -= Time.deltaTime / 0.25f) { t.localPosition = rest + dir * depth * Easing.Apply(Ease.OutBack, k); yield return null; }
t.localPosition = rest;
}
IEnumerator Slide(Transform t, Vector3 rest, float to, float time, float delay = 0f)
{
if (delay > 0f) yield return new WaitForSeconds(delay);
var dir = t.parent ? t.parent.InverseTransformDirection(Out) : Out;
var from = t.localPosition;
for (float k = 0f; k < 1f; k += Time.deltaTime / time)
{
t.localPosition = Vector3.LerpUnclamped(from, rest + dir * to, Easing.Apply(Ease.OutBack, k));
yield return null;
}
t.localPosition = rest + dir * to;
}
IEnumerator Flap(Transform t, Quaternion rest)
{
for (float k = 0f; k < 1f; k += Time.deltaTime / 0.5f)
{
float a = Mathf.Sin(k * Mathf.PI * 3f) * 28f * (1f - k);
t.localRotation = rest * Quaternion.Euler(a, 0f, 0f);
yield return null;
}
t.localRotation = rest;
}
void Update()
{
float dt = Time.deltaTime;
_flash = Mathf.MoveTowards(_flash, 0f, dt * 1.4f);
_deny = Mathf.MoveTowards(_deny, 0f, dt * 1.5f);
_shudder = Mathf.MoveTowards(_shudder, 0f, dt * 1.3f);
// a neon sign: a slow breath, the odd flicker, and a flare when it's working
float flicker = Random.value < dt * 0.6f ? 0.35f : 1f;
float breath = 1.6f + 0.35f * Mathf.Sin(Time.time * 2.1f + transform.position.x);
var glow = _signBase * (breath * flicker + _flash * 4f);
if (_deny > 0f) glow = Color.Lerp(glow, Deny * 3f, Mathf.Abs(Mathf.Sin(_deny * 18f)));
Paint(sign, glow);
float blink = (Mathf.Repeat(Time.time * (_flash > 0f ? 6f : 1.2f), 1f) < 0.6f ? 1f : 0.4f) * (1.4f + _flash * 2f);
Paint(display, _deny > 0f ? Deny * 2.5f : _displayBase * blink);
if (body)
{
float s = _shudder > 0f ? Mathf.Sin(Time.time * 55f) * 0.012f * _shudder : 0f;
body.localScale = new Vector3(_bodyScale.x * (1f - s * 0.5f), _bodyScale.y * (1f + s), _bodyScale.z * (1f - s * 0.5f));
}
}
void Paint(Renderer r, Color c)
{
if (!r) return;
// only the first slot glows: the beer sign's picture keeps its own colours
if (r.sharedMaterials.Length > 1)
{
r.GetPropertyBlock(_mpb, 0);
_mpb.SetColor(EmissionId, c);
r.SetPropertyBlock(_mpb, 0);
return;
}
r.GetPropertyBlock(_mpb);
_mpb.SetColor(EmissionId, c);
r.SetPropertyBlock(_mpb);
}
}
}