-
Notifications
You must be signed in to change notification settings - Fork 0
/
Block.cs
69 lines (59 loc) · 1.48 KB
/
Block.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Block : MonoBehaviour
{
[SerializeField] AudioClip destroySound;
[SerializeField] Sprite[] hitSprites;
Level level;
GameStatus score;
[SerializeField] int timesHit; // serialized for debug purposes only
public void Start()
{
CountBreakableBlocks();
}
private void CountBreakableBlocks()
{
level = FindObjectOfType<Level>();
if (tag == "Breakable")
{
level.CountBreakableBlocks();
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (tag == "Breakable")
{
OneHit();
}
}
private void OneHit()
{
FindObjectOfType<GameStatus>().AddToScore();
AudioSource.PlayClipAtPoint(destroySound, Camera.main.transform.position);
timesHit++;
int maxHits = hitSprites.Length + 1;
if (timesHit >= maxHits)
{
Destroy(gameObject);
level.DecountBlocks();
}
else
{
ShowNextHitSprite();
}
}
private void ShowNextHitSprite()
{
int spriteIndex = timesHit - 1;
if (hitSprites[spriteIndex] != null)
{
GetComponent<SpriteRenderer>().sprite = hitSprites[spriteIndex];
}
else
{
Debug.Log("Error: Block Sprite Missing "+ gameObject.name);
}
}
}