SupaLidlGame/State/Character/PlayerState.cs

73 lines
2.2 KiB
C#

using Godot;
using SupaLidlGame.Characters;
namespace SupaLidlGame.State.Character
{
public abstract partial class PlayerState : CharacterState
{
protected Player _player => Character as Player;
[Export]
public PlayerIdleState IdleState { get; set; }
public override CharacterState Input(InputEvent @event)
{
var inventory = Character.Inventory;
if (this is PlayerIdleState or PlayerMoveState &&
!_player.Inventory.IsUsingItem)
{
if (@event.IsActionPressed("equip_1"))
{
inventory.SelectedItem = inventory.GetItemByMap("equip_1");
}
}
return base.Input(@event);
}
public override CharacterState Process(double delta)
{
Character.Direction = Godot.Input.GetVector("ui_left", "ui_right",
"ui_up", "ui_down");
Character.LookTowardsDirection();
Vector2 mousePos = Character.GetGlobalMousePosition();
Vector2 dirToMouse = Character.GlobalPosition.DirectionTo(mousePos);
bool targetTowards(Items.Item item)
{
if (Character.Inventory.SelectedItem is Items.Weapon weapon)
{
if (!weapon.IsUsing)
{
var isPressed = Godot.Input.IsActionPressed("attack1");
var ret = false;
if (!weapon.ShouldHideIdle || isPressed)
{
Character.Target = dirToMouse;
ret = true;
}
if (isPressed)
{
Character.UseCurrentItem();
}
return ret;
}
}
return false;
}
var item = Character.Inventory.SelectedItem;
var offhand = Character.Inventory.OffhandItem;
var _ = targetTowards(item) || targetTowards(offhand);
return base.Process(delta);
}
}
}