using UnityEngine; namespace GrassCutter { public enum PhoneMode : byte { Away, Side, Up } /// /// The phone in your left hand. V takes it out to the side of the view, where a page keeps playing while you mow; X brings /// it up to your face and frees the cursor to tap on it; Z turns it on its side. It moves on a spring, so every change of /// pose is a swing of the arm rather than a slide. /// public class Phone : MonoBehaviour { // the lit part of the front, which is what the screen's texture covers; keep in step with gc_phone.py public const float ScreenW = 0.094f, ScreenH = 0.206f, ScreenZ = -0.0046f; public static Phone I { get; private set; } public static bool IsOut => I && I._mode != PhoneMode.Away; public static bool Focused => I && I._mode == PhoneMode.Up; /// The wheel belongs to the phone while there's a page on it, rather than to the tool belt. public static bool TakesWheel => IsOut && I.Screen != null && I.Screen.ShowingPage; public PhoneMode Mode => _mode; public PhoneScreen Screen { get; private set; } PlayerAgent _p; PhoneMode _mode; Transform _pivot; Renderer _body; int _glowSlot = -1; MaterialPropertyBlock _glow; Renderer _screenR; Camera _vm; Vector3 _pos, _vel, _eul, _eulVel; Vector2 _sway; float _bob; bool _wide; // for each mode, upright and then on its side. Upright it stands off to the left, turned to face you; on its side // it's a little picture up in the corner static readonly Vector3[,] Poses = { { new Vector3(-0.2f, -0.6f, 0.32f), new Vector3(-0.2f, -0.6f, 0.32f) }, { new Vector3(-0.215f, -0.012f, 0.31f), new Vector3(-0.165f, 0.075f, 0.335f) }, { new Vector3(0f, 0f, 0.207f), new Vector3(0f, 0f, 0.122f) }, }; static readonly Vector3[,] Angles = { { new Vector3(58f, 26f, -24f), new Vector3(58f, 26f, 40f) }, { new Vector3(4f, -27f, 4f), new Vector3(4f, -19f, 90f) }, { Vector3.zero, new Vector3(0f, 0f, 90f) }, }; void Awake() { I = this; _vm = GetComponentInParent(true); _pivot = new GameObject("Phone").transform; _pivot.SetParent(transform, false); var bank = AssetBank.I; var phone = Instantiate(bank.Model("SM_Phone"), _pivot); phone.name = "PhoneBody"; foreach (var r in phone.GetComponentsInChildren(true)) { if (r.name == "Screen") { _screenR = r; r.sharedMaterial = bank.Mat("M_PhoneScreen"); } else _body = r; } if (_body) { var mats = _body.sharedMaterials; for (int i = 0; i < mats.Length; i++) if (mats[i] && mats[i].name.StartsWith("M_PhoneGlow")) _glowSlot = i; _glow = new MaterialPropertyBlock(); } foreach (var t in _pivot.GetComponentsInChildren(true)) { t.gameObject.layer = Layers.ViewModel; var r = t.GetComponent(); if (r) { r.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; r.receiveShadows = false; } } _pos = Poses[0, 0]; _eul = Angles[0, 0]; _pivot.gameObject.SetActive(false); } public void Bind(PlayerAgent p) { _p = p; } /// The match is over: the phone goes away, and the browser with it. public void Unbind() { Show(PhoneMode.Away); Screen?.Close(); _p = null; Ctl.PhoneFocus = false; Ctl.Typing = false; } public void Show(PhoneMode m) { if (m == _mode) return; if (Screen == null) { Screen = new PhoneScreen(gameObject); if (_screenR) _screenR.material.SetTexture("_BaseMap", Screen.Tex); } bool wasAway = _mode == PhoneMode.Away; _mode = m; Ctl.PhoneFocus = m == PhoneMode.Up; if (m == PhoneMode.Away || wasAway) Sfx.Play("plastic_grab", 0.35f, m == PhoneMode.Away ? 0.8f : 1.15f); else Sfx.Play("plastic_grab", 0.2f, 1.3f); Screen.ModeChanged(m); } /// The screen went on its side or back, by key or by the button on it. public void Turned(bool wide) { _wide = wide; } /// Esc: first the keyboard, then the phone comes down from your face. False if it had nothing to do with the phone. public static bool Back() { if (!Focused) return false; if (I.Screen.Typing) I.Screen.CancelTyping(); else I.Show(PhoneMode.Side); return true; } void Update() { if (!_p || !_p.IsSpawned) return; if (_p.PhoneOut.Value != IsOut) _p.PhoneOut.Value = IsOut; Screen?.Tick(); if (Ctl.PhonePressed) Show(_mode == PhoneMode.Away ? PhoneMode.Side : PhoneMode.Away); if (_mode == PhoneMode.Away) return; if (Ctl.PhoneUpPressed) Show(_mode == PhoneMode.Up ? PhoneMode.Side : PhoneMode.Up); if (Ctl.PhoneTurnPressed) Screen.Turn(); float w = Ctl.PhoneWheel; if (w != 0f) Screen.Wheel(w < 0f ? 1 : -1); if (_mode == PhoneMode.Up) { var uv = ScreenToPhone(Ctl.Pointer); Screen.Hover(uv); Screen.Pointer(uv, Ctl.PointerDown, Ctl.PointerHeld, Ctl.PointerUp); } } /// Where a screen point lands on the display, 0..1 (x right, y up), or negative if it misses. public Vector2 ScreenToPhone(Vector2 mouse) { if (!_vm) return new Vector2(-1f, -1f); var ray = _vm.ScreenPointToRay(mouse); var plane = new Plane(_pivot.forward, _pivot.TransformPoint(new Vector3(0f, 0f, ScreenZ))); if (!plane.Raycast(ray, out float d)) return new Vector2(-1f, -1f); var lp = _pivot.InverseTransformPoint(ray.GetPoint(d)); return new Vector2(lp.x / ScreenW + 0.5f, lp.y / ScreenH + 0.5f); } 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; } void LateUpdate() { float dt = Mathf.Min(Time.unscaledDeltaTime, 0.05f); int mode = (int)_mode, side = _wide ? 1 : 0; bool away = ViewModel.Hidden || _mode == PhoneMode.Away && (_pos - Poses[0, side]).sqrMagnitude < 0.002f; if (away) { if (_pivot.gameObject.activeSelf) _pivot.gameObject.SetActive(false); return; } if (!_pivot.gameObject.activeSelf) _pivot.gameObject.SetActive(true); // about 15% overshoot: alive, but it never wobbles in front of your eyes var tp = Poses[mode, side]; var ta = Angles[mode, side]; float stiff = _mode == PhoneMode.Away ? 70f : 95f, damp = _mode == PhoneMode.Away ? 15f : 15.5f; // fixed little steps, so a hitch can't throw the spring 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, stiff, damp, h); Spring(ref _pos.y, ref _vel.y, tp.y, stiff, damp, h); Spring(ref _pos.z, ref _vel.z, tp.z, stiff, damp, h); Spring(ref _eul.x, ref _eulVel.x, ta.x, stiff * 0.8f, damp, h); Spring(ref _eul.y, ref _eulVel.y, ta.y, stiff * 0.8f, damp, h); Spring(ref _eul.z, ref _eulVel.z, ta.z, stiff * 0.8f, damp, h); } var look = Ctl.Look; float settled = _mode == PhoneMode.Up ? 0.3f : 1f; _sway = Vector2.Lerp(_sway, new Vector2(Mathf.Clamp(look.x, -6f, 6f), Mathf.Clamp(look.y, -6f, 6f)), dt * 9f); float flat = _p ? new Vector2(_p.Velocity.x, _p.Velocity.z).magnitude : 0f; if (_p && _p.Grounded) _bob += dt * flat * 1.6f; float amt = Mathf.Clamp01(flat / 6f) * settled; var bob = new Vector3(Mathf.Cos(_bob) * 0.009f, Mathf.Abs(Mathf.Sin(_bob)) * 0.012f, 0f) * amt; var breathe = new Vector3(0f, Mathf.Sin(Time.unscaledTime * 1.5f) * 0.0022f, 0f); var sway = new Vector3(-_sway.x * 0.0032f, -_sway.y * 0.0032f, 0f) * settled; _pivot.localPosition = _pos + bob + breathe + sway; _pivot.localRotation = Quaternion.Euler(_eul + new Vector3(_sway.y * 0.5f, _sway.x * 0.7f, -_sway.x * 0.5f) * settled); // the light in the rim breathes, and runs quicker while the relay is fetching a page if (_glowSlot >= 0) { bool busy = Screen != null && Screen.Web.Loading; float k = busy ? 0.6f + 0.6f * Mathf.Abs(Mathf.Sin(Time.unscaledTime * 5f)) : 1f + 0.18f * Mathf.Sin(Time.unscaledTime * 1.4f); _glow.SetColor("_EmissionColor", new Color(0.36f, 1f, 0.7f) * (1.9f * k)); _body.SetPropertyBlock(_glow, _glowSlot); } } } }