aboutsummaryrefslogtreecommitdiff
path: root/Assets/Scripts/ICanDie.cs
blob: d6ff6d1f3784706c1845f153f6d7c9e5ecf1c508 (plain) (blame)
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ICanDie : MonoBehaviour {
  public float health = 3;

  public AudioClip deathNoise;
  public float deathNoiseVolume = 1f;

  private AudioSource audio;

  void Start() {
    audio = GetComponent<AudioSource>();
  }

  public void Hit(float damage) {
    health -= damage;

    if (health <= 0) {
      Die();
    }
  }

  public void Die() {
    if (deathNoise) audio.PlayOneShot(deathNoise, deathNoiseVolume);

    GetComponent<SpriteRenderer>().enabled = false;
    GetComponent<BoxCollider2D>().enabled = false;
  }
}