using UnityEngine;
namespace GrassCutter
{
/// A belt piece with a choice to make: the modes it can switch between.
public interface IJunction
{
int ModeCount { get; }
string ModeName(int mode);
Color ModeColor(int mode);
}
/// E on a splitter or a merger switches what it does, and the little sign on it shows which.
public class Junction : MonoBehaviour, IInteractable
{
public Renderer sign;
BuildPiece _piece;
IJunction _junction;
MaterialPropertyBlock _mpb;
int _shown = -1;
static readonly int BaseColor = Shader.PropertyToID("_BaseColor");
BuildPiece Piece => _piece ? _piece : (_piece = GetComponent());
IJunction Parts => _junction ?? (_junction = GetComponentInChildren());
public string Prompt => Loc.F("{0}: {1} (E changes it)", Piece.Def.name, Parts.ModeName(Piece.Mode.Value));
public bool CanInteract => true;
public void Interact()
{
Piece.NextModeRpc(Parts.ModeCount);
Sfx.Play("ui_click", 0.7f);
}
void Update()
{
int mode = Piece.Mode.Value;
if (mode == _shown || !sign) return;
_shown = mode;
if (_mpb == null) _mpb = new MaterialPropertyBlock();
sign.GetPropertyBlock(_mpb);
_mpb.SetColor(BaseColor, Parts.ModeColor(mode));
sign.SetPropertyBlock(_mpb);
}
}
}