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

109 lines
2.7 KiB
C#

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