Files
grass-cutters/Assets/Scripts/UI/Assistant.cs
NiemandKG d1c3761e58 Grass Cutters Together: the project as of moon-sweepers f3666f0
A clean start for the shared repository: the Unity project (Assets, Packages, ProjectSettings), ArtSource, Docs and
Tools as they stood at f3666f0 in the old local repository, with its generated prefabs and scene brought up to date.
Images, audio, models and binaries go through Git LFS (.gitattributes).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 18:44:40 +06:00

258 lines
12 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
namespace GrassCutter
{
/// <summary>
/// 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.
/// </summary>
public class Assistant : MonoBehaviour
{
public static Assistant I { get; private set; }
/// <summary>She's talking.</summary>
public static bool Busy => I && I._on;
// the words as recorded; the caption is the same with the tags taken out
static readonly Dictionary<string, string> Lines = new Dictionary<string, string>
{
["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.",
};
// how long before a line may be said again, seconds (the rest: three minutes)
static readonly Dictionary<string, float> Rest = new Dictionary<string, float>
{
["asst_hello"] = 1e9f, ["asst_drive"] = 1e9f, ["asst_nocash"] = 60f, ["asst_thrown"] = 90f, ["asst_full"] = 240f, ["asst_hopper"] = 240f,
};
const float StaleAfter = 25f;
const int PicW = 300, PicH = 380;
readonly Queue<(string line, float at)> _queue = new Queue<(string, float)>();
readonly Dictionary<string, float> _said = new Dictionary<string, float>();
readonly float[] _mood = new float[7];
AudioSource _voice;
VisualElement _panel, _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<AudioSource>();
_voice.playOnAwake = false;
_voice.spatialBlend = 0f;
_voice.priority = 0;
}
/// <summary>She says this line when she can (unless she said it lately).</summary>
public static void Say(string line)
{
if (!I || !Lines.ContainsKey(line) || !GameSettings.Tips) return;
if (I._said.TryGetValue(line, out var at) && Time.time - at < (Rest.TryGetValue(line, out var r) ? r : 180f)) 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));
}
/// <summary>The trailer: she may say anything again.</summary>
public static void Forget()
{
if (I) I._said.Clear();
}
/// <summary>Stops her mid-sentence (the game is ending, a film starts).</summary>
public static void Hush()
{
if (!I) return;
I.StopAllCoroutines();
I._voice.Stop();
I._queue.Clear();
I.Panel(false);
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;
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;
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;
}
/// <summary>The video window: her picture, her name, and what she's saying beside it.</summary>
void Panel(bool on)
{
if (on && (_panel == null || _panel.panel == null)) Build();
if (_panel == null) return;
_booth?.Show(on);
if (on) _talk?.Play(0);
else _talk?.Stop();
if (on)
{
_panel.style.display = DisplayStyle.Flex;
_panel.BringToFront();
UIK.PopIn(_panel, 0f, 0.25f);
}
else _panel.style.display = DisplayStyle.None;
}
void Build()
{
var root = UIRoot.I ? UIRoot.I.Cinema : null;
if (root == null) return;
_talk = new TalkVideo("asst", new[] { "happy", "stern", "fed" }, 400, 406);
_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;
frame.style.backgroundColor = new Color(0.08f, 0.12f, 0.22f);
var bg = AssetBank.I ? AssetBank.I.Tex("T_Dir_Bg") : null;
if (bg)
{
frame.style.backgroundImage = new StyleBackground(bg);
frame.style.unityBackgroundScaleMode = ScaleMode.ScaleAndCrop;
frame.style.unityBackgroundImageTintColor = new Color(0.82f, 0.86f, 0.95f);
}
_panel.Add(frame);
_picture = new VisualElement { pickingMode = PickingMode.Ignore };
_picture.style.flexGrow = 1;
_picture.style.unityBackgroundScaleMode = ScaleMode.ScaleAndCrop;
_talk.Switched = rt => _picture.style.backgroundImage = new StyleBackground(Background.FromRenderTexture(rt));
_talk.Switched(_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;
}
}
}