-
Notifications
You must be signed in to change notification settings - Fork 1
/
enemy.cs
131 lines (93 loc) · 2.79 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using UnityEngine;
using System.Collections;
public class enemy : MonoBehaviour
{
private int hitpoints = 5;
private int damage = 5;
public GameObject fireball_prefab;
private GameObject _player;
private GameObject _attack_spawnpoint;
private Vector3 _temp_rot;
private bool _killed = false;
public GameObject[] loot_prefabs;
void Start()
{
_attack_spawnpoint = transform.Find("attack_spawnpoint").gameObject;
_player = GameObject.Find("/player");
if (_player != null)
{
int difficulty = _player.GetComponent<player>().dungeon_level;
hitpoints *= difficulty;
damage *= difficulty;
StartCoroutine(attack());
}
animation.Play("Idle");
}
void Update ()
{
if (_player == null)
return;
if (_killed)
return;
if (Vector3.Distance(transform.position, _player.transform.position) <= 20)
{
transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(_player.transform.position - transform.position), 200 * Time.deltaTime);
_temp_rot = transform.rotation.eulerAngles;
_temp_rot.x = 0;
transform.rotation = Quaternion.Euler(_temp_rot);
}
}
void FixedUpdate()
{
if (_player == null)
return;
if (_killed)
return;
rigidbody.MovePosition(Vector3.MoveTowards(transform.position, _player.transform.position, 0.7f * Time.deltaTime));
}
void receive_damage(int amount)
{
if (_killed)
return;
hitpoints -= amount;
if (hitpoints <= 0)
{
_player.BroadcastMessage("killed_enemy", 10);
_killed = true;
animation.CrossFade("Death", 0.1f);
audio.Play();
StartCoroutine(death());
}
}
IEnumerator death()
{
yield return new WaitForSeconds(0.5f);
if (Random.Range(0f, 1f) > 0.4f)
Instantiate(loot_prefabs[Random.Range(0, loot_prefabs.Length)], transform.position, Quaternion.identity);
yield return new WaitForSeconds(5f);
Destroy(gameObject);
}
IEnumerator attack()
{
while (true)
{
yield return new WaitForSeconds(Random.Range(3, 9));
if (_killed || _player == null)
break;
if (Vector3.Distance(transform.position, _player.transform.position) <= 20)
{
animation.CrossFade("Attack", 0.2f);
yield return new WaitForSeconds(0.5f);
if (_killed || _player == null)
break;
GameObject o = (GameObject)Instantiate(fireball_prefab, _attack_spawnpoint.transform.position, Quaternion.identity);
o.BroadcastMessage("set_dir", transform.forward);
o.BroadcastMessage("set_damage", damage);
yield return new WaitForSeconds(0.5f);
if (_killed || _player == null)
break;
animation.CrossFade("Idle", 0.2f);
}
}
}
}