Gaussian Spread

This commit is contained in:
Marco 2025-05-01 11:59:32 +02:00
commit 2c72f36108
11 changed files with 84 additions and 11 deletions

View file

@ -16,6 +16,6 @@ InfiniteAmmo = false
ItemKey = &"ICE_SHOTGUN_SAWED"
AmmoKey = &"ICE_AMMO"
BulletsPerShot = 4
SpreadAngle = 30.0
RandomSpread = 0.0
SpreadAngle = 40.0
RandomSpread = 30.0
_rotationOffset = 0.0

View file

@ -16,6 +16,6 @@ InfiniteAmmo = false
ItemKey = &"ICE_SHOTGUN_SAWED_T1"
AmmoKey = &"ICE_AMMO"
BulletsPerShot = 4
SpreadAngle = 25.0
RandomSpread = 0.0
SpreadAngle = 30.0
RandomSpread = 28.0
_rotationOffset = 0.0

View file

@ -16,6 +16,6 @@ InfiniteAmmo = false
ItemKey = &"ICE_SHOTGUN_SAWED_T2"
AmmoKey = &"ICE_AMMO"
BulletsPerShot = 4
SpreadAngle = 25.0
RandomSpread = 0.0
SpreadAngle = 38.0
RandomSpread = 25.0
_rotationOffset = 0.0

View file

@ -17,5 +17,5 @@ ItemKey = &"ICE_SHOTGUN_T1"
AmmoKey = &"ICE_AMMO"
BulletsPerShot = 3
SpreadAngle = 15.0
RandomSpread = 0.0
RandomSpread = 12.0
_rotationOffset = 0.0

View file

@ -17,5 +17,5 @@ ItemKey = &"ICE_SHOTGUN_T2"
AmmoKey = &"ICE_AMMO"
BulletsPerShot = 3
SpreadAngle = 15.0
RandomSpread = 0.0
RandomSpread = 10.0
_rotationOffset = 0.0

View file

@ -17,5 +17,5 @@ ItemKey = &"ICE_SHOTGUN"
AmmoKey = &"ICE_AMMO"
BulletsPerShot = 3
SpreadAngle = 15.0
RandomSpread = 0.0
RandomSpread = 15.0
_rotationOffset = 0.0

View file

@ -9,6 +9,7 @@
script = ExtResource("2_m8dps")
Name = &"Icicle Gun"
BulletData = ExtResource("1_85ef1")
Priority = 0
RateOfFire = 0.3
BulletCapacity = 5
ReloadTime = 0.6
@ -17,8 +18,8 @@ InfiniteAmmo = true
ItemKey = &"ICICLE_GUN"
AmmoKey = &""
BulletsPerShot = 1
SpreadAngle = 0.0
RandomSpread = 0.0
SpreadAngle = 5.0
RandomSpread = 2.5
_rotationOffset = 0.0
ReloadSound = ExtResource("2_sd6j2")
ShootSound = ExtResource("2_r2tre")

View file

@ -716,6 +716,24 @@ script = ExtResource("82_fuaed")
Item = ExtResource("4_swym2")
AutoSpawn = true
[node name="AmmoMarker7" type="Marker2D" parent="Parallax2D/Factory Tilemaps/Debug Room"]
position = Vector2(-823.466, -584.726)
script = ExtResource("82_fuaed")
Item = ExtResource("4_swym2")
AutoSpawn = true
[node name="AmmoMarker8" type="Marker2D" parent="Parallax2D/Factory Tilemaps/Debug Room"]
position = Vector2(-826.374, -604.114)
script = ExtResource("82_fuaed")
Item = ExtResource("4_swym2")
AutoSpawn = true
[node name="AmmoMarker9" type="Marker2D" parent="Parallax2D/Factory Tilemaps/Debug Room"]
position = Vector2(-843.822, -603.871)
script = ExtResource("82_fuaed")
Item = ExtResource("4_swym2")
AutoSpawn = true
[node name="AmmoMarker2" type="Marker2D" parent="Parallax2D/Factory Tilemaps/Debug Room"]
position = Vector2(-808, -397)
script = ExtResource("82_fuaed")

View file

@ -0,0 +1,40 @@
using System;
using Godot;
namespace Cirno.Scripts.Utils;
public static class RandomStuff
{
public static double GaussianRandom(double mean = 0.0, double stdDev = 1.0)
{
// Box-Muller transform
float u1 = GD.Randf();
float u2 = GD.Randf();
// Ensure u1 is strictly > 0
while (u1 <= 0f)
u1 = GD.Randf();
float randStdNormal = Mathf.Sqrt(-2f * Mathf.Log(u1)) * Mathf.Sin(2f * Mathf.Pi * u2);
return mean + stdDev * randStdNormal;
}
public static float GaussianClamped(float mean, float stdDev, float min, float max)
{
float value;
do
{
float u1 = GD.Randf();
float u2 = GD.Randf();
// Ensure u1 > 0 to avoid log(0)
while (u1 <= 0f) u1 = GD.Randf();
float randStdNormal = Mathf.Sqrt(-2f * Mathf.Log(u1)) * Mathf.Sin(2f * Mathf.Pi * u2);
value = mean + stdDev * randStdNormal;
}
while (value < min || value > max); // reject out-of-bounds values
return value;
}
}

View file

@ -0,0 +1 @@
uid://tkwp2w30xi32

View file

@ -4,6 +4,7 @@ using System.Diagnostics;
using Cirno.Scripts;
using Cirno.Scripts.Components;
using Cirno.Scripts.Resources;
using Cirno.Scripts.Utils;
public partial class Weapon : Node2D
{
@ -123,6 +124,18 @@ public partial class Weapon : Node2D
// Calculate angle offset for this bullet
float spreadOffset = -halfSpread + (spreadStep * i);
// Add random spread
if (WeaponData.RandomSpread > 0)
{
// Gaussian with mean = 0, stddev = WeaponData.RandomSpread
spreadOffset += RandomStuff.GaussianClamped(
mean: 0f,
stdDev: WeaponData.RandomSpread, // tuning knob
min: -halfSpread,
max: halfSpread
);
}
// Rotate the ShootDirection by the spread angle
Vector2 spreadDirection = ShootDirection.Rotated(Mathf.DegToRad(spreadOffset));