Scriptum/Assets/BreakOre.cs
2022-12-06 01:27:02 +01:00

88 lines
2.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BreakOre : MonoBehaviour
{
public string name;
public Sprite[] sprites;
private int oldSprite;
private int newSprite;
private List<int> availableSprites = new List<int>();
public ParticleSystem breakParticleSystem;
public bool timeToBreak = false;
public int isBroken;
void Start()
{
oldSprite = 0;
for (int i = 0; i < 2; i++)
{
availableSprites.Add(i);
}
if (OnMapAppearanceMethod.IsNewGame())
{
isBroken = 0; // TODO Like in NPC Type script
}
else
{
isBroken = PlayerPrefs.GetInt(name + "-S");
}
if (isBroken == 1)
{
gameObject.SetActive(false);
}
}
IEnumerator Timer()
{
if (timeToBreak == true)
{
isBroken = 1;
gameObject.SetActive(false);
PlayerPrefs.SetInt(name, isBroken);
Debug.Log("drop item");
// drop item
gameObject.GetComponent<GoldOre>().DropItem();
}
yield return new WaitForSeconds(1);
var em = breakParticleSystem.emission;
em.enabled = false;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "PickaxeHitbox")
{
var em = breakParticleSystem.emission;
em.enabled = true;
breakParticleSystem.Play();
if (oldSprite == 0)
{
newSprite = availableSprites[1];
oldSprite = 1;
GetComponent<SpriteRenderer>().sprite = sprites[newSprite];
}
else if (oldSprite == 1)
{
timeToBreak = true;
StartCoroutine(Timer());
}
GetComponent<SpriteRenderer>().sprite = sprites[newSprite];
StartCoroutine(Timer());
}
}
public void SaveCheckpoint()
{
PlayerPrefs.SetInt(name + "-S", isBroken);
}
}