using UnityEngine;
namespace GrassCutter
{
///
/// Your right hand with the treat in it, in first person. A can of Grass Beer comes up, the tab cracks with a fizz, three
/// long gulps, "ahh", the boost lands in a burst of golden bubbles, and the can is crushed and lobbed over your shoulder.
/// A Herb Cigar comes up, the lighter flicks, the tip glows on a long draw, a cloud and three smoke rings roll out ahead
/// of you as the boost lands, and the stub is flicked away trailing sparks. While a boost lasts it shows: a puff of smoke
/// now and then, the odd hiccup. Everything moves on springs, like the phone, so each change of pose is a swing of the arm.
///
public class TreatHand : MonoBehaviour
{
public static TreatHand I { get; private set; }
/// A treat is in your hand: the tool is put away and the machines wait.
public static bool Busy => I && I._playing;
// the models face away from you in the file: turned round, the label and the fingers face the eye
static readonly Quaternion Flip = Quaternion.Euler(0f, 180f, 0f);
static readonly Vector3 Below = new Vector3(0.24f, -0.62f, 0.3f), BelowEuler = new Vector3(40f, -20f, 30f);
class Rig
{
public Transform pivot, item, tab, ember;
public Renderer emberR;
public Vector3 itemScale;
public Quaternion tabRest;
}
Rig _beer, _cigar;
PlayerAgent _p;
bool _playing;
TreatId _kind;
float _t;
int _step;
Vector3 _pos, _vel, _eul, _eulVel;
Vector3 _tp, _te;
float _glow, _puffDue = 4f, _hicDue = 10f;
MaterialPropertyBlock _mpb;
static readonly int EmissionId = Shader.PropertyToID("_EmissionColor");
void Awake()
{
I = this;
_mpb = new MaterialPropertyBlock();
_beer = Build("SM_HandCan", "Can", "Tab", null);
_cigar = Build("SM_HandCigar", "Cigar", null, "Ember");
Treats.Clear();
}
Rig Build(string model, string itemName, string tabName, string emberName)
{
var r = new Rig { pivot = new GameObject(model).transform };
r.pivot.SetParent(transform, false);
var src = AssetBank.I ? AssetBank.I.Model(model) : null;
GameObject go;
if (src)
{
go = Instantiate(src, r.pivot);
DayNight.KeepOutOfLamp(go);
go.transform.localPosition = Vector3.zero;
go.transform.localRotation = Flip * src.transform.localRotation;
}
else
{
go = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
Destroy(go.GetComponent());
go.transform.SetParent(r.pivot, false);
go.transform.localScale = itemName == "Can" ? new Vector3(0.066f, 0.06f, 0.066f) : new Vector3(0.022f, 0.07f, 0.022f);
if (itemName == "Cigar") go.transform.localRotation = Quaternion.Euler(0f, 0f, 90f);
}
foreach (var t in go.GetComponentsInChildren(true))
{
t.gameObject.layer = Layers.ViewModel;
// first person has no hands: the can and the cigar float in view like the tools do
if (t.name == "Hand") { t.gameObject.SetActive(false); continue; }
if (t.name == itemName) r.item = t;
else if (tabName != null && t.name == tabName) r.tab = t;
else if (emberName != null && t.name == emberName) r.ember = t;
var rr = t.GetComponent();
if (rr) { rr.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; rr.receiveShadows = false; }
var c = t.GetComponent();
if (c) Destroy(c);
}
if (!r.item) r.item = go.transform;
r.itemScale = r.item.localScale;
if (r.tab) r.tabRest = r.tab.localRotation;
if (r.ember) r.emberR = r.ember.GetComponent();
r.pivot.gameObject.SetActive(false);
return r;
}
public void Bind(PlayerAgent p)
{
if (p != _p) { Treats.Clear(); Stop(); }
_p = p;
}
void Stop()
{
_playing = false;
if (_beer != null) _beer.pivot.gameObject.SetActive(false);
if (_cigar != null) _cigar.pivot.gameObject.SetActive(false);
}
/// The treat has flown into your hand: play it, and the boost lands at the high point.
public void Play(TreatId kind)
{
Stop();
_kind = kind;
_playing = true;
_t = 0f;
_step = 0;
_glow = 0f;
var r = Cur;
r.item.localScale = r.itemScale;
if (r.tab) r.tab.localRotation = r.tabRest;
r.item.gameObject.SetActive(true);
r.pivot.gameObject.SetActive(true);
_pos = Below; _eul = BelowEuler; _vel = _eulVel = Vector3.zero;
Sfx.Play("plastic_grab", 0.35f, 1.2f);
}
Rig Cur => _kind == TreatId.Beer ? _beer : _cigar;
Camera Cam => CameraFx.Cam;
/// Where a point of the view model is in the world, near enough for particles and thrown litter.
Vector3 World(Transform t)
{
var cam = Cam;
if (!cam || !t) return transform.position;
var local = transform.InverseTransformPoint(t.position);
return cam.transform.TransformPoint(local);
}
Vector3 Mouth
{
get
{
var cam = Cam;
return cam ? cam.transform.position + cam.transform.forward * 0.22f - cam.transform.up * 0.07f : transform.position;
}
}
bool At(float time)
{
if (_t < time || _step > Mathf.RoundToInt(time * 100f)) return false;
_step = Mathf.RoundToInt(time * 100f) + 1;
return true;
}
void Update()
{
// the view model may be bound before this hand exists: then it takes the local player itself
if (!_p && PlayerAgent.Local) Bind(PlayerAgent.Local);
if (!_p || !_p.IsSpawned) { if (_playing) Stop(); return; }
if (_playing) { _t += Time.deltaTime; if (_kind == TreatId.Beer) Beer(); else Cigar(); }
else Lingering();
}
void Beer()
{
var r = _beer;
var cam = Cam;
if (_t < 0.42f) { _tp = new Vector3(0.17f, -0.15f, 0.33f); _te = new Vector3(-6f, -12f, 6f); }
if (At(0.42f))
{
Sfx.Play("can_open", 0.9f);
if (r.tab) Tween.Run(r.tab, 0.12f, k => { if (r.tab) r.tab.localRotation = r.tabRest * Quaternion.Euler(-75f * k, 0f, 0f); }, Ease.OutBack);
Fx.Fizz(World(r.item) + (cam ? cam.transform.up * 0.07f : Vector3.zero), cam ? cam.transform.up : Vector3.up);
CameraFx.Shake(0.01f);
Tween.Punch(r.item, 0.08f, 0.25f);
}
if (_t >= 0.75f && _t < 2.3f)
{
// up to the mouth and tipped further back with every gulp
float d = Mathf.Clamp01((_t - 1f) / 1.3f);
_tp = new Vector3(0.075f, -0.085f + d * 0.02f, 0.25f);
_te = new Vector3(-34f - d * 22f, -18f, 22f);
}
for (int g = 0; g < 3; g++)
if (At(1.05f + g * 0.42f))
{
Sfx.Play("drink_glug", 0.75f, 1f + g * 0.05f);
CameraFx.FovKick(-1.2f);
_vel += new Vector3(0f, 0.25f, -0.15f);
}
if (At(2.3f))
{
// the boost lands: ahh, a burst of golden bubbles, a flash and a widening of the eyes
_tp = new Vector3(0.19f, -0.17f, 0.32f); _te = new Vector3(4f, -16f, 10f);
Sfx.Play("drink_ahh", 0.85f);
Sfx.Play("buff_shimmer", 0.7f);
Treats.Begin(TreatId.Beer);
TreatHud.Flash(new Color(1f, 0.82f, 0.3f));
if (cam)
{
var front = cam.transform.position + cam.transform.forward * 0.7f;
Fx.Bubbles(front - cam.transform.up * 0.2f, 34);
Fx.Sparkles(front, 30, new Color(1f, 0.85f, 0.35f));
}
CameraFx.FovKick(7f);
CameraFx.Shake(0.04f);
}
if (At(2.75f))
{
// crushed in the fist
Sfx.Play("can_crush", 0.85f);
CameraFx.Shake(0.02f);
var s = r.itemScale;
Tween.Run(r.item, 0.12f, k => { if (r.item) r.item.localScale = new Vector3(s.x * (1f + 0.18f * k), s.y * (1f - 0.45f * k), s.z * (1f + 0.18f * k)); }, Ease.OutQuad);
_tp = new Vector3(0.21f, -0.12f, 0.3f); _te = new Vector3(-10f, -20f, 18f);
}
if (At(3.05f))
{
// and lobbed over the shoulder
Throw("SM_BeerCanCrushed", World(r.item), false);
r.item.gameObject.SetActive(false);
_tp = Below; _te = BelowEuler;
}
if (_t > 3.5f) Stop();
}
void Cigar()
{
var r = _cigar;
var cam = Cam;
if (_t < 0.45f) { _tp = new Vector3(0.16f, -0.14f, 0.33f); _te = new Vector3(-4f, -8f, -4f); }
if (At(0.45f))
{
// the lighter flicks and the tip catches
Sfx.Play("lighter_flick", 0.85f);
Fx.Embers(World(r.ember ? r.ember : r.item), 16);
Tween.Punch(r.item, 0.06f, 0.2f);
}
if (_t > 0.45f) _glow = Mathf.MoveTowards(_glow, 1f, Time.deltaTime * 3f);
if (_t >= 0.85f && _t < 1.85f)
{
// to the lips, a long draw: the ember flares
_tp = new Vector3(0.05f, -0.07f, 0.14f); _te = new Vector3(-8f, 22f, -12f);
float draw = Mathf.Clamp01((_t - 1f) / 0.8f);
_glow = 1f + draw * 2.2f + Mathf.Sin(_t * 30f) * 0.25f * draw;
if (Random.value < Time.deltaTime * 10f * draw) Fx.Embers(World(r.ember ? r.ember : r.item), 2);
}
if (At(1.0f)) { Sfx.Play("cigar_inhale", 0.8f); CameraFx.FovKick(-2.5f); }
if (At(1.85f))
{
_tp = new Vector3(0.2f, -0.16f, 0.33f); _te = new Vector3(2f, -10f, 4f);
_glow = 1.2f;
}
if (At(2.0f))
{
// the cloud rolls out and the boost lands
Sfx.Play("smoke_exhale", 0.85f);
Sfx.Play("buff_shimmer", 0.6f, 0.9f);
if (cam) Fx.Exhale(Mouth, cam.transform.forward, 1.3f);
Treats.Begin(TreatId.Cigar);
TreatHud.Flash(new Color(1f, 0.6f, 0.28f));
CameraFx.FovKick(6f);
CameraFx.Shake(0.03f);
}
for (int k = 0; k < 3; k++)
if (At(2.45f + k * 0.32f) && cam) Fx.SmokeRing(Mouth + cam.transform.forward * 0.05f, cam.transform.forward + cam.transform.up * 0.08f);
if (At(3.5f))
{
// the stub flicked away, trailing sparks
Throw("SM_Cigar", World(r.item), true);
r.item.gameObject.SetActive(false);
Sfx.Play("throw_whoosh", 0.4f, 1.4f);
_tp = Below; _te = BelowEuler;
}
if (_t > 3.95f) Stop();
}
/// While a boost lasts: a breath of smoke now and then, a hiccup, a few bubbles.
void Lingering()
{
var cam = Cam;
if (!cam) return;
if (Treats.On(TreatId.Cigar))
{
_puffDue -= Time.deltaTime;
if (_puffDue <= 0f)
{
_puffDue = Random.Range(5f, 8f);
Fx.Exhale(Mouth, cam.transform.forward, 0.45f);
if (Random.value < 0.4f) Fx.SmokeRing(Mouth, cam.transform.forward);
}
}
if (Treats.On(TreatId.Beer))
{
_hicDue -= Time.deltaTime;
if (_hicDue <= 0f)
{
_hicDue = Random.Range(10f, 16f);
Sfx.Play("hiccup", 0.6f, Random.Range(0.95f, 1.15f));
CameraFx.FovKick(-1.5f);
CameraFx.Shake(0.015f);
Fx.Bubbles(cam.transform.position + cam.transform.forward * 0.6f - cam.transform.up * 0.1f, 8);
}
}
}
void Throw(string model, Vector3 at, bool ember)
{
var cam = Cam;
var src = AssetBank.I ? AssetBank.I.Model(model) : null;
GameObject go;
if (src) go = Instantiate(src, at, Random.rotation);
else
{
go = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
go.transform.position = at;
go.transform.localScale = ember ? new Vector3(0.02f, 0.05f, 0.02f) : new Vector3(0.07f, 0.035f, 0.07f);
}
foreach (var c in go.GetComponentsInChildren()) Destroy(c);
var col = go.AddComponent();
col.radius = ember ? 0.02f : 0.04f;
var rb = go.AddComponent();
rb.mass = 0.05f;
rb.useGravity = false;
var right = cam ? cam.transform.right : transform.right;
var up = Planet.Up(at);
var fwd = cam ? cam.transform.forward : transform.forward;
rb.linearVelocity = right * (ember ? 2.5f : 2.2f) + up * 3.2f + fwd * (ember ? 2.5f : -0.6f) + (_p ? _p.Velocity : Vector3.zero);
rb.angularVelocity = Random.insideUnitSphere * 18f;
var l = go.AddComponent();
l.ember = ember;
}
void LateUpdate()
{
var r = _playing ? Cur : null;
if (r == null) return;
if (ViewModel.Hidden) { r.pivot.gameObject.SetActive(false); return; }
float dt = Mathf.Min(Time.deltaTime, 0.05f);
int n = Mathf.Clamp(Mathf.CeilToInt(dt / 0.008f), 1, 12);
float h = dt / n;
for (int s = 0; s < n; s++)
{
Spring(ref _pos.x, ref _vel.x, _tp.x, 130f, 16f, h);
Spring(ref _pos.y, ref _vel.y, _tp.y, 130f, 16f, h);
Spring(ref _pos.z, ref _vel.z, _tp.z, 130f, 16f, h);
Spring(ref _eul.x, ref _eulVel.x, _te.x, 110f, 15f, h);
Spring(ref _eul.y, ref _eulVel.y, _te.y, 110f, 15f, h);
Spring(ref _eul.z, ref _eulVel.z, _te.z, 110f, 15f, h);
}
float breathe = Mathf.Sin(Time.time * 1.6f) * 0.003f;
r.pivot.localPosition = _pos + new Vector3(0f, breathe, 0f);
r.pivot.localRotation = Quaternion.Euler(_eul);
if (r.emberR)
{
r.emberR.GetPropertyBlock(_mpb);
_mpb.SetColor(EmissionId, new Color(1f, 0.38f, 0.08f) * (0.2f + _glow * 3.5f));
r.emberR.SetPropertyBlock(_mpb);
}
}
static void Spring(ref float x, ref float v, float target, float k, float c, float dt)
{
v += (k * (target - x) - c * v) * dt;
x += v * dt;
}
}
/// A crushed can or a cigar stub thrown away: it falls toward the planet, bounces with a rattle, and is gone in a while.
public class Litter : MonoBehaviour
{
public bool ember;
Rigidbody _rb;
bool _landed;
float _age;
Vector3 _scale;
void Start()
{
_rb = GetComponent();
_scale = transform.localScale;
}
void OnCollisionEnter(Collision c) => Land();
void Land()
{
if (_landed) return;
_landed = true;
Sfx.PlayAt(ember ? "acid_sizzle" : "can_bounce", transform.position, ember ? 0.25f : 0.6f);
if (ember) { Fx.Embers(transform.position, 10); Fx.Smoke(transform.position, 0.15f); }
else Fx.Dust(transform.position, 0.15f, 4);
}
void FixedUpdate()
{
if (_rb) _rb.AddForce(-Planet.Up(transform.position) * 9.81f, ForceMode.Acceleration);
}
void Update()
{
_age += Time.deltaTime;
if (ember && _age < 2.5f && Random.value < Time.deltaTime * 25f) Fx.Embers(transform.position, 1);
// the ground itself, where there's nothing to bump into on the way down
if (Planet.Altitude(transform.position) < 0.03f)
{
transform.position = Planet.Surface(transform.position) + Planet.Up(transform.position) * 0.03f;
if (_rb) _rb.linearVelocity = Vector3.ProjectOnPlane(_rb.linearVelocity, Planet.Up(transform.position)) * 0.5f;
Land();
}
if (_age > 7f)
{
float k = Mathf.Clamp01(1f - (_age - 7f) / 0.6f);
transform.localScale = _scale * k;
if (k <= 0f) Destroy(gameObject);
}
}
}
}