-
Notifications
You must be signed in to change notification settings - Fork 0
/
Block.java
97 lines (91 loc) · 2.88 KB
/
Block.java
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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Block extends Actor
{
// Setup BlockInfo
public String type;
private int health;
public Block() {
String img;
// Randomly generate Block Type
int rand = Greenfoot.getRandomNumber(100);
if (rand <= 1) {
img = "blockHeart.png";
health = 1;
this.type = "extraLive";
} else if (rand <= 6) {
img = "blockLaser.png";
health = 1;
this.type = "laser";
} else if (rand <= 16) {
img = "blockBomb.png";
health = 1;
this.type = "bomb";
} else if (rand <= 46) {
img = "blockDouble.png";
health = 2;
this.type = "double";
} else {
img = "blockNormal.png";
health = 1;
this.type = "normal";
}
setImage(img);
}
public void act()
{
Board board = (Board) getWorld();
// Move Block
if (board.tick%6 == 0) {
setLocation (getX(), getY() + board.blockSpeed);
}
// Check for Block out of Bounce
if (getY() >= board.getHeight()-1) {
board.lose("block");
}
}
//
public void hit () {
this.hit("");
}
public void hit(String DamageType) {
Board board = (Board) getWorld();
//set Damage and update BlockHealth
int damage = board.damage;
if (DamageType == "Bomb") {
damage = board.damage * 3;
}
health-=damage;
// Check BlockType
if (type == "bomb") {
// Create and set off Bomb / Add Score
Bomb bomb = new Bomb();
getWorld().addObject( bomb, getX(), getY());
//bomb.setTick();
board.addScore(1);
} else if (type == "laser") {
// Create laser type Upgrade / Add Score
Upgrade upgrade = new Upgrade("laser");
getWorld().addObject( upgrade, getX(), getY());
// Add Score
board.addScore(1);
} else if (type == "extraLive") {
// Create laser type Upgrade / Add Score
Upgrade upgrade = new Upgrade("extraLive");
getWorld().addObject( upgrade, getX(), getY());
// Add Score
board.addScore(1);
} else if (type == "double") {
// Set "broken" Image / Add Score
setImage("blockDouble2.png");
board.addScore(4);
} else {
// Normal Block add Score
board.addScore(2);
}
// Delete if no Health
if (health < 1) {
getWorld().removeObject(this);
board.addGameInfo("destroyed", type);
}
}
}