|
| 1 | +using System.Collections; |
| 2 | +using System.Linq; |
| 3 | +using MiraAPI.GameOptions; |
| 4 | +using MiraAPI.Hud; |
| 5 | +using MiraAPI.Utilities.Assets; |
| 6 | +using UnityEngine; |
| 7 | +using Pb = NewMod.Roles.ImpostorRoles.PulseBlade; |
| 8 | +using MiraAPI.Networking; |
| 9 | +using NewMod.Options.Roles.PulseBladeOptions; |
| 10 | +using Reactor.Utilities; |
| 11 | +using MiraAPI.Utilities; |
| 12 | +using NewMod.Utilities; |
| 13 | +using Reactor.Networking.Attributes; |
| 14 | +using Rewired; |
| 15 | + |
| 16 | +namespace NewMod.Buttons.Pulseblade |
| 17 | +{ |
| 18 | + /// <summary> |
| 19 | + /// Custom button for the Pulseblade role to perform a high-speed strike on the closest player in aim direction. |
| 20 | + /// The strike teleports the user toward the target and executes a stealthy instant kill. |
| 21 | + /// </summary> |
| 22 | + public class StrikeButton : CustomActionButton |
| 23 | + { |
| 24 | + /// <summary> |
| 25 | + /// Display name for the button (not shown by default). |
| 26 | + /// </summary> |
| 27 | + public override string Name => "Strike"; |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Cooldown between strikes, pulled from <see cref="PulseBladeOptions.StrikeCooldown"/>. |
| 31 | + /// </summary> |
| 32 | + public override float Cooldown => OptionGroupSingleton<PulseBladeOptions>.Instance.StrikeCooldown; |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Maximum number of strike uses, from <see cref="PulseBladeOptions.MaxStrikeUses"/>. |
| 36 | + /// </summary> |
| 37 | + public override int MaxUses => (int)OptionGroupSingleton<PulseBladeOptions>.Instance.MaxStrikeUses; |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Effect duration (not used for this button). |
| 41 | + /// </summary> |
| 42 | + public override float EffectDuration => 0f; |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Placement of the button on the HUD. |
| 46 | + /// </summary> |
| 47 | + public override ButtonLocation Location => ButtonLocation.BottomRight; |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Default keybind for Pulseblade's Strike ability. |
| 51 | + /// Requires Shift as a modifier to prevent accidental use. |
| 52 | + /// </summary> |
| 53 | + public override KeyboardKeyCode Defaultkeybind => KeyboardKeyCode.G; |
| 54 | + public override ModifierKey Modifier1 => ModifierKey.Shift; |
| 55 | + /// <summary> |
| 56 | + /// Sprite used for the button — set to empty; |
| 57 | + /// </summary> |
| 58 | + public override LoadableAsset<Sprite> Sprite => NewModAsset.StrikeButton; |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Determines whether the button is active for a given role. |
| 62 | + /// </summary> |
| 63 | + /// <param name="role">The current player's role.</param> |
| 64 | + /// <returns>True only for Pulseblade role.</returns> |
| 65 | + public override bool Enabled(RoleBehaviour role) => role is Pb; |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Called when the button is pressed by the player. |
| 69 | + /// Searches for a valid target and executes the strike. |
| 70 | + /// </summary> |
| 71 | + protected override void OnClick() |
| 72 | + { |
| 73 | + var player = PlayerControl.LocalPlayer; |
| 74 | + |
| 75 | + var target = PlayerControl.AllPlayerControls |
| 76 | + .ToArray() |
| 77 | + .Where(p => p != player && !p.Data.IsDead && !p.Data.Disconnected && !p.inVent) |
| 78 | + .OrderBy(p => Vector2.Distance(player.GetTruePosition(), p.GetTruePosition())) |
| 79 | + .FirstOrDefault(p => Vector2.Distance(player.GetTruePosition(), p.GetTruePosition()) <= OptionGroupSingleton<PulseBladeOptions>.Instance.StrikeRange); |
| 80 | + |
| 81 | + RpcPulseStrike(player, target); |
| 82 | + } |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// RPC method to perform a Pulseblade strike on a target. |
| 86 | + /// </summary> |
| 87 | + /// <param name="source">The player performing the strike.</param> |
| 88 | + /// <param name="target">The victim of the strike.</param> |
| 89 | + [MethodRpc((uint)CustomRPC.Dash)] |
| 90 | + public static void RpcPulseStrike(PlayerControl source, PlayerControl target) |
| 91 | + { |
| 92 | + Coroutines.Start(DoPulseStrike(source, target)); |
| 93 | + } |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// Executes the strike: dashes to the target and performs a kill. |
| 97 | + /// Hides the body for a short time. |
| 98 | + /// </summary> |
| 99 | + /// <param name="killer">The Pulseblade player.</param> |
| 100 | + /// <param name="target">The struck victim.</param> |
| 101 | + /// <returns>IEnumerator.</returns> |
| 102 | + public static IEnumerator DoPulseStrike(PlayerControl killer, PlayerControl target) |
| 103 | + { |
| 104 | + var sound = NewModAsset.StrikeSound.LoadAsset(); |
| 105 | + float originalSpeed = killer.MyPhysics.Speed; |
| 106 | + float dashSpeed = OptionGroupSingleton<PulseBladeOptions>.Instance.DashSpeed; |
| 107 | + |
| 108 | + killer.moveable = false; |
| 109 | + killer.MyPhysics.inputHandler.enabled = false; |
| 110 | + killer.MyPhysics.Speed = dashSpeed; |
| 111 | + |
| 112 | + while (Vector2.Distance(killer.GetTruePosition(), target.GetTruePosition()) > 0.1f) |
| 113 | + { |
| 114 | + Vector2 dir = target.GetTruePosition() - killer.GetTruePosition(); |
| 115 | + killer.MyPhysics.SetNormalizedVelocity(dir.normalized); |
| 116 | + |
| 117 | + float step = killer.MyPhysics.TrueSpeed * Time.fixedDeltaTime; |
| 118 | + if (step >= dir.magnitude) break; |
| 119 | + |
| 120 | + yield return new WaitForFixedUpdate(); |
| 121 | + } |
| 122 | + |
| 123 | + killer.MyPhysics.SetNormalizedVelocity(Vector2.zero); |
| 124 | + killer.MyPhysics.Speed = originalSpeed; |
| 125 | + killer.MyPhysics.inputHandler.enabled = true; |
| 126 | + killer.moveable = true; |
| 127 | + |
| 128 | + SoundManager.Instance.PlaySound(sound, false, 1f); |
| 129 | + |
| 130 | + killer.RpcCustomMurder( |
| 131 | + target, |
| 132 | + didSucceed: true, |
| 133 | + resetKillTimer: false, |
| 134 | + createDeadBody: true, |
| 135 | + teleportMurderer: false, |
| 136 | + showKillAnim: false, |
| 137 | + playKillSound: false |
| 138 | + ); |
| 139 | + |
| 140 | + Utils.RegisterStrikeKill(killer, target); |
| 141 | + |
| 142 | + var notif = Helpers.CreateAndShowNotification($"Perfect kill {target.Data.PlayerName} eliminated", new(1f, 0.25f, 0.25f), spr: NewModAsset.StrikeIcon.LoadAsset()); |
| 143 | + notif.Text.SetOutlineThickness(0.30f); |
| 144 | + |
| 145 | + var bodies = Helpers.GetNearestDeadBodies(target.GetTruePosition(), 0.5f, Helpers.CreateFilter(Constants.NotShipMask)); |
| 146 | + if (bodies != null && bodies.Count > 0) |
| 147 | + { |
| 148 | + foreach (var b in bodies) if (b) b.gameObject.SetActive(false); |
| 149 | + yield return new WaitForSeconds(OptionGroupSingleton<PulseBladeOptions>.Instance.HideBodyDuration); |
| 150 | + foreach (var b in bodies) if (b) b.gameObject.SetActive(true); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments