using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace GrassCutter
{
///
/// The takes the scripted trailer (Docs/TRAILER_SCRIPT.md) needs that the song trailer never shot: one pair of nail
/// scissors against the whole planet, a bundle thrown in a friend's face, and a belt that "mostly works".
///
public partial class Trailer
{
IEnumerator Polden()
{
if (!Wanted("z_")) yield break;
Sky(0.42f, false);
// the hook: close on the scissors, then up and back until the crewman is a speck in a lawn without end
float x = -150f, z = 380f;
if (Wanted("z_blade"))
{
var e = Actor(1, On(x, z), Vector3.forward, ToolId.NailScissors, true, s => On(x, z + s * 0.25f));
e.Width = 0.4f; e.Sweep = 0.12f;
yield return Take("z_blade", 6f, 60, t =>
{
var near = Ride(e.transform, new Vector3(0.8f, 0.98f, 1.5f), new Vector3(0.1f, 0.74f, 0.25f), 36f);
var b = e.transform.position;
var up = Planet.Up(b);
var far = new Lens(b + up * 30f - e.transform.forward * 26f, On(x, z + 30f), 66f);
float u = Ease(Mathf.InverseLerp(0.28f, 1f, t));
return new Lens(Vector3.Lerp(near.at, far.at, u * u), Vector3.Lerp(near.look, far.look, u), Mathf.Lerp(near.fov, far.fov, u));
}, 0.4f);
Strike();
}
// "Catch!": through the foreman's eyes, a bundle from the gremlin, full in the face
x = -300f; z = 420f;
if (Wanted("z_catch"))
{
Clear(x, z + 4f, 6f);
var g = Actor(4, On(x + 0.5f, z + 4f), Vector3.back, ToolId.Scythe);
g.Pitch = 4f;
var m1 = Actor(1, On(x - 5f, z + 9f), Vector3.forward, ToolId.Scythe, true, s => On(x - 5f, z + 9f + s * 1.2f));
var m2 = Actor(6, On(x + 5.5f, z + 10f), Vector3.forward, ToolId.WideScythe, true, s => On(x + 5.5f, z + 10f + s * 1.2f));
var eye = On(x, z, 1.55f);
var look = On(x + 0.4f, z + 4f, 1.2f);
Cargo bundle = null;
Rigidbody body = null;
Vector3 from = Vector3.zero;
const float Throw = 0.32f, Fly = 0.42f;
float hitAt = -1f;
yield return Take("z_catch", 3.2f, 60, t =>
{
var l = new Lens(eye, look, 50f);
if (hitAt >= 0f && t >= hitAt)
{
float since = (t - hitAt) * 3.2f;
float shake = Mathf.Exp(-since * 5f);
var up = Planet.Up(eye);
l.at += up * (-0.25f * shake) + Random.insideUnitSphere * 0.05f * shake;
l.look += up * (-1.6f * shake);
l.roll = 9f * shake * Mathf.Sin(since * 30f);
}
return l;
}, 0.6f, k =>
{
if (bundle == null && k >= Throw)
{
from = g.transform.position + Planet.Up(g.transform.position) * 1.2f - g.transform.forward * -0.6f;
bundle = Spawner.Instance.SpawnCargo(CargoKind.Grass, from, Quaternion.Euler(20f, 40f, 0f), Vector3.zero);
body = bundle ? bundle.GetComponent() : null;
if (body) body.isKinematic = true;
}
if (!bundle) return;
float f = (k - Throw) * 3.2f / Fly;
if (f <= 1f && body && body.isKinematic)
{
var to = eye + (look - eye).normalized * 0.25f;
var p = Vector3.Lerp(from, to, f) + Planet.Up(eye) * (Mathf.Sin(f * Mathf.PI) * 0.7f);
bundle.transform.SetPositionAndRotation(p, Quaternion.Euler(20f + f * 300f, 40f + f * 120f, f * 90f));
}
else if (body && body.isKinematic)
{
hitAt = k;
body.isKinematic = false;
body.linearVelocity = -Planet.Up(eye) * 2f + (look - eye).normalized * 1.5f;
Fx.Dust(eye + (look - eye).normalized * 0.6f, 1.4f, 70);
Fx.Dust(eye + (look - eye).normalized * 1.2f, 0.9f, 40);
g.Laughing = true;
}
});
foreach (var c in Cargo.All.ToList()) if (c && c.IsSpawned) c.NetworkObject.Despawn(true);
Strike();
}
// "It works. It actually works." ... the belt runs to nowhere and the bundles pour off the end
x = -360f; z = -420f;
if (Wanted("z_spill"))
{
BuildRules.AnyGround = true;
for (int i = -12; i <= 12; i += 6) MowMap.Cut(On(x - 26f, z + i * 2f), On(x + 26f, z + i * 2f), 13f);
var at = On(x, z);
Ts.BuildRpc((byte)PieceId.Binder, at, Quaternion.LookRotation(Vector3.ProjectOnPlane(Vector3.forward, Planet.Up(at)), Planet.Up(at)), Vector3.zero);
yield return new WaitForSeconds(0.5f);
var binder = BuildPiece.All.Where(p => p.piece == PieceId.Binder).OrderBy(p => (p.transform.position - at).sqrMagnitude).FirstOrDefault();
if (binder)
{
var ports = new List();
binder.Ports(ports);
var start = ports[0].at;
var end = Planet.Surface(ports[0].at + ports[0].dir * 12f);
end += Planet.Up(end);
Ts.BuildRpc((byte)PieceId.Belt, start, BuildRules.RunRotation(start, end), end);
yield return null;
binder.GetComponent().TakeRpc(60f);
var w = (end - start).normalized;
var u = Planet.Up(end);
var side = Vector3.Cross(u, w);
var eng = Actor(2, end + w * 2.2f + side * 2.4f, -side, ToolId.Hammer);
eng.Pitch = 22f;
int poured = 0;
var rng = new System.Random(7);
yield return Take("z_spill", 4.5f, 60, t =>
new Lens(end + w * 5.5f - side * 3.2f + u * (1.3f - t * 0.2f), end + w * 1.2f + side * 0.4f + u * 0.2f, 50f - t * 6f), 6f, k =>
{
// the jam: more than the end can let go of
int want = Mathf.FloorToInt(k * 26f);
while (poured < want)
{
poured++;
float s = (float)(rng.NextDouble() - 0.5) * 2f;
var p = end + w * 0.4f + u * (0.6f + (float)rng.NextDouble() * 0.3f) + side * s * 0.3f;
Spawner.Instance.SpawnCargo(CargoKind.Grass, p, Quaternion.Euler(rng.Next(360), rng.Next(360), 0f), w * (1.2f + (float)rng.NextDouble() * 1.0f) + u * 0.5f + side * s * 1.0f);
}
});
}
foreach (var c in Cargo.All.ToList()) if (c && c.IsSpawned) c.NetworkObject.Despawn(true);
Strike();
}
}
}
}