-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemyWatcher.java
More file actions
55 lines (45 loc) · 1.89 KB
/
Copy pathEnemyWatcher.java
File metadata and controls
55 lines (45 loc) · 1.89 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
import greenfoot.Actor;
import greenfoot.Greenfoot;
import greenfoot.GreenfootImage;
import java.awt.Color;
import java.util.List;
public class EnemyWatcher extends Actor {
private int count;
public EnemyWatcher() {
count = 0;
}
public void act(){
TypingMaster world = (TypingMaster) getWorld();
if(count++ % world.getDifficuly().getGameSpeed() == 0) {
// Figure out number of enemies to be added
if(world.getDifficuly().getMaxNumberOfEnemies() > world.getObjects(Enemy.class).size()) {
Enemy enemy = world.getTheme().getEnemiesClass().generateEnemy(world);
int x = TypingMaster.GRID_WIDTH;
int y = 0;
boolean haveSlot = false;
do {
// Only add to odd places
do {
//System.out.println("y: "+ y+" Grid_height: "+TypingGame.GRID_HEIGHT);
y = Greenfoot.getRandomNumber(TypingMaster.GRID_HEIGHT - 1);
} while(y % 2 != 0);
// Verify no overlap
List<Enemy> enemies = world.getObjects(Enemy.class);
if(enemies == null || enemies.isEmpty()){
haveSlot = true;
} else {
for(Enemy env : enemies) {
if(env.getY() != y || env.getY() != y +1) {
haveSlot = true;
break;
}
}
}
} while(!haveSlot);
enemy.setImage(new GreenfootImage(enemy.getWord(), 20, Color.RED, Color.BLACK));
// Add to World
world.addObject(enemy, x, y);
}
}
}
}