Scriptum/Assets/BreakOre.cs
kabix09 602c94726a Add scene manager for items
Start rebuild Save & Load game system
2022-06-19 01:25:49 +02:00

89 lines
2.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BreakOre : MonoBehaviour
{
public string name;
public string name2;
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);
}
int continued = PlayerPrefs.GetInt("continued");
if (continued == 1)
{
isBroken = PlayerPrefs.GetInt(name2);
}
else
{
isBroken = PlayerPrefs.GetInt(name);
}
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 == "AttackHitbox")
{
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(name2, isBroken);
}
}