using System.Collections;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UIElements;
namespace GrassCutter
{
///
/// The opening of a new game. Black, and echoes in an empty hangar; the black thins slowly onto the planks of the base; the
/// phone rings and comes up, and the Director of G.R.A.S.S.A. gives his orders from Earth. Then a sharp look up at the
/// meadow and hard cuts climbing higher and higher until the whole planet hangs in space, "we take grass very seriously",
/// and a hard cut back into your own eyes: the game is yours. Space or Enter skips it.
///
[DefaultExecutionOrder(10000)]
public class IntroCinematic : MonoBehaviour
{
public static bool Running { get; private set; }
/// The dev bot asks for it on a world that isn't new.
public static bool Force;
/// The trailer films it without the "Space: skip" in the corner.
public static bool NoSkipHint;
/// A game starting now could open on the film (not under a dev bot unless it asked).
public static bool MightRun => Force || (!CommandLine.Has("-autodemo") && !EndingCinematic.Preview);
/// Whether the film plays has been decided for this game and it isn't playing: until then nobody talks.
public static bool Settled { get; private set; }
/// The trailer: the film again, now, on whatever world this is.
public static void Again()
{
Force = true;
var i = FindAnyObjectByType();
if (!i) return;
i._tried = false;
i._since = Time.time - 1f;
}
enum Shot { None, Eyes, Lens }
Shot _shot;
float _pitch, _pitchTo;
Vector3 _eye, _look, _drift;
float _shotT;
float _near = 0.05f, _far = 14000f;
Coroutine _run;
bool _tried;
float _since = -1f;
VisualElement _black, _top, _bottom;
Label _skip;
void Update()
{
var me = PlayerAgent.Local;
var ts = TeamState.Instance;
if (!me || !me.IsSpawned || !ts || !ts.IsSpawned) { _tried = false; _since = -1f; Settled = false; if (Running) Finish(); return; }
if (_since < 0f) _since = Time.time;
if (!_tried && Time.time - _since > 0.8f)
{
_tried = true;
bool fresh = ts.Shipped.Value <= 0d && ts.Mown.Value <= 0d;
bool bot = CommandLine.Has("-autodemo") || EndingCinematic.Preview;
if ((fresh && !bot) || Force) _run = StartCoroutine(Run());
else { UIRoot.I?.ReleaseFade(0.6f); Settled = true; }
}
if (Running && Keyboard.current != null && (Keyboard.current.spaceKey.wasPressedThisFrame || Keyboard.current.enterKey.wasPressedThisFrame)) Skip();
}
void Skip()
{
if (_run != null) StopCoroutine(_run);
_run = null;
DirectorCall.Abort();
Sfx.Play("cut_return", 0.5f, 1f, 0f);
Finish();
}
IEnumerator Run()
{
Running = true;
Force = false;
Ctl.Cinematic = true;
UIRoot.I.HideHud(true);
Sfx.Duck(0f);
Build();
_black.style.opacity = 1f;
_black.style.display = DisplayStyle.Flex;
UIRoot.I.ReleaseFade(0f);
// face the ramp: when the eyes come up they look out over the meadow, not at the cabin
var me0 = PlayerAgent.Local;
if (me0) me0.LookAt(Planet.Surface(new Vector3(0f, 0f, -40f)) + Planet.Up(me0.transform.position) * 1.5f);
// the shots from the air want the meadow furry far out
GrassField.FurReach = 3.5f;
_skip.style.display = NoSkipHint ? DisplayStyle.None : DisplayStyle.Flex;
Phone.I?.Show(PhoneMode.Away);
// 1. black, and the echo of an empty hangar
_shot = Shot.Eyes;
_pitch = _pitchTo = 64f;
Sfx.Play("intro_echo", 0.9f, 1f, 0f);
yield return new WaitForSeconds(2.4f);
// 2. the black thins slowly onto the floor of the base
Fade(_black, 0f, 3.6f);
yield return new WaitForSeconds(3.0f);
// 3. the phone rings; it comes up to the face, and he's on it
Sfx.Play("phone_ring", 0.5f, 1f, 0f);
yield return new WaitForSeconds(1.2f);
Phone.I?.Show(PhoneMode.Up);
// still looking down at the planks over the phone: the world waits for the end of the call
_pitchTo = 58f;
yield return new WaitForSeconds(0.7f);
yield return DirectorCall.Speak("dir_intro", true);
// 4. the phone goes down and the head comes up, quick but smooth, onto the meadow; then cuts climbing until
// the planet hangs in space
Phone.I?.Show(PhoneMode.Away);
yield return new WaitForSeconds(0.15f);
Bars(true);
Sfx.Play("cut_whoosh_1", 0.38f, 1.15f, 0f);
Lift(-3f, 0.6f);
yield return new WaitForSeconds(0.6f);
CameraFx.FovKick(-6f);
yield return new WaitForSeconds(1.0f);
var me = PlayerAgent.Local;
var cam = CameraFx.Cam;
Vector3 eye = cam ? cam.transform.parent ? cam.transform.parent.position : cam.transform.position : me.transform.position;
var up = Planet.Up(eye);
var fwd = Vector3.ProjectOnPlane(me.transform.forward, up).normalized;
var home = Planet.OnSurface(Vector3.up);
var away = -fwd;
var side = Vector3.Cross(up, fwd);
(float h, float back, float hold)[] climb = { (9f, 16f, 0.55f), (110f, 150f, 0.5f), (420f, 380f, 0.5f), (1400f, 700f, 0.55f) };
for (int i = 0; i < climb.Length; i++)
{
var (h, back, hold) = climb[i];
Sfx.Play(i % 2 == 0 ? "cut_whoosh_2" : "cut_whoosh_1", 0.42f, 1f + i * 0.07f, 0f);
var at = Planet.Surface(home + away * back + side * back * 0.35f);
var e = at + Planet.Up(at) * h;
Lens(e, home, Vector3.ProjectOnPlane(home - e, Planet.Up(e)).normalized * h * 0.05f);
CameraFx.FovKick(4f);
yield return new WaitForSeconds(hold);
}
// the whole ball, and the agency's motto over it
Sfx.Play("cut_whoosh_2", 0.5f, 0.8f, 0f);
Sfx.Play("space_rumble", 0.85f, 1f, 0f);
var tilt = (Vector3.up + away * 0.55f + side * 0.25f).normalized;
var far = Planet.Center + tilt * (Planet.Radius * 2.54f); // the same framing on a smaller ball
Lens(far, Planet.Center, side * 60f);
Space(true, far);
yield return new WaitForSeconds(0.5f);
yield return DirectorCall.Speak("dir_intro_tag", false);
yield return new WaitForSeconds(0.7f);
// 5. the hard cut back, and it's yours
Sfx.Play("cut_return", 0.55f, 1f, 0f);
_run = null;
Finish();
CameraFx.FovKick(14f);
CameraFx.Shake(0.45f);
}
void Finish()
{
Running = false;
Settled = true;
_liftAt = -1f;
Space(false, Vector3.zero);
GrassField.FurReach = 1f;
_shot = Shot.None;
var cam = CameraFx.Cam;
if (cam) { cam.nearClipPlane = 0.05f; cam.farClipPlane = 14000f; }
if (cam && cam.transform.parent) cam.transform.SetLocalPositionAndRotation(Vector3.zero, Quaternion.identity);
Ctl.Cinematic = false;
if (UIRoot.I) UIRoot.I.HideHud(false);
// the assistant says hello once the film is over: from here on she's the one with the hints
Assistant.Say("asst_hello");
Sfx.Duck(1f);
if (_black != null) { Tween.Kill(_black); _black.style.display = DisplayStyle.None; }
if (_skip != null) _skip.style.display = DisplayStyle.None;
Bars(false);
if (Phone.I && Phone.I.Mode != PhoneMode.Away) Phone.I.Show(PhoneMode.Away);
}
// the head coming up: from where it was to _pitchTo in a set time, eased in and out
float _liftAt = -1f, _liftFrom, _liftFor;
void Lift(float to, float seconds)
{
_liftFrom = _pitch;
_pitchTo = to;
_liftAt = Time.time;
_liftFor = seconds;
}
void Lens(Vector3 eye, Vector3 look, Vector3 drift)
{
_shot = Shot.Lens;
_eye = eye; _look = look; _drift = drift;
_shotT = 0f;
bool high = Planet.Altitude(eye) > 700f;
_near = high ? 4f : 0.05f;
_far = high ? 60000f : 14000f;
}
void LateUpdate()
{
var cam = CameraFx.Cam;
if (!cam || _shot == Shot.None) return;
var rig = cam.transform.parent;
if (_shot == Shot.Eyes && rig)
{
// our own eyes, held looking down at the planks
if (_liftAt >= 0f)
{
float k = Mathf.Clamp01((Time.time - _liftAt) / _liftFor);
// quick off the mark and settling softly: an ease in-out skewed toward the start
k = Mathf.Pow(k, 0.7f);
k = k * k * (3f - 2f * k);
_pitch = Mathf.Lerp(_liftFrom, _pitchTo, k);
if (Time.time - _liftAt >= _liftFor) _liftAt = -1f;
}
else _pitch = Mathf.Lerp(_pitch, _pitchTo, Time.deltaTime * 1.6f);
var up = Planet.Up(rig.position);
var fwd = Vector3.ProjectOnPlane(rig.forward, up).normalized;
if (fwd.sqrMagnitude < 0.01f) fwd = Vector3.ProjectOnPlane(Vector3.forward, up).normalized;
var turn = Quaternion.AngleAxis(_pitch, Vector3.Cross(up, fwd)) * Quaternion.LookRotation(fwd, up);
cam.transform.SetPositionAndRotation(rig.position, turn);
}
else if (_shot == Shot.Lens)
{
// each shot creeps a little, so a cut lands on something alive
_shotT += Time.deltaTime;
var e = _eye + _drift * _shotT;
cam.transform.SetPositionAndRotation(e, Quaternion.LookRotation(_look - e, Planet.Up(e)));
}
cam.nearClipPlane = _near;
cam.farClipPlane = _far;
}
// ───────── out in space: black sky and stars instead of the day's blue ─────────
static void Space(bool on, Vector3 eye) => SpaceSky.Set(on, eye);
// ───────── the screen ─────────
void Build()
{
var root = UIRoot.I.Cinema;
if (_black == null || _black.panel == null)
{
_black = new VisualElement { pickingMode = PickingMode.Ignore };
_black.AddToClassList("cinema-black");
root.Add(_black);
_top = Bar(root, true);
_bottom = Bar(root, false);
_skip = UIK.Text(Loc.T("SPACE skip"), "cinema-skip t-outline", root, false);
}
// the call's captions go over the black and the bars
_black.SendToBack();
}
static VisualElement Bar(VisualElement root, bool top)
{
var b = new VisualElement { pickingMode = PickingMode.Ignore };
b.AddToClassList("cinema-bars");
if (top) b.style.top = 0; else b.style.bottom = 0;
b.style.display = DisplayStyle.None;
root.Add(b);
b.SendToBack();
return b;
}
void Bars(bool on)
{
if (_top == null) return;
_top.style.display = _bottom.style.display = on ? DisplayStyle.Flex : DisplayStyle.None;
}
static void Fade(VisualElement e, float to, float seconds)
{
float from = e.resolvedStyle.opacity;
Tween.Kill(e);
Tween.Run(e, seconds, k => e.style.opacity = Mathf.Lerp(from, to, k), Ease.InOutQuad);
}
}
}