using System.Collections;
using System.Linq;
using UnityEngine;
using UnityEngine.UIElements;
namespace GrassCutter
{
///
/// The gameplay takes, `g_`: through the player's own eyes, with the hands, the tool and the HUD, played by the script the
/// way a player would. The HUD draws into a texture of its own and goes over each frame.
///
public partial class Trailer
{
RenderTexture _ui;
Texture2D _uiRead;
/// The camera back on the player's head with its own shake and field of view, the hands and the HUD on; or off again.
void Eyes(bool on)
{
var fx = _cam.GetComponent();
// the HUD's own document: the phone has one of its own, drawn into its screen in the hand
var doc = UIRoot.I ? UIRoot.I.GetComponent() : null;
if (on)
{
_cam.transform.SetParent(Me.head, false);
_cam.transform.localPosition = Vector3.zero;
_cam.transform.localRotation = Quaternion.identity;
_cam.fieldOfView = CameraFx.BaseFov;
if (fx) fx.enabled = true;
ViewModel.Hidden = false;
if (!_ui)
{
_ui = new RenderTexture(W, H, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB) { name = "TrailerUI" };
_ui.Create();
_uiRead = new Texture2D(W, H, TextureFormat.RGBA32, false, false);
}
if (doc && doc.panelSettings)
{
doc.panelSettings.targetTexture = _ui;
doc.panelSettings.clearColor = true;
doc.panelSettings.colorClearValue = new Color(0f, 0f, 0f, 0f);
}
}
else
{
Ctl.BotUseHeld = false;
Ctl.BotMove = Vector2.zero;
_cam.transform.SetParent(null, true);
if (fx) fx.enabled = false;
ViewModel.Hidden = true;
if (doc && doc.panelSettings) doc.panelSettings.targetTexture = null;
}
}
/// The HUD over the frame in _read. It comes premultiplied over clear black: out = ui + scene * (1 - ui.a).
void Overlay()
{
var was = RenderTexture.active;
RenderTexture.active = _ui;
_uiRead.ReadPixels(new Rect(0, 0, W, H), 0, 0, false);
RenderTexture.active = was;
var u = _uiRead.GetRawTextureData();
var s = _read.GetRawTextureData();
for (int i = 0, j = 0; i < s.Length; i += 3, j += 4)
{
int a = u[j + 3];
if (a == 0) continue;
int k = 255 - a;
s[i] = (byte)Mathf.Min(255, u[j] + s[i] * k / 255);
s[i + 1] = (byte)Mathf.Min(255, u[j + 1] + s[i + 1] * k / 255);
s[i + 2] = (byte)Mathf.Min(255, u[j + 2] + s[i + 2] * k / 255);
}
}
static Vector3 North(Vector3 at) => Vector3.ProjectOnPlane(Vector3.forward, Planet.Up(at)).normalized;
/// The first spot from x, z eastward with nothing standing in the next 40 m north of it: no rock, tree or wreck.
static Vector2 Open(float x, float z)
{
for (int i = 0; i < 40; i++, x += 9f)
{
var at = On(x, z);
var up = Planet.Up(at);
var hits = Physics.OverlapCapsule(at + up * 1.6f, at + North(at) * 40f + up * 2.4f, 3.2f, ~0, QueryTriggerInteraction.Ignore);
if (hits.All(h => h.attachedRigidbody || h.GetComponentInParent())) return new Vector2(x, z);
}
return new Vector2(x, z);
}
/// Stands the player at x, z facing north, looking `ahead` metres on.
IEnumerator Stand(float x, float z, float ahead = 5f)
{
var at = On(x, z);
Me.Teleport(at + Planet.Up(at) * 0.2f);
yield return null;
Me.LookAt(Planet.Surface(at + North(at) * ahead));
yield return new WaitForSeconds(0.3f);
}
/// A strip mown on foot: a slow walk north, the aim swinging across the grass ahead the way a mouse does.
IEnumerator Mow(string name, ToolId tool, float x, float z, float seconds, float swing, float pace = 0.45f, float ahead = 3.2f, double money = -1)
{
if (!Wanted(name)) yield break;
if (money >= 0) Cheats.Run("money set " + money);
Me.Tools.Equip(tool);
yield return Stand(x, z, ahead);
var way = North(Me.transform.position);
var side = Vector3.Cross(Planet.Up(Me.transform.position), way);
float t = 0f;
Ctl.BotUseHeld = true;
Ctl.BotMove = Vector2.up * pace;
yield return Take(name, seconds, 60, null, 0.8f, k =>
{
t += Time.deltaTime;
var p = Me.transform.position;
Me.LookAt(Planet.Surface(p + way * ahead + side * Mathf.Sin(t * 2.1f) * swing));
Me.Tools.CheatEmpty();
}, true);
Ctl.BotUseHeld = false;
Ctl.BotMove = Vector2.zero;
}
/// One of the game's own screens, open over the world.
IEnumerator OnScreen(string name, float seconds, System.Action open, Vector3 lookAt)
{
if (!Wanted(name)) yield break;
Me.Tools.Select(ToolId.None);
Me.LookAt(lookAt);
yield return null;
open();
yield return Take(name, seconds, 60, null, 0.6f, null, true);
UIRoot.I?.CloseTopModal();
yield return new WaitForSeconds(0.3f);
}
IEnumerator Gameplay()
{
if (!Wanted("g_")) yield break;
Sky(0.42f, false);
Me.Character.Value = 0;
// past the first deliveries, so the HUD has no beginner's hints over the picture
Cheats.Run("ship 60");
Eyes(true);
yield return new WaitForSeconds(0.5f);
// from the nail scissors to the two-handed scythe, each a little further out from the base
yield return Mow("g_scissors", ToolId.NailScissors, 28f, 8f, 4f, 0.35f, 0.25f, 1.8f, 3);
yield return Mow("g_shears", ToolId.GardenShears, 31f, 8f, 3.5f, 0.45f, 0.3f, 2f, 18);
yield return Mow("g_sickle", ToolId.Sickle, 34f, 8f, 3.5f, 0.6f, 0.35f, 2.4f, 41);
yield return Mow("g_scythe", ToolId.Scythe, 38f, 8f, 5f, 1.1f, 0.45f, 3.2f, 127);
yield return Mow("g_wide", ToolId.WideScythe, 44f, 8f, 5f, 1.5f, 0.5f, 2.8f, 486);
// the crew alongside, keeping pace on either side
if (Wanted("g_coop"))
{
Cheats.Run("money set 2310");
for (int i = 0; i < 4; i++)
{
// a step or two ahead on either side, so they are in the picture
float off = (i < 2 ? -1 : 1) * (2.6f + (i % 2) * 2.6f), back = -(3f + (i % 2) * 2.5f);
Actor(new[] { 4, 6, 2, 7 }[i], On(56f + off, 8f - back), Vector3.forward, i % 2 == 0 ? ToolId.WideScythe : ToolId.LongScythe, true,
s => Me.transform.position + Vector3.Cross(Planet.Up(Me.transform.position), North(Me.transform.position)) * off - North(Me.transform.position) * back);
}
yield return Mow("g_coop", ToolId.WideScythe, 56f, 8f, 6f, 1.2f, 0.5f, 2.8f);
Strike();
}
// a full basket handed in at the depot: the money comes up on the HUD
var depot = Depot.All.FirstOrDefault();
if (depot && Wanted("g_deposit"))
{
Cheats.Run("money set 127");
Me.Tools.Equip(ToolId.Scythe);
// the scythe has to be in the hands before the cheat can fill it
yield return new WaitForSeconds(0.5f);
Cheats.Run("tank");
var d = depot.transform.position;
var from = d + new Vector3(0f, 0.2f, -6.5f);
Me.Teleport(from);
yield return null;
Me.LookAt(d + Planet.Up(d) * 0.9f);
bool paid = false;
Ctl.BotMove = Vector2.up * 0.6f;
yield return Take("g_deposit", 4f, 60, null, 0.4f, k =>
{
Me.LookAt(d + Planet.Up(d) * 0.9f);
if ((Me.transform.position - d).magnitude < 3.2f) Ctl.BotMove = Vector2.zero;
if (!paid && k > 0.5f) { paid = true; Me.Tools.Deposit(depot); }
}, true);
Ctl.BotMove = Vector2.zero;
}
// the base's own screens
var shop = FindAnyObjectByType();
var wall = FindAnyObjectByType();
Cheats.Run("money set 1840");
if (wall) yield return OnScreen("g_supply", 3f, () => UIRoot.I.OpenSupply(), wall.transform.position + Vector3.up * 1.4f);
if (shop) yield return OnScreen("g_shop", 3.5f, () => UIRoot.I.OpenShop(), shop.transform.position + Vector3.up * 1.2f);
yield return OnScreen("g_map", 3f, () => UIRoot.I.OpenMap(), Me.transform.position + North(Me.transform.position) * 10f);
yield return OnScreen("g_character", 3f, () => UIRoot.I.OpenCharacter(), Me.transform.position + North(Me.transform.position) * 10f);
// the hammer: a press set down on mown ground, the ghost sweeping in before the click
if (Wanted("g_build") && PlayerBuild.Local)
{
Cheats.Run("money set 4200");
float x = 70f, z = 8f;
for (float dz = -4f; dz <= 18f; dz += 2f) MowMap.Cut(On(x - 9f, z + dz), On(x + 9f, z + dz), 2.4f);
Me.Tools.Equip(ToolId.Hammer);
yield return Stand(x, z - 2f, 7f);
var b = PlayerBuild.Local;
b.Select(PieceId.Press);
var way = North(Me.transform.position);
var side = Vector3.Cross(Planet.Up(Me.transform.position), way);
var p0 = Me.transform.position;
bool built = false;
yield return Take("g_build", 5f, 60, null, 0.5f, k =>
{
// in to the spot, the click, then on to the next free spot so the ghost doesn't sit red on the new press
float s = k < 0.62f ? Mathf.Lerp(-3f, 0f, Ease(Mathf.Clamp01(k * 1.8f))) : Mathf.Lerp(0f, 5f, Ease(Mathf.Clamp01((k - 0.62f) * 6f)));
Me.LookAt(Planet.Surface(p0 + way * 5.5f + side * s));
Ctl.BotUsePressed = !built && k > 0.6f;
if (Ctl.BotUsePressed) built = true;
}, true);
Ctl.BotUsePressed = false;
}
// in the seat: the rider, then the combine, mowing on
foreach (var (take, kind, x) in new[] { ("g_ride", VehicleKind.Rider, 90f), ("g_combine", VehicleKind.Combine, 120f) })
{
if (!Wanted(take)) continue;
Cheats.Run("money set " + (kind == VehicleKind.Rider ? 12400 : 286000));
var spot = Open(x, 0f);
var at = On(spot.x, spot.y);
var up = Planet.Up(at);
var turn = Quaternion.LookRotation(North(at), up);
Spawner.Instance.SpawnVehicle(kind, Me.SaveId, at + up * 1.2f, turn);
yield return null;
var v = Vehicle.All.Last(o => o.kind == kind);
v.ServerPut(at + up * 1.2f, turn);
yield return new WaitForSeconds(1f);
Me.Teleport(v.transform.position + v.transform.right * 2.2f + up * 0.2f);
yield return new WaitForSeconds(0.3f);
v.Interact();
float t0 = Time.time;
while (!Me.Driving.Value && Time.time - t0 < 3f) yield return null;
Hud.ClearToasts();
Me.LookAt(v.transform.position + v.transform.forward * 25f);
Ctl.BotMove = Vector2.up;
yield return Take(take, 6f, 60, null, 2f, k => Me.LookAt(v.transform.position + v.transform.forward * 25f + Planet.Up(v.transform.position) * 0.8f), true);
Log($"{take}: at {spot}, {v.Body.linearVelocity.magnitude:0.0} m/s at the end");
Ctl.BotMove = Vector2.zero;
v.RequestExit();
yield return new WaitForSeconds(0.6f);
v.NetworkObject.Despawn(true);
}
// the phone, out at the side while the grass waits
if (Wanted("g_phone"))
{
Me.Tools.Equip(ToolId.Scythe);
yield return Stand(28f, 30f, 6f);
Phone.I.Show(PhoneMode.Side);
yield return Take("g_phone", 3.5f, 60, null, 1.2f, null, true);
Phone.I.Show(PhoneMode.Away);
}
Eyes(false);
Strike();
}
}
}