From 1bf37d7afa1f99526d977f22c4132066ea6f1f34 Mon Sep 17 00:00:00 2001 From: HumanoidSandvichDispenser Date: Sat, 12 Aug 2023 23:18:12 -0700 Subject: [PATCH 1/8] controller support wip --- State/Character/PlayerState.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/State/Character/PlayerState.cs b/State/Character/PlayerState.cs index 0c49024..73adcc5 100644 --- a/State/Character/PlayerState.cs +++ b/State/Character/PlayerState.cs @@ -50,6 +50,8 @@ public abstract partial class PlayerState : CharacterState Vector2 mousePos = Character.GetGlobalMousePosition(); Vector2 dirToMouse = Character.GlobalPosition.DirectionTo(mousePos); + Vector2 joystick = Godot.Input.GetVector("look_left", "look_right", + "look_up", "look_down"); if (Character.Inventory.SelectedItem is Items.Weapon weapon) { @@ -58,7 +60,14 @@ public abstract partial class PlayerState : CharacterState if (!weapon.ShouldHideIdle || isAttack1On) { - Character.Target = dirToMouse; + if (joystick.IsZeroApprox()) + { + Character.Target = Character.Direction; + } + else + { + Character.Target = joystick; + } } if (isAttack1On) From 7a0e8e29a3d42c1433b7794a5d28066752648e60 Mon Sep 17 00:00:00 2001 From: HumanoidSandvichDispenser Date: Tue, 15 Aug 2023 00:44:44 -0700 Subject: [PATCH 2/8] switching items with controller --- Items/Inventory.cs | 24 ++++++++++++++++++++++++ State/Character/PlayerState.cs | 8 ++++++++ project.godot | 10 ++++++++++ 3 files changed, 42 insertions(+) diff --git a/Items/Inventory.cs b/Items/Inventory.cs index 56ef5aa..c8e9ef8 100644 --- a/Items/Inventory.cs +++ b/Items/Inventory.cs @@ -27,6 +27,20 @@ public partial class Inventory : Node2D set => EquipItem(value, ref _selectedItem); } + private int _quickSwitchIndex = -1; + + public const int QUICKSWITCH_SIZE = 3; + + public int CurrentQuickSwitchIndex + { + get => _quickSwitchIndex; + set + { + const int size = QUICKSWITCH_SIZE; + _quickSwitchIndex = (value % size + size) % size; + } + } + public bool IsUsingItem => SelectedItem?.IsUsing ?? false; public Inventory() @@ -57,6 +71,16 @@ public partial class Inventory : Node2D base._Ready(); } + public bool EquipIndex(int index) + { + if (index < Items.Count) + { + return EquipItem(Items[index], ref _selectedItem); + } + + return EquipItem(null, ref _selectedItem); + } + private bool EquipItem(Item item, ref Item slot) { if (item is not null) diff --git a/State/Character/PlayerState.cs b/State/Character/PlayerState.cs index 73adcc5..b780fa0 100644 --- a/State/Character/PlayerState.cs +++ b/State/Character/PlayerState.cs @@ -30,6 +30,14 @@ public abstract partial class PlayerState : CharacterState { inventory.SelectedItem = inventory.GetItemByMap("equip_3"); } + else if (@event.IsActionPressed("next_item")) + { + inventory.EquipIndex(++inventory.CurrentQuickSwitchIndex); + } + else if (@event.IsActionPressed("prev_item")) + { + inventory.EquipIndex(--inventory.CurrentQuickSwitchIndex); + } if (@event.IsActionPressed("interact")) { diff --git a/project.godot b/project.godot index 55cbffc..854203e 100644 --- a/project.godot +++ b/project.godot @@ -172,6 +172,16 @@ look_right={ "events": [Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":3,"axis_value":1.0,"script":null) ] } +next_item={ +"deadzone": 0.5, +"events": [Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":5,"pressure":0.0,"pressed":false,"script":null) +] +} +prev_item={ +"deadzone": 0.5, +"events": [Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":4,"pressure":0.0,"pressed":false,"script":null) +] +} [internationalization] From 236e21c7c9d9fe9d2df39004983c0a588cc27b8e Mon Sep 17 00:00:00 2001 From: HumanoidSandvichDispenser Date: Sat, 12 Aug 2023 23:18:12 -0700 Subject: [PATCH 3/8] controller support wip --- State/Character/PlayerState.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/State/Character/PlayerState.cs b/State/Character/PlayerState.cs index 0c49024..73adcc5 100644 --- a/State/Character/PlayerState.cs +++ b/State/Character/PlayerState.cs @@ -50,6 +50,8 @@ public abstract partial class PlayerState : CharacterState Vector2 mousePos = Character.GetGlobalMousePosition(); Vector2 dirToMouse = Character.GlobalPosition.DirectionTo(mousePos); + Vector2 joystick = Godot.Input.GetVector("look_left", "look_right", + "look_up", "look_down"); if (Character.Inventory.SelectedItem is Items.Weapon weapon) { @@ -58,7 +60,14 @@ public abstract partial class PlayerState : CharacterState if (!weapon.ShouldHideIdle || isAttack1On) { - Character.Target = dirToMouse; + if (joystick.IsZeroApprox()) + { + Character.Target = Character.Direction; + } + else + { + Character.Target = joystick; + } } if (isAttack1On) From 24001c4ba1543086c4298b4677fd42ee227f2327 Mon Sep 17 00:00:00 2001 From: HumanoidSandvichDispenser Date: Tue, 15 Aug 2023 00:44:44 -0700 Subject: [PATCH 4/8] switching items with controller --- Items/Inventory.cs | 24 ++++++++++++++++++++++++ State/Character/PlayerState.cs | 8 ++++++++ project.godot | 10 ++++++++++ 3 files changed, 42 insertions(+) diff --git a/Items/Inventory.cs b/Items/Inventory.cs index 56ef5aa..c8e9ef8 100644 --- a/Items/Inventory.cs +++ b/Items/Inventory.cs @@ -27,6 +27,20 @@ public partial class Inventory : Node2D set => EquipItem(value, ref _selectedItem); } + private int _quickSwitchIndex = -1; + + public const int QUICKSWITCH_SIZE = 3; + + public int CurrentQuickSwitchIndex + { + get => _quickSwitchIndex; + set + { + const int size = QUICKSWITCH_SIZE; + _quickSwitchIndex = (value % size + size) % size; + } + } + public bool IsUsingItem => SelectedItem?.IsUsing ?? false; public Inventory() @@ -57,6 +71,16 @@ public partial class Inventory : Node2D base._Ready(); } + public bool EquipIndex(int index) + { + if (index < Items.Count) + { + return EquipItem(Items[index], ref _selectedItem); + } + + return EquipItem(null, ref _selectedItem); + } + private bool EquipItem(Item item, ref Item slot) { if (item is not null) diff --git a/State/Character/PlayerState.cs b/State/Character/PlayerState.cs index 73adcc5..b780fa0 100644 --- a/State/Character/PlayerState.cs +++ b/State/Character/PlayerState.cs @@ -30,6 +30,14 @@ public abstract partial class PlayerState : CharacterState { inventory.SelectedItem = inventory.GetItemByMap("equip_3"); } + else if (@event.IsActionPressed("next_item")) + { + inventory.EquipIndex(++inventory.CurrentQuickSwitchIndex); + } + else if (@event.IsActionPressed("prev_item")) + { + inventory.EquipIndex(--inventory.CurrentQuickSwitchIndex); + } if (@event.IsActionPressed("interact")) { diff --git a/project.godot b/project.godot index 55cbffc..854203e 100644 --- a/project.godot +++ b/project.godot @@ -172,6 +172,16 @@ look_right={ "events": [Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":3,"axis_value":1.0,"script":null) ] } +next_item={ +"deadzone": 0.5, +"events": [Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":5,"pressure":0.0,"pressed":false,"script":null) +] +} +prev_item={ +"deadzone": 0.5, +"events": [Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":4,"pressure":0.0,"pressed":false,"script":null) +] +} [internationalization] From 5ef3229e93a4b1733b87df20eec1b54ec6a4689a Mon Sep 17 00:00:00 2001 From: HumanoidSandvichDispenser Date: Tue, 29 Aug 2023 22:24:20 -0700 Subject: [PATCH 5/8] internally switch input methods; closes #11 --- State/Character/PlayerState.cs | 22 ++++++++++++++++------ State/Global/GameSettings.cs | 16 ++++++++++++++++ State/Global/GlobalState.cs | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 State/Global/GameSettings.cs diff --git a/State/Character/PlayerState.cs b/State/Character/PlayerState.cs index b780fa0..3bfe666 100644 --- a/State/Character/PlayerState.cs +++ b/State/Character/PlayerState.cs @@ -68,13 +68,23 @@ public abstract partial class PlayerState : CharacterState if (!weapon.ShouldHideIdle || isAttack1On) { - if (joystick.IsZeroApprox()) + var inputMethod = Utils.World.Instance.GlobalState + .Settings.InputMethod; + switch (inputMethod) { - Character.Target = Character.Direction; - } - else - { - Character.Target = joystick; + case Global.InputMethod.Joystick: + if (joystick.IsZeroApprox()) + { + Character.Target = Character.Direction; + } + else + { + Character.Target = joystick; + } + break; + default: + Character.Target = dirToMouse; + break; } } diff --git a/State/Global/GameSettings.cs b/State/Global/GameSettings.cs new file mode 100644 index 0000000..baf8174 --- /dev/null +++ b/State/Global/GameSettings.cs @@ -0,0 +1,16 @@ +using Godot; + +namespace SupaLidlGame.State.Global; + +public enum InputMethod +{ + Mouse, + Joystick, + MouseCentered, +} + +public partial class GameSettings : Resource +{ + [Export] + public InputMethod InputMethod { get; set; } +} diff --git a/State/Global/GlobalState.cs b/State/Global/GlobalState.cs index 51b42a4..5471371 100644 --- a/State/Global/GlobalState.cs +++ b/State/Global/GlobalState.cs @@ -14,12 +14,44 @@ public partial class GlobalState : Node [Export] public Stats Stats { get; set; } = new(); + [Export] + public GameSettings Settings { get; set; } = new(); + [Signal] public delegate void SummonBossEventHandler(string bossName); public override void _Ready() { ProcessMode = ProcessModeEnum.Always; + LoadSettings(); + } + + public override void _Notification(int what) + { + if (what == NotificationWMCloseRequest) + { + // TODO: quit prompt + GetTree().Root + .PropagateNotification((int)NotificationWMCloseRequest); + SaveSettings(); + } + } + + private void LoadSettings() + { + if (ResourceLoader.Exists("user://settings.tres")) + { + Settings = ResourceLoader.Load("user://settings.tres"); + } + else + { + Settings = new GameSettings(); + } + } + + public void SaveSettings() + { + ResourceSaver.Save(Settings, "user://settings.tres"); } public void ImportFromSave(Save save) From b1a1b4dfc42f4b37509ffd70ae85ceb9746a1269 Mon Sep 17 00:00:00 2001 From: HumanoidSandvichDispenser Date: Thu, 31 Aug 2023 12:19:50 -0700 Subject: [PATCH 6/8] redirect shungite to player target; resolves #16 --- Entities/ShungiteSpike.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Entities/ShungiteSpike.cs b/Entities/ShungiteSpike.cs index 26fc2a2..c26ccf3 100644 --- a/Entities/ShungiteSpike.cs +++ b/Entities/ShungiteSpike.cs @@ -69,8 +69,7 @@ public partial class ShungiteSpike : Projectile // spawn a dart towards where the player is aiming if (inflictor is Characters.Player player) { - var mousePos = GetGlobalMousePosition(); - var dart = CreateDart(GlobalPosition.DirectionTo(mousePos)); + var dart = CreateDart(player.Target.Normalized()); dart.Hitbox.Faction = player.Faction; Hitbox.IsDisabled = true; } From 17104e7b744aa63af1df3f678b909353703b77db Mon Sep 17 00:00:00 2001 From: HumanoidSandvichDispenser Date: Wed, 6 Sep 2023 23:07:51 -0700 Subject: [PATCH 7/8] use Player.DesiredTarget --- Characters/Player.cs | 22 ++++++++++++++++++++++ State/Character/PlayerState.cs | 26 +++----------------------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/Characters/Player.cs b/Characters/Player.cs index 26eab10..44050a0 100644 --- a/Characters/Player.cs +++ b/Characters/Player.cs @@ -119,4 +119,26 @@ public sealed partial class Player : Character .WithPitchDeviation(0.125f) .Play(); } + + public Vector2 GetDesiredInputFromInput() + { + Vector2 mousePos = GetGlobalMousePosition(); + Vector2 dirToMouse = GlobalPosition.DirectionTo(mousePos); + Vector2 joystick = Godot.Input.GetVector("look_left", "look_right", + "look_up", "look_down"); + + var inputMethod = Utils.World.Instance.GlobalState + .Settings.InputMethod; + switch (inputMethod) + { + case State.Global.InputMethod.Joystick: + if (joystick.IsZeroApprox()) + { + return Direction; + } + return joystick; + default: + return dirToMouse; + } + } } diff --git a/State/Character/PlayerState.cs b/State/Character/PlayerState.cs index 26182e7..b8590fb 100644 --- a/State/Character/PlayerState.cs +++ b/State/Character/PlayerState.cs @@ -55,10 +55,8 @@ public abstract partial class PlayerState : CharacterState "up", "down"); Character.LookTowardsDirection(); - Vector2 mousePos = Character.GetGlobalMousePosition(); - Vector2 dirToMouse = Character.GlobalPosition.DirectionTo(mousePos); - Vector2 joystick = Godot.Input.GetVector("look_left", "look_right", - "look_up", "look_down"); + var player = _player; + player.DesiredTarget = player.GetDesiredInputFromInput(); if (Character.Inventory.SelectedItem is Items.Weapon weapon) { @@ -67,24 +65,7 @@ public abstract partial class PlayerState : CharacterState if (!weapon.ShouldHideIdle || isAttack1On) { - var inputMethod = Utils.World.Instance.GlobalState - .Settings.InputMethod; - switch (inputMethod) - { - case Global.InputMethod.Joystick: - if (joystick.IsZeroApprox()) - { - Character.Target = Character.Direction; - } - else - { - Character.Target = joystick; - } - break; - default: - Character.Target = dirToMouse; - break; - } + player.Target = player.DesiredTarget; } if (isAttack1On) @@ -95,7 +76,6 @@ public abstract partial class PlayerState : CharacterState { Character.UseCurrentItemAlt(); } - } return base.Process(delta); From 88c296dd133c882e064f2eb7a4eb945d60c9a544 Mon Sep 17 00:00:00 2001 From: HumanoidSandvichDispenser Date: Wed, 6 Sep 2023 23:18:53 -0700 Subject: [PATCH 8/8] Player looks in last direction; fix #17 --- Characters/Character.cs | 11 +++++++++-- Characters/Player.cs | 15 ++++++++++++++- State/Character/PlayerState.cs | 7 ++++++- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Characters/Character.cs b/Characters/Character.cs index 2fec6b9..192b9ab 100644 --- a/Characters/Character.cs +++ b/Characters/Character.cs @@ -130,7 +130,14 @@ public partial class Character : CharacterBody2D, IFaction StunAnimation.Stop(); } - Sprite.FlipH = Target.X < 0; + if (Target.X < 0) + { + Sprite.FlipH = true; + } + else if (Target.X > 0) + { + Sprite.FlipH = false; + } DrawTarget(); } @@ -214,7 +221,7 @@ public partial class Character : CharacterBody2D, IFaction scale.Y = -1; angle = Mathf.Pi - angle; } - else + else if (target.X > 0) { scale.Y = 1; } diff --git a/Characters/Player.cs b/Characters/Player.cs index 44050a0..57f7fa8 100644 --- a/Characters/Player.cs +++ b/Characters/Player.cs @@ -11,7 +11,20 @@ public sealed partial class Player : Character { private string _spriteAnim; - public Vector2 DesiredTarget { get; set; } + private Vector2 _desiredTarget; + + public Vector2 DesiredTarget + { + get => _desiredTarget; + set + { + if (value.IsZeroApprox()) + { + return; + } + _desiredTarget = value; + } + } [Export] public PlayerCamera Camera { get; set; } diff --git a/State/Character/PlayerState.cs b/State/Character/PlayerState.cs index b8590fb..aea87c6 100644 --- a/State/Character/PlayerState.cs +++ b/State/Character/PlayerState.cs @@ -56,7 +56,12 @@ public abstract partial class PlayerState : CharacterState Character.LookTowardsDirection(); var player = _player; - player.DesiredTarget = player.GetDesiredInputFromInput(); + var desiredTarget = player.GetDesiredInputFromInput(); + if (!desiredTarget.IsZeroApprox()) + { + // can never be zero + player.DesiredTarget = desiredTarget; + } if (Character.Inventory.SelectedItem is Items.Weapon weapon) {