This commit is contained in:
Savya Bikram Shah
2026-06-07 16:07:30 +05:45
parent 521b5c3b7d
commit 0b9d0ca8f2
5 changed files with 372 additions and 101 deletions

View File

@@ -107,31 +107,32 @@ namespace Darkmatter.Features.ShapeBuilder.Systems
_bus.Publish(new ShapeBuilderStartedSignal(template.Id));
var colorByShapeId = BuildColorMap(template, slots);
// Distinct color per element — every piece and slot gets its own color,
// even when two pieces share the same shape. Hues are stepped by the
// golden ratio so successive colors land far apart on the wheel.
float hue = UnityEngine.Random.value;
foreach (var s in slots)
if (s != null && s.Shape != null && colorByShapeId.TryGetValue(s.Shape.Id, out var c))
s.SetColor(c);
if (s != null)
s.SetColor(NextDistinctColor(ref hue));
CreateShapePieceInstances(template, preSnappedIds, count, slots, colorByShapeId);
CreateShapePieceInstances(template, preSnappedIds, count, slots, ref hue);
CheckIfShapeAssembled();
}
private static Dictionary<string, Color> BuildColorMap(IDrawingTemplate template, SlotMarker[] slots)
// Conjugate of the golden ratio; adding it (mod 1) to a hue each step
// produces a maximally-spread, low-collision sequence of distinct colors.
private const float GoldenHueStep = 0.61803398875f;
private static Color NextDistinctColor(ref float hue)
{
var map = new Dictionary<string, Color>();
foreach (var p in template.Pieces)
if (p != null && !string.IsNullOrEmpty(p.Id) && !map.ContainsKey(p.Id))
map[p.Id] = Color.HSVToRGB(UnityEngine.Random.value, 0.7f, 0.95f);
foreach (var s in slots)
if (s != null && s.Shape != null && !string.IsNullOrEmpty(s.Shape.Id) && !map.ContainsKey(s.Shape.Id))
map[s.Shape.Id] = Color.HSVToRGB(UnityEngine.Random.value, 0.7f, 0.95f);
return map;
hue = Mathf.Repeat(hue + GoldenHueStep, 1f);
return Color.HSVToRGB(hue, 0.7f, 0.95f);
}
private void CreateShapePieceInstances(IDrawingTemplate template, IReadOnlyCollection<string> preSnappedIds,
int count,
SlotMarker[] slots, Dictionary<string, Color> colorByShapeId)
SlotMarker[] slots, ref float hue)
{
var preSnapCounts = new Dictionary<string, int>();
if (preSnappedIds != null)
@@ -152,8 +153,7 @@ namespace Darkmatter.Features.ShapeBuilder.Systems
}
var piece = _factory.Create(_piecePrefab, shape, candidates, Vector2.zero);
if (colorByShapeId != null && colorByShapeId.TryGetValue(shape.Id, out var c))
piece.SetColor(c);
piece.SetColor(NextDistinctColor(ref hue));
_pieces.Add(piece);
if (preSnapCounts.TryGetValue(shape.Id, out var remaining) && remaining > 0)