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
SpreadAngle = 0.0
RandomSpread = 0.0
ItemKey = "CheatGun"
AmmoKey = ""
_rotationOffset = 0.0

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -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();

View file

@ -11,13 +11,16 @@ public partial class InventoryManager : Node2D
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]
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

View file

@ -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;

View file

@ -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;

View file

@ -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);