Weapon sounds

This commit is contained in:
Marco 2025-04-08 17:59:20 +02:00
commit 3c531bbe04
14 changed files with 174 additions and 52 deletions

View file

@ -95,8 +95,7 @@ public partial class PlayerWeaponProvider : Node2D
public void Shoot(Vector2 direction)
{
if (EquippedWeapon == null) return;
EquippedWeapon.ShootDirection = direction;
EquippedWeapon.Shoot();
}

View file

@ -170,8 +170,6 @@ public partial class Active : PlayerStateBase
//CallDeferred(MethodName.PauseDeferred);
PauseDeferred();
}
}
private void PauseDeferred()

View file

@ -29,16 +29,15 @@ public partial class WeaponResource : Resource
[Export] public bool InfiniteAmmo = true;
[Export] public StringName ItemKey;
[Export] public StringName AmmoKey;
#region Bullet spawn data
[ExportCategory("Bullet Spawn Data")]
[Export] public int BulletsPerShot = 1;
[Export] public float SpreadAngle = 0f;
[Export] public float RandomSpread = 0f;
[Export] public StringName ItemKey;
#region Bullet spawn data
[Export] public StringName AmmoKey;
[Export] public float RandomSpread = 0f;
//[Export] public float BulletSpeed = 100f;
//[Export] public float BulletDamage = 1;
//[Export] public float LifeTime = 10f;
@ -49,6 +48,11 @@ public partial class WeaponResource : Resource
//[Export] private Array<Resource> _timeModifiers;
#endregion
[ExportCategory("Sounds")]
[Export] public AudioStream ReloadSound { get; set; }
[Export] public AudioStream ShootSound { get; set; }
[Export] public AudioStream EmptySound { get; set; }
public BulletInfo MakeBullet(Vector2 position)
{

View file

@ -17,6 +17,14 @@ public partial class Weapon : Node2D
[Export]
public Marker2D Muzzle { get; set; }
[Signal]
public delegate void ShootingEventHandler();
[Signal]
public delegate void ReloadingEventHandler();
[Signal] public delegate void EmptyEventHandler();
public int Ammo { get; set; } = 0;
private int _loadedAmmo;
@ -60,6 +68,8 @@ public partial class Weapon : Node2D
public void Reload()
{
EmitSignalReloading();
_cooldownTimer.Start(WeaponData.ReloadTime);
if (WeaponData.InfiniteAmmo || string.IsNullOrWhiteSpace(WeaponData.AmmoKey))
@ -77,7 +87,8 @@ public partial class Weapon : Node2D
}
else
{
GD.Print("Out of ammo");
EmitSignalEmpty();
//GD.Print("Out of ammo");
}
}
}
@ -100,11 +111,12 @@ public partial class Weapon : Node2D
return;
}
EmitSignalShooting();
// TODO: Shoot at muzzle position, need to provide a way to turn it, on a radius?
float halfSpread = WeaponData.SpreadAngle / 2f;
float spreadStep = WeaponData.BulletsPerShot > 1 ? WeaponData.SpreadAngle / (WeaponData.BulletsPerShot - 1) : 0;
for (int i = 0; i < WeaponData.BulletsPerShot; i++)
{

View file

@ -0,0 +1,40 @@
using Godot;
namespace Cirno.Scripts.Weapons;
public partial class WeaponSoundModule : Node2D
{
[Export]
public Weapon Weapon { get; private set; }
[Export]
public AudioStreamPlayer2D ShootSound { get; private set; }
[Export]
public AudioStreamPlayer2D ReloadSound { get; private set; }
[Export]
public AudioStreamPlayer2D EmptySound { get; private set; }
public override void _Ready()
{
if (Weapon?.WeaponData is null) return;
ShootSound.Stream = Weapon.WeaponData.ShootSound;
ReloadSound.Stream = Weapon.WeaponData.ReloadSound;
EmptySound.Stream = Weapon.WeaponData.EmptySound;
}
public void PlayReloadSound()
{
ReloadSound?.Play();
}
public void PlayShootSound()
{
ShootSound?.Play();
}
public void PlayEmptySound()
{
EmptySound?.Play();
}
}

View file

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