-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAI.java
More file actions
219 lines (200 loc) · 5.57 KB
/
AI.java
File metadata and controls
219 lines (200 loc) · 5.57 KB
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
/**
* Controls my Enemies movement and attacks
* @see Enemy
* @see Boss
* @see EnemyBullet
* @see HomingBullet
* @author elan
*
*/
public class AI
{
/**
* Spawns My Enemies at the beginning of each level,
* Checks fields in the Level class to determine which enemies to spanw and how many
*/
public static void initializeEnemies()
{
if(Level.getLevel() % 5 == 0)
Controller.enemies.add(new Boss(Level.getLevel()));
else
{
States.enemySize = States.WINDOW_CENTRE/(double)(Level.getEnemyCols() + 1);
for(int x = 0; x < Level.getEnemyCols(); x++)
for(int y = 0; y < Level.getEnemyRows(); y++)
if((int)(Math.random()*(Level.getEnemyCols()*Level.getEnemyRows())/2) >= (Level.getEnemyCols()*Level.getEnemyRows())/1.8 - Level.getLevel())
Controller.enemies.add(new LockOnEnemy(States.enemySize * 0.6 + x*(States.enemySize + States.enemySize*0.1), States.WINDOW_RESOLUTION - States.enemySize *1.5 - y*(States.enemySize + States.enemySize*0.1), 0, Controller.enemies.size(), States.enemyHealth));
else
Controller.enemies.add(new Enemy(States.enemySize * 0.6 + x*(States.enemySize + States.enemySize*0.1), States.WINDOW_RESOLUTION - States.enemySize *1.5 - y*(States.enemySize + States.enemySize*0.1), 0, Controller.enemies.size(), States.enemyHealth));
}
}
/**
* moves all enemies Right
*/
private static void moveAllEnemiesRight()
{
for (int i = 0; i < Controller.enemies.size(); i++)
Controller.enemies.get(i).moveRight(States.enemySpeed);
}
/**
* moves all enemies Left
*/
private static void moveAllEnemiesLeft()
{
for (int i = 0; i < Controller.enemies.size(); i++)
Controller.enemies.get(i).moveLeft(States.enemySpeed);
}
/**
* moves all enemies Down
*/
private static void moveAllBasicEnemiesDown()
{
for (int i = 0; i < Controller.enemies.size(); i++)
Controller.enemies.get(i).moveDown(States.enemySpeed * 0.7);
}
/**
* Decides which Enemies are on the screen and calls the applicable AI function to control them
*/
public static void enemyAI()
{
if(Level.getLevel() % 5 == 0)
bossAI();
else
basicEnemyAI();
}
/**
* Controls the movement of all basic enemies,
* i.e. making them move from one side of the screen to the other
* and down every time the reach a side
* @see Enemy
*/
public static void basicEnemyAI()
{
if(!Enemy.isMovingDown())
{
if(Enemy.isMovingRight())
moveAllEnemiesRight();
else
moveAllEnemiesLeft();
Enemy.IncreaseHorizontalDisplacement(States.enemySpeed);
//edge detection, if an enemy is on the screen border: change direction
for (int i = 0; i < Controller.enemies.size(); i++)
{
if(!Controller.enemies.get(i).canMoveLeft() || !Controller.enemies.get(i).canMoveRight())
{
Enemy.invertHorizontalMotion();
Enemy.startOrStopVerticalMotion();
Enemy.resetHorizontalDisplacement();
basicEnemyAI();
break;
}
}
}
else
{
moveAllBasicEnemiesDown();
Enemy.IncreaseVerticalDisplacement(States.enemySpeed);
if(Enemy.getVerticalDisplacement() > States.enemySize)
{
Enemy.startOrStopVerticalMotion();
Enemy.resetVerticalDisplacement();
}
}
}
/**
* Causes a random enemy on the screen to shoot
* @see EnemyBullet
* @see HomingBullet
*/
public static void basicEnemyShoot()
{
if(Controller.enemies.size() > 0)
{
int enemyIndex = (int) (Math.random()*Controller.enemies.size());
if((int)(Math.random() * (40 - Level.getLevel())) <= Level.getLevel())
Controller.enemyBullets.add(new HomingBullet(Controller.enemies.get(enemyIndex), Controller.enemyBullets.size()));
else
Controller.enemyBullets.add(new EnemyBullet(Controller.enemies.get(enemyIndex), Controller.enemyBullets.size()));
}
}
/**
* Causes the Boss to activate his shoot method
* @see Boss
*/
public static void bossShoot()
{
for(int i = 0; i < Controller.enemies.size(); i++)
((Boss) Controller.enemies.get(i)).shoot();
}
/**
* Controls the movement of the Bosses,
* i.e. making them move from one side of the screen to the other
* and Rotate
* @see Boss
*/
public static void bossAI()
{
if(Enemy.isMovingRight())
moveAllEnemiesRight();
else
moveAllEnemiesLeft();
Enemy.IncreaseHorizontalDisplacement(States.enemySpeed);
for (int i = 0; i < Controller.enemies.size(); i++)
{
if(!Controller.enemies.get(i).canMoveLeft() || !Controller.enemies.get(i).canMoveRight())
{
Enemy.invertHorizontalMotion();
Enemy.resetHorizontalDisplacement();
bossAI();
break;
}
}
if(Level.getLevel() <= 10) {}
else if(Level.getLevel() <= 15)
{
if(Boss.isRotatingClock())
{
rotateBossClock();
}
else
{
rotateBossAntiClock();
}
Boss.increaseRotationalDisplacement(States.enemySpeed);
if(Boss.getRotationalDisplacement() > 90)
{
Boss.invertRotation();
Boss.resetRotationalDisplacement();
}
}
else
{
lockBossOntoPlayer();
}
}
/**
* Rotates the Boss clockwise
*/
private static void rotateBossClock()
{
for (int i = 0; i < Controller.enemies.size(); i++)
Controller.enemies.get(i).rotateClockWise(States.enemySpeed);
}
/**
* Rotates the Boss anti-clockwise
*/
private static void rotateBossAntiClock()
{
for (int i = 0; i < Controller.enemies.size(); i++)
Controller.enemies.get(i).rotateAntiClockWise(States.enemySpeed);
}
/**
* Causes the Boss to "Lock onto" the player,
* i.e. always aim directly at the shooter object
*/
private static void lockBossOntoPlayer()
{
for (int i = 0; i < Controller.enemies.size(); i++)
((Boss) Controller.enemies.get(i)).lockOntoPlayer();
}
}