Grass Cutters Together: the project as of moon-sweepers f3666f0
A clean start for the shared repository: the Unity project (Assets, Packages, ProjectSettings), ArtSource, Docs and Tools as they stood at f3666f0 in the old local repository, with its generated prefabs and scene brought up to date. Images, audio, models and binaries go through Git LFS (.gitattributes). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
94
Assets/Scripts/Gameplay/VehicleShed.cs
Normal file
94
Assets/Scripts/Gameplay/VehicleShed.cs
Normal file
@ -0,0 +1,94 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace GrassCutter
|
||||
{
|
||||
/// <summary>
|
||||
/// 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 (<see cref="Vehicle.ServerRollOut"/>), then the
|
||||
/// door comes down again. The door is purely for show and is opened on every machine by <see cref="TeamState.ShedDoorRpc"/>.
|
||||
/// </summary>
|
||||
public class VehicleShed : MonoBehaviour
|
||||
{
|
||||
/// <summary>The shed at the base, if the scene has one.</summary>
|
||||
public static VehicleShed Base;
|
||||
|
||||
/// <summary>The door, pivot on its top edge: it tilts up and in, under the roof, like a farm garage's up-and-over door.</summary>
|
||||
public Transform door;
|
||||
/// <summary>Where a machine waits inside and where it stops outside, in the shed's own space; out is along +Z.</summary>
|
||||
public Vector3 inside = new Vector3(0f, 0.6f, 0f), outside = new Vector3(0f, 0.6f, 7f);
|
||||
public bool isBase;
|
||||
/// <summary>Its size in its own space: half the width, the inside of the back wall, the door line, the eaves.</summary>
|
||||
public float halfWide = 8f, backWall = -1.9f, doorLine = 2.3f, eaves = 2.5f;
|
||||
|
||||
public static readonly System.Collections.Generic.List<VehicleShed> All = new System.Collections.Generic.List<VehicleShed>();
|
||||
void OnEnable() => All.Add(this);
|
||||
void OnDisable() => All.Remove(this);
|
||||
|
||||
/// <summary>The shed this spot is inside or on the pad in front of.</summary>
|
||||
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);
|
||||
|
||||
/// <summary>
|
||||
/// Where a machine whose tail reaches <paramref name="back"/> 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.
|
||||
/// </summary>
|
||||
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));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>Where it stops outside: far enough out that its tail clears the door.</summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>Raise the door for as long as a machine needs to get out.</summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user