fish-station/Content.Server/Weapons/Ranged/Systems/GunSystem.Battery.cs
Vigers Ray ed0a30bb64 Merge remote-tracking branch 'space-wizards/master'
# Conflicts:
#	Content.Client/Weapons/Ranged/Systems/GunSystem.cs
#	Content.IntegrationTests/Tests/PostMapInitTest.cs
#	Content.Server/Entry/EntryPoint.cs
#	Content.Server/Ghost/GhostSystem.cs
#	Content.Server/IoC/ServerContentIoC.cs
#	Content.Server/Shuttles/Systems/ArrivalsSystem.cs
#	Content.Server/Shuttles/Systems/EmergencyShuttleSystem.cs
#	Content.Server/Storage/EntitySystems/SpawnItemsOnUseSystem.cs
#	Content.Server/Voting/Managers/VoteManager.DefaultVotes.cs
#	Content.Server/Weapons/Ranged/Systems/GunSystem.Battery.cs
#	Content.Server/Weapons/Ranged/Systems/GunSystem.Cartridges.cs
#	Content.Server/Weapons/Ranged/Systems/GunSystem.cs
#	Content.Shared/Damage/Systems/DamageableSystem.cs
#	Content.Shared/Follower/FollowerSystem.cs
#	Resources/Locale/en-US/_strings/actions/actions/dna-scrambler.ftl
#	Resources/Locale/en-US/_strings/administration/commands/change-cvar-command.ftl
#	Resources/Locale/en-US/discord/vote-notifications.ftl
#	Resources/Prototypes/Catalog/Fills/Crates/armory.yml
#	Resources/Prototypes/Catalog/Fills/Lockers/security.yml
#	Resources/Prototypes/Entities/Clothing/OuterClothing/misc.yml
#	Resources/Prototypes/Entities/Objects/Specific/Medical/handheld_crew_monitor.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Rifles/rifles.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/SMGs/smgs.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Shotguns/shotguns.yml
#	Resources/Prototypes/Voice/speech_emotes.yml
2025-02-19 13:16:37 +03:00

139 lines
5.1 KiB
C#

using Content.Server.Power.Components;
using Content.Shared.Damage;
using Content.Shared.Damage.Events;
using Content.Shared.FixedPoint;
using Content.Shared.Projectiles;
using Content.Shared.Weapons.Ranged;
using Content.Shared.Weapons.Ranged.Components;
using Robust.Shared.Prototypes;
namespace Content.Server.Weapons.Ranged.Systems;
public sealed partial class GunSystem
{
protected override void InitializeBattery()
{
base.InitializeBattery();
// Hitscan
SubscribeLocalEvent<HitscanBatteryAmmoProviderComponent, ComponentStartup>(OnBatteryStartup);
SubscribeLocalEvent<HitscanBatteryAmmoProviderComponent, ChargeChangedEvent>(OnBatteryChargeChange);
SubscribeLocalEvent<HitscanBatteryAmmoProviderComponent, DamageExamineEvent>(OnBatteryDamageExamine);
// Projectile
SubscribeLocalEvent<ProjectileBatteryAmmoProviderComponent, ComponentStartup>(OnBatteryStartup);
SubscribeLocalEvent<ProjectileBatteryAmmoProviderComponent, ChargeChangedEvent>(OnBatteryChargeChange);
SubscribeLocalEvent<ProjectileBatteryAmmoProviderComponent, DamageExamineEvent>(OnBatteryDamageExamine);
}
private void OnBatteryStartup(EntityUid uid, BatteryAmmoProviderComponent component, ComponentStartup args)
{
UpdateShots(uid, component);
}
private void OnBatteryChargeChange(EntityUid uid, BatteryAmmoProviderComponent component, ref ChargeChangedEvent args)
{
UpdateShots(uid, component, args.Charge, args.MaxCharge);
}
private void UpdateShots(EntityUid uid, BatteryAmmoProviderComponent component)
{
if (!TryComp<BatteryComponent>(uid, out var battery))
return;
UpdateShots(uid, component, battery.CurrentCharge, battery.MaxCharge);
}
private void UpdateShots(EntityUid uid, BatteryAmmoProviderComponent component, float charge, float maxCharge)
{
var shots = (int) (charge / component.FireCost);
var maxShots = (int) (maxCharge / component.FireCost);
if (component.Shots != shots || component.Capacity != maxShots)
{
Dirty(uid, component);
}
component.Shots = shots;
component.Capacity = maxShots;
UpdateBatteryAppearance(uid, component);
}
private void OnBatteryDamageExamine(EntityUid uid, BatteryAmmoProviderComponent component, ref DamageExamineEvent args)
{
var damageSpec = GetDamage(component);
if (damageSpec == null)
return;
string damageType;
var shotCount = 1;
var shootModifier = ShootModifier.None;
switch (component)
{
case HitscanBatteryAmmoProviderComponent hitscan:
var hitScanPrototype = _proto.Index<HitscanPrototype>(hitscan.Prototype);
if (hitScanPrototype.ShootModifier == ShootModifier.Split)
{
shotCount = hitScanPrototype.SplitCount;
shootModifier = ShootModifier.Split;
}
else if (hitScanPrototype.ShootModifier == ShootModifier.Spread)
{
shotCount = hitScanPrototype.SpreadCount;
shootModifier = ShootModifier.Spread;
}
damageType = Loc.GetString("damage-hitscan");
break;
case ProjectileBatteryAmmoProviderComponent projectile:
var prototype = _proto.Index<EntityPrototype>(projectile.Prototype);
if (prototype.TryGetComponent<ProjectileSpreadComponent>(out var ammoSpreadComp, _componentFactory))
{
shotCount = ammoSpreadComp.Count;
shootModifier = ShootModifier.Spread;
}
damageType = Loc.GetString("damage-projectile");
break;
default:
throw new ArgumentOutOfRangeException();
}
_damageExamine.AddDamageExamine(args.Message, Damageable.ApplyUniversalAllModifiers(damageSpec), shotCount, shootModifier, damageType);
}
private DamageSpecifier? GetDamage(BatteryAmmoProviderComponent component)
{
if (component is ProjectileBatteryAmmoProviderComponent battery)
{
if (ProtoManager.Index<EntityPrototype>(battery.Prototype).Components
.TryGetValue(_factory.GetComponentName(typeof(ProjectileComponent)), out var projectile))
{
var p = (ProjectileComponent) projectile.Component;
if (!p.Damage.Empty)
{
return p.Damage * Damageable.UniversalProjectileDamageModifier;
}
}
return null;
}
if (component is HitscanBatteryAmmoProviderComponent hitscan)
{
var dmg = ProtoManager.Index<HitscanPrototype>(hitscan.Prototype).Damage;
return dmg == null ? dmg : dmg * Damageable.UniversalHitscanDamageModifier;
}
return null;
}
protected override void TakeCharge(EntityUid uid, BatteryAmmoProviderComponent component)
{
// Will raise ChargeChangedEvent
_battery.UseCharge(uid, component.FireCost);
}
}