-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExplosion.java
More file actions
102 lines (94 loc) · 2.67 KB
/
Explosion.java
File metadata and controls
102 lines (94 loc) · 2.67 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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.ArrayList;
/**
* Explosion is triggered by Bomb. It can remove all of the pedestrians
* and vehicles in the explosion range, along with sound effect.
*
* @author Yixin Cai
* @version 2022-10-24
*/
public class Explosion extends Actor implements iSoundMaker
{
// instance variables
private GreenfootImage[] images;
private GreenfootSound sound;
private int duration;
/**
* Constructor
*/
public Explosion() {
images = new GreenfootImage[12];
for (int i = 0; i < 12;i++) {
images[i] = new GreenfootImage("bomb" + i + ".png");
}
// pre-load sound file
sound = new GreenfootSound("bomb.wav");
duration = 35;
}
/**
* This method is called by the Greenfoot system when this actor has been inserted into the world.
*/
public void addedToWorld(World w) {
// Update volume after being added to World
VehicleWorld vw = (VehicleWorld)w;
if (sound != null) {
setVolume(vw.getVolume());
}
}
/**
* Act - do whatever the Explosion wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if (duration % 3 == 0) {
// update explosion image every 3 acts
int index = 11 - duration / 3;
setImage(images[index]);
}
if (duration == 0) {
sound.play();
explode();
getWorld().removeObject(this);
}
duration--;
}
/**
* Trigger explosion. Remove all of pedestrians and vehicles in the explosion range.
*/
private void explode() {
ArrayList<Pedestrian> p = (ArrayList<Pedestrian>)getObjectsInRange(130, Pedestrian.class);
ArrayList<Vehicle> v = (ArrayList<Vehicle>)getObjectsInRange(130, Vehicle.class);
for (Vehicle vehicle : v){
vehicle.removeSelf();
}
for (Pedestrian pedestrian : p){
getWorld().removeObject(pedestrian);
}
}
/**
* Set volume if there is sound
* @param volume The current volume
*/
public void setVolume(int volume) {
if (sound != null) {
sound.setVolume(volume);
}
}
/**
* Play sound if there is sound
*/
public void playSound() {
if (sound != null) {
sound.play();
}
}
/**
* Stop sound if there is sound
*/
public void stopSound() {
if (sound != null) {
sound.stop();
}
}
}