using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UIElements; namespace GrassCutter { /// /// The Director's assistant, in your ear the whole game: she says the hints out loud (what to do next, the weather /// coming, a twister, the saucer, a full container, no money) from a little video window at the side of the view, /// talking the same South Park way he does. She waits while he's on the phone or a film is playing, says nothing stale, /// and doesn't nag: each line has its own rest before it may come again. /// public class Assistant : MonoBehaviour { public static Assistant I { get; private set; } /// She's talking. public static bool Busy => I && I._on; // the words as recorded; the caption is the same with the tags taken out static readonly Dictionary Lines = new Dictionary { ["asst_hello"] = "[deadpan] Hi. I'm the Director's assistant. I'll be in your ear the whole time. [pause] He shouts. I explain.", ["asst_tools"] = "Your hands are empty. Walk to the supply wall on the west side of the deck and take the nail scissors. Press E.", ["asst_mow"] = "Hop down to the meadow, space at the railing. Then hold left click and sweep the blade over the grass. [deadpan] Every blade counts. Apparently.", ["asst_full"] = "Your container's full. Press E at the depot to hand it all in. Or Q to tie a bundle you can carry and throw.", ["asst_spend"] = "You've got cash. Better tools at the supply wall, upgrades at the console. Mowers come from a garage. [pause] Build one with the hammer.", ["asst_nocash"] = "[sighs] Not enough money. Mow more.", ["asst_drive"] = "W A S D to drive. Shift to boost. R for the chase view. E to hop off.", ["asst_hopper"] = "That machine's hopper is full. Unload it at the base yard or a garage pad.", ["asst_rain"] = "Rain's coming. The solar panels will give less for a while.", ["asst_acid"] = "[concerned] Acid rain. It burns patches of the meadow. That grass is gone, and it pays nothing.", ["asst_wind"] = "The wind is picking up. Keep an eye out for twisters.", ["asst_storm"] = "Thunderstorm. Dark skies, weak sun, and twisters about. [pause] Stay sharp.", ["asst_meteors"] = "[softly] Meteor shower tonight. Look up. [pause] It's actually pretty.", ["asst_twister"] = "[alarmed] Twister incoming! It rips up the grass in its path and it will throw you around!", ["asst_thrown"] = "[deadpan] You got thrown by a twister. [pause] Noted.", ["asst_ufo"] = "[nervous] Something is coming down out of the sky. [pause] I'm sure it's fine.", }; // the few lines she may say again, and how long after, seconds. Everything else she explains once, ever: players got // tired of hearing how the depot works every four minutes static readonly Dictionary Rest = new Dictionary { ["asst_twister"] = 600f, ["asst_nocash"] = 300f, }; const string HeardKey = "assistant_heard"; static HashSet s_heard; static bool s_forgot; /// Lines this player has already heard, kept between games. static HashSet Heard { get { if (s_heard == null) { s_heard = new HashSet(); // the test bots run on someone's own PC: they neither read nor fill that player's list if (!CommandLine.Has("-autodemo")) foreach (var k in PlayerPrefs.GetString(HeardKey, "").Split(',')) if (k.Length > 0) s_heard.Add(k); } return s_heard; } } static void MarkHeard(string line) { if (s_forgot || Rest.ContainsKey(line) || !Heard.Add(line) || CommandLine.Has("-autodemo")) return; PlayerPrefs.SetString(HeardKey, string.Join(",", Heard)); PlayerPrefs.Save(); } const float StaleAfter = 25f; const int PicW = 300, PicH = 380; readonly Queue<(string line, float at)> _queue = new Queue<(string, float)>(); readonly Dictionary _said = new Dictionary(); readonly float[] _mood = new float[7]; AudioSource _voice; VisualElement _panel, _frame, _picture; Label _caption; Booth _booth; // the user's filmed loops of her, one per mood TalkVideo _talk; bool _on; float _open, _openVel; void Awake() { I = this; _voice = gameObject.AddComponent(); _voice.playOnAwake = false; _voice.spatialBlend = 0f; _voice.priority = 0; } /// She says this line when she can (unless she said it lately). public static void Say(string line) { if (!I || !Lines.ContainsKey(line) || !GameSettings.Tips) return; if (!s_forgot && !Rest.ContainsKey(line) && Heard.Contains(line)) return; if (I._said.TryGetValue(line, out var at) && Time.time - at < (Rest.TryGetValue(line, out var r) ? r : 1e9f)) return; foreach (var q in I._queue) if (q.line == line) return; // the greeting goes first, whatever was asked before it if (line == "asst_hello" && I._queue.Count > 0) { var rest = I._queue.ToArray(); I._queue.Clear(); I._queue.Enqueue((line, Time.time)); foreach (var q in rest) I._queue.Enqueue(q); return; } I._queue.Enqueue((line, Time.time)); } /// The trailer: she may say anything again. public static void Forget() { if (I) I._said.Clear(); s_forgot = true; } /// Stops her mid-sentence (the game is ending, a film starts). public static void Hush() { if (!I) return; I.StopAllCoroutines(); I._voice.Stop(); I._queue.Clear(); I.Panel(false); I._talk?.Cut(); I._on = false; } void Update() { // she lives on the cinema layer, above the windows: a window opened mid-line hides her till it closes if (_panel != null) _panel.style.visibility = UIRoot.I && UIRoot.I.AnyModal ? Visibility.Hidden : Visibility.Visible; // the frame lights up ahead of the hologram filling it, and goes out after if (_frame != null && _talk != null) _frame.style.opacity = Mathf.Clamp01(_talk.Appear * 3f); if (_on || _queue.Count == 0) return; if (!Ready()) return; var (line, at) = _queue.Dequeue(); // what's no longer news isn't said if (Time.time - at > StaleAfter) return; _said[line] = Time.time; MarkHeard(line); StartCoroutine(Talk(line)); } static bool Ready() { var me = PlayerAgent.Local; return me && me.IsSpawned && IntroCinematic.Settled && !IntroCinematic.Running && !Ctl.Cinematic && !Ctl.UIOpen && !DirectorCall.Busy && UIRoot.I; } IEnumerator Talk(string line) { _on = true; Panel(true); var clip = AssetBank.I.Clip(line); var words = DirectorCall.Captions(Lines[line]); var moods = DirectorCall.Moods(Lines[line]); var env = clip ? DirectorCall.Envelope(clip) : null; Sfx.Play("call_connect", 0.25f, 1.4f, 0f); yield return new WaitForSeconds(0.25f); if (clip) { _voice.clip = clip; _voice.volume = DirectorCall.VoiceGain(env); _voice.Play(); Sfx.Heard?.Invoke(clip.name, 1f, 1f, null); } float length = clip ? clip.length : 3f, t = 0f; for (int i = 0; i < _mood.Length; i++) _mood[i] = 0f; while (t < length) { // the Director cuts in: she stops if (DirectorCall.Busy || Ctl.Cinematic) break; Sfx.Duck(0.6f); _voice.volume = DirectorCall.VoiceGain(env); float said = 0f, target = 0f; if (env != null && env.level.Length > 0) { int f = Mathf.Clamp((int)(t * DirectorCall.EnvRate), 0, env.level.Length - 1); target = env.level[f]; said = env.said[f]; } _open = Mathf.SmoothDamp(_open, target, ref _openVel, target > _open ? 0.035f : 0.07f); var mood = DirectorCall.MoodAt(moods, said); float dt = Time.deltaTime; for (int i = 0; i < _mood.Length; i++) _mood[i] = Mathf.MoveTowards(_mood[i], i == (int)mood ? 1f : 0f, dt * 3f); _booth?.Puppet?.Tick(t, _open, _mood); _talk?.Play(TalkVideo.AssistantMood(TalkVideo.Strongest(_mood))); if (_caption != null) _caption.text = DirectorCall.CaptionAt(words, said); t += dt; yield return null; } _voice.Stop(); Sfx.Duck(1f); // a breath with the mouth shut before the window goes float shut = 0f; while (shut < 0.5f) { _open = Mathf.SmoothDamp(_open, 0f, ref _openVel, 0.07f); _booth?.Puppet?.Tick(t + shut, _open, _mood); shut += Time.deltaTime; yield return null; } Panel(false); _on = false; } /// The video window: her picture, her name, and what she's saying beside it. void Panel(bool on) { if (on && (_panel == null || _panel.panel == null)) Build(); if (_panel == null) return; _booth?.Show(on); // the picture comes in and goes out as a hologram; the panel goes once it has (TalkVideo.Gone) _talk?.Show(on); _caption.style.display = on ? DisplayStyle.Flex : DisplayStyle.None; if (on) { _panel.style.display = DisplayStyle.Flex; _panel.BringToFront(); UIK.PopIn(_caption, 0.2f, 0.25f); } else if (_talk == null) _panel.style.display = DisplayStyle.None; } void Build() { var root = UIRoot.I ? UIRoot.I.Cinema : null; if (root == null) return; // the window inside its 3 px border, at 1.5x int outW = Mathf.RoundToInt((PicW * 0.8f - 6f) * 1.5f), outH = Mathf.RoundToInt((PicH * 0.8f - 6f) * 1.5f); _talk = new TalkVideo("asst", new[] { "happy", "stern", "fed" }, 400, 406, outW, outH, new Rect(0f, 0f, 1f, 1f), "T_Talk_AsstBg"); _talk.Gone = () => { if (_panel != null) _panel.style.display = DisplayStyle.None; }; _panel = new VisualElement { pickingMode = PickingMode.Ignore }; _panel.style.position = Position.Absolute; _panel.style.left = 22; _panel.style.bottom = 235; _panel.style.flexDirection = FlexDirection.Row; _panel.style.alignItems = Align.FlexEnd; root.Add(_panel); var frame = new VisualElement { pickingMode = PickingMode.Ignore }; frame.style.width = PicW * 0.8f; frame.style.height = PicH * 0.8f; frame.style.borderTopLeftRadius = frame.style.borderTopRightRadius = frame.style.borderBottomLeftRadius = frame.style.borderBottomRightRadius = 14; frame.style.borderLeftWidth = frame.style.borderRightWidth = frame.style.borderTopWidth = frame.style.borderBottomWidth = 3; var edge = new Color(0.55f, 0.85f, 0.95f, 0.9f); frame.style.borderLeftColor = frame.style.borderRightColor = frame.style.borderTopColor = frame.style.borderBottomColor = edge; frame.style.overflow = Overflow.Hidden; // see-through glass: the room behind her is in the picture, which the hologram brings in frame.style.backgroundColor = new Color(0.02f, 0.06f, 0.12f, 0.35f); frame.style.opacity = 0f; _frame = frame; _panel.Add(frame); _picture = new VisualElement { pickingMode = PickingMode.Ignore }; _picture.style.flexGrow = 1; _picture.style.unityBackgroundScaleMode = ScaleMode.StretchToFill; _picture.style.backgroundImage = new StyleBackground(Background.FromRenderTexture(_talk.Rt)); frame.Add(_picture); var tag = UIK.Text(Loc.T("ASSISTANT ยท G.R.A.S.S.A."), "", frame, true); tag.style.position = Position.Absolute; tag.style.left = 0; tag.style.right = 0; tag.style.bottom = 0; tag.style.fontSize = 11; tag.style.unityTextAlign = TextAnchor.MiddleCenter; tag.style.color = new Color(0.86f, 0.95f, 1f); tag.style.backgroundColor = new Color(0.02f, 0.05f, 0.12f, 0.75f); tag.style.paddingTop = tag.style.paddingBottom = 3; _caption = UIK.Text("", "", _panel, false); _caption.style.maxWidth = 420; _caption.style.marginLeft = 10; _caption.style.marginBottom = 8; _caption.style.fontSize = 19; _caption.style.whiteSpace = WhiteSpace.Normal; _caption.style.color = Color.white; _caption.style.backgroundColor = new Color(0.02f, 0.05f, 0.12f, 0.78f); _caption.style.paddingLeft = _caption.style.paddingRight = 12; _caption.style.paddingTop = _caption.style.paddingBottom = 8; _caption.style.borderTopLeftRadius = _caption.style.borderTopRightRadius = _caption.style.borderBottomRightRadius = 12; _panel.style.display = DisplayStyle.None; } } }