-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInvader.java
More file actions
198 lines (173 loc) · 4.76 KB
/
Invader.java
File metadata and controls
198 lines (173 loc) · 4.76 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
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
import java.awt.Graphics2D;
import java.awt.Image;
import javax.sound.sampled.Clip;
import javax.swing.ImageIcon;
public abstract class Invader extends Ship {
public abstract int pointValue();
private Missile missile;
private final Clip hit = getSound("aud_hit.wav");
private boolean left, right = true, ab = true, bb, sw, change, dead;
private String level = "";
private Image a = new ImageIcon("src/img_invader" + level + "A.gif")
.getImage();
private Image b = new ImageIcon("src/img_invader" + level + "B.gif")
.getImage();
private Image i = a;
private int x = 220, y = 10, speed = 5;
private Base base;
private int delayCount = 0;
private int deadCount = 0;
@Override
public int getX() {
return x;
}
@Override
public int getY() {
return y;
}
public Base getBase() {
return base;
}
public int getDelayCount() {
return delayCount;
}
public boolean isDead() {
return dead;
}
public Missile getMissile() {
return missile;
}
@Override
public void hitSound() {
hit.setFramePosition(0);
hit.start();
}
@Override
public int deadCount() {
return deadCount++;
}
public boolean hasMissile() {
return missile != null;
}
public boolean getChange() {
return change;
}
public void changeDirection() {
right = !right;
left = !left;
if (left) {
y += 19;
}
}
public void changeImage() {
sw = true;
}
public Invader(int xi, int yi, Base base, String lvl) {
x = xi;
y = yi;
level = lvl;
if (!lvl.equals("mystery")) {
a = new ImageIcon("src/img_invader" + level + "A.gif").getImage();
b = new ImageIcon("src/img_invader" + level + "B.gif").getImage();
i = a;
}
if (lvl.equals("mystery")) {
i = a = b = new ImageIcon("src/img_mystery.gif").getImage();
int chance = (int) (Math.random() * 2);
switch (chance) {
case 0:
left = true;
right = false;
x = 450;
y = 30;
break;
case 1:
left = false;
right = true;
x = 0;
y = 30;
break;
}
}
this.base = base;
}
@Override
public void hitByMissile() {
if (base.hasMissile() && !dead) {
if (base.getMissile().getX() + 1 > x
&& base.getMissile().getX() + 1 < x + 30
&& base.getMissile().getY() > y
&& base.getMissile().getY() < y + 24) {
hitImage();
hitSound();
dead = true;
base.getMissile().setValid(false);
base.addScore(this.pointValue());
}
}
}
@Override
public void hitImage() {
i = a = b = new ImageIcon("src/img_invaderhit.gif").getImage();
}
@Override
public void draw(Graphics2D g2) {
g2.drawImage(i, x, y, null);
if (missile != null) {
missile.draw(g2);
}
if (sw) {
sw = false;
i = ab ? a : b;
ab = !ab;
bb = !bb;
}
}
@Override
public void update() {
if (!dead) {
delayCount++;
hitByMissile();
sw = false;
if (left && x <= 0 || right && x >= 460) {
change = true;
if (x <= 0) {
x += 2 * speed;
}
else {
x += speed;
}
}
else {
change = false;
}
if (left && x > 0) {
x -= speed;
}
if (right && x < 460) {
x += speed;
}
sw = false;
}
else {
hitImage();
}
}
public void updateMissile() {
if (missile != null) {
missile.update();
if (!missile.isValid()) {
missile = null;
}
}
}
public void shoot() {
if (missile == null) {
int mx = x + 15;
int my = y + 12;
missile = new Missile(mx, my, this);
// blaster.setFramePosition(0);
// blaster.start();
}
}
}