Scriptum/Assets/breakable.cs

73 lines
1.8 KiB
C#

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