Files

321 lines
12 KiB
C#
Raw Permalink Normal View History

using System.Diagnostics.CodeAnalysis;
using Content.Shared._White.Telescope;
2024-03-24 21:28:59 +05:00
using Content.Shared._White.WeaponModules;
using Content.Shared.Weapons.Ranged.Components;
using Content.Shared.Weapons.Ranged.Systems;
using Content.Shared.Verbs;
using Content.Shared.Tag;
2024-03-18 01:25:09 +05:00
using Robust.Server.GameObjects;
using Robust.Shared.Containers;
using Robust.Shared.Utility;
using Robust.Shared.Audio;
using Robust.Shared.Audio.Systems;
2024-03-18 01:25:09 +05:00
namespace Content.Server._White.WeaponModules;
public sealed class WeaponModulesSystem : EntitySystem
{
protected static readonly Dictionary<string, Enum> Slots = new()
{
{ "handguard_module", ModuleVisualState.HandGuardModule }, { "barrel_module", ModuleVisualState.BarrelModule }, { "aim_module", ModuleVisualState.AimModule }, { "shutter_module", ModuleVisualState.ShutterModule }
};
[Dependency] private readonly PointLightSystem _lightSystem = default!;
2024-03-22 13:06:36 +05:00
[Dependency] private readonly SharedAppearanceSystem _appearanceSystem = default!;
[Dependency] private readonly SharedGunSystem _gunSystem = default!;
[Dependency] private readonly TagSystem _tagSystem = default!;
[Dependency] private readonly SharedAudioSystem _audioSystem = default!;
2024-03-18 01:25:09 +05:00
public override void Initialize()
{
base.Initialize();
2024-03-22 19:36:18 +05:00
SubscribeLocalEvent<LightModuleComponent, EntGotInsertedIntoContainerMessage>(LightModuleOnInsert);
SubscribeLocalEvent<LightModuleComponent, EntGotRemovedFromContainerMessage>(LightModuleOnEject);
2024-03-22 16:27:51 +05:00
2024-03-22 19:36:18 +05:00
SubscribeLocalEvent<LaserModuleComponent, EntGotInsertedIntoContainerMessage>(LaserModuleOnInsert);
SubscribeLocalEvent<LaserModuleComponent, EntGotRemovedFromContainerMessage>(LaserModuleOnEject);
2024-03-22 16:27:51 +05:00
2024-03-22 19:36:18 +05:00
SubscribeLocalEvent<FlameHiderModuleComponent, EntGotInsertedIntoContainerMessage>(FlameHiderModuleOnInsert);
SubscribeLocalEvent<FlameHiderModuleComponent, EntGotRemovedFromContainerMessage>(FlameHiderModuleOnEject);
2024-03-22 16:27:51 +05:00
SubscribeLocalEvent<SilencerModuleComponent, EntGotInsertedIntoContainerMessage>(SilencerModuleOnInsert);
SubscribeLocalEvent<SilencerModuleComponent, EntGotRemovedFromContainerMessage>(SilencerModuleOnEject);
2024-03-22 19:36:18 +05:00
SubscribeLocalEvent<AcceleratorModuleComponent, EntGotInsertedIntoContainerMessage>(AcceleratorModuleOnInsert);
SubscribeLocalEvent<AcceleratorModuleComponent, EntGotRemovedFromContainerMessage>(AcceleratorModuleOnEject);
SubscribeLocalEvent<AimModuleComponent, EntGotInsertedIntoContainerMessage>(EightAimModuleOnInsert);
SubscribeLocalEvent<AimModuleComponent, EntGotRemovedFromContainerMessage>(EightAimModuleOnEject);
SubscribeLocalEvent<ShutterModuleComponent, EntGotInsertedIntoContainerMessage>(ShutterModuleOnInsert);
SubscribeLocalEvent<ShutterModuleComponent, EntGotRemovedFromContainerMessage>(ShutterModuleOnEject);
SubscribeLocalEvent<WeaponModulesComponent, GetVerbsEvent<AlternativeVerb>>(AddSwitchLightVerd);
2024-03-22 16:27:51 +05:00
}
2024-03-25 21:18:12 +05:00
2024-03-25 14:40:56 +05:00
private bool TryInsertModule(EntityUid module, EntityUid weapon, BaseModuleComponent component,
2024-03-25 21:18:12 +05:00
string containerId, [NotNullWhen(true)] out WeaponModulesComponent? weaponModulesComponent)
2024-03-24 16:00:19 +05:00
{
2024-03-25 21:18:12 +05:00
if (!TryComp(weapon, out weaponModulesComponent) || !TryComp<AppearanceComponent>(weapon, out var appearanceComponent) ||
!Slots.ContainsKey(containerId))
2024-03-24 16:00:19 +05:00
{
weaponModulesComponent = null;
appearanceComponent = null;
return false;
2024-03-24 16:00:19 +05:00
}
if (!weaponModulesComponent.Modules.Contains(module))
2024-03-25 14:40:56 +05:00
weaponModulesComponent.Modules.Add(module);
if (!Slots.TryGetValue(containerId, out var value))
return false;
_appearanceSystem.SetData(weapon, value, component.AppearanceValue, appearanceComponent);
return true;
2024-03-24 16:00:19 +05:00
}
2024-03-25 21:18:12 +05:00
private bool TryEjectModule(EntityUid module, EntityUid weapon, string containerId, [NotNullWhen(true)] out WeaponModulesComponent? weaponModulesComponent)
2024-03-24 16:00:19 +05:00
{
if (!TryComp(weapon, out weaponModulesComponent) || !TryComp<AppearanceComponent>(weapon, out var appearanceComponent) || !Slots.ContainsKey(containerId))
2024-03-24 16:00:19 +05:00
{
weaponModulesComponent = null;
appearanceComponent = null;
return false;
2024-03-24 16:00:19 +05:00
}
if (weaponModulesComponent.Modules.Contains(module))
2024-03-25 14:40:56 +05:00
weaponModulesComponent.Modules.Remove(module);
if (!Slots.TryGetValue(containerId, out var value))
return false;
_appearanceSystem.SetData(weapon, value, "none", appearanceComponent);
2024-03-25 14:40:56 +05:00
return true;
2024-03-24 21:28:59 +05:00
}
private void AddSwitchLightVerd(EntityUid uid, WeaponModulesComponent component, GetVerbsEvent<AlternativeVerb> args)
{
if (!args.CanInteract || !args.CanAccess)
return;
if (!_tagSystem.HasTag(args.Target, "HasLightModule"))
return;
AlternativeVerb verb = new()
{
Act = () =>
{
SetLight(args.Target);
},
Text = Loc.GetString("toggle-flashlight-verb-get-data-text"),
Icon = new SpriteSpecifier.Texture(new("/Textures/Interface/VerbIcons/light.svg.192dpi.png")),
Priority = 0
};
args.Verbs.Add(verb);
}
private void SetLight(EntityUid weapon)
{
_lightSystem.TryGetLight(weapon, out var light);
if (light == null)
return;
_lightSystem.SetEnabled(weapon, !light.Enabled, light);
_audioSystem.PlayPredicted(new SoundPathSpecifier("/Audio/Items/flashlight_pda.ogg"), weapon, weapon);
}
2024-03-22 16:27:51 +05:00
#region InsertModules
2024-03-22 19:36:18 +05:00
private void LightModuleOnInsert(EntityUid module, LightModuleComponent component, EntGotInsertedIntoContainerMessage args)
2024-03-22 16:27:51 +05:00
{
EntityUid weapon = args.Container.Owner;
2024-03-22 19:36:18 +05:00
if (!TryInsertModule(module, weapon, component, args.Container.ID, out var weaponModulesComponent))
2024-03-24 16:00:19 +05:00
return;
2024-03-22 16:27:51 +05:00
2024-03-25 21:18:12 +05:00
TryComp<AppearanceComponent>(weapon, out var appearanceComponent);
SharedPointLightComponent light = _lightSystem.EnsureLight(weapon);
2024-03-22 16:27:51 +05:00
_appearanceSystem.SetData(weapon, Modules.Light, "none", appearanceComponent);
_lightSystem.SetRadius(weapon, component.Radius, light);
2024-03-22 16:27:51 +05:00
_lightSystem.SetEnabled(weapon, true, light);
_tagSystem.AddTag(weapon, "HasLightModule");
2024-03-22 16:27:51 +05:00
}
2024-03-22 19:36:18 +05:00
private void LaserModuleOnInsert(EntityUid module, LaserModuleComponent component, EntGotInsertedIntoContainerMessage args)
2024-03-22 16:27:51 +05:00
{
EntityUid weapon = args.Container.Owner;
2024-03-22 19:36:18 +05:00
2024-03-24 14:35:45 +05:00
if (!TryComp<GunComponent>(weapon, out var gunComp)) return;
if (!TryInsertModule(module, weapon, component, args.Container.ID, out var weaponModulesComponent))
2024-03-25 21:18:12 +05:00
return;
2024-03-24 14:35:45 +05:00
component.OldProjectileSpeed = gunComp.ProjectileSpeed;
2024-03-22 16:27:51 +05:00
_gunSystem.SetProjectileSpeed(weapon, component.OldProjectileSpeed + component.ProjectileSpeedAdd);
2024-03-22 16:27:51 +05:00
}
2024-03-22 19:36:18 +05:00
private void FlameHiderModuleOnInsert(EntityUid module, FlameHiderModuleComponent component, EntGotInsertedIntoContainerMessage args)
2024-03-22 16:27:51 +05:00
{
EntityUid weapon = args.Container.Owner;
2024-03-22 19:36:18 +05:00
if (!TryInsertModule(module, weapon, component, args.Container.ID, out var weaponModulesComponent))
2024-03-24 16:00:19 +05:00
return;
2024-03-22 16:27:51 +05:00
2024-03-25 21:18:12 +05:00
weaponModulesComponent.WeaponFireEffect = true;
Dirty(weapon, weaponModulesComponent);
2024-03-22 16:27:51 +05:00
}
private void SilencerModuleOnInsert(EntityUid module, SilencerModuleComponent component, EntGotInsertedIntoContainerMessage args)
{
EntityUid weapon = args.Container.Owner;
2024-03-22 19:36:18 +05:00
2024-03-22 16:27:51 +05:00
if (!TryComp<GunComponent>(weapon, out var gunComp)) return;
if (!TryInsertModule(module, weapon, component, args.Container.ID, out var weaponModulesComponent))
2024-03-25 21:18:12 +05:00
return;
2024-03-22 16:27:51 +05:00
component.OldSoundGunshot = gunComp.SoundGunshot;
2024-03-25 21:18:12 +05:00
weaponModulesComponent.WeaponFireEffect = true;
2024-03-24 14:35:45 +05:00
_gunSystem.SetSound(weapon, component.NewSoundGunshot);
2024-03-22 16:27:51 +05:00
Dirty(weapon, weaponModulesComponent);
2024-03-18 01:25:09 +05:00
}
2024-03-22 19:36:18 +05:00
private void AcceleratorModuleOnInsert(EntityUid module, AcceleratorModuleComponent component, EntGotInsertedIntoContainerMessage args)
2024-03-18 01:25:09 +05:00
{
EntityUid weapon = args.Container.Owner;
2024-03-22 19:36:18 +05:00
2024-03-24 14:35:45 +05:00
if (!TryComp<GunComponent>(weapon, out var gunComp)) return;
if (!TryInsertModule(module, weapon, component, args.Container.ID, out var weaponModulesComponent))
2024-03-25 21:18:12 +05:00
return;
2024-03-24 14:35:45 +05:00
component.OldFireRate = gunComp.FireRate;
_gunSystem.SetFireRate(weapon, component.OldFireRate + component.FireRateAdd);
2024-03-18 01:25:09 +05:00
}
private void EightAimModuleOnInsert(EntityUid module, AimModuleComponent component, EntGotInsertedIntoContainerMessage args)
{
EntityUid weapon = args.Container.Owner;
if (!TryComp<GunComponent>(weapon, out var gunComp)) return;
if (!TryInsertModule(module, weapon, component, args.Container.ID, out var weaponModulesComponent))
return;
EnsureComp<TelescopeComponent>(weapon).Divisor = component.Divisor;
}
private void ShutterModuleOnInsert(EntityUid module, ShutterModuleComponent component, EntGotInsertedIntoContainerMessage args)
{
EntityUid weapon = args.Container.Owner;
if (!TryComp<GunComponent>(weapon, out var gunComp)) return;
if (!TryInsertModule(module, weapon, component, args.Container.ID, out var weaponModulesComponent))
return;
if (!TryComp<BallisticAmmoProviderComponent>(weapon, out var ballisticAmmo))
return;
ballisticAmmo.Whitelist?.Tags?.Add(component.Tag);
Dirty(weapon, ballisticAmmo);
}
2024-03-22 16:27:51 +05:00
#endregion
2024-03-18 01:25:09 +05:00
2024-03-22 16:27:51 +05:00
#region EjectModules
2024-03-22 19:36:18 +05:00
private void LightModuleOnEject(EntityUid module, LightModuleComponent component, EntGotRemovedFromContainerMessage args)
2024-03-18 01:25:09 +05:00
{
EntityUid weapon = args.Container.Owner;
2024-03-22 19:36:18 +05:00
if (!TryEjectModule(module, weapon, args.Container.ID, out var weaponModulesComponent))
2024-03-25 21:18:12 +05:00
return;
if (!_lightSystem.TryGetLight(weapon, out var light))
2024-03-24 16:00:19 +05:00
return;
2024-03-22 19:36:18 +05:00
_lightSystem.SetRadius(weapon, 0F, light);
2024-03-22 16:27:51 +05:00
_lightSystem.SetEnabled(weapon, false, light);
_tagSystem.RemoveTag(weapon, "HasLightModule");
2024-03-18 01:25:09 +05:00
}
2024-03-22 19:36:18 +05:00
private void LaserModuleOnEject(EntityUid module, LaserModuleComponent component, EntGotRemovedFromContainerMessage args)
2024-03-18 01:25:09 +05:00
{
2024-03-22 16:27:51 +05:00
EntityUid weapon = args.Container.Owner;
2024-03-22 19:36:18 +05:00
if (!TryEjectModule(module, weapon, args.Container.ID, out var weaponModulesComponent))
2024-03-24 16:00:19 +05:00
return;
2024-03-22 19:36:18 +05:00
2024-03-24 14:35:45 +05:00
_gunSystem.SetProjectileSpeed(weapon, component.OldProjectileSpeed);
2024-03-18 01:25:09 +05:00
}
2024-03-22 19:36:18 +05:00
private void FlameHiderModuleOnEject(EntityUid module, FlameHiderModuleComponent component, EntGotRemovedFromContainerMessage args)
2024-03-18 01:25:09 +05:00
{
2024-03-22 16:27:51 +05:00
EntityUid weapon = args.Container.Owner;
2024-03-22 19:36:18 +05:00
if (!TryEjectModule(module, weapon, args.Container.ID, out var weaponModulesComponent))
2024-03-24 16:00:19 +05:00
return;
2024-03-22 19:36:18 +05:00
2024-03-25 21:18:12 +05:00
weaponModulesComponent.WeaponFireEffect = false;
Dirty(weapon, weaponModulesComponent);
2024-03-18 01:25:09 +05:00
}
2024-03-22 16:27:51 +05:00
private void SilencerModuleOnEject(EntityUid module, SilencerModuleComponent component, EntGotRemovedFromContainerMessage args)
2024-03-18 01:25:09 +05:00
{
2024-03-22 16:27:51 +05:00
EntityUid weapon = args.Container.Owner;
2024-03-22 19:36:18 +05:00
if (!TryEjectModule(module, weapon, args.Container.ID, out var weaponModulesComponent))
2024-03-24 16:00:19 +05:00
return;
2024-03-22 19:36:18 +05:00
2024-03-25 21:18:12 +05:00
weaponModulesComponent.WeaponFireEffect = false;
2024-03-24 14:35:45 +05:00
_gunSystem.SetSound(weapon, component.OldSoundGunshot!);
Dirty(weapon, weaponModulesComponent);
2024-03-18 01:25:09 +05:00
}
2024-03-22 19:36:18 +05:00
private void AcceleratorModuleOnEject(EntityUid module, AcceleratorModuleComponent component, EntGotRemovedFromContainerMessage args)
2024-03-18 01:25:09 +05:00
{
2024-03-22 16:27:51 +05:00
EntityUid weapon = args.Container.Owner;
2024-03-22 19:36:18 +05:00
if (!TryEjectModule(module, weapon, args.Container.ID, out var weaponModulesComponent))
2024-03-24 16:00:19 +05:00
return;
2024-03-22 19:36:18 +05:00
2024-03-24 14:35:45 +05:00
_gunSystem.SetFireRate(weapon, component.OldFireRate);
2024-03-18 01:25:09 +05:00
}
private void EightAimModuleOnEject(EntityUid module, AimModuleComponent component, EntGotRemovedFromContainerMessage args)
{
EntityUid weapon = args.Container.Owner;
if (!TryEjectModule(module, weapon, args.Container.ID, out var weaponModulesComponent))
return;
RemComp<TelescopeComponent>(weapon);
}
private void ShutterModuleOnEject(EntityUid module, ShutterModuleComponent component, EntGotRemovedFromContainerMessage args)
{
EntityUid weapon = args.Container.Owner;
if (!TryEjectModule(module, weapon, args.Container.ID, out var weaponModulesComponent))
return;
if (!TryComp<BallisticAmmoProviderComponent>(weapon, out var ballisticAmmo))
return;
ballisticAmmo.Whitelist?.Tags?.Remove(component.Tag);
Dirty(weapon, ballisticAmmo);
}
2024-03-22 16:27:51 +05:00
#endregion
2024-03-18 01:25:09 +05:00
}