-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSpawnerTile.java
More file actions
104 lines (86 loc) · 3.5 KB
/
Copy pathSpawnerTile.java
File metadata and controls
104 lines (86 loc) · 3.5 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
import greenfoot.*;
import java.util.Random;
public class SpawnerTile extends Tile {
private boolean spawned = false;
Random random = new Random();
private boolean topHalf;
public SpawnerTile() {
super("tile_floor.png", 'p');
}
public void act() {
super.act();
if (spawned) return;
if (isInTopHalfOfBoard() && allTopHalfGatesActivated()) {
spawnEnemies(4);
spawned = true;
} else{
if (!isInTopHalfOfBoard() && allBottomHalfGatesActivated()){
spawnEnemies(4);
spawned = true;
}
}
/*if (allBottomHalfGatesActivated() && !isInTopHalfOfBoard()) {
spawnEnemies(4);
spawned = true;
}*/
}
private boolean topHalfGateActivated() {
MyWorld world = (MyWorld) getWorld();
Board board = world.getObjects(Board.class).get(0);
int midRow = board.getTileRowCount() / 2;
for (GateTileEnter gate : world.getObjects(GateTileEnter.class)) {
int gateRow = (gate.getY() + Tile.SIZE / 2) / Tile.SIZE;
if (gateRow < midRow && !gate.isActivated()) {
return false;
}
}
for (GateTileExit gate : world.getObjects(GateTileExit.class)) {
int gateRow = (gate.getY() + Tile.SIZE / 2) / Tile.SIZE;
if (gateRow < midRow && !gate.isActivated()) {
return false;
}
}
return true;
}
private boolean allTopHalfGatesActivated() {
MyWorld world = (MyWorld) getWorld();
Board board = world.getObjects(Board.class).get(0);
int midRow = board.getTileRowCount() / 2;
for (GateTileEnter gate : world.getObjects(GateTileEnter.class)) {
if (board.getTileRow(gate) < midRow && !gate.isActivated()) {
return false;
}
}
for (GateTileExit gate : world.getObjects(GateTileExit.class)) {
if (board.getTileRow(gate) < midRow && !gate.isActivated()) {
return false;
}
}
return true;
}
private boolean allBottomHalfGatesActivated() {
MyWorld world = (MyWorld) getWorld();
Board board = world.getObjects(Board.class).get(0);
int midRow = board.getTileRowCount() / 2;
for (GateTileEnter gate : world.getObjects(GateTileEnter.class)) {
if (board.getTileRow(gate) >= midRow && !gate.isActivated()) return false;
}
for (GateTileExit gate : world.getObjects(GateTileExit.class)) {
if (board.getTileRow(gate) >= midRow && !gate.isActivated()) return false;
}
return true;
}
private void spawnEnemies(int count) {
World world = getWorld();
double centerX = getRealX(); // realX of the spawner
double centerY = getRealY();
System.out.println("Spawning enemies at: (" + centerX + ", " + centerY + ")");
for (int i = 0; i < count; i++) {
int dx = Greenfoot.getRandomNumber(257) - 128; // -128 to +128
int dy = Greenfoot.getRandomNumber(257) - 128;
Enemy enemy = Greenfoot.getRandomNumber(2) == 0 ? new Melee() : new Ranged();
world.addObject(enemy, (int)this.getRealX() + random.nextInt(257) - 128, (int)this.getRealY() + random.nextInt(257)-128); // placeholder on-screen position
enemy.updateLocation(); // applies real position visually
}
}
}