From ff6d46ebcdbcc59e9d8edf23945d1d582f4a3d60 Mon Sep 17 00:00:00 2001 From: Marco Date: Tue, 25 Feb 2025 18:42:11 +0100 Subject: [PATCH] Ability to switch weapons through the menu --- Resources/Weapons/Cheat_Gun.tres | 1 + Resources/Weapons/EnemyWeapon.tres | 1 + Resources/Weapons/EnemyWeapon_simple.tres | 1 + Resources/Weapons/Ice_Shotgun_Weapon.tres | 1 + Resources/Weapons/IcicleGun.tres | 1 + Resources/Weapons/IcicleRepeater.tres | 1 + Scripts/GameManager.cs | 4 ++- Scripts/InventoryManager.cs | 36 ++++++++++++++++++++++- Scripts/PlayerMovement.cs | 10 +++++++ Scripts/Resources/WeaponResource.cs | 2 ++ Scripts/UI/ItemsMenu.cs | 7 +++-- 11 files changed, 61 insertions(+), 4 deletions(-) diff --git a/Resources/Weapons/Cheat_Gun.tres b/Resources/Weapons/Cheat_Gun.tres index 5777bcc7..bd01c898 100644 --- a/Resources/Weapons/Cheat_Gun.tres +++ b/Resources/Weapons/Cheat_Gun.tres @@ -15,5 +15,6 @@ InfiniteAmmo = true BulletsPerShot = 1 SpreadAngle = 0.0 RandomSpread = 0.0 +ItemKey = "CheatGun" AmmoKey = "" _rotationOffset = 0.0 diff --git a/Resources/Weapons/EnemyWeapon.tres b/Resources/Weapons/EnemyWeapon.tres index cd800675..292b1ee2 100644 --- a/Resources/Weapons/EnemyWeapon.tres +++ b/Resources/Weapons/EnemyWeapon.tres @@ -15,5 +15,6 @@ InfiniteAmmo = true BulletsPerShot = 5 SpreadAngle = 180.0 RandomSpread = 0.0 +ItemKey = "EnemyWeapon" AmmoKey = "" _rotationOffset = 0.0 diff --git a/Resources/Weapons/EnemyWeapon_simple.tres b/Resources/Weapons/EnemyWeapon_simple.tres index 7c67ce05..fe2014d1 100644 --- a/Resources/Weapons/EnemyWeapon_simple.tres +++ b/Resources/Weapons/EnemyWeapon_simple.tres @@ -15,5 +15,6 @@ InfiniteAmmo = true BulletsPerShot = 1 SpreadAngle = 0.0 RandomSpread = 0.0 +ItemKey = "EnemyWeapon_Simple" AmmoKey = "" _rotationOffset = 0.0 diff --git a/Resources/Weapons/Ice_Shotgun_Weapon.tres b/Resources/Weapons/Ice_Shotgun_Weapon.tres index 33f999b6..fd98d3d9 100644 --- a/Resources/Weapons/Ice_Shotgun_Weapon.tres +++ b/Resources/Weapons/Ice_Shotgun_Weapon.tres @@ -15,5 +15,6 @@ InfiniteAmmo = false BulletsPerShot = 3 SpreadAngle = 15.0 RandomSpread = 0.0 +ItemKey = "IceShotgun" AmmoKey = "Ammo1" _rotationOffset = 0.0 diff --git a/Resources/Weapons/IcicleGun.tres b/Resources/Weapons/IcicleGun.tres index 6b7d15e0..6a259b59 100644 --- a/Resources/Weapons/IcicleGun.tres +++ b/Resources/Weapons/IcicleGun.tres @@ -15,5 +15,6 @@ InfiniteAmmo = true BulletsPerShot = 1 SpreadAngle = 0.0 RandomSpread = 0.0 +ItemKey = "IcicleGun" AmmoKey = "" _rotationOffset = 0.0 diff --git a/Resources/Weapons/IcicleRepeater.tres b/Resources/Weapons/IcicleRepeater.tres index bf61f4c3..db9c1c00 100644 --- a/Resources/Weapons/IcicleRepeater.tres +++ b/Resources/Weapons/IcicleRepeater.tres @@ -15,5 +15,6 @@ InfiniteAmmo = true BulletsPerShot = 1 SpreadAngle = 0.0 RandomSpread = 0.0 +ItemKey = "IcicleRepeater" AmmoKey = "" _rotationOffset = 0.0 diff --git a/Scripts/GameManager.cs b/Scripts/GameManager.cs index 56771feb..34b7aad1 100644 --- a/Scripts/GameManager.cs +++ b/Scripts/GameManager.cs @@ -133,7 +133,7 @@ public partial class GameManager : Node2D _player.ShieldChanged += (newShield, maxShield) => _hud.UpdateShield(newShield, maxShield); _player.InteractableAreaEntered += (interactable) => _hud.UpdateInteractable(interactable); - + _player.Death += () => { // Show Game Over @@ -155,6 +155,8 @@ public partial class GameManager : Node2D SpawnPlayerWeapon(item); } }; + + _inventoryManager.WeaponEquip += _player.EquipWeapon; } SpawnWeapons(); diff --git a/Scripts/InventoryManager.cs b/Scripts/InventoryManager.cs index f9f988f8..e4797834 100644 --- a/Scripts/InventoryManager.cs +++ b/Scripts/InventoryManager.cs @@ -11,13 +11,16 @@ public partial class InventoryManager : Node2D private Dictionary _itemsDict = new Dictionary(); - public Dictionary Items => _itemsDict; + public List Items => _itemsDict.Where(x => x.Value.Count > 0).Select(x => x.Value).ToList(); [Signal] public delegate void ItemAddedEventHandler(LootItem item, int currentAmount); [Signal] public delegate void ItemRemovedEventHandler(string itemKey, int currentAmount); + + [Signal] + public delegate void WeaponEquipEventHandler(string itemKey); // Called when the node enters the scene tree for the first time. public override void _Ready() @@ -105,6 +108,37 @@ public partial class InventoryManager : Node2D { RedKeycard = false; } + + public bool UseItem(string itemKey) + { + if (!_itemsDict.TryGetValue(itemKey, out var itm)) return false; + + if (itm.Count == 0) return false; + + switch (itm.Item.Item) + { + case ItemTypes.Medkit: + // Heal + break; + case ItemTypes.FrogBomb: + // Use Bomb + break; + case ItemTypes.Bomb: + // Bomb + break; + case ItemTypes.Mine: + break; + case ItemTypes.Battery: + break; + case ItemTypes.Weapon: + // Equip weapon + EmitSignal(SignalName.WeaponEquip, itm.Item.ItemKey); + break; + + } + + return true; + } } public class ItemContainer diff --git a/Scripts/PlayerMovement.cs b/Scripts/PlayerMovement.cs index f7f55fba..419c32a7 100644 --- a/Scripts/PlayerMovement.cs +++ b/Scripts/PlayerMovement.cs @@ -206,6 +206,16 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible EquippedWeapons.Add(weapon); } + public void EquipWeapon(string itemKey) + { + if (string.IsNullOrWhiteSpace(itemKey)) return; + var weapon = EquippedWeapons.FirstOrDefault(x => x.WeaponData.ItemKey == itemKey); + + if (weapon is null) return; + + EquipWeapon(weapon); + } + public void EquipWeapon(Weapon weapon) { EquippedWeapon = weapon; diff --git a/Scripts/Resources/WeaponResource.cs b/Scripts/Resources/WeaponResource.cs index 1da96a8a..a696350d 100644 --- a/Scripts/Resources/WeaponResource.cs +++ b/Scripts/Resources/WeaponResource.cs @@ -34,6 +34,8 @@ public partial class WeaponResource : Resource [Export] public float SpreadAngle = 0f; [Export] public float RandomSpread = 0f; + [Export] public string ItemKey; + #region Bullet spawn data [Export] public string AmmoKey; diff --git a/Scripts/UI/ItemsMenu.cs b/Scripts/UI/ItemsMenu.cs index d7fa6dd2..2c10ff30 100644 --- a/Scripts/UI/ItemsMenu.cs +++ b/Scripts/UI/ItemsMenu.cs @@ -43,6 +43,8 @@ public partial class ItemsMenu : ItemList { var item = _itemsDic[index]; GD.Print("Item: " + item); + + _inventoryManager.UseItem(item); HideInventory(); } @@ -57,9 +59,10 @@ public partial class ItemsMenu : ItemList public void ShowInventory() { this.Show(); - foreach (var itm in _inventoryManager.Items) + foreach (var item in _inventoryManager.Items) { - var item = itm.Value; + if (item.Count <= 0) continue; + var index = this.AddItem($"{item.Item.ItemName} x{item.Count}", item.Item.InventorySprite, item.Item.Selectable);