Files
OldThink/Content.Server/_White/WeaponModules/WeaponModulesSystem.cs
2024-03-25 00:23:23 +05:00

227 lines
9.6 KiB
C#

using System.Diagnostics.CodeAnalysis;
using Content.Shared._White.WeaponModules;
using Content.Shared.Weapons.Ranged.Components;
using Content.Shared.Weapons.Ranged.Systems;
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!;
[Dependency] private readonly SharedAppearanceSystem _appearanceSystem = default!;
[Dependency] private readonly SharedGunSystem _gunSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<LightModuleComponent, EntGotInsertedIntoContainerMessage>(LightModuleOnInsert);
SubscribeLocalEvent<LightModuleComponent, EntGotRemovedFromContainerMessage>(LightModuleOnEject);
SubscribeLocalEvent<LaserModuleComponent, EntGotInsertedIntoContainerMessage>(LaserModuleOnInsert);
SubscribeLocalEvent<LaserModuleComponent, EntGotRemovedFromContainerMessage>(LaserModuleOnEject);
SubscribeLocalEvent<FlameHiderModuleComponent, EntGotInsertedIntoContainerMessage>(FlameHiderModuleOnInsert);
SubscribeLocalEvent<FlameHiderModuleComponent, EntGotRemovedFromContainerMessage>(FlameHiderModuleOnEject);
SubscribeLocalEvent<SilencerModuleComponent, EntGotInsertedIntoContainerMessage>(SilencerModuleOnInsert);
SubscribeLocalEvent<SilencerModuleComponent, EntGotRemovedFromContainerMessage>(SilencerModuleOnEject);
SubscribeLocalEvent<AcceleratorModuleComponent, EntGotInsertedIntoContainerMessage>(AcceleratorModuleOnInsert);
SubscribeLocalEvent<AcceleratorModuleComponent, EntGotRemovedFromContainerMessage>(AcceleratorModuleOnEject);
}
private bool TryInsertModule(EntityUid module, EntityUid weapon, EntGotInsertedIntoContainerMessage args, [NotNullWhen(true)] out WeaponModulesComponent? weaponModulesComponent)
{
if (TryComp<WeaponModulesComponent>(weapon, out var weaponModulesComponents) && ModulesSlot == args.Container.ID)
{
if(!weaponModulesComponents.Modules.Contains(module))
weaponModulesComponents.Modules.Add(module);
weaponModulesComponent = weaponModulesComponents;
return true;
}
weaponModulesComponent = weaponModulesComponents;
return false;
}
private bool TryEjectModule(EntityUid module, EntityUid weapon, EntGotRemovedFromContainerMessage args, [NotNullWhen(true)] out WeaponModulesComponent? weaponModulesComponent)
{
if (TryComp<WeaponModulesComponent>(weapon, out var weaponModulesComponents) && ModulesSlot == args.Container.ID)
{
if(weaponModulesComponents.Modules.Contains(module))
weaponModulesComponents.Modules.Remove(module);
weaponModulesComponent = weaponModulesComponents;
return true;
}
weaponModulesComponent = weaponModulesComponents;
return false;
}
private void SetAppearance(EntityUid weapon, Enum key, string value, AppearanceComponent appearanceComponent)
{
_appearanceSystem.SetData(weapon, key, value, appearanceComponent);
}
#region InsertModules
private void LightModuleOnInsert(EntityUid module, LightModuleComponent component, EntGotInsertedIntoContainerMessage args)
{
EntityUid weapon = args.Container.Owner;
if(!TryInsertModule(module, weapon, args, out var weaponModulesComponent))
return;
if(!TryComp<AppearanceComponent>(weapon, out var appearanceComponent)) return;
SetAppearance(weapon, ModuleVisualState.Module, component.AppearanceValue, appearanceComponent);
_lightSystem.EnsureLight(weapon);
_lightSystem.TryGetLight(weapon, out var light);
_appearanceSystem.SetData(weapon, Modules.Light, "none", appearanceComponent);
_lightSystem.SetRadius(weapon, 4F, light);
_lightSystem.SetEnabled(weapon, true, light);
}
private void LaserModuleOnInsert(EntityUid module, LaserModuleComponent component, EntGotInsertedIntoContainerMessage args)
{
EntityUid weapon = args.Container.Owner;
if(!TryInsertModule(module, weapon, args, out var weaponModulesComponent))
return;
if(!TryComp<AppearanceComponent>(weapon, out var appearanceComponent)) return;
if (!TryComp<GunComponent>(weapon, out var gunComp)) return;
component.OldProjectileSpeed = gunComp.ProjectileSpeed;
SetAppearance(weapon, ModuleVisualState.Module, component.AppearanceValue, appearanceComponent);
_gunSystem.SetProjectileSpeed(weapon, component.OldProjectileSpeed + 15F);
}
private void FlameHiderModuleOnInsert(EntityUid module, FlameHiderModuleComponent component, EntGotInsertedIntoContainerMessage args)
{
EntityUid weapon = args.Container.Owner;
if(!TryInsertModule(module, weapon, args, out var weaponModulesComponent))
return;
if(!TryComp<AppearanceComponent>(weapon, out var appearanceComponent)) return;
SetAppearance(weapon, ModuleVisualState.Module, component.AppearanceValue, appearanceComponent);
weaponModulesComponent.UseEffect = true;
Dirty(module, weaponModulesComponent);
}
private void SilencerModuleOnInsert(EntityUid module, SilencerModuleComponent component, EntGotInsertedIntoContainerMessage args)
{
EntityUid weapon = args.Container.Owner;
if(!TryInsertModule(module, weapon, args, out var weaponModulesComponent))
return;
if(!TryComp<AppearanceComponent>(weapon, out var appearanceComponent)) return;
if (!TryComp<GunComponent>(weapon, out var gunComp)) return;
component.OldSoundGunshot = gunComp.SoundGunshot;
SetAppearance(weapon, ModuleVisualState.Module, component.AppearanceValue, appearanceComponent);
weaponModulesComponent.UseEffect = true;
_gunSystem.SetSound(weapon, component.NewSoundGunshot);
Dirty(module, weaponModulesComponent);
}
private void AcceleratorModuleOnInsert(EntityUid module, AcceleratorModuleComponent component, EntGotInsertedIntoContainerMessage args)
{
EntityUid weapon = args.Container.Owner;
if(!TryInsertModule(module, weapon, args, out var weaponModulesComponent))
return;
if(!TryComp<AppearanceComponent>(weapon, out var appearanceComponent)) return;
if (!TryComp<GunComponent>(weapon, out var gunComp)) return;
component.OldFireRate = gunComp.FireRate;
SetAppearance(weapon, ModuleVisualState.Module, component.AppearanceValue, appearanceComponent);
_gunSystem.SetFireRate(weapon, component.OldFireRate + 2F);
}
#endregion
#region EjectModules
private void LightModuleOnEject(EntityUid module, LightModuleComponent component, EntGotRemovedFromContainerMessage args)
{
EntityUid weapon = args.Container.Owner;
if(!TryEjectModule(module, weapon, args, out var weaponModulesComponent))
return;
if(!TryComp<AppearanceComponent>(weapon, out var appearanceComponent)) return;
SetAppearance(weapon, ModuleVisualState.Module, "none", appearanceComponent);
_lightSystem.TryGetLight(weapon, out var light);
_lightSystem.SetRadius(weapon, 0F, light);
_lightSystem.SetEnabled(weapon, false, light);
}
private void LaserModuleOnEject(EntityUid module, LaserModuleComponent component, EntGotRemovedFromContainerMessage args)
{
EntityUid weapon = args.Container.Owner;
if(!TryEjectModule(module, weapon, args, out var weaponModulesComponent))
return;
if(!TryComp<AppearanceComponent>(weapon, out var appearanceComponent)) return;
SetAppearance(weapon, ModuleVisualState.Module, "none", appearanceComponent);
_gunSystem.SetProjectileSpeed(weapon, component.OldProjectileSpeed);
}
private void FlameHiderModuleOnEject(EntityUid module, FlameHiderModuleComponent component, EntGotRemovedFromContainerMessage args)
{
EntityUid weapon = args.Container.Owner;
if(!TryEjectModule(module, weapon, args, out var weaponModulesComponent))
return;
if(!TryComp<AppearanceComponent>(weapon, out var appearanceComponent)) return;
SetAppearance(weapon, ModuleVisualState.Module, "none", appearanceComponent);
weaponModulesComponent.UseEffect = false;
Dirty(module, weaponModulesComponent);
}
private void SilencerModuleOnEject(EntityUid module, SilencerModuleComponent component, EntGotRemovedFromContainerMessage args)
{
EntityUid weapon = args.Container.Owner;
if(!TryEjectModule(module, weapon, args, out var weaponModulesComponent))
return;
if(!TryComp<AppearanceComponent>(weapon, out var appearanceComponent)) return;
SetAppearance(weapon, ModuleVisualState.Module, "none", appearanceComponent);
weaponModulesComponent.UseEffect = false;
_gunSystem.SetSound(weapon, component.OldSoundGunshot!);
Dirty(module, weaponModulesComponent);
}
private void AcceleratorModuleOnEject(EntityUid module, AcceleratorModuleComponent component, EntGotRemovedFromContainerMessage args)
{
EntityUid weapon = args.Container.Owner;
if(!TryEjectModule(module, weapon, args, out var weaponModulesComponent))
return;
if(!TryComp<AppearanceComponent>(weapon, out var appearanceComponent)) return;
SetAppearance(weapon, ModuleVisualState.Module, "none", appearanceComponent);
_gunSystem.SetFireRate(weapon, component.OldFireRate);
}
#endregion
}