-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enemy.cs
57 lines (55 loc) · 1.66 KB
/
Enemy.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
public class Enemy : MonoBehaviour
{
public int health;
public bool isDead = false;
public GameObject enemyProjectile;
public AudioClip projectileSFX;
protected float fireRate = 1;
protected float startDelay = 3;
protected int score;
protected int weaponDamage;
protected GameObject player;
protected PlayerController playerStats;
protected AudioSource enemyAudio;
protected Animator animatorController;
public virtual void Start()
{
player = GameObject.FindWithTag("Player");
playerStats = player.GetComponent<PlayerController>();
enemyAudio = GetComponent<AudioSource>();
enemyAudio.volume = GameManager.volume;
animatorController = GetComponent<Animator>();
InvokeRepeating("FireProjectile", startDelay, fireRate);
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Player Projectile"))
{
TakeDamage(playerStats.weaponDamage);
Destroy(collision.gameObject);
}
}
public void TakeDamage(int damage)
{
health -= damage;
if(health <= 0)
{
isDead = true;
GameManager.score += score;
Destroy(gameObject);
}
}
protected virtual void FireProjectile()
{
Instantiate(enemyProjectile, transform.position, enemyProjectile.transform.rotation);
enemyAudio.PlayOneShot(projectileSFX, GameManager.volume);
}
private void OnDestroy()
{
StopAllCoroutines();
}
}