Scriptum/Assets/breakable.cs

73 lines
1.8 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 Sprite[] sprites;
private int oldSprite;
private int newSprite;
private List<int> availableSprites = new List<int>();
public ParticleSystem collisionParticleSystem;
public bool timeToBreak = false;
2022-04-25 21:41:47 +02:00
void Start()
{
oldSprite = 0;
for(int i=0; i<3; i++)
{
availableSprites.Add(i);
}
int rock = PlayerPrefs.GetInt("rock");
if(rock == 0)
{
2022-06-12 17:18:43 +02:00
gameObject.SetActive(false);
}
}
IEnumerator Timer()
{
if (timeToBreak == true){
PlayerPrefs.SetInt("rock", 0);
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 == "AttackHitbox")
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];
}
else if (oldSprite == 1)
{
newSprite = availableSprites[2];
oldSprite = 2;
2022-04-25 21:41:47 +02:00
GetComponent<SpriteRenderer>().sprite = sprites[newSprite];
}
else if (oldSprite == 2)
{
timeToBreak = true;
StartCoroutine(Timer());
}
GetComponent<SpriteRenderer>().sprite = sprites[newSprite];
StartCoroutine(Timer());
2022-04-25 21:41:47 +02:00
}
}
2022-04-25 21:41:47 +02:00
}