Scriptum/Assets/breakable.cs

108 lines
2.7 KiB
C#
Raw Normal View History

2022-04-25 21:41:47 +02:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class breakable : MonoBehaviour
{
public string name;
public string name2;
2022-04-25 21:41:47 +02:00
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;
2022-04-25 21:41:47 +02:00
void Start()
{
2022-04-25 21:41:47 +02:00
for(int i=0; i<3; i++)
{
availableSprites.Add(i);
}
int continued = PlayerPrefs.GetInt("continued");
if (continued == 1)
{
isBroken = PlayerPrefs.GetInt(name2);
}
else
{
isBroken = PlayerPrefs.GetInt(name);
}
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)
{
2022-06-12 17:18:43 +02:00
gameObject.SetActive(false);
}
}
IEnumerator Timer()
{
if (timeToBreak == true){
isBroken = 3;
PlayerPrefs.SetInt(name, isBroken);
2022-06-12 17:18:43 +02:00
gameObject.SetActive(false);
}
yield return new WaitForSeconds(1);
var em = collisionParticleSystem.emission;
em.enabled = false;
2022-04-25 21:41:47 +02:00
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "PickaxeHitbox")
2022-04-25 21:41:47 +02:00
{
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;
2022-04-25 21:41:47 +02:00
GetComponent<SpriteRenderer>().sprite = sprites[newSprite];
isBroken = 2;
}
else if (oldSprite == 2)
{
timeToBreak = true;
StartCoroutine(Timer());
}
GetComponent<SpriteRenderer>().sprite = sprites[newSprite];
StartCoroutine(Timer());
2022-04-25 21:41:47 +02:00
}
}
public void SaveCheckpoint()
{
PlayerPrefs.SetInt(name2, isBroken);
}
2022-04-25 21:41:47 +02:00
}