equipping items with keys

pull/3/head
John Montagu, the 4th Earl of Sandvich 2023-04-16 14:11:17 -07:00
parent 4d70037685
commit abed26d13b
Signed by: sandvich
GPG Key ID: 9A39BE37E602B22D
7 changed files with 102 additions and 21 deletions

View File

@ -163,8 +163,10 @@ shape = SubResource("RectangleShape2D_8lxmf")
[node name="Inventory" type="Node2D" parent="."] [node name="Inventory" type="Node2D" parent="."]
y_sort_enabled = true y_sort_enabled = true
script = ExtResource("7_43gq8") script = ExtResource("7_43gq8")
Items = [null]
[node name="Sword" parent="Inventory" instance=ExtResource("8_s3c8r")] [node name="Sword" parent="Inventory" instance=ExtResource("8_s3c8r")]
Knockback = 100.0
[node name="FlashAnimation" type="AnimationPlayer" parent="."] [node name="FlashAnimation" type="AnimationPlayer" parent="."]
libraries = { libraries = {

View File

@ -193,6 +193,10 @@ horizontal_alignment = 1
y_sort_enabled = true y_sort_enabled = true
position = Vector2(0, 2) position = Vector2(0, 2)
script = ExtResource("7_xyenu") script = ExtResource("7_xyenu")
InventoryMap = {
"equip_1": 0,
"equip_2": 1
}
[node name="Sword" parent="Inventory" instance=ExtResource("7_4rxuv")] [node name="Sword" parent="Inventory" instance=ExtResource("7_4rxuv")]

View File

@ -13,13 +13,32 @@ namespace SupaLidlGame.Characters.State
public override CharacterState Input(InputEvent @event) public override CharacterState Input(InputEvent @event)
{ {
var inventory = Character.Inventory;
#if DEBUG #if DEBUG
if (@event.IsActionPressed("equip")) //if (@event.IsActionPressed("equip"))
{ //{
Character.Inventory.SelectedItem = Character.Inventory.GetNode<Items.Item>("Sword"); // inventory.SelectedItem = inventory.GetNode<Items.Item>("Sword");
} //}
#endif #endif
if (this is PlayerIdleState or PlayerMoveState
&& !_player.Inventory.IsUsingItem)
{
if (@event.IsActionPressed("equip_1"))
{
inventory.SelectedItem = inventory.GetItemByMap("equip_1");
}
else if (@event.IsActionPressed("equip_2"))
{
inventory.SelectedItem = inventory.GetItemByMap("equip_2");
}
else if (@event.IsActionPressed("equip_3"))
{
inventory.SelectedItem = inventory.GetItemByMap("equip_3");
}
}
return base.Input(@event); return base.Input(@event);
} }
@ -36,6 +55,10 @@ namespace SupaLidlGame.Characters.State
Character.Target = dirToMouse; Character.Target = dirToMouse;
} }
} }
else if (!Character.Direction.IsZeroApprox())
{
Character.Target = Character.Direction;
}
if (Godot.Input.IsActionPressed("attack1")) if (Godot.Input.IsActionPressed("attack1"))
{ {

View File

@ -1,6 +1,6 @@
using System.Collections.Generic;
using Godot; using Godot;
using SupaLidlGame.Characters; using SupaLidlGame.Characters;
using Godot.Collections;
namespace SupaLidlGame.Items namespace SupaLidlGame.Items
{ {
@ -8,7 +8,11 @@ namespace SupaLidlGame.Items
{ {
public Character Character { get; private set; } public Character Character { get; private set; }
public List<Item> Items { get; private set; } = new List<Item>(); [Export]
public Array<Item> Items { get; private set; } = new Array<Item>();
[Export]
public Dictionary<string, int> InventoryMap { get; set; }
public const int MaxCapacity = 32; public const int MaxCapacity = 32;
@ -28,9 +32,22 @@ namespace SupaLidlGame.Items
set => EquipItem(value, ref _offhandItem); set => EquipItem(value, ref _offhandItem);
} }
public bool IsUsingItem => (SelectedItem?.IsUsing ?? false) ||
(OffhandItem?.IsUsing ?? false);
public Inventory()
{
InventoryMap = new Dictionary<string, int>();
InventoryMap.Add("equip_1", 0);
InventoryMap.Add("equip_2", 1);
InventoryMap.Add("equip_3", 2);
}
private bool EquipItem(Item item, ref Item slot) private bool EquipItem(Item item, ref Item slot)
{ {
if (item is not null && item.IsOneHanded) if (item is not null)
{
if (item.IsOneHanded)
{ {
// we can not equip this if either hand is occupied by // we can not equip this if either hand is occupied by
// two-handed item // two-handed item
@ -51,6 +68,7 @@ namespace SupaLidlGame.Items
GD.PrintErr("Tried to equip an item not in the inventory."); GD.PrintErr("Tried to equip an item not in the inventory.");
return false; return false;
} }
}
if (slot is not null) if (slot is not null)
{ {
@ -67,6 +85,20 @@ namespace SupaLidlGame.Items
return true; return true;
} }
public Item GetItemByMap(string keymap)
{
if (InventoryMap.ContainsKey(keymap))
{
int idx = InventoryMap[keymap];
if (idx < Items.Count)
{
return Items[InventoryMap[keymap]];
}
}
else GD.Print(keymap + " does not exist");
return null;
}
public Item AddItem(Item item) public Item AddItem(Item item)
{ {
if (Items.Count >= MaxCapacity) if (Items.Count >= MaxCapacity)
@ -76,7 +108,10 @@ namespace SupaLidlGame.Items
item.CharacterOwner = Character; item.CharacterOwner = Character;
item.Visible = false; item.Visible = false;
if (!Items.Contains(item))
{
Items.Add(item); Items.Add(item);
}
return item; return item;
} }

View File

@ -20,6 +20,8 @@ namespace SupaLidlGame.Items
public Character CharacterOwner { get; set; } public Character CharacterOwner { get; set; }
public virtual bool IsUsing => false;
/// <summary> /// <summary>
/// Determines if this item can directly stack with other items /// Determines if this item can directly stack with other items
/// </summary> /// </summary>

View File

@ -8,7 +8,7 @@ namespace SupaLidlGame.Items
{ {
public double RemainingUseTime { get; protected set; } = 0; public double RemainingUseTime { get; protected set; } = 0;
public bool IsUsing => RemainingUseTime > 0; public override bool IsUsing => RemainingUseTime > 0;
/// <summary> /// <summary>
/// How much damage in HP that this weapon deals. /// How much damage in HP that this weapon deals.

View File

@ -66,6 +66,21 @@ interact={
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":70,"key_label":0,"unicode":0,"echo":false,"script":null) "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":70,"key_label":0,"unicode":0,"echo":false,"script":null)
] ]
} }
equip_1={
"deadzone": 0.5,
"events": [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":49,"key_label":0,"unicode":49,"echo":false,"script":null)
]
}
equip_2={
"deadzone": 0.5,
"events": [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":50,"key_label":0,"unicode":50,"echo":false,"script":null)
]
}
equip_3={
"deadzone": 0.5,
"events": [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":51,"key_label":0,"unicode":51,"echo":false,"script":null)
]
}
[layer_names] [layer_names]