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 5496048..1ea3afd 100644 --- a/Characters/Player.cs +++ b/Characters/Player.cs @@ -11,9 +11,22 @@ public sealed partial class Player : Character { private string _spriteAnim; - private TargetTracer _targetTracer; + private Vector2 _desiredTarget; - public Vector2 DesiredTarget { get; set; } + public Vector2 DesiredTarget + { + get => _desiredTarget; + set + { + if (value.IsZeroApprox()) + { + return; + } + _desiredTarget = value; + } + } + + private TargetTracer _targetTracer; [Export] public PlayerCamera Camera { get; set; } @@ -125,4 +138,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/Entities/ShungiteSpike.cs b/Entities/ShungiteSpike.cs index 983241a..e9e9c2f 100644 --- a/Entities/ShungiteSpike.cs +++ b/Entities/ShungiteSpike.cs @@ -70,8 +70,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; } diff --git a/Items/Inventory.cs b/Items/Inventory.cs index 321a9a7..25048cb 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() @@ -56,6 +70,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 602dd86..aea87c6 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")) { @@ -47,8 +55,13 @@ public abstract partial class PlayerState : CharacterState "up", "down"); Character.LookTowardsDirection(); - Vector2 mousePos = Character.GetGlobalMousePosition(); - Vector2 dirToMouse = Character.GlobalPosition.DirectionTo(mousePos); + var player = _player; + var desiredTarget = player.GetDesiredInputFromInput(); + if (!desiredTarget.IsZeroApprox()) + { + // can never be zero + player.DesiredTarget = desiredTarget; + } if (Character.Inventory.SelectedItem is Items.Weapon weapon) { @@ -57,7 +70,7 @@ public abstract partial class PlayerState : CharacterState if (!weapon.ShouldHideIdle || isAttack1On) { - Character.Target = dirToMouse; + player.Target = player.DesiredTarget; } if (isAttack1On) @@ -68,7 +81,6 @@ public abstract partial class PlayerState : CharacterState { Character.UseCurrentItemAlt(); } - } return base.Process(delta); 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) diff --git a/project.godot b/project.godot index 64ee799..5f318a1 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]