|
| 1 | +using Game.Shared; |
| 2 | + |
| 3 | +using Source; |
| 4 | +using Source.Common; |
| 5 | +using Source.Common.Audio; |
| 6 | +using Source.Common.Engine; |
| 7 | +using Source.Common.Mathematics; |
| 8 | +using Source.Common.SoundEmitterSystem; |
| 9 | + |
| 10 | +using System.Numerics; |
| 11 | + |
| 12 | +namespace Game.Client; |
| 13 | + |
| 14 | +public static class FX_Tracer |
| 15 | +{ |
| 16 | + static float NextWhizTime; |
| 17 | + |
| 18 | + public static Vector3 GetTracerOrigin(EffectData data) { |
| 19 | + Vector3 vecStart = data.Start; |
| 20 | + int attachment = data.AttachmentIndex; |
| 21 | + |
| 22 | + if ((data.Flags & TRACER_FLAG_USEATTACHMENT) == 0) |
| 23 | + return vecStart; |
| 24 | + |
| 25 | + IClientRenderable? renderable = cl_entitylist.GetClientRenderableFromHandle(data.Entity); |
| 26 | + if (renderable == null) |
| 27 | + return vecStart; |
| 28 | + |
| 29 | + C_BaseEntity? ent = C_BaseEntity.Instance(data.Entity); |
| 30 | + if (ent != null && ent.IsDormant()) |
| 31 | + return vecStart; |
| 32 | + |
| 33 | + if (ent is C_BaseCombatWeapon wpn) { |
| 34 | + if (wpn.GetTracerOrigin(ref vecStart)) |
| 35 | + return vecStart; |
| 36 | + |
| 37 | + C_BasePlayer? player = ToBasePlayer(wpn.GetOwner()); |
| 38 | + C_BaseViewModel? viewModel = player?.GetViewModel(0); |
| 39 | + if (viewModel != null && wpn.ShouldDrawUsingViewModel()) |
| 40 | + renderable = viewModel; |
| 41 | + } |
| 42 | + |
| 43 | + if (renderable.GetAttachment(attachment, out Vector3 attachOrigin, out _)) |
| 44 | + vecStart = attachOrigin; |
| 45 | + |
| 46 | + return vecStart; |
| 47 | + } |
| 48 | + |
| 49 | + public static void FX_TracerSound(in Vector3 start, in Vector3 end, int tracerType, ReadOnlySpan<char> customSoundName = default) { |
| 50 | + Vector3 vecListenOrigin = MainViewOrigin(); |
| 51 | + ReadOnlySpan<char> soundName; |
| 52 | + float whizDistSqr; |
| 53 | + float minWhizTime = 0.1f; |
| 54 | + float maxWhizTime = 0.1f; |
| 55 | + |
| 56 | + switch (tracerType) { |
| 57 | + case TRACER_TYPE_DEFAULT: { |
| 58 | + soundName = "Bullets.DefaultNearmiss"; |
| 59 | + whizDistSqr = 24.0f * 24.0f; |
| 60 | + |
| 61 | + Ray bullet = default; |
| 62 | + bullet.Init(start, end); |
| 63 | + |
| 64 | + Vector3 vecLower = vecListenOrigin; |
| 65 | + vecLower.Z -= 24.0f; |
| 66 | + Ray listener = default; |
| 67 | + listener.Init(vecListenOrigin, vecLower); |
| 68 | + |
| 69 | + CollisionUtils.IntersectRayWithRay(bullet, listener, out _, out float t); |
| 70 | + if (t <= 0.0f) |
| 71 | + t = 0.0f; |
| 72 | + if (1.0f <= t) |
| 73 | + t = 1.0f; |
| 74 | + vecListenOrigin.Z -= t * 24.0f; |
| 75 | + } |
| 76 | + break; |
| 77 | + case TRACER_TYPE_GUNSHIP: |
| 78 | + soundName = "Bullets.GunshipNearmiss"; |
| 79 | + whizDistSqr = 72.0f * 72.0f; |
| 80 | + break; |
| 81 | + case TRACER_TYPE_STRIDER: |
| 82 | + soundName = "Bullets.StriderNearmiss"; |
| 83 | + whizDistSqr = 72.0f * 72.0f; |
| 84 | + break; |
| 85 | + case TRACER_TYPE_WATERBULLET: |
| 86 | + soundName = "Underwater.BulletImpact"; |
| 87 | + whizDistSqr = 48.0f * 48.0f; |
| 88 | + minWhizTime = 0.3f; |
| 89 | + maxWhizTime = 0.6f; |
| 90 | + break; |
| 91 | + default: |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + if (!customSoundName.IsEmpty) |
| 96 | + soundName = customSoundName; |
| 97 | + |
| 98 | + if ((float)(NextWhizTime - gpGlobals.CurTime) > 0.0f) |
| 99 | + return; |
| 100 | + |
| 101 | + if (MathLib.CalcDistanceSqrToLineSegment(vecListenOrigin, start, end) >= whizDistSqr) |
| 102 | + return; |
| 103 | + |
| 104 | + SoundParameters parms = new(); |
| 105 | + if (C_BaseEntity.GetParametersForSound(soundName, ref parms, null)) { |
| 106 | + Vector3 shotDir = end - start; |
| 107 | + MathLib.VectorNormalize(ref shotDir); |
| 108 | + |
| 109 | + LocalPlayerFilter filter = new(); |
| 110 | + enginesound.EmitSound(filter, EngineSoundGlobals.SOUND_FROM_WORLD, (int)SoundEntityChannel.Static, ((Span<char>)parms.SoundName).SliceNullTerminatedString(), |
| 111 | + parms.Volume, AttenuationValues.SNDLVL_TO_ATTN(parms.SoundLevel), 0, parms.Pitch, 0, start, shotDir, null, true); |
| 112 | + } |
| 113 | + |
| 114 | + NextWhizTime = (float)(gpGlobals.CurTime + random.RandomFloat(minWhizTime, maxWhizTime)); |
| 115 | + } |
| 116 | + |
| 117 | + static void ParticleTracerCallback(EffectData data) { |
| 118 | + C_BasePlayer? player = C_BasePlayer.GetLocalPlayer(); |
| 119 | + if (player == null) |
| 120 | + return; |
| 121 | + |
| 122 | + Vector3 vecStart = GetTracerOrigin(data); |
| 123 | + Vector3 vecEnd = data.Origin; |
| 124 | + |
| 125 | + C_BaseEntity? entity = C_BaseEntity.Instance(data.Entity); |
| 126 | + int entIndex = entity != null ? entity.EntIndex() : -1; |
| 127 | + if (entIndex != 0 && entIndex == player.EntIndex()) { |
| 128 | + engine.GetViewAngles(out QAngle vangles); |
| 129 | + MathLib.AngleVectors(vangles, out _, out Vector3 vright, out _); |
| 130 | + |
| 131 | + MathLib.VectorMA(data.Start, 4, vright, out vecStart); |
| 132 | + vecStart.Z -= 0.5f; |
| 133 | + } |
| 134 | + |
| 135 | + Vector3 vecToEnd = vecEnd - vecStart; |
| 136 | + MathLib.VectorNormalize(ref vecToEnd); |
| 137 | + MathLib.VectorAngles(vecToEnd, out QAngle vecAngles); |
| 138 | + DispatchParticleEffect(data.HitBox, vecStart, vecEnd, vecAngles, entity); |
| 139 | + |
| 140 | + if ((data.Flags & TRACER_FLAG_WHIZ) != 0) |
| 141 | + FX_TracerSound(vecStart, vecEnd, TRACER_TYPE_DEFAULT); |
| 142 | + } |
| 143 | + |
| 144 | + static readonly ClientEffectRegistration ClientEffectReg_ParticleTracerCallback = new("ParticleTracer", ParticleTracerCallback); |
| 145 | +} |
0 commit comments