The prompts on the HUD and in build mode named E, R, T, LMB and the rest outright, so they were wrong for anyone who had set other keys. Sixteen of them now take the key from Binds.Cap: throw / drop / turn, hop off, piece upgrades, the build help strip, belt laying and moving, the pickup's bed, 'too far', and the three opening hints. The build help strip redraws when a key is set. The English text is the key of every translation, so the eleven tables and the translators' context notes carry the same placeholders (extract.py --strict: nothing missing, no placeholder mismatch). Turkish drops the case endings that hung on the key letter. Ctl: C no longer means 'down' in the cheat fly mode (it is Cruise; the console's help already said Ctrl), and ShakePressed, which nothing called, is gone. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
698 lines
32 KiB
C#
698 lines
32 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace GrassCutter
|
|
{
|
|
/// <summary>
|
|
/// Local build mode (Build Hammer): Q switches category, the wheel picks a piece, a green/red ghost with its footprint marked
|
|
/// on the ground follows the aim. Things only go on cut ground. LMB places: a belt takes two clicks, start and end, and goes on from there until it lands on a port or RMB
|
|
/// stops it. Ctrl+wheel sets a belt's height, R rotates, T moves a piece, the middle button copies, RMB removes (full refund).
|
|
/// On a foundation things sit on its half-metre grid; F places freely.
|
|
/// </summary>
|
|
public class PlayerBuild : MonoBehaviour
|
|
{
|
|
public static PlayerBuild Local { get; private set; }
|
|
|
|
const float Reach = 14f;
|
|
const int AimMask = (1 << Layers.Default) | (1 << Layers.Build);
|
|
|
|
PlayerAgent _p;
|
|
public bool Active { get; private set; }
|
|
public int Selected { get; private set; }
|
|
public PieceCat Category { get; private set; } = PieceCat.Logistics;
|
|
public string Reason { get; private set; }
|
|
public bool Valid { get; private set; }
|
|
public BuildPiece Target { get; private set; }
|
|
/// <summary>What the panel says instead of the piece's description.</summary>
|
|
public string Note { get; private set; }
|
|
/// <summary>Metres a belt stands above what it's laid on.</summary>
|
|
public float Height { get; set; } = GameDefs.BeltLowest;
|
|
/// <summary>Metres a new foundation is lifted off the ground onto stilts (Ctrl+wheel).</summary>
|
|
public float Raise { get; set; }
|
|
// the last spot was found by a wall, a floor or a roof snapping to its place
|
|
bool _snapped;
|
|
public bool Freehand { get; private set; }
|
|
public bool Laying { get; private set; }
|
|
public BuildPiece Moving { get; private set; }
|
|
|
|
readonly Dictionary<PieceId, Transform> _ghosts = new Dictionary<PieceId, Transform>();
|
|
Material _ghostOk, _ghostBad, _ghostRemove, _ghostIn, _lineOk, _lineBad;
|
|
Renderer[] _targetRenderers;
|
|
readonly List<Material[]> _targetMats = new List<Material[]>();
|
|
readonly List<Collider> _hidden = new List<Collider>();
|
|
|
|
Transform _runGhost;
|
|
BeltShape _runShape;
|
|
Vector3 _shapedA, _shapedB;
|
|
|
|
const int MostRings = 10, RingPoints = 48, MostMarks = 16, FramePoints = 36;
|
|
const float MarkRange = 14f, RingRange = 60f;
|
|
readonly List<LineRenderer> _rings = new List<LineRenderer>();
|
|
readonly List<Transform> _marks = new List<Transform>();
|
|
readonly List<Port> _ports = new List<Port>();
|
|
readonly Vector3[] _ring = new Vector3[RingPoints], _edge = new Vector3[FramePoints];
|
|
LineRenderer _frame;
|
|
float _nextMarks;
|
|
|
|
float _yaw;
|
|
Vector3 _from, _point, _up;
|
|
bool _aimed;
|
|
BuildPiece _on;
|
|
|
|
public event System.Action Changed;
|
|
|
|
void Awake() { _p = GetComponent<PlayerAgent>(); }
|
|
|
|
void Start()
|
|
{
|
|
if (!_p.IsOwner) { enabled = false; return; }
|
|
Local = this;
|
|
var baseMat = AssetBank.I.Mat("M_Ghost");
|
|
_ghostOk = GhostMat(baseMat, new Color(0.3f, 1f, 0.55f, 0.45f));
|
|
_ghostBad = GhostMat(baseMat, new Color(1f, 0.3f, 0.3f, 0.45f));
|
|
_ghostRemove = GhostMat(baseMat, new Color(1f, 0.25f, 0.25f, 0.55f));
|
|
_ghostIn = GhostMat(baseMat, new Color(0.3f, 0.7f, 1f, 0.6f));
|
|
_lineOk = new Material(_ghostOk);
|
|
_lineOk.SetFloat("_Flat", 1f);
|
|
_lineBad = new Material(_ghostBad);
|
|
_lineBad.SetFloat("_Flat", 1f);
|
|
}
|
|
|
|
void OnDestroy()
|
|
{
|
|
if (Local == this) Local = null;
|
|
foreach (var g in _ghosts.Values) if (g) Destroy(g.gameObject);
|
|
if (_runGhost) Destroy(_runGhost.gameObject);
|
|
_runShape?.Destroy();
|
|
RestoreTarget();
|
|
}
|
|
|
|
static Material GhostMat(Material src, Color c)
|
|
{
|
|
var m = new Material(src);
|
|
m.SetColor("_BaseColor", c);
|
|
return m;
|
|
}
|
|
|
|
public List<PieceDef> Available()
|
|
{
|
|
var l = new List<PieceDef>();
|
|
var ts = TeamState.Instance;
|
|
foreach (var d in GameDefs.Pieces) if (ts && d.cat == Category && !d.company) l.Add(d);
|
|
return l;
|
|
}
|
|
|
|
public bool CategoryUnlocked(PieceCat c)
|
|
{
|
|
var ts = TeamState.Instance;
|
|
if (!ts) return false;
|
|
foreach (var d in GameDefs.Pieces) if (d.cat == c && !d.company) return true;
|
|
return false;
|
|
}
|
|
|
|
void CycleCategory()
|
|
{
|
|
int all = (int)PieceCat.Count;
|
|
for (int k = 1; k <= all; k++)
|
|
{
|
|
var c = (PieceCat)(((int)Category + k) % all);
|
|
if (!CategoryUnlocked(c)) continue;
|
|
Category = c;
|
|
Selected = 0;
|
|
Laying = false;
|
|
Sfx.Play("ui_click", 0.6f, 1.1f);
|
|
Changed?.Invoke();
|
|
return;
|
|
}
|
|
}
|
|
|
|
public PieceDef Current
|
|
{
|
|
get
|
|
{
|
|
var l = Available();
|
|
if (l.Count == 0) return null;
|
|
return l[Mathf.Clamp(Selected, 0, l.Count - 1)];
|
|
}
|
|
}
|
|
|
|
/// <summary>Takes up a piece by name: the middle button and the test bot.</summary>
|
|
public void Select(PieceId id)
|
|
{
|
|
Category = GameDefs.Piece(id).cat;
|
|
Selected = Mathf.Max(0, Available().FindIndex(d => d.id == id));
|
|
Laying = false;
|
|
Changed?.Invoke();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
bool active = _p.IsSpawned && _p.Tools.Current == ToolId.Hammer && !_p.Driving.Value && !_p.Grab.Holding;
|
|
if (active != Active)
|
|
{
|
|
Active = active;
|
|
Laying = false;
|
|
Changed?.Invoke();
|
|
}
|
|
if (!active) { EndMove(); HideGhosts(); RestoreTarget(); ShowPorts(false); return; }
|
|
|
|
if (Ctl.CategoryPressed && !Moving) CycleCategory();
|
|
var list = Available();
|
|
if (list.Count == 0) { CycleCategory(); list = Available(); }
|
|
if (list.Count == 0) { HideGhosts(); return; }
|
|
Selected = Mathf.Clamp(Selected, 0, list.Count - 1);
|
|
var def = list[Selected];
|
|
|
|
float scroll = Ctl.Scroll;
|
|
if (Mathf.Abs(scroll) > 0.01f && !Moving)
|
|
{
|
|
if (Ctl.CtrlHeld && GameDefs.IsFoundation(def.id)) Raise = Mathf.Clamp(Raise + Mathf.Sign(scroll) * 0.25f, 0f, Foundation.MaxRaise);
|
|
else if (Ctl.CtrlHeld) Height = Mathf.Clamp(Height + Mathf.Sign(scroll) * 0.2f, GameDefs.BeltLowest, GameDefs.BeltHighest);
|
|
else
|
|
{
|
|
Selected = (Selected + (scroll < 0 ? 1 : -1) + list.Count) % list.Count;
|
|
def = list[Selected];
|
|
Laying = false;
|
|
Changed?.Invoke();
|
|
}
|
|
Sfx.Play("ui_hover", 0.5f, 1.3f);
|
|
}
|
|
if (Ctl.RotatePressed)
|
|
{
|
|
_yaw = Mathf.Repeat(_yaw + Step(Moving ? Moving.piece : def.id), 360f);
|
|
Sfx.Play("build_rotate", 0.8f);
|
|
}
|
|
if (Ctl.FreePressed)
|
|
{
|
|
Freehand = !Freehand;
|
|
Sfx.Play("ui_click", 0.6f, 1.1f);
|
|
}
|
|
|
|
Aim();
|
|
Note = null;
|
|
|
|
if (Moving) { UpdateMove(); return; }
|
|
|
|
HighlightTarget(Target && !_on && !Laying ? Target : null);
|
|
if (Ctl.AltPressed)
|
|
{
|
|
if (Laying) { Laying = false; return; }
|
|
if (Target) { Remove(Target); return; }
|
|
}
|
|
if (Target && !Laying)
|
|
{
|
|
if (Ctl.CopyPressed && !Target.Def.company)
|
|
{
|
|
Select(Target.piece);
|
|
if (Target.Run) Height = Mathf.Clamp(Planet.Altitude(Target.transform.position), GameDefs.BeltLowest, GameDefs.BeltHighest);
|
|
if (GameDefs.IsFoundation(Target.piece)) Raise = Mathf.Clamp(Mathf.Round(Planet.Altitude(Target.transform.position) * 4f) / 4f, 0f, Foundation.MaxRaise);
|
|
Sfx.Play("ui_click", 0.6f, 1.1f);
|
|
return;
|
|
}
|
|
if (Ctl.MovePressed && !Target.Def.company && !Target.Run) { BeginMove(Target); return; }
|
|
}
|
|
|
|
if (def.id == PieceId.Belt) UpdateBelt(def);
|
|
else UpdatePiece(def);
|
|
ShowPorts(def.cat == PieceCat.Logistics);
|
|
}
|
|
|
|
float Step(PieceId id) => GameDefs.FitOf(id) == Fit.Edge ? 180f : id == PieceId.Hex ? 30f : GameDefs.IsSquareDeck(id) || _on || GameDefs.FitOf(id) == Fit.Top ? 90f : 45f;
|
|
|
|
// ───────────────────────── aim ─────────────────────────
|
|
|
|
/// <summary>The soil has no collider: the ray walks down to it, and whatever solid it meets first wins.</summary>
|
|
void Aim()
|
|
{
|
|
_aimed = false;
|
|
Target = null;
|
|
_on = null;
|
|
var cam = CameraFx.Cam;
|
|
if (!cam) return;
|
|
var from = cam.transform.position;
|
|
var dir = cam.transform.forward;
|
|
bool solid = Physics.Raycast(from, dir, out var hit, Reach, AimMask, QueryTriggerInteraction.Ignore);
|
|
if (Planet.Cast(from, dir, solid ? hit.distance : Reach, out var soil))
|
|
{
|
|
_point = soil;
|
|
_up = Planet.Up(soil);
|
|
_aimed = true;
|
|
return;
|
|
}
|
|
if (!solid) return;
|
|
_point = hit.point;
|
|
_up = Planet.Up(hit.point);
|
|
Target = hit.collider.GetComponentInParent<BuildPiece>();
|
|
bool level = Vector3.Dot(hit.normal, _up) > 0.7f;
|
|
if (Target && GameDefs.IsDeck(Target.piece) && level) _on = Target;
|
|
_aimed = level || Target;
|
|
}
|
|
|
|
static Vector3 Heading(Vector3 up)
|
|
{
|
|
var h = Vector3.ProjectOnPlane(Vector3.forward, up);
|
|
return h.sqrMagnitude > 1e-3f ? h.normalized : Vector3.ProjectOnPlane(Vector3.right, up).normalized;
|
|
}
|
|
|
|
/// <summary>Where a piece aimed at this point would stand.</summary>
|
|
void Spot(PieceId id, out Vector3 at, out Quaternion rot)
|
|
{
|
|
bool slab = GameDefs.IsDeck(id);
|
|
var fit = GameDefs.FitOf(id);
|
|
_snapped = true;
|
|
if (!Freehand && fit == Fit.Top && Snap.OnTop(id, _point, Target, _on, _yaw, out at, out rot)) return;
|
|
if (!Freehand && fit == Fit.Edge && Snap.Edge(_point, _on, _yaw, out at, out rot)) return;
|
|
if (slab && !Freehand && Foundation.Beside(_point, id, out at, out rot)) return;
|
|
_snapped = false;
|
|
if (_on && !slab)
|
|
{
|
|
at = Freehand ? _point : Foundation.Grid(_on.transform, _point);
|
|
rot = _on.transform.rotation * Quaternion.Euler(0f, _yaw, 0f);
|
|
return;
|
|
}
|
|
at = _point;
|
|
rot = Quaternion.LookRotation(Quaternion.AngleAxis(_yaw, _up) * Heading(_up), _up);
|
|
// a foundation stays level (it is a floor to build on); everything else sits on the slope it stands on
|
|
if (GameDefs.IsFoundation(id)) at += _up * Raise;
|
|
else if (!_on) Lean(GameDefs.Piece(id), ref at, ref rot);
|
|
}
|
|
|
|
/// <summary>The most a piece leans with the ground under it.</summary>
|
|
const float MaxLean = 22f;
|
|
|
|
/// <summary>
|
|
/// Tilts a piece standing on the soil to the ground under its footprint (the plane through its four corners),
|
|
/// then lowers it until no corner hangs in the air.
|
|
/// </summary>
|
|
internal static void Lean(PieceDef d, ref Vector3 at, ref Quaternion rot)
|
|
{
|
|
if (d == null || Planet.Altitude(at) > 0.3f) return;
|
|
var up = Planet.Up(at);
|
|
float hx = Mathf.Max(0.5f, d.sizeX * 0.5f), hz = Mathf.Max(0.5f, d.sizeZ * 0.5f);
|
|
var r = rot * Vector3.right * hx;
|
|
var f = rot * Vector3.forward * hz;
|
|
var fr = Planet.Surface(at + r + f);
|
|
var fl = Planet.Surface(at - r + f);
|
|
var bl = Planet.Surface(at - r - f);
|
|
var br = Planet.Surface(at + r - f);
|
|
var n = Vector3.Cross(fr + fl - bl - br, fr + br - fl - bl);
|
|
if (n.sqrMagnitude < 1e-6f) return;
|
|
n.Normalize();
|
|
if (Vector3.Dot(n, up) < 0f) n = -n;
|
|
n = Vector3.RotateTowards(up, n, MaxLean * Mathf.Deg2Rad, 0f).normalized;
|
|
var mid = (fr + fl + bl + br) * 0.25f;
|
|
var c = Planet.Surface(at);
|
|
var p = mid + Vector3.ProjectOnPlane(c - mid, n);
|
|
// sink to the lowest of the corners and the middle, a little more than that so no gap shows
|
|
float drop = 0f;
|
|
foreach (var s in new[] { fr, fl, bl, br, c }) drop = Mathf.Min(drop, Vector3.Dot(s - p, n));
|
|
at = p + n * (Mathf.Max(drop, -0.6f) - 0.04f);
|
|
var fwd = Vector3.ProjectOnPlane(rot * Vector3.forward, n);
|
|
if (fwd.sqrMagnitude > 1e-6f) rot = Quaternion.LookRotation(fwd.normalized, n);
|
|
}
|
|
|
|
// ───────────────────────── pieces ─────────────────────────
|
|
|
|
void UpdatePiece(PieceDef def)
|
|
{
|
|
var fit = GameDefs.FitOf(def.id);
|
|
// a floor or a roof is aimed at the wall it rests on
|
|
if (!_aimed || (Target && !_on && !(fit == Fit.Top && GameDefs.Holds(Target.piece)))) { Idle(); return; }
|
|
Spot(def.id, out var at, out var rot);
|
|
Valid = BuildRules.CanPlace(def, at, rot, null, out var reason);
|
|
if (fit == Fit.Top && !_snapped) { Valid = false; reason = def.id == PieceId.Roof ? Loc.T("Aim at the top of a wall, or stand it on a floor") : Loc.T("Aim at the top of a wall: it rests on the walls below"); }
|
|
var ts = TeamState.Instance;
|
|
if (Valid && ts && ts.Money.Value < def.price) { Valid = false; reason = Loc.T("Not enough cash"); }
|
|
Reason = reason;
|
|
ShowGhostAt(def, at, rot, Valid);
|
|
ShowFrame(def, at, rot, Valid);
|
|
ShowReach(def, at);
|
|
if (def.kw > 0f) Note = PowerGrid.Covers(at) ? Loc.T("Power reaches here") : Loc.T("No power here yet: it needs a panel or a pole nearby");
|
|
else if (GameDefs.IsFoundation(def.id)) Note = _snapped ? Loc.T("Lines up with its neighbour") : Raise > 0f ? Loc.F("On stilts, {0} m up · Ctrl+wheel", Loc.Num(Raise, "0.##")) : Loc.T("Ctrl+wheel lifts it onto stilts");
|
|
else if (fit == Fit.Edge) Note = _snapped ? Loc.T("On the edge of the deck · R turns it round") : Loc.T("Free-standing · aim near a deck to put it on its edge");
|
|
if (!Ctl.UsePressed) return;
|
|
if (Valid) { ts.BuildRpc((byte)def.id, at, rot, Vector3.zero); CameraFx.Shake(0.06f); _p.Tools.Struck(); }
|
|
else Refuse(reason);
|
|
}
|
|
|
|
void Idle()
|
|
{
|
|
HideGhosts();
|
|
Valid = false;
|
|
Reason = Target ? (Target.Def.company ? Loc.T("Company property") : Target.Run ? Loc.F("{0:0.#} m of belt · {1} copy · {2} remove", Target.Run.Metres, Binds.Cap(Act.Copy), Binds.Cap(Act.Alt))
|
|
: Loc.F("{0} move · {1} copy · {2} remove", Binds.Cap(Act.MovePiece), Binds.Cap(Act.Copy), Binds.Cap(Act.Alt))) : null;
|
|
}
|
|
|
|
static void Refuse(string reason)
|
|
{
|
|
Sfx.Play("error", 0.6f);
|
|
if (!string.IsNullOrEmpty(reason)) Hud.Toast(reason, Hud.ToastKind.Warn);
|
|
}
|
|
|
|
void Remove(BuildPiece p)
|
|
{
|
|
if (GameDefs.IsDeck(p.piece) && BuildRules.Riders(p).Count > 0) { Refuse(Loc.T("Clear the deck first")); return; }
|
|
TeamState.Instance?.RemovePieceRpc(p.NetworkObjectId);
|
|
CameraFx.Shake(0.1f);
|
|
_p.Tools.Struck();
|
|
}
|
|
|
|
// ───────────────────────── belts ─────────────────────────
|
|
|
|
/// <summary>A point in the air above where you aim, on the foundation's grid if there is one.</summary>
|
|
Vector3 Lifted()
|
|
{
|
|
var at = _on && !Freehand ? Foundation.Grid(_on.transform, _point) : _point;
|
|
return at + Planet.Up(at) * Height;
|
|
}
|
|
|
|
void UpdateBelt(PieceDef def)
|
|
{
|
|
var ts = TeamState.Instance;
|
|
if (!Laying)
|
|
{
|
|
bool port = BeltNet.Nearest(_aimed ? _point : transform.position, false, BeltNet.Snap, null, out _, out _, out var from);
|
|
if (!_aimed || (Target && !_on && !port)) { Idle(); return; }
|
|
var at = port ? from.at : Lifted();
|
|
Valid = port || BuildRules.CutUnder(at);
|
|
Reason = Valid ? null : BuildRules.Uncut;
|
|
Note = port ? Loc.F("{0}: go on from this end", Binds.Cap(Act.Use)) : Loc.F("{1} where the belt starts · height {0:0.0} m", Height, Binds.Cap(Act.Use));
|
|
ShowRun(at, at + (port ? from.dir : Heading(Planet.Up(at))), Valid);
|
|
if (!Ctl.UsePressed) return;
|
|
if (Valid) { _from = at; Laying = true; }
|
|
else Refuse(Reason);
|
|
return;
|
|
}
|
|
if (!_aimed) { HideGhosts(); Valid = false; Reason = null; return; }
|
|
bool landed = BeltNet.Nearest(_point, true, BeltNet.Snap, null, out _, out _, out var into) && !BeltNet.Taken(into.at);
|
|
var to = landed ? into.at : Lifted();
|
|
// run into the side of another belt
|
|
if (!landed && Target && Target.Run)
|
|
{
|
|
var l = Target.transform.InverseTransformPoint(_point);
|
|
to = Target.transform.TransformPoint(new Vector3(0f, 0f, Mathf.Clamp(l.z, 0f, Target.Run.Metres)));
|
|
landed = true;
|
|
}
|
|
Valid = BuildRules.CanRun(_from, to, out var reason);
|
|
double price = GameDefs.BeltPrice(Vector3.Distance(_from, to));
|
|
if (Valid && ts && ts.Money.Value < price) { Valid = false; reason = Loc.T("Not enough cash"); }
|
|
Reason = reason;
|
|
Note = Loc.F("{0:0.#} m, {1} · height {2:0.0} m · {3} stops", Vector3.Distance(_from, to), UIK.Money(price), Height, Binds.Cap(Act.Alt));
|
|
ShowRun(_from, to, Valid);
|
|
if (!Ctl.UsePressed) return;
|
|
if (!Valid) { Refuse(reason); return; }
|
|
ts.BuildRpc((byte)def.id, _from, BuildRules.RunRotation(_from, to), to);
|
|
CameraFx.Shake(0.06f);
|
|
_p.Tools.Struck();
|
|
_from = to;
|
|
if (landed) Laying = false;
|
|
}
|
|
|
|
void ShowRun(Vector3 a, Vector3 b, bool ok)
|
|
{
|
|
foreach (var kv in _ghosts) if (kv.Value) kv.Value.gameObject.SetActive(false);
|
|
if (_frame) _frame.enabled = false;
|
|
if (!_runGhost)
|
|
{
|
|
_runGhost = new GameObject("Ghost_Belt").transform;
|
|
_runShape = new BeltShape(_runGhost, gameObject.layer);
|
|
}
|
|
_runGhost.gameObject.SetActive(true);
|
|
if (a != _shapedA || b != _shapedB)
|
|
{
|
|
_shapedA = a; _shapedB = b;
|
|
_runGhost.SetPositionAndRotation(a, BuildRules.RunRotation(a, b));
|
|
// it bends out of the port it starts from, and round into the belt or port it ends on, as the built belt will
|
|
_runShape.Fit(a, b, BeltRun.JointAt(b, true, null, null), BeltRun.JointAt(a, false, null, null));
|
|
}
|
|
_runShape.Paint(ok ? _ghostOk : _ghostBad);
|
|
}
|
|
|
|
// ───────────────────────── what you're building toward ─────────────────────────
|
|
|
|
/// <summary>Rings on the ground: how far this power piece would reach, and how far the ones nearby do.</summary>
|
|
void ShowReach(PieceDef def, Vector3 at)
|
|
{
|
|
int used = 0;
|
|
if (def.cat == PieceCat.Power || def.kw > 0f)
|
|
{
|
|
float own = GameDefs.Reach(def.id);
|
|
if (own > 0f) Ring(used++, at, own);
|
|
foreach (var n in PowerNode.All)
|
|
{
|
|
if (used >= MostRings) break;
|
|
if (n && Vector3.Distance(n.transform.position, at) < RingRange) Ring(used++, n.transform.position, n.Radius);
|
|
}
|
|
}
|
|
for (int i = used; i < _rings.Count; i++) _rings[i].enabled = false;
|
|
}
|
|
|
|
void Ring(int i, Vector3 at, float radius)
|
|
{
|
|
if (i >= _rings.Count)
|
|
{
|
|
var go = new GameObject("Reach");
|
|
go.transform.SetParent(transform.parent, false);
|
|
var made = go.AddComponent<LineRenderer>();
|
|
made.sharedMaterial = _lineOk;
|
|
made.widthMultiplier = 0.14f;
|
|
made.positionCount = RingPoints;
|
|
made.loop = true;
|
|
made.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
|
|
_rings.Add(made);
|
|
}
|
|
var line = _rings[i];
|
|
line.enabled = true;
|
|
var up = Planet.Up(at);
|
|
var side = Vector3.Cross(up, Vector3.ProjectOnPlane(Vector3.forward, up).sqrMagnitude > 1e-3f ? Vector3.forward : Vector3.right).normalized;
|
|
var ahead = Vector3.Cross(side, up);
|
|
for (int k = 0; k < RingPoints; k++)
|
|
{
|
|
float a = k * Mathf.PI * 2f / RingPoints;
|
|
var p = at + (side * Mathf.Cos(a) + ahead * Mathf.Sin(a)) * radius;
|
|
_ring[k] = Planet.Surface(p) + Planet.Up(p) * 0.3f;
|
|
}
|
|
line.SetPositions(_ring);
|
|
}
|
|
|
|
/// <summary>The piece's outline on the ground, so you can see what has to be cut for it.</summary>
|
|
void ShowFrame(PieceDef def, Vector3 at, Quaternion rot, bool ok)
|
|
{
|
|
if (!_frame)
|
|
{
|
|
var go = new GameObject("Footprint");
|
|
go.transform.SetParent(transform.parent, false);
|
|
_frame = go.AddComponent<LineRenderer>();
|
|
_frame.widthMultiplier = 0.09f;
|
|
_frame.positionCount = FramePoints;
|
|
_frame.loop = true;
|
|
_frame.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
|
|
}
|
|
_frame.enabled = true;
|
|
_frame.sharedMaterial = ok ? _lineOk : _lineBad;
|
|
bool hex = def.id == PieceId.Hex, raised = Planet.Altitude(at) > 0.2f;
|
|
int sides = hex ? 6 : 4;
|
|
Vector3 Corner(int i)
|
|
{
|
|
float a = (hex ? 30f + i * 60f : 45f + i * 90f) * Mathf.Deg2Rad;
|
|
float x = Mathf.Sin(a), z = Mathf.Cos(a);
|
|
if (hex) return at + rot * new Vector3(x, 0f, z) * Foundation.Across / Mathf.Sqrt(3f);
|
|
return at + rot * new Vector3(x * def.sizeX, 0f, z * def.sizeZ) * Mathf.Sqrt(0.5f);
|
|
}
|
|
for (int k = 0; k < FramePoints; k++)
|
|
{
|
|
float along = k * sides / (float)FramePoints;
|
|
int side = (int)along;
|
|
var p = Vector3.Lerp(Corner(side), Corner(side + 1), along - side);
|
|
_edge[k] = (raised ? p : Planet.Surface(p)) + Planet.Up(p) * 0.05f;
|
|
}
|
|
_frame.SetPositions(_edge);
|
|
}
|
|
|
|
/// <summary>Arrows over the ports that are still free: green where a belt can start, blue where one can end.</summary>
|
|
void ShowPorts(bool show)
|
|
{
|
|
if (Time.time < _nextMarks && show) return;
|
|
_nextMarks = Time.time + 0.2f;
|
|
int used = 0;
|
|
if (show && AssetBank.I)
|
|
foreach (var p in BuildPiece.All)
|
|
{
|
|
if (!p || !p.IsSpawned || used >= MostMarks || GameDefs.IsFoundation(p.piece)) continue;
|
|
if (Vector3.Distance(p.transform.position, _point) > MarkRange + (p.Run ? p.Run.Metres : 0f)) continue;
|
|
_ports.Clear();
|
|
p.Ports(_ports);
|
|
foreach (var port in _ports)
|
|
{
|
|
if (used >= MostMarks || Vector3.Distance(port.at, _point) > MarkRange) continue;
|
|
bool free = port.input ? !BeltNet.Taken(port.at) : !BeltNet.Nearest(port.at, true, BeltNet.Joined, p, out _, out _, out _);
|
|
if (free) Mark(used++, port);
|
|
}
|
|
}
|
|
for (int i = used; i < _marks.Count; i++) if (_marks[i]) _marks[i].gameObject.SetActive(false);
|
|
}
|
|
|
|
void Mark(int i, Port port)
|
|
{
|
|
var model = AssetBank.I.Model("SM_IconArrow");
|
|
if (!model) return;
|
|
if (i >= _marks.Count)
|
|
{
|
|
var made = Instantiate(model, transform.parent).transform;
|
|
foreach (var c in made.GetComponentsInChildren<Collider>(true)) Destroy(c);
|
|
foreach (var r in made.GetComponentsInChildren<Renderer>(true)) r.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
|
|
made.localScale = Vector3.one * 0.55f;
|
|
_marks.Add(made);
|
|
}
|
|
var mark = _marks[i];
|
|
var up = Planet.Up(port.at);
|
|
mark.gameObject.SetActive(true);
|
|
mark.SetPositionAndRotation(port.at + up * 0.55f, Quaternion.LookRotation(port.dir, up) * Quaternion.Euler(90f, 0f, 0f) * model.transform.localRotation);
|
|
var mat = port.input ? _ghostIn : _ghostOk;
|
|
foreach (var r in mark.GetComponentsInChildren<Renderer>())
|
|
{
|
|
var mats = r.sharedMaterials;
|
|
for (int k = 0; k < mats.Length; k++) mats[k] = mat;
|
|
r.sharedMaterials = mats;
|
|
}
|
|
}
|
|
|
|
// ───────────────────────── moving ─────────────────────────
|
|
|
|
void BeginMove(BuildPiece p)
|
|
{
|
|
Moving = p;
|
|
RestoreTarget();
|
|
Hide(p);
|
|
if (GameDefs.IsDeck(p.piece)) foreach (var r in BuildRules.Riders(p)) Hide(r);
|
|
_yaw = 0f;
|
|
Sfx.Play("ui_click", 0.6f, 1.1f);
|
|
}
|
|
|
|
/// <summary>The aim passes through what's being carried.</summary>
|
|
void Hide(BuildPiece p)
|
|
{
|
|
foreach (var c in p.GetComponentsInChildren<Collider>())
|
|
if (c.enabled) { c.enabled = false; _hidden.Add(c); }
|
|
}
|
|
|
|
void EndMove()
|
|
{
|
|
Moving = null;
|
|
foreach (var c in _hidden) if (c) c.enabled = true;
|
|
_hidden.Clear();
|
|
}
|
|
|
|
void UpdateMove()
|
|
{
|
|
var p = Moving;
|
|
if (!p || !p.IsSpawned || Ctl.AltPressed) { EndMove(); HideGhosts(); return; }
|
|
Note = Loc.F("Moving {0}: {1} put down · {2} rotate · {3} cancel", p.Def.name, Binds.Cap(Act.Use), Binds.Cap(Act.Rotate), Binds.Cap(Act.Alt));
|
|
if (!_aimed) { HideGhosts(); Valid = false; Reason = null; return; }
|
|
Spot(p.piece, out var at, out var rot);
|
|
Valid = BuildRules.CanPlace(p.Def, at, rot, p, out var reason);
|
|
Reason = reason;
|
|
ShowGhostAt(p.Def, at, rot, Valid);
|
|
ShowFrame(p.Def, at, rot, Valid);
|
|
if (!Ctl.UsePressed) return;
|
|
if (!Valid) { Refuse(reason); return; }
|
|
TeamState.Instance.MovePieceRpc(p.NetworkObjectId, at, rot);
|
|
_p.Tools.Struck();
|
|
EndMove();
|
|
HideGhosts();
|
|
}
|
|
|
|
// ───────────────────────── ghosts ─────────────────────────
|
|
|
|
void ShowGhostAt(PieceDef def, Vector3 target, Quaternion rot, bool ok)
|
|
{
|
|
if (_runGhost) _runGhost.gameObject.SetActive(false);
|
|
foreach (var kv in _ghosts) if (kv.Key != def.id && kv.Value) kv.Value.gameObject.SetActive(false);
|
|
if (!_ghosts.TryGetValue(def.id, out var g) || !g)
|
|
{
|
|
g = MakeGhost(def);
|
|
_ghosts[def.id] = g;
|
|
}
|
|
if (!g) return;
|
|
g.gameObject.SetActive(true);
|
|
g.position = Vector3.Distance(g.position, target) > 3f ? target : Vector3.Lerp(g.position, target, Time.deltaTime * 25f);
|
|
g.rotation = Quaternion.Slerp(g.rotation, rot, Time.deltaTime * 20f);
|
|
var mat = ok ? _ghostOk : _ghostBad;
|
|
foreach (var r in g.GetComponentsInChildren<Renderer>())
|
|
{
|
|
var mats = r.sharedMaterials;
|
|
if (mats.Length > 0 && mats[0] == mat) continue;
|
|
for (int i = 0; i < mats.Length; i++) mats[i] = mat;
|
|
r.sharedMaterials = mats;
|
|
}
|
|
float pulse = 1f + Mathf.Sin(Time.time * 8f) * 0.015f;
|
|
g.localScale = new Vector3(pulse, pulse, pulse);
|
|
}
|
|
|
|
Transform MakeGhost(PieceDef def)
|
|
{
|
|
var root = new GameObject("Ghost_" + def.id).transform;
|
|
if (GameDefs.IsPanel(def.id))
|
|
{
|
|
SolarPanel.Build(root, def.id);
|
|
return root;
|
|
}
|
|
var model = AssetBank.I.Model(def.model);
|
|
if (!model) return root;
|
|
var inst = Instantiate(model, root);
|
|
inst.transform.localPosition = Vector3.zero;
|
|
inst.transform.localScale *= def.scale;
|
|
foreach (var c in inst.GetComponentsInChildren<Collider>(true)) Destroy(c);
|
|
foreach (var r in inst.GetComponentsInChildren<Renderer>(true)) r.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
|
|
// flow arrow so you can see which way it runs
|
|
var arrowModel = AssetBank.I.Model("SM_IconArrow");
|
|
if (arrowModel && def.cat == PieceCat.Logistics)
|
|
{
|
|
var a = Instantiate(arrowModel, root);
|
|
a.transform.localPosition = new Vector3(0f, 1.0f, 0f);
|
|
a.transform.localRotation = Quaternion.Euler(90f, 0f, 0f) * arrowModel.transform.localRotation;
|
|
a.transform.localScale = Vector3.one * 0.9f;
|
|
foreach (var c in a.GetComponentsInChildren<Collider>(true)) Destroy(c);
|
|
}
|
|
return root;
|
|
}
|
|
|
|
void HideGhosts()
|
|
{
|
|
foreach (var g in _ghosts.Values) if (g) g.gameObject.SetActive(false);
|
|
if (_runGhost) _runGhost.gameObject.SetActive(false);
|
|
if (_frame) _frame.enabled = false;
|
|
foreach (var r in _rings) r.enabled = false;
|
|
}
|
|
|
|
BuildPiece _highlighted;
|
|
|
|
void HighlightTarget(BuildPiece p)
|
|
{
|
|
if (p == _highlighted) return;
|
|
RestoreTarget();
|
|
_highlighted = p;
|
|
if (!p) return;
|
|
_targetRenderers = p.GetComponentsInChildren<Renderer>();
|
|
_targetMats.Clear();
|
|
foreach (var r in _targetRenderers)
|
|
{
|
|
_targetMats.Add(r.sharedMaterials);
|
|
var mats = r.sharedMaterials;
|
|
for (int i = 0; i < mats.Length; i++) mats[i] = _ghostRemove;
|
|
r.sharedMaterials = mats;
|
|
}
|
|
}
|
|
|
|
void RestoreTarget()
|
|
{
|
|
if (_highlighted && _targetRenderers != null)
|
|
for (int i = 0; i < _targetRenderers.Length && i < _targetMats.Count; i++)
|
|
if (_targetRenderers[i]) _targetRenderers[i].sharedMaterials = _targetMats[i];
|
|
_highlighted = null;
|
|
_targetRenderers = null;
|
|
_targetMats.Clear();
|
|
}
|
|
}
|
|
}
|