Skip to content
This repository has been archived by the owner on Sep 14, 2024. It is now read-only.

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 64a12861cef626fa27a65c3a18f562eaf312bd6e.

* fix: sound
  • Loading branch information
Spatison authored and PuroSlavKing committed Sep 13, 2024
1 parent c68f853 commit 3a39586
Show file tree
Hide file tree
Showing 12 changed files with 568 additions and 189 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);
}
}
Loading

0 comments on commit 3a39586

Please sign in to comment.