using UnityEngine; namespace GrassCutter { /// /// A building machines come out of: the shed at the base and every garage the crew builds. A bought or called machine waits /// inside, the door tilts up and it drives out onto the pad in front (), then the /// door comes down again. The door is purely for show and is opened on every machine by . /// public class VehicleShed : MonoBehaviour { /// The shed at the base, if the scene has one. public static VehicleShed Base; /// The door, pivot on its top edge: it tilts up and in, under the roof, like a farm garage's up-and-over door. public Transform door; /// Where a machine waits inside and where it stops outside, in the shed's own space; out is along +Z. public Vector3 inside = new Vector3(0f, 0.6f, 0f), outside = new Vector3(0f, 0.6f, 7f); public bool isBase; /// Its size in its own space: half the width, the inside of the back wall, the door line, the eaves. public float halfWide = 8f, backWall = -1.9f, doorLine = 2.3f, eaves = 2.5f; public static readonly System.Collections.Generic.List All = new System.Collections.Generic.List(); void OnEnable() => All.Add(this); void OnDisable() => All.Remove(this); /// The shed this spot is inside or on the pad in front of. public static VehicleShed Near(Vector3 p) { foreach (var s in All) { if (!s) continue; var l = s.transform.InverseTransformPoint(p); if (Mathf.Abs(l.x) < s.halfWide && l.z > s.backWall && l.z < s.doorLine + 11f && Mathf.Abs(l.y) < 4f) return s; } return null; } public const float DoorUp = 1.1f, Wait = 0.9f, Drive = 3.2f, Hold = 1.6f; Quaternion _doorRest; const float DoorTilt = 86f; float _open, _openUntil = -1f; public Vector3 Inside => transform.TransformPoint(inside); public Vector3 Outside => transform.TransformPoint(outside); /// /// Where a machine whose tail reaches metres behind its middle waits: as deep inside as it fits. /// One taller than the eaves would stick up through the roof, so it waits with its tail just inside the door instead. /// public Vector3 InsideFor(float back, float top) { float z = top > eaves ? doorLine - 0.6f + back : Mathf.Min(backWall + 0.4f + back, doorLine + 1.5f); return transform.TransformPoint(new Vector3(inside.x, inside.y, z)); } /// Where it stops outside: far enough out that its tail clears the door. public Vector3 OutsideFor(float back) => transform.TransformPoint(new Vector3(outside.x, outside.y, Mathf.Max(outside.z, doorLine + 0.8f + back))); public Quaternion Facing => Quaternion.LookRotation(transform.forward, transform.up); void Awake() { if (isBase) Base = this; if (door) _doorRest = Quaternion.Inverse(transform.rotation) * door.rotation; } void OnDestroy() { if (Base == this) Base = null; } /// Raise the door for as long as a machine needs to get out. public void Open() { if (_open < 0.05f && door) Sfx.PlayAt("gate_shutter", door.position, 0.7f); _openUntil = Time.time + Wait + Drive + Hold; } void Update() { if (!door) return; float want = Time.time < _openUntil ? 1f : 0f; if (Mathf.Approximately(_open, want)) return; if (want < 0.5f && _open >= 1f) Sfx.PlayAt("gate_shutter", door.position, 0.5f, 0.85f); _open = Mathf.MoveTowards(_open, want, Time.deltaTime / DoorUp); float e = _open * _open * (3f - 2f * _open); // about the shed's own X axis: the bottom edge swings in (toward -Z) and up door.rotation = transform.rotation * Quaternion.AngleAxis(DoorTilt * e, Vector3.right) * _doorRest; } } }