Ability to switch weapons through the menu

This commit is contained in:
Marco 2025-02-25 18:42:11 +01:00
commit ff6d46ebcd
11 changed files with 61 additions and 4 deletions

View file

@ -15,5 +15,6 @@ InfiniteAmmo = true
BulletsPerShot = 1 BulletsPerShot = 1
SpreadAngle = 0.0 SpreadAngle = 0.0
RandomSpread = 0.0 RandomSpread = 0.0
ItemKey = "CheatGun"
AmmoKey = "" AmmoKey = ""
_rotationOffset = 0.0 _rotationOffset = 0.0

View file

@ -15,5 +15,6 @@ InfiniteAmmo = true
BulletsPerShot = 5 BulletsPerShot = 5
SpreadAngle = 180.0 SpreadAngle = 180.0
RandomSpread = 0.0 RandomSpread = 0.0
ItemKey = "EnemyWeapon"
AmmoKey = "" AmmoKey = ""
_rotationOffset = 0.0 _rotationOffset = 0.0

View file

@ -15,5 +15,6 @@ InfiniteAmmo = true
BulletsPerShot = 1 BulletsPerShot = 1
SpreadAngle = 0.0 SpreadAngle = 0.0
RandomSpread = 0.0 RandomSpread = 0.0
ItemKey = "EnemyWeapon_Simple"
AmmoKey = "" AmmoKey = ""
_rotationOffset = 0.0 _rotationOffset = 0.0

View file

@ -15,5 +15,6 @@ InfiniteAmmo = false
BulletsPerShot = 3 BulletsPerShot = 3
SpreadAngle = 15.0 SpreadAngle = 15.0
RandomSpread = 0.0 RandomSpread = 0.0
ItemKey = "IceShotgun"
AmmoKey = "Ammo1" AmmoKey = "Ammo1"
_rotationOffset = 0.0 _rotationOffset = 0.0

View file

@ -15,5 +15,6 @@ InfiniteAmmo = true
BulletsPerShot = 1 BulletsPerShot = 1
SpreadAngle = 0.0 SpreadAngle = 0.0
RandomSpread = 0.0 RandomSpread = 0.0
ItemKey = "IcicleGun"
AmmoKey = "" AmmoKey = ""
_rotationOffset = 0.0 _rotationOffset = 0.0

View file

@ -15,5 +15,6 @@ InfiniteAmmo = true
BulletsPerShot = 1 BulletsPerShot = 1
SpreadAngle = 0.0 SpreadAngle = 0.0
RandomSpread = 0.0 RandomSpread = 0.0
ItemKey = "IcicleRepeater"
AmmoKey = "" AmmoKey = ""
_rotationOffset = 0.0 _rotationOffset = 0.0

View file

@ -155,6 +155,8 @@ public partial class GameManager : Node2D
SpawnPlayerWeapon(item); SpawnPlayerWeapon(item);
} }
}; };
_inventoryManager.WeaponEquip += _player.EquipWeapon;
} }
SpawnWeapons(); SpawnWeapons();

View file

@ -11,7 +11,7 @@ public partial class InventoryManager : Node2D
private Dictionary<string, ItemContainer> _itemsDict = new Dictionary<string, ItemContainer>(); private Dictionary<string, ItemContainer> _itemsDict = new Dictionary<string, ItemContainer>();
public Dictionary<string, ItemContainer> Items => _itemsDict; public List<ItemContainer> Items => _itemsDict.Where(x => x.Value.Count > 0).Select(x => x.Value).ToList();
[Signal] [Signal]
public delegate void ItemAddedEventHandler(LootItem item, int currentAmount); public delegate void ItemAddedEventHandler(LootItem item, int currentAmount);
@ -19,6 +19,9 @@ public partial class InventoryManager : Node2D
[Signal] [Signal]
public delegate void ItemRemovedEventHandler(string itemKey, int currentAmount); 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. // Called when the node enters the scene tree for the first time.
public override void _Ready() public override void _Ready()
{ {
@ -105,6 +108,37 @@ public partial class InventoryManager : Node2D
{ {
RedKeycard = false; 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 public class ItemContainer

View file

@ -206,6 +206,16 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
EquippedWeapons.Add(weapon); 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) public void EquipWeapon(Weapon weapon)
{ {
EquippedWeapon = weapon; EquippedWeapon = weapon;

View file

@ -34,6 +34,8 @@ public partial class WeaponResource : Resource
[Export] public float SpreadAngle = 0f; [Export] public float SpreadAngle = 0f;
[Export] public float RandomSpread = 0f; [Export] public float RandomSpread = 0f;
[Export] public string ItemKey;
#region Bullet spawn data #region Bullet spawn data
[Export] public string AmmoKey; [Export] public string AmmoKey;

View file

@ -44,6 +44,8 @@ public partial class ItemsMenu : ItemList
var item = _itemsDic[index]; var item = _itemsDic[index];
GD.Print("Item: " + item); GD.Print("Item: " + item);
_inventoryManager.UseItem(item);
HideInventory(); HideInventory();
} }
@ -57,9 +59,10 @@ public partial class ItemsMenu : ItemList
public void ShowInventory() public void ShowInventory()
{ {
this.Show(); 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, var index = this.AddItem($"{item.Item.ItemName} x{item.Count}", item.Item.InventorySprite,
item.Item.Selectable); item.Item.Selectable);