-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBomb.java
More file actions
38 lines (34 loc) · 1.08 KB
/
Copy pathBomb.java
File metadata and controls
38 lines (34 loc) · 1.08 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
import greenfoot.*;
import java.util.ArrayList;
/**
* Bomb is a trap used to trigger the explosion. If pedestrians
* or vihicles are intersecting with a bomb, explosion will be triggered.
*
* @author Yixin Cai
* @version 2022-10-24
*/
public class Bomb extends Actor
{
/**
* Act - do whatever the Bomb wants to do. This method is
* called whenever the 'Act' or 'Run' button gets pressed in
* the environment.
*/
public void act()
{
// check if bomb is trigger by checking interesecting pedestrians and vehicles
ArrayList<Pedestrian> p = (ArrayList<Pedestrian>) getIntersectingObjects(Pedestrian.class);
ArrayList<Vehicle> v = (ArrayList<Vehicle>) getIntersectingObjects(Vehicle.class);
if (p.size() > 0 || v.size() > 0) {
explode();
}
}
/**
* Trigger bomb explosion effect.
*/
private void explode() {
Explosion explosion = new Explosion();
getWorld().addObject(explosion, this.getX() - 8, this.getY() + 10);
getWorld().removeObject(this);
}
}