-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDestructibleTile.cs
More file actions
34 lines (29 loc) · 955 Bytes
/
DestructibleTile.cs
File metadata and controls
34 lines (29 loc) · 955 Bytes
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestructibleTile : MonoBehaviour
{
[SerializeField] private Sprite[] sprites = new Sprite[2];
[SerializeField] private AudioSource brokenSound;
private SpriteRenderer spriteRenderer;
[SerializeField] private int strength = 100;
private int currStrength;
private void Start() {
currStrength = strength;
spriteRenderer = GetComponent<SpriteRenderer>();
brokenSound = GetComponent<AudioSource>();
}
private void Update() {
if (.5 * strength < currStrength) {
spriteRenderer.sprite = sprites[0];
} else if (0 < currStrength) {
spriteRenderer.sprite = sprites[1];
} else {
Destroy(gameObject);
}
}
public void TakeDamage(int damage) {
if (brokenSound != null) brokenSound.Play();
currStrength -= damage;
}
}