added item probability

This commit is contained in:
2026-10-24 20:32:37 +02:00
parent 1996b9c83e
commit 471dc33f0e
132 changed files with 99 additions and 36 deletions
+17 -4
View File
@@ -39,16 +39,29 @@ public sealed class ItemRegistry
}
}
/// <summary>Decides whether a new piece carries an item and in which of its minos.</summary>
/// <summary>Decides which minos of a new piece carry an item. Each mino rolls <c>chance</c> (0..1) on its own.</summary>
public sealed class ItemSpawner(ItemRegistry registry, double chance, Random random)
{
private const int MinosPerPiece = 4;
private readonly double _chance = double.IsNaN(chance) ? 0 : Math.Clamp(chance, 0, 1);
public Core.PieceSpec Decorate(Core.PieceType type)
{
if (registry.All.Count == 0 || random.NextDouble() >= chance)
if (registry.All.Count == 0)
{
return new Core.PieceSpec(type);
}
var item = registry.PickRandom(random);
return new Core.PieceSpec(type, item.Id, random.Next(4));
var items = new string?[MinosPerPiece];
var any = false;
for (var i = 0; i < MinosPerPiece; i++)
{
if (random.NextDouble() < _chance)
{
items[i] = registry.PickRandom(random).Id;
any = true;
}
}
return any ? new Core.PieceSpec(type, items) : new Core.PieceSpec(type);
}
}