2022-04-25 21:41:47 +02:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
|
|
public class breakable : MonoBehaviour
|
|
|
|
{
|
2022-06-15 19:54:44 +02:00
|
|
|
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>();
|
2022-06-02 09:49:51 +02:00
|
|
|
public ParticleSystem collisionParticleSystem;
|
|
|
|
public bool timeToBreak = false;
|
2022-06-15 19:54:44 +02:00
|
|
|
private int isBroken = 0;
|
2022-06-02 09:49:51 +02:00
|
|
|
|
2022-04-25 21:41:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
void Start()
|
|
|
|
{
|
2022-06-15 19:54:44 +02:00
|
|
|
|
2022-04-25 21:41:47 +02:00
|
|
|
for(int i=0; i<3; i++)
|
|
|
|
{
|
|
|
|
availableSprites.Add(i);
|
|
|
|
}
|
2022-06-15 19:54:44 +02:00
|
|
|
|
|
|
|
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-02 18:32:18 +02:00
|
|
|
{
|
2022-06-12 17:18:43 +02:00
|
|
|
gameObject.SetActive(false);
|
2022-06-02 18:32:18 +02:00
|
|
|
}
|
2022-06-02 09:49:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
IEnumerator Timer()
|
|
|
|
{
|
|
|
|
if (timeToBreak == true){
|
2022-06-15 19:54:44 +02:00
|
|
|
isBroken = 3;
|
|
|
|
PlayerPrefs.SetInt(name, isBroken);
|
2022-06-12 17:18:43 +02:00
|
|
|
gameObject.SetActive(false);
|
2022-06-02 09:49:51 +02:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
2022-06-12 11:28:14 +02:00
|
|
|
if (collision.tag == "AttackHitbox")
|
2022-04-25 21:41:47 +02:00
|
|
|
{
|
2022-06-12 11:28:14 +02:00
|
|
|
var em = collisionParticleSystem.emission;
|
|
|
|
em.enabled = true;
|
2022-06-02 09:49:51 +02:00
|
|
|
collisionParticleSystem.Play();
|
|
|
|
if (oldSprite == 0)
|
|
|
|
{
|
|
|
|
newSprite = availableSprites[1];
|
|
|
|
oldSprite = 1;
|
|
|
|
GetComponent<SpriteRenderer>().sprite = sprites[newSprite];
|
2022-06-15 19:54:44 +02:00
|
|
|
isBroken = 1;
|
2022-06-02 09:49:51 +02:00
|
|
|
}
|
|
|
|
else if (oldSprite == 1)
|
|
|
|
{
|
|
|
|
newSprite = availableSprites[2];
|
|
|
|
oldSprite = 2;
|
2022-04-25 21:41:47 +02:00
|
|
|
GetComponent<SpriteRenderer>().sprite = sprites[newSprite];
|
2022-06-15 19:54:44 +02:00
|
|
|
isBroken = 2;
|
2022-06-02 09:49:51 +02:00
|
|
|
}
|
|
|
|
else if (oldSprite == 2)
|
|
|
|
{
|
|
|
|
timeToBreak = true;
|
|
|
|
StartCoroutine(Timer());
|
|
|
|
}
|
|
|
|
GetComponent<SpriteRenderer>().sprite = sprites[newSprite];
|
|
|
|
StartCoroutine(Timer());
|
2022-04-25 21:41:47 +02:00
|
|
|
}
|
|
|
|
|
2022-06-02 09:49:51 +02:00
|
|
|
}
|
2022-06-15 19:54:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
public void SaveCheckpoint()
|
|
|
|
{
|
|
|
|
PlayerPrefs.SetInt(name2, isBroken);
|
|
|
|
}
|
2022-04-25 21:41:47 +02:00
|
|
|
}
|