Skip to content

Commit

Permalink
fix: init fix for play gun sound
Browse files Browse the repository at this point in the history
  • Loading branch information
VALERA771 committed Jan 7, 2025
1 parent babee1b commit f4d5541
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 16 deletions.
63 changes: 49 additions & 14 deletions EXILED/Exiled.API/Extensions/MirrorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,19 @@ namespace Exiled.API.Extensions
using System.Reflection.Emit;
using System.Text;

using AudioPooling;
using Exiled.API.Enums;
using Features;
using Features.Pools;

using InventorySystem.Items.Firearms;
using InventorySystem.Items.Firearms.Modules;
using Mirror;

using PlayerRoles;
using PlayerRoles.FirstPersonControl;
using PlayerRoles.PlayableScps.Scp049.Zombies;
using PlayerRoles.Voice;
using RelativePositioning;

using Respawning;

using UnityEngine;

/// <summary>
Expand Down Expand Up @@ -167,20 +166,56 @@ public static ReadOnlyDictionary<string, string> RpcFullNames
/// <param name="itemType">Weapon' sound to play.</param>
/// <param name="volume">Sound's volume to set.</param>
/// <param name="audioClipId">GunAudioMessage's audioClipId to set (default = 0).</param>
[Obsolete("This method is not working. Use PlayGunSound(Player, Vector3, ItemType, float, int, bool) overload instead.")]
public static void PlayGunSound(this Player player, Vector3 position, ItemType itemType, byte volume, byte audioClipId = 0)
{
// TODO: Not finish
/*
GunAudioMessage message = new()
}

/// <summary>
/// Plays a gun sound that only the <paramref name="player"/> can hear.
/// </summary>
/// <param name="player">Target to play.</param>
/// <param name="position">Position to play on.</param>
/// <param name="itemType">Weapon' sound to play.</param>
/// <param name="pitch">Speed of sound.</param>
/// <param name="clipIndex">Index of clip.</param>
/// <param name="isDryFire">Indicates whether a dry fire should be used.</param>
public static void PlayGunSound(this Player player, Vector3 position, ItemType itemType, float pitch = 1, int clipIndex = 0, bool isDryFire = false)
{
Firearm firearm = itemType.GetItemBase<Firearm>();

if (!firearm.TryGetModule(out AudioModule audioModule))
return;

IActionModule actionModule = firearm.Modules.OfType<IActionModule>().FirstOrDefault();
AudioClip clip;

switch (actionModule)
{
Weapon = itemType,
AudioClipId = audioClipId,
MaxDistance = volume,
ShooterHub = player.ReferenceHub,
ShooterPosition = new RelativePosition(position),
};
case AutomaticActionModule automaticActionModule:
if (isDryFire)
{
clip = automaticActionModule._dryfireSound;
}
else
{
AudioClip[] clips = automaticActionModule._gunshotSounds.SelectMany(x => x.RandomSounds).ToArray();
clip = clips[Mathf.Clamp(clipIndex, 0, clips.Length)];
}

break;
case PumpActionModule pumpActionModule:
clip = isDryFire ? pumpActionModule._dryFireClip : pumpActionModule._shotClipPerBarrelIndex[Mathf.Clamp(clipIndex, 0, pumpActionModule._shotClipPerBarrelIndex.Length)];
break;
case DoubleActionModule doubleActionModule:
clip = isDryFire ? doubleActionModule._dryFireClip : doubleActionModule._fireClips[Mathf.Clamp(clipIndex, 0, doubleActionModule._fireClips.Length)];
break;
default:
return;
}

player.Connection.Send(message);*/
audioModule.SendRpc(player.ReferenceHub, writer =>
audioModule.ServerSend(writer, audioModule._clipToIndex[clip], pitch, isDryFire ? MixerChannel.DefaultSfx : MixerChannel.Weapons, float.MaxValue, position, false));
}

/// <summary>
Expand Down
10 changes: 8 additions & 2 deletions EXILED/Exiled.API/Features/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3439,8 +3439,14 @@ public void Reconnect(ushort newPort = 0, float delay = 5, bool reconnect = true
}

/// <inheritdoc cref="MirrorExtensions.PlayGunSound(Player, Vector3, ItemType, byte, byte)"/>
public void PlayGunSound(ItemType type, byte volume, byte audioClipId = 0) =>
MirrorExtensions.PlayGunSound(this, Position, type, volume, audioClipId);
[Obsolete("Use PlayGunSound(Player, Vector3, ItemType, byte, byte) instead.")]
public void PlayGunSound(ItemType type, byte volume, byte audioClipId = 0)
{
}

/// <inheritdoc cref="MirrorExtensions.PlayGunSound(Player, Vector3, ItemType, float, int, bool)"/>
public void PlayGunSound(ItemType itemType, float pitch = 1, int clipIndex = 0, bool isDryFire = false) =>
this.PlayGunSound(Position, itemType, pitch, clipIndex, isDryFire);

/// <inheritdoc cref="Map.PlaceBlood(Vector3, Vector3)"/>
public void PlaceBlood(Vector3 direction) => Map.PlaceBlood(Position, direction);
Expand Down

0 comments on commit f4d5541

Please sign in to comment.