using UnityEngine;

/// <summary>
/// Точка спавна предмета в мире. Создаёт WorldItem при старте или по вызову Spawn().
/// </summary>
public class ItemSpawner : MonoBehaviour
{
    public ItemData item;
    public int amount = 1;
    public float despawnTime = 300f;
    public bool spawnOnStart = false;

    public void Setup(ItemData itemData, int itemAmount, float despawn)
    {
        item        = itemData;
        amount      = itemAmount;
        despawnTime = despawn;
    }

    void Start()
    {
        if (spawnOnStart) Spawn();
    }

    public void Spawn()
    {
        if (InventoryItemFactory.Instance != null)
        {
            var worldItem = InventoryItemFactory.Instance.CreateWorldItem(item, amount, transform.position);
            if (worldItem != null && despawnTime > 0)
                Destroy(worldItem.gameObject, despawnTime);
        }
    }

    public void Despawn()
    {
        Destroy(gameObject);
    }
}
