diff --git a/Characters/Player.cs b/Characters/Player.cs index 8bf64cf..5d38798 100644 --- a/Characters/Player.cs +++ b/Characters/Player.cs @@ -75,7 +75,9 @@ public sealed partial class Player : Character this.GetEventBus().EmitSignal(signal, args); }; - Inventory.AddItemToHotbar(Inventory.Items[0]); + //GD.Print("Inventory: " + Inventory.Items); + //Inventory.AddItemToHotbar(Inventory.Items[0]); + Inventory.SelectedIndex = 0; } public override void _Process(double delta) diff --git a/Characters/Player.tscn b/Characters/Player.tscn index fe094cc..613b210 100644 --- a/Characters/Player.tscn +++ b/Characters/Player.tscn @@ -12,7 +12,6 @@ [ext_resource type="Script" path="res://State/Character/PlayerIdleState.cs" id="6_wkfdm"] [ext_resource type="Script" path="res://State/Character/PlayerMoveState.cs" id="7_dfqd8"] [ext_resource type="Script" path="res://Utils/AnimationManager.cs" id="7_sdgvb"] -[ext_resource type="Script" path="res://Items/Inventory.cs" id="7_xyenu"] [ext_resource type="Script" path="res://State/Character/PlayerRollState.cs" id="8_fy0v5"] [ext_resource type="Script" path="res://State/Character/PlayerEmoteState.cs" id="8_hd2lw"] [ext_resource type="Animation" uid="uid://8e8r3y1imvsx" path="res://Assets/Animations/stun.res" id="8_m08fh"] @@ -34,7 +33,8 @@ [ext_resource type="PackedScene" uid="uid://ce0ph4wk0ylra" path="res://UI/TargetTracer.tscn" id="22_hxi53"] [ext_resource type="Texture2D" uid="uid://bd8l8kafb42dt" path="res://Assets/Sprites/Particles/circle.png" id="22_uefct"] [ext_resource type="Texture2D" uid="uid://bcgm3r168qjn3" path="res://Assets/Sprites/Particles/cast-effect.png" id="24_njn4h"] -[ext_resource type="Resource" uid="uid://cl7jvdu2lnv2d" path="res://Items/Weapons/Sword.tres" id="31_vr68e"] +[ext_resource type="Script" path="res://Items/PlayerInventory.cs" id="30_y2wmw"] +[ext_resource type="PackedScene" uid="uid://dvqap2uhcah63" path="res://Items/Weapons/Sword.tscn" id="31_ql4as"] [sub_resource type="ShaderMaterial" id="ShaderMaterial_h78y7"] shader = ExtResource("2_ngsgt") @@ -817,9 +817,8 @@ horizontal_alignment = 1 [node name="Inventory" type="Node2D" parent="." node_paths=PackedStringArray("Hotbar")] y_sort_enabled = true position = Vector2(0, -2) -script = ExtResource("7_xyenu") -Hotbar = [] -Items = [ExtResource("31_vr68e")] +script = ExtResource("30_y2wmw") +Hotbar = [NodePath("Sword"), null, null] InventoryMap = { "equip_1": 0, "equip_2": 1, @@ -829,6 +828,9 @@ InventoryMap = { [node name="RemoteTransform2D2" type="RemoteTransform2D" parent="Inventory"] position = Vector2(0, 4) +[node name="Sword" parent="Inventory" instance=ExtResource("31_ql4as")] +visible = false + [node name="Hurtbox" parent="." node_paths=PackedStringArray("InvincibilityTimer") instance=ExtResource("9_avyu4")] visible = false InvincibilityTimer = NodePath("Timer") diff --git a/Items/Inventory.cs b/Items/Inventory.cs index 305afbd..772d6e4 100644 --- a/Items/Inventory.cs +++ b/Items/Inventory.cs @@ -27,7 +27,15 @@ public partial class Inventory : Node2D public Item SelectedItem { get => _selectedItem; - set => EquipItem(value, ref _selectedItem); + set => EquipItem(value); + } + + private int _selectedIndex; + + public int SelectedIndex + { + get => _selectedIndex; + set => EquipIndex(value); } private int _quickSwitchIndex = -1; @@ -46,14 +54,6 @@ public partial class Inventory : Node2D public bool IsUsingItem => SelectedItem?.IsUsing ?? false; - public Inventory() - { - //InventoryMap = new Dictionary(); - //InventoryMap.Add("equip_1", 0); - //InventoryMap.Add("equip_2", 1); - //InventoryMap.Add("equip_3", 2); - } - public override void _Ready() { if (Hotbar is null) @@ -77,50 +77,50 @@ public partial class Inventory : Node2D } } - Events.EventBus.Instance.EmitSignal( - Events.EventBus.SignalName.PlayerInventoryUpdate, this); base._Ready(); } - public bool EquipIndex(int index) + public virtual bool EquipIndex(int index) { - if (index < Hotbar.Count) + if (index >= Hotbar.Count) { - return EquipItem(Hotbar[index], ref _selectedItem); + return false; } - return EquipItem(null, ref _selectedItem); - } + _selectedItem?.Unequip(Character); + _selectedIndex = index; - private bool EquipItem(Item item, ref Item slot) - { - if (item is not null) + if (index >= 0) { - if (!Hotbar.Contains(item)) - { - GD.PrintErr("Tried to equip an item not in the inventory."); - return false; - } + _selectedItem = Hotbar[index]; + _selectedItem?.Equip(Character); + } + else + { + _selectedItem = null; } - if (slot is not null) - { - slot.Unequip(Character); - } - - slot = item; - - if (item is not null) - { - item.Equip(Character); - } - - Events.EventBus.Instance.EmitSignal( - Events.EventBus.SignalName.PlayerInventoryUpdate, this); + GD.Print($"Inventory: {index} is new selected index."); return true; } + protected virtual bool EquipItem(Item item) + { + if (item is null) + { + EquipIndex(-1); + } + + int index = Hotbar.IndexOf(item); + if (index < 0) + { + GD.PushWarning("Trying to equip item not in the hot inventory."); + } + return EquipIndex(index); + } + + [System.Obsolete] public Item GetItemByMap(string keymap) { if (InventoryMap.ContainsKey(keymap)) diff --git a/Items/PlayerInventory.cs b/Items/PlayerInventory.cs new file mode 100644 index 0000000..a512c80 --- /dev/null +++ b/Items/PlayerInventory.cs @@ -0,0 +1,17 @@ +namespace SupaLidlGame.Items; + +public partial class PlayerInventory : Inventory +{ + public override bool EquipIndex(int index) + { + bool result = base.EquipIndex(index); + + if (result) + { + Events.EventBus.Instance.EmitSignal( + Events.EventBus.SignalName.PlayerInventoryUpdate, this); + } + + return result; + } +} diff --git a/State/Character/PlayerState.cs b/State/Character/PlayerState.cs index db3f57f..515e344 100644 --- a/State/Character/PlayerState.cs +++ b/State/Character/PlayerState.cs @@ -23,15 +23,18 @@ public abstract partial class PlayerState : CharacterState { if (@event.IsActionPressed("equip_1")) { - inventory.SelectedItem = inventory.GetItemByMap("equip_1"); + //inventory.SelectedItem = inventory.GetItemByMap("equip_1"); + inventory.SelectedIndex = 0; } else if (@event.IsActionPressed("equip_2")) { - inventory.SelectedItem = inventory.GetItemByMap("equip_2"); + //inventory.SelectedItem = inventory.GetItemByMap("equip_2"); + inventory.SelectedIndex = 1; } else if (@event.IsActionPressed("equip_3")) { - inventory.SelectedItem = inventory.GetItemByMap("equip_3"); + //inventory.SelectedItem = inventory.GetItemByMap("equip_3"); + inventory.SelectedIndex = 2; } else if (@event.IsActionPressed("next_item")) { diff --git a/UI/Hotbar.cs b/UI/Hotbar.cs index b29711f..5b311da 100644 --- a/UI/Hotbar.cs +++ b/UI/Hotbar.cs @@ -15,12 +15,12 @@ public partial class Hotbar : GridContainer public void OnInventoryUpdate(Inventory inventory) { + GD.Print($"UPDATE: {inventory.SelectedIndex} is selected index."); for (int i = 0; i < 3; i++) { var slot = _slots[i]; - slot.Item = inventory.Hotbar[i].Metadata; - slot.IsSelected = inventory.SelectedItem == inventory.Hotbar[i]; - GD.Print(inventory.Hotbar[i].Metadata.Name); + slot.Item = inventory.Hotbar[i]?.Metadata; + slot.IsSelected = inventory.SelectedIndex == i; } } } diff --git a/UI/InventorySlot.cs b/UI/InventorySlot.cs index 6ca977c..2952419 100644 --- a/UI/InventorySlot.cs +++ b/UI/InventorySlot.cs @@ -29,7 +29,8 @@ public partial class InventorySlot : ColorRect if (_item is null) { - _textureRect.Texture = _placeholderTexture; + //_textureRect.Texture = _placeholderTexture; + _textureRect.Texture = null; } else {