forked from PhillipNguyen1/Space_Lux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2
52 lines (44 loc) · 1.5 KB
/
2
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
using System.Linq;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent (typeof(Collider2D))]
public class TurretAI : MonoBehaviour {
public Gun TurretGun;
private List<GameObject> enemies;
private Vector2 targetVec;
private float targetAngle;
private bool isSearchingAndDestroying;
public TurretAI() {
enemies = new List<GameObject>();
isSearchingAndDestroying = false;
}
private void OnTriggerEnter2D(Collider2D collision) {
if (collision.gameObject.tag == "Enemy") {
enemies.Add(collision.gameObject);
}
}
private void OnTriggerExit2D(Collider2D collision) {
if (collision.gameObject.tag == "Enemy") {
enemies.RemoveAll(x => x == collision.gameObject);
}
}
private void trackEnemy(GameObject enemy) {
}
private void untrackEnemy(GameObject enemy) {
}
private IEnumerator searchAndDestroy(GameObject enemy) {
if (enemy != null) { // Aim, shoot, wait for gun cooldown, and try again
targetVec = target.transform.position - TurretGun.transform.position;
targetAngle = Mathf.Atan2(targetVec.y, targetVec.x) * Mathf.Rad2Deg;
TurretGun.transform.rotation = Quaternion.Euler(0.0f, 0.0f, targetAngle);
TurretGun.Use();
yield new return WaitForSeconds(TurretGun.FireRate);
StartCoroutine(attackUntilNoEnemies(target));
} else { // Search for another in our vicinity
target = enemies.Take(1);
if (target != null) {
StartExe
}
}
}
}