-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPileOfBoxes.java
More file actions
57 lines (54 loc) · 1.73 KB
/
PileOfBoxes.java
File metadata and controls
57 lines (54 loc) · 1.73 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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
* Write a description of class ButtonDoor here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class PileOfBoxes extends ScrollingActor
{
int crack; //increases when boxes are hit, cracks once then removes boxes
private static final int NUM_FRAGMENTS = 60;
private GreenfootSound boxHit = new GreenfootSound("singleBoxHit.mp3");
private GreenfootSound boxCrash = new GreenfootSound("boxesBreak.mp3");
public void act()
{
breakApart();
if (crack < 2) block();
}
public void block()
{
List<Meatball> meatball = getIntersectingObjects(Meatball.class);
List<Bullet> bullet = getIntersectingObjects(Bullet.class);
if (!bullet.isEmpty()) getWorld().removeObject(bullet.get(0));
}
public void breakApart()
{
Meatball meatball = (Meatball) getOneIntersectingObject(Meatball.class);
if (meatball != null) {
getWorld().removeObject(meatball);
boxHit.play();
crack++;
}
if (crack == 1) setImage("boxes_stage2_scaled.png");
if (crack == 2) {
explode();
boxCrash.play();
getWorld().removeObject(this);
return;
}
}
public void explode()
{
placeDebris(getX(), getY(), NUM_FRAGMENTS);
//getWorld().removeObjects(this);
}
private void placeDebris(int x, int y, int numFragments)
{
for (int i = 0; i < numFragments; i++)
{
getWorld().addObject(new BoxDebris(), x, y);
}
}
}