-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboss.cs
220 lines (210 loc) · 7.24 KB
/
boss.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//This class contain the script for the boss game object.
public class boss : MonoBehaviour
{
private int state;
public Slider slider;
public GameObject bossrb;
public GameObject shields;
public GameObject player;
public GameObject red;
public GameObject blue;
public GameObject white;
public GameObject bossbullet;
public GameObject victory;
public Transform spawn1;
public Transform spawn2;
public Transform spawn3;
public Transform spawn4;
public Transform firepoint1;
public Transform firepoint2;
public Transform firepoint3;
public Transform firepoint4;
private float bulletForce;
private float activating_distance;
private int health = 50;
private bool candmg = false;
private int waves = 0;
private GameObject array;
private int shootcounter = 0;
// Start is called before the first frame update to initialize the boss.
void Start()
{
slider.value = health;
state = 1;
activating_distance = 5.7f;
bulletForce = 6f;
}
// This function is called at a fixed interval to perform updates to the game for those timed effects.
void FixedUpdate()
{
shields.transform.Rotate(0,0,4,Space.Self);
float distance = Vector3.Distance(player.transform.position, transform.position);
if(distance <= activating_distance)
{
if(state == 1)
{
boss_spawn(3);
}else if(state == 2)
{
state2();
}else if(state == 3)
{
state3();
}
else if(state == 4)
{
state4();
}
}
}
//the second state of the boss is to shoot projectiles.
private void state2()
{
shoot(40);
}
//the third state of the boss is to spawn more minons.
private void state3()
{
boss_spawn(5);
}
//the last state of the boss is both to shoot projectiles and spawn enermies.
private void state4()
{
shoot(35);
boss_spawn(5);
}
//This function is called by unity when this game object collides with another game object. This function updates the game state of the boss.
void OnCollisionEnter2D(Collision2D collision)
{
if (candmg == true)
{
string tagg = collision.gameObject.tag;
if (tagg == "bullet")
{
health -= 1;
}
else if (tagg == "bullet2")
{
health -= 3;
}
slider.value = health;
if (health == 0)
{
victory.SetActive(true);
Time.timeScale = 0f;
}
else if (health == 15)
{
state = 4;
}
else if (health == 25)
{
state = 3;
candmg = false;
shields.SetActive(true);
}
else if (health == 40)
{
state = 2;
activating_distance = 10f;
}
}
}
//This function instantiate the bullets and apply a force to the bullets to move them. It also have a counter to fire the bullets in a fixed interval.
private void shoot(int interval)
{
if (shootcounter == interval)
{
shootcounter = 0;
GameObject bullet1 = Instantiate(bossbullet, firepoint1.position, firepoint1.rotation);
Rigidbody2D rb1 = bullet1.GetComponent<Rigidbody2D>();
rb1.AddForce(firepoint1.up * bulletForce, ForceMode2D.Impulse);
GameObject bullet2 = Instantiate(bossbullet, firepoint2.position, firepoint2.rotation);
Rigidbody2D rb2 = bullet2.GetComponent<Rigidbody2D>();
rb2.AddForce(firepoint2.up * bulletForce, ForceMode2D.Impulse);
GameObject bullet3 = Instantiate(bossbullet, firepoint3.position, firepoint3.rotation);
Rigidbody2D rb3 = bullet3.GetComponent<Rigidbody2D>();
rb3.AddForce(firepoint3.up * bulletForce, ForceMode2D.Impulse);
GameObject bullet4 = Instantiate(bossbullet, firepoint4.position, firepoint4.rotation);
Rigidbody2D rb4 = bullet4.GetComponent<Rigidbody2D>();
rb4.AddForce(firepoint4.up * bulletForce, ForceMode2D.Impulse);
}
else
{
shootcounter += 1;
}
}
//This function return the type of enermy from a integer value.
private GameObject enermytospawn(int index)
{
if (index == 0)
{
return white;
}
else if (index == 1)
{
return red;
}
else
{
return blue;
}
}
//This function allows the boss to initantiate enermies at fixed positions
private void boss_spawn(int wavesnum)
{
array = GameObject.FindWithTag("Enermy");
if (array == null)
{
if (waves == 0)
{
Instantiate(red, spawn1.position, spawn1.rotation);
Instantiate(red, spawn2.position, spawn2.rotation);
Instantiate(red, spawn3.position, spawn3.rotation);
Instantiate(red, spawn4.position, spawn4.rotation);
waves += 1;
}
else if (waves == 1)
{
Instantiate(white, spawn1.position, spawn1.rotation);
Instantiate(white, spawn2.position, spawn2.rotation);
Instantiate(white, spawn3.position, spawn3.rotation);
Instantiate(white, spawn4.position, spawn4.rotation);
waves += 1;
}
else if(waves == 2)
{
Instantiate(blue, spawn1.position, spawn1.rotation);
Instantiate(blue, spawn2.position, spawn2.rotation);
Instantiate(blue, spawn3.position, spawn3.rotation);
Instantiate(blue, spawn4.position, spawn4.rotation);
waves += 1;
}else
{
if (waves == wavesnum)
{
shields.SetActive(false);
candmg = true;
waves = 0;
state = 0;
}
else
{
GameObject enermy = enermytospawn(Random.Range(0, 2));
Instantiate(enermy, spawn1.position, spawn1.rotation);
enermy = enermytospawn(Random.Range(0, 2));
Instantiate(enermy, spawn2.position, spawn2.rotation);
enermy = enermytospawn(Random.Range(0, 2));
Instantiate(enermy, spawn3.position, spawn3.rotation);
enermy = enermytospawn(Random.Range(0, 2));
Instantiate(enermy, spawn4.position, spawn4.rotation);
waves += 1;
}
}
}
}
}