From c0ad61933ecc77046668e33c6e41ac3d6f4ccf8e Mon Sep 17 00:00:00 2001 From: Lilith Schier Date: Tue, 14 Jul 2026 15:24:08 +0200 Subject: [PATCH] Made player movement fully digital --- project.godot | 8 ++++---- src/player/Player.cs | 39 +++++++++++++++++++++++++++++++++------ 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/project.godot b/project.godot index 488f5c4..f325b49 100644 --- a/project.godot +++ b/project.godot @@ -39,25 +39,25 @@ enabled=PackedStringArray("res://addons/AsepriteWizard/plugin.cfg", "res://addon [input] move_left={ -"deadzone": 0.2, +"deadzone": 0.25, "events": [Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":0,"axis_value":-1.0,"script":null) , Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":65,"key_label":0,"unicode":97,"location":0,"echo":false,"script":null) ] } move_right={ -"deadzone": 0.2, +"deadzone": 0.25, "events": [Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":0,"axis_value":1.0,"script":null) , Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":68,"key_label":0,"unicode":100,"location":0,"echo":false,"script":null) ] } move_up={ -"deadzone": 0.2, +"deadzone": 0.25, "events": [Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":1,"axis_value":-1.0,"script":null) , Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":87,"key_label":0,"unicode":119,"location":0,"echo":false,"script":null) ] } move_down={ -"deadzone": 0.2, +"deadzone": 0.25, "events": [Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":1,"axis_value":1.0,"script":null) , Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":83,"key_label":0,"unicode":115,"location":0,"echo":false,"script":null) ] diff --git a/src/player/Player.cs b/src/player/Player.cs index 28777af..5a11b08 100644 --- a/src/player/Player.cs +++ b/src/player/Player.cs @@ -1,6 +1,6 @@ using System; using System.Collections.Generic; -using System.Collections.Specialized; +using System.Diagnostics; using Chickensoft.LogicBlocks; using Chickensoft.LogicBlocks.Auto; @@ -95,11 +95,11 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner, || @event.IsAction("move_up") || @event.IsAction("move_down")) { - _currentDirection = Input.GetVector( + _currentDirection = To8DirectionVector(Input.GetVector( "move_left", "move_right", "move_up", - "move_down"); + "move_down")); } if (@event.IsAction("jump") || @event.IsAction("attack")) @@ -130,7 +130,7 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner, // Walking on the floor else if (IsOnFloor()) { - var walkInput = IsInputVecSide(_currentDirection) ? _currentDirection.X : 0f; + var walkInput = _currentDirection.X; if (Mathf.Sign(velocity.X) != Mathf.Sign(walkInput)) velocity.X *= Mathf.Pow(GroundedSpeedStopDecay, deltaF); else @@ -261,7 +261,7 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner, if (input.Event.IsAction("jump")) { - if (input.Event.IsPressed() && IsInputVecDown(input.Direction) && IsOnFloor()) + if (input.Event.IsPressed() && input.Direction.Y > 0 && IsOnFloor()) { inputFound = true; DropDown(); @@ -269,7 +269,7 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner, else if ( input.Event.IsPressed() && JumpAllowedTime > 0 - && (!IsOnFloor() || !IsInputVecDown(input.Direction))) + && (!IsOnFloor() || input.Direction.Y <= 0)) { inputFound = true; GroundJump(); @@ -345,6 +345,33 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner, throw new NotImplementedException(); } + private static Vector2 To8DirectionVector(Vector2 input) + { + if (input.IsEqualApprox(Vector2.Zero)) return Vector2.Zero; + var angle = input.Angle(); + const float eighthCircle = float.Pi / 4; + const float sixteenthCircle = float.Pi / 8; + var sector = Mathf.FloorToInt((angle + sixteenthCircle) / eighthCircle) % 8; + if (sector < 0) sector += 8; + return sector switch + { + 0 => Vector2.Right, + 1 => Vector2.Right + Vector2.Down, + 2 => Vector2.Down, + 3 => Vector2.Down + Vector2.Left, + 4 => Vector2.Left, + 5 => Vector2.Left + Vector2.Up, + 6 => Vector2.Up, + 7 => Vector2.Up + Vector2.Right, + _ => throw new UnreachableException(), + }; + } + + public override void _Draw() + { + DrawLine(GlobalPosition, GlobalPosition + _currentDirection * 20f, Colors.Aquamarine); + } + private bool IsInputVecDown(Vector2 vec) { return Mathf.Abs(vec.AngleTo(Vector2.Down)) < DownInputAngle && vec.Length() > 0.01f;