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>
96 lines
3.7 KiB
C#
96 lines
3.7 KiB
C#
using System.Collections.Generic;
|
|
using Unity.Netcode;
|
|
using UnityEngine;
|
|
|
|
namespace GrassCutter
|
|
{
|
|
/// <summary>
|
|
/// A shippable item: grass bundle, hay or pressed brick. Physics + Grabbable carry it around; when it tumbles off the
|
|
/// deck it lands in the meadow with a puff of clippings. A depot takes it the moment it drops in.
|
|
/// </summary>
|
|
[RequireComponent(typeof(Grabbable))]
|
|
public class Cargo : NetworkBehaviour
|
|
{
|
|
public static readonly List<Cargo> All = new List<Cargo>();
|
|
|
|
public CargoKind kind;
|
|
/// <summary>Half height (ground contact checks).</summary>
|
|
public float halfHeight = 0.33f;
|
|
public readonly NetworkVariable<float> Kg = new NetworkVariable<float>(GameDefs.BundleKg);
|
|
|
|
Grabbable _g;
|
|
bool _wasInGrass;
|
|
public bool Loaded { get; set; }
|
|
/// <summary>Server: the belt this item is locked onto (null when loose).</summary>
|
|
public Conveyor Belt { get; set; }
|
|
public Grabbable Grab => _g ? _g : (_g = GetComponent<Grabbable>());
|
|
|
|
void Awake() { _g = GetComponent<Grabbable>(); }
|
|
|
|
/// <summary>Server: unlock from the belt and become a loose physics body again.</summary>
|
|
public void ServerLeaveBelt(Vector3 velocity)
|
|
{
|
|
if (Belt) Belt.ServerRemove(this, velocity);
|
|
}
|
|
|
|
/// <summary>E at the depot or a machine with this bundle in your arms.</summary>
|
|
[Rpc(SendTo.Server)]
|
|
public void HandInRpc(ulong piece, RpcParams rpc = default)
|
|
{
|
|
if (Grab.Holder.Value != rpc.Receive.SenderClientId || Grab.Consumed) return;
|
|
if (!NetworkManager.SpawnManager.SpawnedObjects.TryGetValue(piece, out var no)) return;
|
|
var sink = no.GetComponentInChildren<ICargoSink>();
|
|
if (sink == null || (transform.position - no.transform.position).sqrMagnitude > 49f) return;
|
|
Grab.ServerForceRelease();
|
|
sink.ServerAccept(this, transform.position, Vector3.down);
|
|
}
|
|
|
|
public override void OnNetworkSpawn()
|
|
{
|
|
All.Add(this);
|
|
if (Time.timeSinceLevelLoad > 3f) Tween.Scale(transform, Vector3.one * 0.3f, Vector3.one, 0.35f, Ease.OutBack);
|
|
}
|
|
|
|
public override void OnNetworkDespawn()
|
|
{
|
|
All.Remove(this);
|
|
if (Belt) { Belt.ServerRemove(this, Vector3.zero); Belt = null; }
|
|
}
|
|
|
|
public override void OnDestroy()
|
|
{
|
|
All.Remove(this);
|
|
base.OnDestroy();
|
|
}
|
|
|
|
float _fallV;
|
|
|
|
void FixedUpdate()
|
|
{
|
|
if (!IsSpawned) return;
|
|
var rb = _g.Body;
|
|
var p = transform.position;
|
|
|
|
// tossed into a depot
|
|
if (IsServer && !_g.IsHeld && !_g.Consumed && !Belt)
|
|
foreach (var d in Depot.All)
|
|
if (d && (p - d.Mouth).sqrMagnitude < 1.1f) { d.ServerAccept(this, p, Vector3.down); return; }
|
|
|
|
var up = Planet.Up(p);
|
|
bool inGrass = Planet.Altitude(p) - halfHeight < 0.25f;
|
|
// thump into the meadow: a puff of clippings
|
|
if (inGrass && !_wasInGrass && _fallV < -2.5f)
|
|
{
|
|
Fx.Splash(Planet.Surface(p), Mathf.Clamp01(-_fallV / 10f) + 0.3f);
|
|
Sfx.PlayAt(_fallV < -6f ? "splash_big" : "splash_small", p, 0.8f, 1.1f, 0.1f);
|
|
}
|
|
_wasInGrass = inGrass;
|
|
_fallV = rb.isKinematic ? 0f : Vector3.Dot(rb.linearVelocity, up);
|
|
if (!IsOwner || rb.isKinematic || _g.IsHeld || !inGrass) return;
|
|
// the thick grass brakes rolling cargo
|
|
rb.AddForce(-Vector3.ProjectOnPlane(rb.linearVelocity, up) * 2.2f, ForceMode.Acceleration);
|
|
rb.angularVelocity *= 0.96f;
|
|
}
|
|
}
|
|
}
|