Files
OldThink/Content.Server/_White/WeaponModules/WeaponModulesSystem.cs

203 lines
8.4 KiB
C#
Raw Normal View History

2024-03-24 21:28:59 +05:00
using System.Diagnostics.CodeAnalysis;
using Content.Shared._White.WeaponModules;
using Content.Shared.Weapons.Ranged.Components;
using Content.Shared.Weapons.Ranged.Systems;
using Linguini.Syntax.Ast;
2024-03-18 01:25:09 +05:00
using Robust.Server.GameObjects;
using Robust.Shared.Containers;
namespace Content.Server._White.WeaponModules;
public sealed class WeaponModulesSystem : EntitySystem
{
protected const string ModulesSlot = "gun_modules";
[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!;
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);
2024-03-22 16:27:51 +05:00
}
2024-03-25 14:40:56 +05:00
private bool TryInsertModule(EntityUid module, EntityUid weapon, BaseModuleComponent component,
EntGotInsertedIntoContainerMessage args, [NotNullWhen(true)] out WeaponModulesComponent? weaponModulesComponent,
[NotNullWhen(true)] out AppearanceComponent? appearanceComponent)
2024-03-24 16:00:19 +05:00
{
2024-03-25 14:40:56 +05:00
if (!TryComp(weapon, out weaponModulesComponent) || !TryComp(weapon, out appearanceComponent) ||
ModulesSlot != args.Container.ID)
2024-03-24 16:00:19 +05:00
{
weaponModulesComponent = null;
appearanceComponent = null;
return false;
2024-03-24 16:00:19 +05:00
}
2024-03-25 14:40:56 +05:00
if(!weaponModulesComponent.Modules.Contains(module))
weaponModulesComponent.Modules.Add(module);
_appearanceSystem.SetData(weapon, ModuleVisualState.Module, component.AppearanceValue, appearanceComponent);
return true;
2024-03-24 16:00:19 +05:00
}
2024-03-25 14:40:56 +05:00
private bool TryEjectModule(EntityUid module, EntityUid weapon, EntGotRemovedFromContainerMessage args, [NotNullWhen(true)] out WeaponModulesComponent? weaponModulesComponent, [NotNullWhen(true)] out AppearanceComponent? appearanceComponent)
2024-03-24 16:00:19 +05:00
{
2024-03-25 14:40:56 +05:00
if (!TryComp(weapon, out weaponModulesComponent) || !TryComp(weapon, out appearanceComponent) || ModulesSlot != args.Container.ID)
2024-03-24 16:00:19 +05:00
{
weaponModulesComponent = null;
appearanceComponent = null;
return false;
2024-03-24 16:00:19 +05:00
}
2024-03-25 14:40:56 +05:00
if(weaponModulesComponent.Modules.Contains(module))
weaponModulesComponent.Modules.Remove(module);
_appearanceSystem.SetData(weapon, ModuleVisualState.Module, "none", appearanceComponent);
2024-03-25 14:40:56 +05:00
return true;
2024-03-24 21:28:59 +05:00
}
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
2024-03-25 14:40:56 +05:00
if(!TryInsertModule(module, weapon, component, args, out var weaponModulesComponent, out var appearanceComponent))
2024-03-24 16:00:19 +05:00
return;
2024-03-22 16:27:51 +05:00
_lightSystem.EnsureLight(weapon);
_lightSystem.TryGetLight(weapon, out var light);
_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);
}
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-25 14:40:56 +05:00
if(!TryInsertModule(module, weapon, component, args, out var weaponModulesComponent, out var appearanceComponent))
2024-03-24 16:00:19 +05:00
return;
2024-03-22 16:27:51 +05:00
2024-03-24 14:35:45 +05:00
if (!TryComp<GunComponent>(weapon, out var gunComp)) return;
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
2024-03-25 14:40:56 +05:00
if(!TryInsertModule(module, weapon, component, args, out var weaponModulesComponent, out var appearanceComponent))
2024-03-24 16:00:19 +05:00
return;
2024-03-22 16:27:51 +05:00
2024-03-22 19:36:18 +05:00
weaponModulesComponent.UseEffect = true;
Dirty(module, 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-25 14:40:56 +05:00
if(!TryInsertModule(module, weapon, component, args, out var weaponModulesComponent, out var appearanceComponent))
2024-03-24 16:00:19 +05:00
return;
2024-03-22 16:27:51 +05:00
if (!TryComp<GunComponent>(weapon, out var gunComp)) return;
component.OldSoundGunshot = gunComp.SoundGunshot;
2024-03-22 19:36:18 +05:00
weaponModulesComponent.UseEffect = true;
2024-03-24 14:35:45 +05:00
_gunSystem.SetSound(weapon, component.NewSoundGunshot);
2024-03-22 16:27:51 +05:00
2024-03-22 19:36:18 +05:00
Dirty(module, 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-25 14:40:56 +05:00
if(!TryInsertModule(module, weapon, component, args, out var weaponModulesComponent, out var appearanceComponent))
2024-03-24 16:00:19 +05:00
return;
2024-03-18 01:25:09 +05:00
2024-03-24 14:35:45 +05:00
if (!TryComp<GunComponent>(weapon, out var gunComp)) return;
component.OldFireRate = gunComp.FireRate;
_gunSystem.SetFireRate(weapon, component.OldFireRate + component.FireRateAdd);
2024-03-18 01:25:09 +05:00
}
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
2024-03-25 14:40:56 +05:00
if(!TryEjectModule(module, weapon, args, out var weaponModulesComponent, out var appearanceComponent))
2024-03-24 16:00:19 +05:00
return;
2024-03-22 19:36:18 +05:00
2024-03-22 16:27:51 +05:00
_lightSystem.TryGetLight(weapon, out var light);
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);
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
2024-03-25 14:40:56 +05:00
if(!TryEjectModule(module, weapon, args, out var weaponModulesComponent, out var appearanceComponent))
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
2024-03-25 14:40:56 +05:00
if(!TryEjectModule(module, weapon, args, out var weaponModulesComponent, out var appearanceComponent))
2024-03-24 16:00:19 +05:00
return;
2024-03-22 19:36:18 +05:00
weaponModulesComponent.UseEffect = false;
Dirty(module, 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
2024-03-25 14:40:56 +05:00
if(!TryEjectModule(module, weapon, args, out var weaponModulesComponent, out var appearanceComponent))
2024-03-24 16:00:19 +05:00
return;
2024-03-22 19:36:18 +05:00
weaponModulesComponent.UseEffect = false;
2024-03-24 14:35:45 +05:00
_gunSystem.SetSound(weapon, component.OldSoundGunshot!);
2024-03-22 19:36:18 +05:00
Dirty(module, 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
2024-03-25 14:40:56 +05:00
if(!TryEjectModule(module, weapon, args, out var weaponModulesComponent, out var appearanceComponent))
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
}
2024-03-22 16:27:51 +05:00
#endregion
2024-03-18 01:25:09 +05:00
}