Skip to content

Commit

Permalink
[Port] Энергетический Меч / Energy Sword (#6)
Browse files Browse the repository at this point in the history
* add: Flip on hit system

* WD EDIT

* fix: animation

* fix: sound

* fix: sound

* Revert "fix: sound"

This reverts commit 64a1286.

* fix: sound
  • Loading branch information
Spatison committed Aug 31, 2024
1 parent c66f0f9 commit f54716b
Show file tree
Hide file tree
Showing 12 changed files with 298 additions and 130 deletions.
75 changes: 75 additions & 0 deletions Content.Client/_White/Animations/FlipOnHitSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Content.Shared._White.Animations;
using Robust.Client.Animations;
using Robust.Client.GameObjects;
using Robust.Shared.Animations;
using Robust.Shared.Timing;

namespace Content.Client._White.Animations;

public sealed class FlipOnHitSystem : SharedFlipOnHitSystem
{
[Dependency] private readonly AnimationPlayerSystem _animationSystem = default!;
[Dependency] private readonly IGameTiming _timing = default!;

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

SubscribeLocalEvent<FlippingComponent, AnimationCompletedEvent>(OnAnimationComplete);
SubscribeAllEvent<FlipOnHitEvent>(ev => PlayAnimation(GetEntity(ev.User)));
}

private void OnAnimationComplete(Entity<FlippingComponent> ent, ref AnimationCompletedEvent args)
{
if (args.Key != FlippingComponent.AnimationKey)
return;

PlayAnimation(ent);
}

protected override void PlayAnimation(EntityUid user)
{
if (!_timing.IsFirstTimePredicted)
return;

if (TerminatingOrDeleted(user))
return;

if (_animationSystem.HasRunningAnimation(user, FlippingComponent.AnimationKey))
{
EnsureComp<FlippingComponent>(user);
return;
}

RemComp<FlippingComponent>(user);

var baseAngle = Angle.Zero;
if (EntityManager.TryGetComponent(user, out SpriteComponent? sprite))
baseAngle = sprite.Rotation;

var degrees = baseAngle.Degrees;

var animation = new Animation
{
Length = TimeSpan.FromMilliseconds(400),
AnimationTracks =
{
new AnimationTrackComponentProperty
{
ComponentType = typeof(SpriteComponent),
Property = nameof(SpriteComponent.Rotation),
InterpolationMode = AnimationInterpolationMode.Linear,
KeyFrames =
{
new AnimationTrackProperty.KeyFrame(Angle.FromDegrees(degrees - 10), 0f),
new AnimationTrackProperty.KeyFrame(Angle.FromDegrees(degrees + 180), 0.2f),
new AnimationTrackProperty.KeyFrame(Angle.FromDegrees(degrees + 360), 0.2f),
new AnimationTrackProperty.KeyFrame(Angle.FromDegrees(degrees), 0f)
}
}
}
};

_animationSystem.Play(user, animation, FlippingComponent.AnimationKey);
}
}
7 changes: 7 additions & 0 deletions Content.Client/_White/Animations/FlippingComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Content.Client._White.Animations;

[RegisterComponent]
public sealed partial class FlippingComponent : Component
{
public const string AnimationKey = "flip";
}
23 changes: 21 additions & 2 deletions Content.Server/Construction/ConstructionSystem.Graph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Robust.Shared.Containers;
using Robust.Shared.Prototypes;
using System.Linq;
using Content.Shared.Hands.Components;

namespace Content.Server.Construction
{
Expand Down Expand Up @@ -304,8 +305,8 @@ public bool ChangeNode(EntityUid uid, EntityUid? userUid, string id, bool perfor
return null;

// [Optional] Exit if the new entity's prototype is a parent of the original
// E.g., if an entity with the 'AirlockCommand' prototype was to be replaced with a new entity that
// had the 'Airlock' prototype, and DoNotReplaceInheritingEntities was true, the code block would
// E.g., if an entity with the 'AirlockCommand' prototype was to be replaced with a new entity that
// had the 'Airlock' prototype, and DoNotReplaceInheritingEntities was true, the code block would
// exit here because 'AirlockCommand' is derived from 'Airlock'
if (GetCurrentNode(uid, construction)?.DoNotReplaceInheritingEntities == true &&
metaData.EntityPrototype?.ID != null)
Expand Down Expand Up @@ -394,6 +395,17 @@ public bool ChangeNode(EntityUid uid, EntityUid? userUid, string id, bool perfor
}
}

// WD EDIT START
if (userUid != null && IsTransformParentOf(userUid.Value, transform) && TryComp(userUid, out HandsComponent? hands))
{
var hand = hands.Hands.Values.FirstOrDefault(h => h.HeldEntity == uid);
if (hand != null)
_handsSystem.TryDrop(userUid.Value, hand, handsComp: hands);

_handsSystem.PickupOrDrop(userUid, newUid, handsComp: hands);
}
// WD EDIT END

var entChangeEv = new ConstructionChangeEntityEvent(newUid, uid);
RaiseLocalEvent(uid, entChangeEv);
RaiseLocalEvent(newUid, entChangeEv, broadcast: true);
Expand All @@ -410,6 +422,13 @@ public bool ChangeNode(EntityUid uid, EntityUid? userUid, string id, bool perfor
return newUid;
}

private bool IsTransformParentOf(EntityUid uid, TransformComponent target) // WD EDIT
{
var parentUid = target.ParentUid;

return parentUid == uid || TryComp(parentUid, out TransformComponent? trans) && IsTransformParentOf(uid, trans);
}

/// <summary>
/// Performs a construction graph change on a construction entity, also changing the node to a valid one on
/// the new graph.
Expand Down
17 changes: 17 additions & 0 deletions Content.Server/_White/Animations/FlipOnHitSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Content.Shared._White.Animations;
using Robust.Shared.Player;

namespace Content.Server._White.Animations;

public sealed class FlipOnHitSystem : SharedFlipOnHitSystem
{
protected override void PlayAnimation(EntityUid user)
{
var filter = Filter.Pvs(user, entityManager: EntityManager);

if (TryComp<ActorComponent>(user, out var actor))
filter.RemovePlayer(actor.PlayerSession);

RaiseNetworkEvent(new FlipOnHitEvent(GetNetEntity(user)), filter);
}
}
16 changes: 8 additions & 8 deletions Content.Shared/Item/ItemToggle/SharedItemToggleSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,11 @@ private void Activate(EntityUid uid, ItemToggleComponent itemToggle, bool predic
/// </summary>
private void Deactivate(EntityUid uid, ItemToggleComponent itemToggle, bool predicted, EntityUid? user = null)
{
if (_netManager.IsClient) // WD EDIT
return;

var soundToPlay = itemToggle.SoundDeactivate;
if (predicted)
_audio.PlayPredicted(soundToPlay, uid, user);
else
_audio.PlayPvs(soundToPlay, uid);
_audio.PlayPvs(soundToPlay, uid);
// END FIX HARDCODING

var toggleUsed = new ItemToggledEvent(predicted, Activated: false, user);
Expand Down Expand Up @@ -240,14 +240,14 @@ private void OnIsHotEvent(EntityUid uid, ItemToggleHotComponent itemToggleHot, I
/// </summary>
private void UpdateActiveSound(EntityUid uid, ItemToggleActiveSoundComponent activeSound, ref ItemToggledEvent args)
{
if (_netManager.IsClient) // WD EDIT
return;

if (args.Activated)
{
if (activeSound.ActiveSound != null && activeSound.PlayingStream == null)
{
if (args.Predicted)
activeSound.PlayingStream = _audio.PlayPredicted(activeSound.ActiveSound, uid, args.User, AudioParams.Default.WithLoop(true)).Value.Entity;
else
activeSound.PlayingStream = _audio.PlayPvs(activeSound.ActiveSound, uid, AudioParams.Default.WithLoop(true)).Value.Entity;
activeSound.PlayingStream = _audio.PlayPvs(activeSound.ActiveSound, uid, AudioParams.Default.WithLoop(true)).Value.Entity;
}
}
else
Expand Down
6 changes: 6 additions & 0 deletions Content.Shared/_White/Animations/FlipOnHitComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using Robust.Shared.GameStates;

namespace Content.Shared._White.Animations;

[RegisterComponent, NetworkedComponent]
public sealed partial class FlipOnHitComponent : Component;
45 changes: 45 additions & 0 deletions Content.Shared/_White/Animations/SharedFlipOnHitSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Content.Shared.Item.ItemToggle.Components;
using Content.Shared.Standing;
using Content.Shared.Weapons.Melee.Events;
using Robust.Shared.Serialization;
using Robust.Shared.Timing;

namespace Content.Shared._White.Animations;

public abstract class SharedFlipOnHitSystem : EntitySystem
{
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly StandingStateSystem _standingState = default!;

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

SubscribeLocalEvent<FlipOnHitComponent, MeleeHitEvent>(OnHit);
}

private void OnHit(Entity<FlipOnHitComponent> ent, ref MeleeHitEvent args)
{
if (!_timing.IsFirstTimePredicted)
return;

if (args.HitEntities.Count == 0)
return;

if (TryComp(ent, out ItemToggleComponent? itemToggle) && !itemToggle.Activated)
return;

if (_standingState.IsDown(args.User))
return;

PlayAnimation(args.User);
}

protected abstract void PlayAnimation(EntityUid user);
}

[Serializable, NetSerializable]
public sealed class FlipOnHitEvent(NetEntity user) : EntityEventArgs
{
public NetEntity User = user;
}
4 changes: 4 additions & 0 deletions Content.Shared/_White/Wield/ToggleableWieldedComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Content.Shared._White.Wield;

[RegisterComponent]
public sealed partial class ToggleableWieldedComponent : Component;
20 changes: 20 additions & 0 deletions Content.Shared/_White/Wield/ToggleableWieldedSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Content.Shared.Item.ItemToggle.Components;
using Content.Shared.Wieldable.Components;

namespace Content.Shared._White.Wield;

public sealed class ToggleableWieldedSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();

SubscribeLocalEvent<ToggleableWieldedComponent, ItemToggleActivateAttemptEvent>(AttemptActivate);
}

private void AttemptActivate(Entity<ToggleableWieldedComponent> ent, ref ItemToggleActivateAttemptEvent args)
{
if (TryComp(ent, out WieldableComponent? wieldable) && !wieldable.Wielded)
args.Cancelled = true;
}
}
Loading

0 comments on commit f54716b

Please sign in to comment.