-
Notifications
You must be signed in to change notification settings - Fork 0
/
Asteroid.java
200 lines (188 loc) · 4.08 KB
/
Asteroid.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import java.awt.image.BufferedImage;
public class Asteroid implements SpaceObject {
private static final int MAX_SPEED = 6;
private int rotationSpeedLimiter = 10;
private int speedOfRotation;
private int speed;
private int x, y, width, height;
private int type;
private int strength, health;
private int score;
private int updateCounter, explosionItr;
private BufferedImage[] asteroids = new BufferedImage[8];
private BufferedImage explosionImages[];
private int currentImage;
private boolean isOnScreen, isExploding;
public Asteroid() {
type = (int)(Math.random()*12+1);
isOnScreen = true;
if(type >= 1 && type <= 4) {
if(type == 1) {
asteroids = getImages(60, 15, 22, 18);
width = 22;
height = 18;
}
else if(type == 2) {
asteroids = getImages(60, 42, 22, 18);
width = 22;
height = 18;
}
else if(type == 3) {
asteroids = getImages(64, 66, 22, 18);
width = 22;
height = 18;
}
else if(type == 4) {
asteroids = getImages(72, 90, 21, 18);
width = 21;
height = 18;
}
strength = 2;
health = 2;
//explosionItr = 15;
}
else if(type >= 5 && type <= 7) {
if(type == 5) {
asteroids = getImages(251, 13, 34, 34);
width = 33;
height = 34;
}
else if(type == 6) {
asteroids = getImages(271, 55, 31, 30);
width = 31;
height = 30;
}
else if(type == 7) {
asteroids = getImages(258, 89, 34, 31);
width = 34;
height = 31;
}
strength = 3;
health = 3;
//explosionItr = 3;
}
else if(type >= 8 && type <= 10) {
if(type == 8) {
asteroids = getImages(128, 157, 46, 60);
width = 46;
height = 60;
}
else if(type == 9) {
asteroids = getImages(98, 214, 51, 62);
width = 51;
height = 62;
}
else if(type == 10) {
asteroids = getImages(100, 279, 50, 56);
width = 50;
height = 56;
}
//explosionItr = 0;
strength = 5;
health = 5;
}
else if(type == 11 || type == 12) {
if(type == 11) {
asteroids = getImages(230, 347, 33, 31);
width = 33;
height = 31;
}
else if(type == 12) {
asteroids = getImages(230, 380, 33, 31);
width = 33;
height = 31;
}
//explosionItr = 7;
strength = 4;
health = 4;
}
x = (int)(Math.random() * (SpaceWarrior.WIDTH - 2 * width) + 1 + width);
y = 1 - height;
speed = (int)(Math.random() * MAX_SPEED + 1);
score = speed * strength;
speedOfRotation = (int)(Math.random() * 3);
explosionImages = new BufferedImage[16];
int in = 2;
for(int i = 0; i < 16; i++) {
explosionImages[i] = SpriteSheets.explosion[in].getSubimage(64 * (i % 4), 64 * (i / 4), 64, 64);
}
}
@Override
public int getX() {
return x;
}
@Override
public int getY() {
return y;
}
@Override
public int getHeight() {
return height;
}
@Override
public int getWidth() {
return width;
}
@Override
public BufferedImage getImage() {
if(!isExploding)
return asteroids[currentImage];
else if(explosionItr < 15)
return explosionImages[explosionItr++];
return null;
}
@Override
public void move() {
updateCounter = (updateCounter + 1) % (rotationSpeedLimiter - speedOfRotation);
if(!isOnScreen)
return;
y += speed;
if(y >= SpaceWarrior.HEIGHT) {
isOnScreen = false;
}
if(updateCounter % 10 == 0) {
if(currentImage == 7)
y += (height * 0.2);
currentImage = (currentImage+1) % 8;
}
if(explosionItr == 15)
isOnScreen = false;
}
public int getType() {
return type;
}
@Override
public boolean isOnScreen() {
return isOnScreen;
}
@Override
public int getStrength() {
return strength;
}
@Override
public void damaged(int h) {
health -= h;
if(health <= 0) {
health = 0;
explode();
//isOnScreen = false;
}
}
public int getHealth() {
return health;
}
public int getScore() {
return score;
}
private BufferedImage[] getImages(int x, int y, int width, int height) {
BufferedImage[] bi = new BufferedImage[8];
for(int i = 0; i < 8; i++) {
bi[i] = SpriteSheets.image2.getSubimage(x + i*width, y, width, height);
}
return bi;
}
public void explode() {
strength = 0;
isExploding = true;
}
}