Files
ClickRPG/Assets/Scripts/LootDrop.cs

45 lines
1.4 KiB
C#

using UnityEngine;
using System.Collections.Generic;
public class LootDrop : MonoBehaviour, IInteractable
{
// This list will hold the loot data after being mapped from the dictionary.
public List<InventoryEntry> itemsToDrop = new();
// This method receives the enemy's inventory and maps it to our list.
public void Setup(IReadOnlyDictionary<string, int> items, int gold)
{
// Add gold to the list for looting
if (gold > 0)
{
itemsToDrop.Add(new InventoryEntry { itemName = "Gold", quantity = gold });
}
// Add other items to the list
foreach (KeyValuePair<string, int> item in items)
{
itemsToDrop.Add(new InventoryEntry { itemName = item.Key, quantity = item.Value });
}
}
// This is from the IInteractable interface.
public string GetInteractPrompt()
{
return "Loot";
}
// This is called when the player clicks on the loot bag.
public void OnInteract()
{
Debug.Log("Looting items...");
foreach (var entry in itemsToDrop)
{
// Add each item to the player's inventory.
PlayerInventory.instance.AddItem(entry.itemName, entry.quantity);
Debug.Log($"Added {entry.quantity} x {entry.itemName} to player inventory.");
}
// Destroy the loot bag after it has been looted.
Destroy(gameObject);
}
}