From 93703fdc5a87f925f9a80c853db9eade23f26ad7 Mon Sep 17 00:00:00 2001 From: copygirl Date: Fri, 14 May 2021 22:16:16 +0200 Subject: [PATCH] Put weapon in lowered state when cursor too close Fixes an issue where the angle calculated is not a valid float --- src/Items/Weapon.cs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/Items/Weapon.cs b/src/Items/Weapon.cs index 6aceded..7fac9d2 100644 --- a/src/Items/Weapon.cs +++ b/src/Items/Weapon.cs @@ -27,6 +27,7 @@ public class Weapon : Sprite public float _fireDelay; public float? _reloading; + public bool _lowered; private float _currentSpreadInc = 0.0F; private float _currentRecoil = 0.0F; @@ -121,9 +122,16 @@ public class Weapon : Sprite var a = TipOffset.y * ((Scale.y > 0) ? 1 : -1); var c = Player.Position.DistanceTo(Cursor.Position); - var angleC = Mathf.Asin(a / c); - AimDirection = Cursor.Position.AngleToPoint(Player.Position) - angleC; - // FIXME: Angle calculation when cursor is too close to player. + if (c < TipOffset.x) { + // If the cursor is too close to the player, put the + // weapon in a "lowered" state, where it can't be shot. + AimDirection = Mathf.Deg2Rad((Cursor.Position.x > Player.Position.x) ? 30 : 150); + _lowered = true; + } else { + var angleC = Mathf.Asin(a / c); + AimDirection = Cursor.Position.AngleToPoint(Player.Position) - angleC; + _lowered = false; + } RPC.Unreliable(1, SendAimAngle, AimDirection); Update(); @@ -163,7 +171,7 @@ public class Weapon : Sprite protected virtual bool FireInternal(float aimDirection, bool toRight, int seed) { - if (!Visible || (_reloading != null) || (Rounds <= 0) || (_fireDelay > 0)) return false; + if (!Visible || _lowered || (_reloading != null) || (Rounds <= 0) || (_fireDelay > 0)) return false; if (this.GetGame() is Client) GetNodeOrNull("Fire")?.Play(); @@ -228,7 +236,7 @@ public class Weapon : Sprite public override void _Draw() { - if (!(Player is LocalPlayer)) return; + if (!(Player is LocalPlayer) || _lowered) return; // Draws an "aiming cone" to show where bullets might travel. var tip = TipOffset + new Vector2(4, 0);