72 lines
2.2 KiB
C#
72 lines
2.2 KiB
C#
|
|
using System.Collections;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using UnityEngine;
|
|||
|
|
|
|||
|
|
public class MoneyController : MonoBehaviour {
|
|||
|
|
[SerializeField] public SpriteRenderer spriteRenderer;
|
|||
|
|
|
|||
|
|
[SerializeField] public float holdTime = 30f;
|
|||
|
|
[SerializeField] public float fadeTime = 0.2f;
|
|||
|
|
|
|||
|
|
[SerializeField] public float upwardForce = 4f;
|
|||
|
|
|
|||
|
|
[SerializeField] public float horizontalForce = 1f;
|
|||
|
|
|
|||
|
|
[SerializeField] public Rigidbody2D body;
|
|||
|
|
|
|||
|
|
[SerializeField] public int amount = 0;
|
|||
|
|
|
|||
|
|
[SerializeField] public AudioClip moneyClip;
|
|||
|
|
|
|||
|
|
[SerializeField] public AudioSource audioSource;
|
|||
|
|
|
|||
|
|
private ItemSpriteCollectionController itemSpriteCollectionController;
|
|||
|
|
|
|||
|
|
private 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 moneyItem) {
|
|||
|
|
item = moneyItem;
|
|||
|
|
spriteRenderer.sprite = itemSpriteCollectionController.itemSprites[((int)item.subType) - 1];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void PickUpMoney() {
|
|||
|
|
if (fadingOut) return;
|
|||
|
|
Debug.Log("You just made money: " + item.count.ToString());
|
|||
|
|
State.state.inventory.money += item.count;
|
|||
|
|
audioSource.PlayOneShot(moneyClip);
|
|||
|
|
StartFadeOut();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
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);
|
|||
|
|
}
|
|||
|
|
}
|