Files
grass-cutters/Assets/Scripts/Gameplay/RobotDock.cs
NiemandKG d1c3761e58 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>
2026-11-06 18:44:40 +06:00

80 lines
2.6 KiB
C#

using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
namespace GrassCutter
{
/// <summary>A robot mower and its dock: it works the rings of sectors around the dock, and the dock ties what it brings into bundles for the belt.</summary>
public class RobotDock : Mowbot, IPorts
{
const float SiloRoom = 100f, UnloadRate = 5f;
public Transform bot;
public readonly NetworkVariable<float> Silo = new NetworkVariable<float>();
bool _unloading;
int _turn, _area;
protected override Transform Body => bot;
protected override bool NoseOnBack => true;
protected override Vector3 Origin => transform.position;
protected override int Reach => Def.reach;
protected override Vector3 HomeDir => Planet.Up(transform.position);
public override string Prompt
{
get
{
if (_area == 0) _area = Area();
return Loc.F("{0}: {1}, {2} of {3}, {4} sectors, dock holds {5}", Def.name, MoodText, GameDefs.Kilos(Hopper.Value), GameDefs.Kilos(HopperCap), _area, GameDefs.Kilos(Silo.Value));
}
}
public override float[] State
{
get => new[] { Hopper.Value, Dir.x, Dir.y, Dir.z, Silo.Value };
set
{
base.State = value;
if (value != null && value.Length > 4) Silo.Value = value[4];
}
}
public void Ports(List<Port> into)
{
into.Add(BeltNet.Front(transform));
}
protected override bool Stay(float dt)
{
BeltNet.Drain(GetComponent<BuildPiece>(), Silo, ref _turn);
if (_unloading)
{
float give = Mathf.Min(Hopper.Value, UnloadRate * dt, SiloRoom - Silo.Value);
if (give > 0f)
{
Hopper.Value -= give;
Silo.Value += give;
}
if (Hopper.Value >= 0.01f)
{
Mood.Value = (byte)(give > 0f ? RobotMood.Unloading : RobotMood.Waiting);
return true;
}
Hopper.Value = 0f;
_unloading = false;
}
if (!Homing && Hopper.Value >= HopperCap) GoHome();
return false;
}
protected override void Arrived() => _unloading = true;
protected override void NoWork()
{
if (Vector3.Dot(Dir, HomeDir) < 0.9999999f || Hopper.Value > 0f) GoHome();
else Mood.Value = (byte)RobotMood.Finished;
}
}
}