77 lines
2.3 KiB
C#
77 lines
2.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class ItemController : MonoBehaviour
|
|
{
|
|
[SerializeField] public SpriteRenderer spriteRenderer;
|
|
|
|
[SerializeField] public float holdTime = 0.3f;
|
|
[SerializeField] public float fadeTime = 0.2f;
|
|
|
|
[SerializeField] public float upwardForce = 4f;
|
|
|
|
[SerializeField] public float horizontalForce = 1f;
|
|
|
|
[SerializeField] public Rigidbody2D body;
|
|
|
|
private ItemSpriteCollectionController itemSpriteCollectionController;
|
|
|
|
public Item item;
|
|
|
|
public bool fadingOut;
|
|
|
|
private void Awake() {
|
|
body = GetComponent<Rigidbody2D>();
|
|
spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
|
|
itemSpriteCollectionController = GameObject.FindGameObjectWithTag("ItemSpriteCollection").GetComponent<ItemSpriteCollectionController>();
|
|
fadingOut = false;
|
|
}
|
|
|
|
// Start is called before the first frame update
|
|
private void Start() {
|
|
body.AddForce(new Vector2(Random.Range(-0.5f, 0.5f) * horizontalForce,
|
|
upwardForce));
|
|
Invoke("StartFadeOut", holdTime);
|
|
}
|
|
|
|
public void Setup(Item _item) {
|
|
item = _item;
|
|
spriteRenderer.sprite = itemSpriteCollectionController.itemSprites[((int)item.subType) - 1];
|
|
}
|
|
|
|
public bool PickUp() {
|
|
if (fadingOut) return false;
|
|
int remaining = State.state.inventory.GainItem(item);
|
|
GameObject[] panels = GameObject.FindGameObjectsWithTag("InventoryPanel");
|
|
foreach (GameObject panel in panels) {
|
|
if (panel != null && panel.activeSelf) {
|
|
panel.GetComponent<InventoryPanelController>().RefreshAll();
|
|
}
|
|
}
|
|
if (remaining == 0) {
|
|
StartFadeOut();
|
|
return true;
|
|
}
|
|
item.count = remaining;
|
|
return false;
|
|
}
|
|
|
|
public void StartFadeOut() {
|
|
if (!fadingOut) {
|
|
fadingOut = true;
|
|
StartCoroutine("FadeOut");
|
|
}
|
|
}
|
|
|
|
IEnumerator FadeOut() {
|
|
for (float i = fadeTime; i >= 0; i -= Time.deltaTime) {
|
|
// TODO: Implement fade
|
|
// text.alpha = i / fadeTime;
|
|
spriteRenderer.color = new Color(1f, 1f, 1f, i / fadeTime);
|
|
yield return new WaitForEndOfFrame();
|
|
}
|
|
Destroy(gameObject);
|
|
}
|
|
}
|