using System.Collections.Generic;
using UnityEngine;
namespace GrassCutter
{
///
/// Now and then a cut turns up the little life under the grass: beetles that scurry off and grasshoppers that hop away,
/// each gone back into the standing grass a moment later. Only seen near the camera, and only by whoever sees it.
///
public class Critters : MonoBehaviour
{
const int Max = 36;
const float Reach = 28f;
class Bug
{
public Transform t;
public bool hopper, live;
public Vector3 at, vel, up;
public float life, age, hopIn, size;
}
static Critters _i;
/// How many are out right now (for the dev bot).
public static int Alive { get { int n = 0; if (_i) foreach (var b in _i._bugs) if (b.live) n++; return n; } }
readonly List _bugs = new List();
float _stirred, _nextSound;
bool _warned;
/// Families that ran out so far (for the dev bot).
public static int Families;
static Critters I
{
get
{
if (!_i && Application.isPlaying)
{
var go = new GameObject("[Critters]");
_i = go.AddComponent();
}
return _i;
}
}
/// Grass was just cut here (square metres of it): maybe something runs out.
public static void Disturb(Vector3 at, float half, float area)
{
var cam = CameraFx.Cam;
if (!cam || (cam.transform.position - at).sqrMagnitude > Reach * Reach) return;
var i = I;
if (!i) return;
i._stirred += area;
// roughly one family per 9 m² of meadow cut, a little luck either way
if (i._stirred < 9f) return;
i._stirred = Random.Range(-3f, 2f);
if (Random.value > 0.55f) return;
Families++;
int n = Random.Range(2, 6);
bool hoppers = Random.value < 0.45f;
var up = Planet.Up(at);
for (int k = 0; k < n; k++)
{
var off = Random.insideUnitCircle * Mathf.Max(0.3f, half);
var p = Planet.Surface(at + Planet.Frame * new Vector3(off.x, 0f, off.y));
i.Spawn(p, up, hoppers ? Random.value < 0.8f : Random.value < 0.15f);
}
if (Time.time > i._nextSound)
{
i._nextSound = Time.time + 1.2f;
Sfx.PlayAt(Random.value < 0.5f ? "bug_scatter_1" : "bug_scatter_2", at, 0.35f, 1f, 0.12f);
}
}
void Spawn(Vector3 at, Vector3 up, bool hopper)
{
Bug b = null;
foreach (var x in _bugs) if (!x.live && x.hopper == hopper) { b = x; break; }
if (b == null)
{
if (_bugs.Count >= Max) return;
var model = AssetBank.I.Model(hopper ? "SM_Grasshopper" : "SM_Bug");
if (!model) { if (!_warned) { _warned = true; Debug.LogWarning("[Critters] no model " + (hopper ? "SM_Grasshopper" : "SM_Bug")); } return; }
var go = Instantiate(model, transform);
foreach (var c in go.GetComponentsInChildren(true)) Destroy(c);
foreach (var r in go.GetComponentsInChildren(true)) { r.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; r.receiveShadows = false; }
b = new Bug { t = go.transform, hopper = hopper };
_bugs.Add(b);
}
var flat = Random.insideUnitCircle.normalized;
var away = Vector3.ProjectOnPlane(Planet.Frame * new Vector3(flat.x, 0f, flat.y), up).normalized;
b.live = true;
b.at = at;
b.up = up;
b.age = 0f;
b.size = Random.Range(2.4f, 3.2f); // cartoon-sized, so they read from standing height
if (hopper)
{
b.life = Random.Range(1.4f, 2.4f);
b.vel = Vector3.zero;
b.hopIn = Random.Range(0.02f, 0.2f);
}
else
{
b.life = Random.Range(0.8f, 1.5f);
b.vel = away * Random.Range(1.2f, 2.4f);
}
b.t.gameObject.SetActive(true);
b.t.localScale = Vector3.one * b.size;
Face(b, away);
}
void Update()
{
float dt = Time.deltaTime;
foreach (var b in _bugs)
{
if (!b.live) continue;
b.age += dt;
if (b.age >= b.life) { b.live = false; b.t.gameObject.SetActive(false); continue; }
var up = Planet.Up(b.at);
if (b.hopper)
{
b.hopIn -= dt;
// on the ground it waits a beat, then springs off in a new direction
if (b.hopIn <= 0f && b.vel == Vector3.zero)
{
var flat = Random.insideUnitCircle.normalized;
var dir = Vector3.ProjectOnPlane(Planet.Frame * new Vector3(flat.x, 0f, flat.y), up).normalized;
b.vel = dir * Random.Range(1.4f, 2.6f) + up * Random.Range(2.2f, 3.4f);
Face(b, dir);
}
if (b.vel != Vector3.zero)
{
b.vel -= up * 12f * dt;
b.at += b.vel * dt;
var ground = Planet.Surface(b.at);
if (Vector3.Dot(b.at - ground, up) <= 0f && Vector3.Dot(b.vel, up) < 0f)
{
b.at = ground;
b.vel = Vector3.zero;
b.hopIn = Random.Range(0.12f, 0.35f);
}
}
}
else
{
// a beetle zigzags a little as it runs
var side = Vector3.Cross(up, b.vel).normalized;
b.at = Planet.Surface(b.at + (b.vel + side * Mathf.Sin(b.age * 18f) * 0.4f) * dt);
}
// the last moment it dives back into the grass
float k = Mathf.Clamp01((b.life - b.age) / 0.25f);
b.t.position = b.at;
b.t.localScale = Vector3.one * b.size * k;
if (!b.hopper) Face(b, b.vel);
}
}
static void Face(Bug b, Vector3 dir)
{
var up = Planet.Up(b.at);
var f = Vector3.ProjectOnPlane(dir, up);
if (f.sqrMagnitude < 1e-5f) return;
b.t.rotation = Quaternion.LookRotation(f, up);
}
}
}