Scriptum/Assets/breakable.cs
2022-06-12 17:18:43 +02:00

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)
{
gameObject.SetActive(false);
}
}
IEnumerator Timer()
{
if (timeToBreak == true){
PlayerPrefs.SetInt("rock", 0);
gameObject.SetActive(false);
}
yield return new WaitForSeconds(1);
var em = collisionParticleSystem.emission;
em.enabled = false;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "AttackHitbox")
{
var em = collisionParticleSystem.emission;
em.enabled = true;
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());
}
}
}