ninja 2 electric boogaloo (#15534)

Co-authored-by: deltanedas <@deltanedas:kde.org>
This commit is contained in:
deltanedas
2023-09-10 07:20:27 +01:00
committed by GitHub
parent 25c8a03276
commit 24810d916b
153 changed files with 3892 additions and 78 deletions

View File

@@ -0,0 +1,118 @@
using Content.Shared.Actions;
using Content.Shared.Charges.Components;
using Content.Shared.Charges.Systems;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.Interaction;
using Content.Shared.Ninja.Components;
using Content.Shared.Physics;
using Content.Shared.Popups;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.Timing;
namespace Content.Shared.Ninja.Systems;
/// <summary>
/// Handles dashing logic including charge consumption and checking attempt events.
/// </summary>
public sealed class DashAbilitySystem : EntitySystem
{
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedChargesSystem _charges = default!;
[Dependency] private readonly SharedHandsSystem _hands = default!;
[Dependency] private readonly SharedInteractionSystem _interaction = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<DashAbilityComponent, GetItemActionsEvent>(OnGetItemActions);
SubscribeLocalEvent<DashAbilityComponent, DashEvent>(OnDash);
}
private void OnGetItemActions(EntityUid uid, DashAbilityComponent comp, GetItemActionsEvent args)
{
var ev = new AddDashActionEvent(args.User);
RaiseLocalEvent(uid, ev);
if (ev.Cancelled)
return;
args.AddAction(ref comp.DashActionEntity, comp.DashAction);
}
/// <summary>
/// Handle charges and teleport to a visible location.
/// </summary>
private void OnDash(EntityUid uid, DashAbilityComponent comp, DashEvent args)
{
if (!_timing.IsFirstTimePredicted)
return;
var user = args.Performer;
args.Handled = true;
var ev = new DashAttemptEvent(user);
RaiseLocalEvent(uid, ev);
if (ev.Cancelled)
return;
if (!_hands.IsHolding(user, uid, out var _))
{
_popup.PopupClient(Loc.GetString("dash-ability-not-held", ("item", uid)), user, user);
return;
}
TryComp<LimitedChargesComponent>(uid, out var charges);
if (_charges.IsEmpty(uid, charges))
{
_popup.PopupClient(Loc.GetString("dash-ability-no-charges", ("item", uid)), user, user);
return;
}
var origin = Transform(user).MapPosition;
var target = args.Target.ToMap(EntityManager, _transform);
// prevent collision with the user duh
if (!_interaction.InRangeUnobstructed(origin, target, 0f, CollisionGroup.Opaque, uid => uid == user))
{
// can only dash if the destination is visible on screen
_popup.PopupClient(Loc.GetString("dash-ability-cant-see", ("item", uid)), user, user);
return;
}
_transform.SetCoordinates(user, args.Target);
_transform.AttachToGridOrMap(user);
_audio.PlayPredicted(comp.BlinkSound, user, user);
if (charges != null)
_charges.UseCharge(uid, charges);
}
}
/// <summary>
/// Raised on the item before adding the dash action
/// </summary>
public sealed class AddDashActionEvent : CancellableEntityEventArgs
{
public EntityUid User;
public AddDashActionEvent(EntityUid user)
{
User = user;
}
}
/// <summary>
/// Raised on the item before dashing is done.
/// </summary>
public sealed class DashAttemptEvent : CancellableEntityEventArgs
{
public EntityUid User;
public DashAttemptEvent(EntityUid user)
{
User = user;
}
}

View File

@@ -0,0 +1,72 @@
using Content.Shared.Administration.Logs;
using Content.Shared.Emag.Systems;
using Content.Shared.Database;
using Content.Shared.Interaction;
using Content.Shared.Ninja.Components;
using Content.Shared.Tag;
using Content.Shared.Whitelist;
namespace Content.Shared.Ninja.Systems;
/// <summary>
/// Handles emagging whitelisted objects when clicked.
/// </summary>
public sealed class EmagProviderSystem : EntitySystem
{
[Dependency] private readonly EmagSystem _emag = default!;
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
[Dependency] private readonly SharedNinjaGlovesSystem _gloves = default!;
[Dependency] private readonly TagSystem _tags = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<EmagProviderComponent, BeforeInteractHandEvent>(OnBeforeInteractHand);
}
/// <summary>
/// Emag clicked entities that are on the whitelist.
/// </summary>
private void OnBeforeInteractHand(EntityUid uid, EmagProviderComponent comp, BeforeInteractHandEvent args)
{
// TODO: change this into a generic check event thing
if (args.Handled || !_gloves.AbilityCheck(uid, args, out var target))
return;
// only allowed to emag entities on the whitelist
if (comp.Whitelist != null && !comp.Whitelist.IsValid(target, EntityManager))
return;
// only allowed to emag non-immune entities
if (_tags.HasTag(target, comp.EmagImmuneTag))
return;
var handled = _emag.DoEmagEffect(uid, target);
if (!handled)
return;
_adminLogger.Add(LogType.Emag, LogImpact.High, $"{ToPrettyString(uid):player} emagged {ToPrettyString(target):target}");
var ev = new EmaggedSomethingEvent(target);
RaiseLocalEvent(uid, ref ev);
args.Handled = true;
}
/// <summary>
/// Set the whitelist for emagging something outside of yaml.
/// </summary>
public void SetWhitelist(EntityUid uid, EntityWhitelist? whitelist, EmagProviderComponent? comp = null)
{
if (!Resolve(uid, ref comp))
return;
comp.Whitelist = whitelist;
Dirty(uid, comp);
}
}
/// <summary>
/// Raised on the player when emagging something.
/// </summary>
[ByRefEvent]
public record struct EmaggedSomethingEvent(EntityUid Target);

View File

@@ -0,0 +1,47 @@
using Content.Shared.Inventory.Events;
using Content.Shared.Ninja.Components;
namespace Content.Shared.Ninja.Systems;
/// <summary>
/// System for katana binding and dash events. Recalling is handled by the suit.
/// </summary>
public sealed class EnergyKatanaSystem : EntitySystem
{
[Dependency] private readonly SharedSpaceNinjaSystem _ninja = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<EnergyKatanaComponent, GotEquippedEvent>(OnEquipped);
SubscribeLocalEvent<EnergyKatanaComponent, AddDashActionEvent>(OnAddDashAction);
SubscribeLocalEvent<EnergyKatanaComponent, DashAttemptEvent>(OnDashAttempt);
}
/// <summary>
/// When equipped by a ninja, try to bind it.
/// </summary>
private void OnEquipped(EntityUid uid, EnergyKatanaComponent comp, GotEquippedEvent args)
{
// check if user isnt a ninja or already has a katana bound
var user = args.Equipee;
if (!TryComp<SpaceNinjaComponent>(user, out var ninja) || ninja.Katana != null)
return;
// bind it since its unbound
_ninja.BindKatana(user, uid, ninja);
}
private void OnAddDashAction(EntityUid uid, EnergyKatanaComponent comp, AddDashActionEvent args)
{
if (!HasComp<SpaceNinjaComponent>(args.User))
args.Cancel();
}
private void OnDashAttempt(EntityUid uid, EnergyKatanaComponent comp, DashAttemptEvent args)
{
if (!TryComp<SpaceNinjaComponent>(args.User, out var ninja) || ninja.Katana != uid)
args.Cancel();
}
}

View File

@@ -0,0 +1,69 @@
using Content.Shared.Ninja.Components;
using Content.Shared.DoAfter;
using Robust.Shared.Serialization;
namespace Content.Shared.Ninja.Systems;
/// <summary>
/// Basic draining prediction and API, all real logic is handled serverside.
/// </summary>
public abstract class SharedBatteryDrainerSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<BatteryDrainerComponent, DoAfterAttemptEvent<DrainDoAfterEvent>>(OnDoAfterAttempt);
SubscribeLocalEvent<BatteryDrainerComponent, DrainDoAfterEvent>(OnDoAfter);
}
/// <summary>
/// Cancel any drain doafters if the battery is removed or gets filled.
/// </summary>
protected virtual void OnDoAfterAttempt(EntityUid uid, BatteryDrainerComponent comp, DoAfterAttemptEvent<DrainDoAfterEvent> args)
{
if (comp.BatteryUid == null)
{
args.Cancel();
}
}
/// <summary>
/// Drain power from a power source (on server) and repeat if it succeeded.
/// Client will predict always succeeding since power is serverside.
/// </summary>
private void OnDoAfter(EntityUid uid, BatteryDrainerComponent comp, DrainDoAfterEvent args)
{
if (args.Cancelled || args.Handled || args.Target == null)
return;
// repeat if there is still power to drain
args.Repeat = TryDrainPower(uid, comp, args.Target.Value);
}
/// <summary>
/// Attempt to drain as much power as possible into the powercell.
/// Client always predicts this as succeeding since power is serverside and it can only fail once, when the powercell is filled or the target is emptied.
/// </summary>
protected virtual bool TryDrainPower(EntityUid uid, BatteryDrainerComponent comp, EntityUid target)
{
return true;
}
/// <summary>
/// Sets the battery field on the drainer.
/// </summary>
public void SetBattery(EntityUid uid, EntityUid? battery, BatteryDrainerComponent? comp = null)
{
if (!Resolve(uid, ref comp))
return;
comp.BatteryUid = battery;
}
}
/// <summary>
/// DoAfter event for <see cref="BatteryDrainerComponent"/>.
/// </summary>
[Serializable, NetSerializable]
public sealed partial class DrainDoAfterEvent : SimpleDoAfterEvent { }

View File

@@ -0,0 +1,116 @@
using Content.Shared.Actions;
using Content.Shared.CombatMode;
using Content.Shared.Communications;
using Content.Shared.Doors.Components;
using Content.Shared.DoAfter;
using Content.Shared.Examine;
using Content.Shared.Hands.Components;
using Content.Shared.IdentityManagement;
using Content.Shared.Interaction;
using Content.Shared.Inventory.Events;
using Content.Shared.Ninja.Components;
using Content.Shared.Popups;
using Content.Shared.Research.Components;
using Content.Shared.Timing;
using Content.Shared.Toggleable;
using Robust.Shared.Timing;
using System.Diagnostics.CodeAnalysis;
namespace Content.Shared.Ninja.Systems;
/// <summary>
/// Provides the toggle action and handles examining and unequipping.
/// </summary>
public abstract class SharedNinjaGlovesSystem : EntitySystem
{
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] protected readonly SharedAppearanceSystem Appearance = default!;
[Dependency] private readonly SharedCombatModeSystem _combatMode = default!;
[Dependency] protected readonly SharedDoAfterSystem _doAfter = default!;
[Dependency] protected readonly SharedInteractionSystem Interaction = default!;
[Dependency] private readonly SharedSpaceNinjaSystem _ninja = default!;
[Dependency] protected readonly SharedPopupSystem Popup = default!;
[Dependency] private readonly UseDelaySystem _useDelay = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<NinjaGlovesComponent, GetItemActionsEvent>(OnGetItemActions);
SubscribeLocalEvent<NinjaGlovesComponent, ExaminedEvent>(OnExamined);
SubscribeLocalEvent<NinjaGlovesComponent, GotUnequippedEvent>(OnUnequipped);
}
/// <summary>
/// Disable glove abilities and show the popup if they were enabled previously.
/// </summary>
public void DisableGloves(EntityUid uid, NinjaGlovesComponent? comp = null)
{
// already disabled?
if (!Resolve(uid, ref comp) || comp.User == null)
return;
var user = comp.User.Value;
comp.User = null;
Dirty(uid, comp);
Appearance.SetData(uid, ToggleVisuals.Toggled, false);
Popup.PopupClient(Loc.GetString("ninja-gloves-off"), user, user);
RemComp<BatteryDrainerComponent>(user);
RemComp<EmagProviderComponent>(user);
RemComp<StunProviderComponent>(user);
RemComp<ResearchStealerComponent>(user);
RemComp<CommsHackerComponent>(user);
}
/// <summary>
/// Adds the toggle action when equipped.
/// </summary>
private void OnGetItemActions(EntityUid uid, NinjaGlovesComponent comp, GetItemActionsEvent args)
{
if (HasComp<SpaceNinjaComponent>(args.User))
args.AddAction(ref comp.ToggleActionEntity, comp.ToggleAction);
}
/// <summary>
/// Show if the gloves are enabled when examining.
/// </summary>
private void OnExamined(EntityUid uid, NinjaGlovesComponent comp, ExaminedEvent args)
{
if (!args.IsInDetailsRange)
return;
args.PushText(Loc.GetString(comp.User != null ? "ninja-gloves-examine-on" : "ninja-gloves-examine-off"));
}
/// <summary>
/// Disable gloves when unequipped and clean up ninja's gloves reference
/// </summary>
private void OnUnequipped(EntityUid uid, NinjaGlovesComponent comp, GotUnequippedEvent args)
{
if (comp.User != null)
{
var user = comp.User.Value;
Popup.PopupClient(Loc.GetString("ninja-gloves-off"), user, user);
DisableGloves(uid, comp);
}
}
// TODO: generic event thing
/// <summary>
/// GloveCheck but for abilities stored on the player, skips some checks.
/// Intended to be more generic, doesn't require the user to be a ninja or have any ninja equipment.
/// </summary>
public bool AbilityCheck(EntityUid uid, BeforeInteractHandEvent args, out EntityUid target)
{
target = args.Target;
return _timing.IsFirstTimePredicted
&& !_combatMode.IsInCombatMode(uid)
&& !_useDelay.ActiveDelay(uid)
&& TryComp<HandsComponent>(uid, out var hands)
&& hands.ActiveHandEntity == null
&& Interaction.InRangeUnobstructed(uid, target);
}
}

View File

@@ -0,0 +1,139 @@
using Content.Shared.Actions;
using Content.Shared.Clothing.Components;
using Content.Shared.Clothing.EntitySystems;
using Content.Shared.Inventory.Events;
using Content.Shared.Ninja.Components;
using Content.Shared.Timing;
using Robust.Shared.Audio;
using Robust.Shared.Network;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
namespace Content.Shared.Ninja.Systems;
/// <summary>
/// Handles (un)equipping and provides some API functions.
/// </summary>
public abstract class SharedNinjaSuitSystem : EntitySystem
{
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedNinjaGlovesSystem _gloves = default!;
[Dependency] protected readonly SharedSpaceNinjaSystem _ninja = default!;
[Dependency] protected readonly StealthClothingSystem StealthClothing = default!;
[Dependency] protected readonly UseDelaySystem UseDelay = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<NinjaSuitComponent, GotEquippedEvent>(OnEquipped);
SubscribeLocalEvent<NinjaSuitComponent, GetItemActionsEvent>(OnGetItemActions);
SubscribeLocalEvent<NinjaSuitComponent, AddStealthActionEvent>(OnAddStealthAction);
SubscribeLocalEvent<NinjaSuitComponent, GotUnequippedEvent>(OnUnequipped);
}
/// <summary>
/// Call the shared and serverside code for when a ninja equips the suit.
/// </summary>
private void OnEquipped(EntityUid uid, NinjaSuitComponent comp, GotEquippedEvent args)
{
var user = args.Equipee;
if (!TryComp<SpaceNinjaComponent>(user, out var ninja))
return;
NinjaEquippedSuit(uid, comp, user, ninja);
}
/// <summary>
/// Add all the actions when a suit is equipped by a ninja.
/// </summary>
private void OnGetItemActions(EntityUid uid, NinjaSuitComponent comp, GetItemActionsEvent args)
{
if (!HasComp<SpaceNinjaComponent>(args.User))
return;
args.AddAction(ref comp.RecallKatanaActionEntity, comp.RecallKatanaAction);
args.AddAction(ref comp.CreateThrowingStarActionEntity, comp.CreateThrowingStarAction);
args.AddAction(ref comp.EmpActionEntity, comp.EmpAction);
}
/// <summary>
/// Only add stealth clothing's toggle action when equipped by a ninja.
/// </summary>
private void OnAddStealthAction(EntityUid uid, NinjaSuitComponent comp, AddStealthActionEvent args)
{
if (!HasComp<SpaceNinjaComponent>(args.User))
args.Cancel();
}
/// <summary>
/// Call the shared and serverside code for when anyone unequips a suit.
/// </summary>
private void OnUnequipped(EntityUid uid, NinjaSuitComponent comp, GotUnequippedEvent args)
{
UserUnequippedSuit(uid, comp, args.Equipee);
}
/// <summary>
/// Called when a suit is equipped by a space ninja.
/// In the future it might be changed to an explicit activation toggle/verb like gloves are.
/// </summary>
protected virtual void NinjaEquippedSuit(EntityUid uid, NinjaSuitComponent comp, EntityUid user, SpaceNinjaComponent ninja)
{
// mark the user as wearing this suit, used when being attacked among other things
_ninja.AssignSuit(user, uid, ninja);
// initialize phase cloak, but keep it off
StealthClothing.SetEnabled(uid, user, false);
}
/// <summary>
/// Force uncloaks the user and disables suit abilities.
/// </summary>
public void RevealNinja(EntityUid uid, EntityUid user, NinjaSuitComponent? comp = null, StealthClothingComponent? stealthClothing = null)
{
if (!Resolve(uid, ref comp, ref stealthClothing))
return;
if (!StealthClothing.SetEnabled(uid, user, false, stealthClothing))
return;
// previously cloaked, disable abilities for a short time
_audio.PlayPredicted(comp.RevealSound, uid, user);
// all abilities check for a usedelay on the ninja
var useDelay = EnsureComp<UseDelayComponent>(user);
useDelay.Delay = comp.DisableTime;
UseDelay.BeginDelay(user, useDelay);
}
// TODO: modify PowerCellDrain
/// <summary>
/// Returns the power used by a suit
/// </summary>
public float SuitWattage(EntityUid uid, NinjaSuitComponent? suit = null)
{
if (!Resolve(uid, ref suit))
return 0f;
float wattage = suit.PassiveWattage;
if (TryComp<StealthClothingComponent>(uid, out var stealthClothing) && stealthClothing.Enabled)
wattage += suit.CloakWattage;
return wattage;
}
/// <summary>
/// Called when a suit is unequipped, not necessarily by a space ninja.
/// In the future it might be changed to also have explicit deactivation via toggle.
/// </summary>
protected virtual void UserUnequippedSuit(EntityUid uid, NinjaSuitComponent comp, EntityUid user)
{
if (!TryComp<SpaceNinjaComponent>(user, out var ninja))
return;
// mark the user as not wearing a suit
_ninja.AssignSuit(user, null, ninja);
// disable glove abilities
if (ninja.Gloves != null && TryComp<NinjaGlovesComponent>(ninja.Gloves.Value, out var gloves))
_gloves.DisableGloves(ninja.Gloves.Value, gloves);
}
}

View File

@@ -0,0 +1,89 @@
using Content.Shared.Clothing.Components;
using Content.Shared.Ninja.Components;
using Content.Shared.Weapons.Melee.Events;
using Content.Shared.Weapons.Ranged.Events;
using Content.Shared.Popups;
namespace Content.Shared.Ninja.Systems;
/// <summary>
/// Provides shared ninja API, handles being attacked revealing ninja and stops guns from shooting.
/// </summary>
public abstract class SharedSpaceNinjaSystem : EntitySystem
{
[Dependency] protected readonly SharedNinjaSuitSystem _suit = default!;
[Dependency] protected readonly SharedPopupSystem _popup = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SpaceNinjaComponent, AttackedEvent>(OnNinjaAttacked);
SubscribeLocalEvent<SpaceNinjaComponent, ShotAttemptedEvent>(OnShotAttempted);
}
/// <summary>
/// Set the ninja's worn suit entity
/// </summary>
public void AssignSuit(EntityUid uid, EntityUid? suit, SpaceNinjaComponent? comp = null)
{
if (!Resolve(uid, ref comp) || comp.Suit == suit)
return;
comp.Suit = suit;
Dirty(uid, comp);
}
/// <summary>
/// Set the ninja's worn gloves entity
/// </summary>
public void AssignGloves(EntityUid uid, EntityUid? gloves, SpaceNinjaComponent? comp = null)
{
if (!Resolve(uid, ref comp) || comp.Gloves == gloves)
return;
comp.Gloves = gloves;
Dirty(uid, comp);
}
/// <summary>
/// Bind a katana entity to a ninja, letting it be recalled and dash.
/// </summary>
public void BindKatana(EntityUid uid, EntityUid? katana, SpaceNinjaComponent? comp = null)
{
if (!Resolve(uid, ref comp) || comp.Katana == katana)
return;
comp.Katana = katana;
Dirty(uid, comp);
}
/// <summary>
/// Gets the user's battery and tries to use some charge from it, returning true if successful.
/// Serverside only.
/// </summary>
public virtual bool TryUseCharge(EntityUid user, float charge)
{
return false;
}
/// <summary>
/// Handle revealing ninja if cloaked when attacked.
/// </summary>
private void OnNinjaAttacked(EntityUid uid, SpaceNinjaComponent comp, AttackedEvent args)
{
if (comp.Suit != null && TryComp<StealthClothingComponent>(comp.Suit, out var stealthClothing) && stealthClothing.Enabled)
{
_suit.RevealNinja(comp.Suit.Value, uid, null, stealthClothing);
}
}
/// <summary>
/// Require ninja to fight with HONOR, no guns!
/// </summary>
private void OnShotAttempted(EntityUid uid, SpaceNinjaComponent comp, ref ShotAttemptedEvent args)
{
_popup.PopupClient(Loc.GetString("gun-disabled"), uid, uid);
args.Cancel();
}
}

View File

@@ -0,0 +1,32 @@
using Content.Shared.Ninja.Components;
namespace Content.Shared.Ninja.Systems;
/// <summary>
/// All interaction logic is implemented serverside.
/// This is in shared for API and access.
/// </summary>
public abstract class SharedStunProviderSystem : EntitySystem
{
/// <summary>
/// Set the battery field on the stun provider.
/// </summary>
public void SetBattery(EntityUid uid, EntityUid? battery, StunProviderComponent? comp = null)
{
if (!Resolve(uid, ref comp))
return;
comp.BatteryUid = battery;
}
/// <summary>
/// Set the no power popup field on the stun provider.
/// </summary>
public void SetNoPowerPopup(EntityUid uid, string popup, StunProviderComponent? comp = null)
{
if (!Resolve(uid, ref comp))
return;
comp.NoPowerPopup = popup;
}
}