- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
- public class ItemStack : MonoBehaviour
- {
- [SerializeField] private int itemAmount;
- [SerializeField] private Item item;
- [SerializeField] private float arcHeight = 1.5f;
- [SerializeField] private float duration = 1f;
- private Vector3 startPosition;
- private Vector3 targetPosition;
- private GameManager gameManager;
- private SpriteRenderer spriteRenderer;
- private void Awake()
- {
- spriteRenderer = GetComponent<SpriteRenderer>();
- }
- private void Start()
- {
- gameManager = GameManager.Instance;
-
- startPosition = transform.position;
-
- targetPosition = startPosition + new Vector3(1.5f, 0f, 0f);
-
- StartCoroutine(ArcMovement());
- }
- private void OnTriggerEnter2D(Collider2D collision)
- {
- if (transform.position == targetPosition)
- {
- if (collision.tag == "Player")
- {
- if (collision.GetComponentInChildren<PlayerController>().InventoryManager.AddItem(item, itemAmount))
- {
- Destroy(gameObject);
- }
- }
- }
- }
- public void ItemStackInit(Item item, int itemAmount)
- {
- this.item = item;
- this.itemAmount = itemAmount;
- spriteRenderer.sprite = item.ItemImage;
- }
- private IEnumerator ArcMovement()
- {
- float elapsedTime = 0f;
- while (elapsedTime < duration)
- {
-
- float t = elapsedTime / duration;
-
- Vector3 currentPosition = Vector3.Lerp(startPosition, targetPosition, t);
-
- float height = Mathf.Sin(Mathf.PI * t) * arcHeight;
-
- currentPosition.y += height;
-
- transform.position = currentPosition;
-
- elapsedTime += Time.deltaTime;
-
- yield return null;
- }
-
- transform.position = targetPosition;
- }
- }