-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShootingEnemy.js
164 lines (135 loc) · 5.02 KB
/
ShootingEnemy.js
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
var enemyTransform : Transform;
var target : Transform;
var health : float = 30.0;
var normalDamage : float = 10.0;
var maxDamage : float = 30.0;
var prefabExplosion:Transform;
//Referens till prefaben för skott.
var prefabShot : Transform;
var attackRepeatTime : float = 0.4;
var rotationSpeed = 3;
var attackThreshold = 5;
private var direction : Vector3;
//Flagga som kollar om fienden attackerar spelaren eller inte.
private var attacking = false;
private var attackTime;
enum Direction {
Up = 0,
Down = 1,
Left = 2,
Right = 3,
}
var _enemyDirection : Direction;
function Start () {
target = GameObject.FindWithTag("Player").transform;
}
function Awake(){
attackTime = Time.time;
enemyTransform = transform;
UpdateRotation();
}
function TakeDamage (damage:float){
health -= damage;
if(health <= 0.0){
Instantiate(prefabExplosion, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
//Sätter orientering hos fienden.
function UpdateRotation(){
if(_enemyDirection == Direction.Down){
enemyTransform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (Vector3(0, 0, -1.0)), Time.deltaTime*100);
direction = Vector3(0, 0, -1.0);
}
else if(_enemyDirection == Direction.Up){
enemyTransform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (Vector3(0, 0, 1.0)), Time.deltaTime*100);
direction = Vector3(0, 0, 1.0);
}
else if(_enemyDirection == Direction.Left){
enemyTransform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (Vector3(-1.0, 0, 0)), Time.deltaTime*100);
direction = Vector3(-1.0, 0, 0);
}
else if(_enemyDirection == Direction.Right){
enemyTransform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (Vector3(1.0, 0, 0)), Time.deltaTime*100);
direction = Vector3(1.0, 0, 0);
}
}
function Update () {
//Avstånd till spelaren
var dist= (target.position - enemyTransform.position).magnitude;
if (attacking) {
if (dist > attackThreshold) {
attacking = false;
}
//Attackerar med tidsintervallet attackTime då spelaren är tillräckligt nära.
if(dist <= attackThreshold && Time.time > attackTime){
//enemyTransform.rotation = Quaternion.Slerp(enemyTransform.rotation, Quaternion.LookRotation(target.position - enemyTransform.position), rotationSpeed*Time.deltaTime);
Attack();
attackTime = Time.time + attackRepeatTime;
}
}
else {
if (dist <= attackThreshold) {
attacking = true;
}
}
}
function Attack(){
//Skapar ett skott utifrån samma position och rotation som karaktären
var positionBuffered : Vector3 = GetBufferedVector();
var shot :Transform = Instantiate(prefabShot, positionBuffered, enemyTransform.rotation);
//Sänder infomration om riktning och element till skottet
if(enemyTransform.gameObject.CompareTag("EnemyFire")){
shot.gameObject.SendMessage("SetDirection", direction);
shot.gameObject.SendMessage("SetElement", "Fire");
}
else if(enemyTransform.gameObject.CompareTag("EnemyIce")){
shot.gameObject.SendMessage("SetDirection", direction);
shot.gameObject.SendMessage("SetElement", "Ice");
}
else if(enemyTransform.gameObject.CompareTag("EnemyWater")){
shot.gameObject.SendMessage("SetDirection", direction);
shot.gameObject.SendMessage("SetElement", "Water");
}
else if(enemyTransform.gameObject.CompareTag("EnemyEarth")){
shot.gameObject.SendMessage("SetDirection", direction);
shot.gameObject.SendMessage("SetElement", "Earth");
}
}
//Lägger på bufferdistans så att inte skottet skall krocka med fienden själv.
function GetBufferedVector(){
var buffer : float = 0.8;
if(_enemyDirection == Direction.Up){
return Vector3(enemyTransform.position.x, enemyTransform.position.y, enemyTransform.position.z + buffer);
}
else if(_enemyDirection == Direction.Down){
return Vector3(enemyTransform.position.x, enemyTransform.position.y, enemyTransform.position.z - buffer);
}
else if(_enemyDirection == Direction.Left){
return Vector3(enemyTransform.position.x - buffer, enemyTransform.position.y, enemyTransform.position.z);
}
else if(_enemyDirection == Direction.Right){
return Vector3(enemyTransform.position.x + buffer, enemyTransform.position.y, enemyTransform.position.z);
}
}
//Kollisionshanterare
function OnTriggerEnter(other:Collider){
if(other.gameObject.CompareTag("Player")){
var damageInformation = new Array();
if(enemyTransform.gameObject.CompareTag("EnemyFire")){
damageInformation[0] = "Fire";
}
else if(enemyTransform.gameObject.CompareTag("EnemyIce")){
damageInformation[0] = "Ice";
}
else if(enemyTransform.gameObject.CompareTag("EnemyWater")){
damageInformation[0] = "Water";
}
else if(enemyTransform.gameObject.CompareTag("EnemyEarth")){
damageInformation[0] = "Water";
}
damageInformation[1] = maxDamage;
damageInformation[2] = normalDamage;
other.gameObject.SendMessage("SetDamage", damageInformation);
}
}