using System.Collections.Generic;
using UnityEngine;
namespace GrassCutter
{
///
/// 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.
///
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; }
/// What the panel says instead of the piece's description.
public string Note { get; private set; }
/// Metres a belt stands above what it's laid on.
public float Height { get; set; } = GameDefs.BeltLowest;
/// Metres a new foundation is lifted off the ground onto stilts (Ctrl+wheel).
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 _ghosts = new Dictionary();
Material _ghostOk, _ghostBad, _ghostRemove, _ghostIn, _lineOk, _lineBad;
Renderer[] _targetRenderers;
readonly List _targetMats = new List();
readonly List _hidden = new List();
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 _rings = new List();
readonly List _marks = new List();
readonly List _ports = new List();
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(); }
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 Available()
{
var l = new List();
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)];
}
}
/// Takes up a piece by name: the middle button and the test bot.
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 ─────────────────────────
/// The soil has no collider: the ray walks down to it, and whatever solid it meets first wins.
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();
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;
}
/// Where a piece aimed at this point would stand.
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;
if (GameDefs.IsFoundation(id)) at += _up * Raise;
rot = Quaternion.LookRotation(Quaternion.AngleAxis(_yaw, _up) * Heading(_up), _up);
}
// ───────────────────────── 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 · MMB copy · RMB remove", Target.Run.Metres) : Loc.T("T move · MMB copy · RMB remove")) : 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 ─────────────────────────
/// A point in the air above where you aim, on the foundation's grid if there is one.
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.T("LMB: go on from this end") : Loc.F("LMB where the belt starts · height {0:0.0} m", Height);
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 · RMB stops", Vector3.Distance(_from, to), UIK.Money(price), Height);
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 ─────────────────────────
/// Rings on the ground: how far this power piece would reach, and how far the ones nearby do.
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();
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);
}
/// The piece's outline on the ground, so you can see what has to be cut for it.
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();
_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);
}
/// Arrows over the ports that are still free: green where a belt can start, blue where one can end.
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(true)) Destroy(c);
foreach (var r in made.GetComponentsInChildren(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())
{
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);
}
/// The aim passes through what's being carried.
void Hide(BuildPiece p)
{
foreach (var c in p.GetComponentsInChildren())
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}: LMB put down · R rotate · RMB cancel", p.Def.name);
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())
{
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(true)) Destroy(c);
foreach (var r in inst.GetComponentsInChildren(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(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();
_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();
}
}
}