using System.Collections; using System.Collections.Generic; using UnityEngine; namespace GrassCutter { /// /// 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. /// 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 All = new List(); 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 => Loc.F("{0}: 1 kg of grass from your tool. {1}, {2} for 2½ minutes", Treats.Name(kind), Treats.Boost(kind), Treats.Effect(kind)); public bool CanInteract => TeamState.Instance && !_busy && !TreatHand.Busy; void OnEnable() => All.Add(this); void OnDisable() => All.Remove(this); void Start() { _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; } var from = me.Tools.Nozzle; me.Tools.Spend(Treats.CostKg); me.TreatRpc((byte)kind, transform.position, from); Play(true, from); } /// Run the machine's show: for the buyer it ends with the treat in their hand, for everyone else it's just the show. 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); 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()) 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); } } }