Skip to content

Commit

Permalink
Add component
Browse files Browse the repository at this point in the history
  • Loading branch information
Schrodinger71 committed Jun 29, 2024
1 parent 315d367 commit 594d279
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
83 changes: 83 additions & 0 deletions Content.Server/ADT/SwitchableWeapon/SwitchableWeaponSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using Content.Shared.Damage.Components;
using Content.Shared.Damage.Events;
using Content.Shared.Examine;
using Content.Shared.Interaction.Events;
using Content.Shared.Item;
using Content.Shared.ADT.SwitchableWeapon;
using Content.Shared.Toggleable;
using Content.Shared.Weapons.Melee.Events;
using Robust.Shared.Audio.Systems;

namespace Content.Server.ADT.SwitchableWeapon;

public sealed class SwitchableWeaponSystem : EntitySystem
{
[Dependency] private readonly SharedItemSystem _item = default!;
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;

public override void Initialize()
{
base.Initialize();

SubscribeLocalEvent<SwitchableWeaponComponent, UseInHandEvent>(Toggle);
SubscribeLocalEvent<SwitchableWeaponComponent, ExaminedEvent>(OnExamined);
SubscribeLocalEvent<SwitchableWeaponComponent, StaminaDamageOnHitAttemptEvent>(OnStaminaHitAttempt);
SubscribeLocalEvent<SwitchableWeaponComponent, GetMeleeDamageEvent>(OnGetMeleeDamage);
SubscribeLocalEvent<SwitchableWeaponComponent, ComponentAdd>(OnComponentAdded);
}

private void OnComponentAdded(EntityUid uid, SwitchableWeaponComponent component, ComponentAdd args)
{
UpdateState(uid, component);
}

//Non-stamina damage
private void OnGetMeleeDamage(EntityUid uid, SwitchableWeaponComponent component, ref GetMeleeDamageEvent args)
{
args.Damage = component.IsOpen ? component.DamageOpen : component.DamageFolded;
}

private void OnStaminaHitAttempt(EntityUid uid, SwitchableWeaponComponent component, ref StaminaDamageOnHitAttemptEvent args)
{
if (!component.IsOpen)
return;

//args.HitSoundOverride = component.BonkSound;
}

private void OnExamined(EntityUid uid, SwitchableWeaponComponent comp, ExaminedEvent args)
{
var msg = comp.IsOpen
? Loc.GetString("comp-switchable-examined-on")
: Loc.GetString("comp-switchable-examined-off");
args.PushMarkup(msg);
}

private void UpdateState(EntityUid uid, SwitchableWeaponComponent comp)
{
if (TryComp<ItemComponent>(comp.Owner, out var item))
{
_item.SetSize(item.Owner, comp.IsOpen ? comp.SizeOpened : comp.SizeClosed, item);
_item.SetHeldPrefix(comp.Owner, comp.IsOpen ? "on" : "off", false, item);
}

if (TryComp<AppearanceComponent>(comp.Owner, out var appearance))
_appearance.SetData(comp.Owner, ToggleVisuals.Toggled, comp.IsOpen, appearance);

// Change stamina damage according to state
if (TryComp<StaminaDamageOnHitComponent>(uid, out var stamComp))
{
stamComp.Damage = comp.IsOpen ? comp.StaminaDamageOpen : comp.StaminaDamageFolded;
}
}

private void Toggle(EntityUid uid, SwitchableWeaponComponent comp, UseInHandEvent args)
{
comp.IsOpen = !comp.IsOpen;
UpdateState(uid, comp);

var soundToPlay = comp.IsOpen ? comp.OpenSound : comp.CloseSound;
_audio.PlayPvs(soundToPlay, args.User);
}
}
50 changes: 50 additions & 0 deletions Content.Shared/ADT/SwitchableWeapon/SwitchableWeaponComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Content.Shared.Damage;
using Content.Shared.Item;
using Robust.Shared.Audio;
using Robust.Shared.Prototypes;

namespace Content.Shared.ADT.SwitchableWeapon;

[RegisterComponent]
public sealed partial class SwitchableWeaponComponent : Component
{
[ViewVariables(VVAccess.ReadWrite)][DataField("damageFolded")]
public DamageSpecifier DamageFolded = new(){
DamageDict = new()
{
{ "Blunt", 0.0f },
}
};

[ViewVariables(VVAccess.ReadWrite)][DataField("damageOpen")]
public DamageSpecifier DamageOpen = new(){
DamageDict = new()
{
{ "Blunt", 4.0f },
}
};

[ViewVariables(VVAccess.ReadWrite)][DataField("staminaDamageFolded")]
public float StaminaDamageFolded = 0;

[ViewVariables(VVAccess.ReadWrite)][DataField("staminaDamageOpen")]
public float StaminaDamageOpen = 28;

[ViewVariables(VVAccess.ReadWrite)][DataField("isOpen")]
public bool IsOpen = false;

[ViewVariables(VVAccess.ReadWrite)][DataField("openSound")]
public SoundSpecifier? OpenSound;

[ViewVariables(VVAccess.ReadWrite)][DataField("closeSound")]
public SoundSpecifier? CloseSound;

[ViewVariables(VVAccess.ReadWrite)][DataField("bonkSound")]
public SoundSpecifier? BonkSound;

[ViewVariables(VVAccess.ReadWrite)][DataField("sizeOpened")]
public ProtoId<ItemSizePrototype> SizeOpened = "Normal";

[ViewVariables(VVAccess.ReadWrite)][DataField("sizeClosed")]
public ProtoId<ItemSizePrototype> SizeClosed = "Normal";
}

0 comments on commit 594d279

Please sign in to comment.