added item probability
This commit is contained in:
Regular → Executable
+17
-4
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user