using System.Collections.Generic; using UnityEngine; namespace AfterHours.Shop { /// /// Zeigt eine Stückzahl als einzelne Verpackungs-Prefabs unter einem /// Wurzel-Transform an (Regalfach, Lieferkarton) – ordentlich in einem /// Raster (Spalten x Reihen) gestapelt, weitere Lagen stapeln sich nach /// oben. Rein kosmetisch, läuft auf jedem Peer. Baut bei Mengenänderung /// nur das Delta neu, nicht die komplette Anzeige – wichtig beim /// Stück-für-Stück-Ausschütten. /// public sealed class PackageDisplay { private readonly Transform _root; private readonly int _columns; private readonly int _rows; private readonly Vector3 _spacing; private readonly Quaternion _rotation; private readonly List _instances = new(); private GameObject _prefab; public PackageDisplay(Transform root, int columns, int rows, Vector3 spacing, Vector3 rotationEuler = default) { _root = root; _columns = Mathf.Max(1, columns); _rows = Mathf.Max(1, rows); _spacing = spacing; _rotation = Quaternion.Euler(rotationEuler); } /// Baut die Anzeige auf Instanzen an (gedeckelt bei maxVisible). public void SetTarget(GameObject prefab, int count, int maxVisible) { if (_root == null) return; if (prefab != _prefab) { Clear(); _prefab = prefab; } if (_prefab == null) return; int target = Mathf.Clamp(count, 0, Mathf.Max(0, maxVisible)); while (_instances.Count < target) { var instance = Object.Instantiate(_prefab, _root); instance.transform.localPosition = SlotLocalPosition(_instances.Count); instance.transform.localRotation = _rotation; _instances.Add(instance); } while (_instances.Count > target) { int last = _instances.Count - 1; if (_instances[last] != null) Object.Destroy(_instances[last]); _instances.RemoveAt(last); } } /// Lokale Rasterposition für einen Index – auch als Flug-Ziel genutzt (siehe PlayerCarry). public Vector3 SlotLocalPosition(int index) { int perLayer = _columns * _rows; int layer = index / perLayer; int rem = index % perLayer; int row = rem / _columns; int col = rem % _columns; return new Vector3(col * _spacing.x, layer * _spacing.y, row * _spacing.z); } public void Clear() { foreach (var instance in _instances) if (instance != null) Object.Destroy(instance); _instances.Clear(); _prefab = null; } } }