-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlien.java
More file actions
199 lines (147 loc) · 4.33 KB
/
Copy pathAlien.java
File metadata and controls
199 lines (147 loc) · 4.33 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
199
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.Rectangle2D;
import java.util.Random;
import javax.swing.JPanel;
public class Alien extends Thread {
private JPanel panel;
private int x;
private int y;
private int width;
private int height;
private int dx; // increment to move along x-axis
private int dy; // increment to move along y-axis when dropping down
private int pointValue; // points awarded when destroyed
private Dimension dimension;
boolean isRunning;
private Image alienImage;
private int tier; // alien tier (1, 2, or 3)
private int fireChance; // chance to fire (1 in fireChance per update)
private static Random fireRandom = new Random();
public Alien (JPanel p, int xPos, int yPos, int alienTier, String spriteName) {
panel = p;
dimension = panel.getSize();
x = xPos;
y = yPos;
tier = alienTier;
// set dimensions, point value, and fire chance based on tier
switch (tier) {
case 1:
width = 30;
height = 30;
pointValue = 10;
fireChance = 100; // Lowest fire chance (1 in 200)
break;
case 2:
width = 35;
height = 30;
pointValue = 20;
fireChance = 50; // Medium fire chance (1 in 100)
break;
case 3:
width = 40;
height = 30;
pointValue = 30;
fireChance = 25; // Highest fire chance (1 in 50)
break;
default:
width = 30;
height = 30;
pointValue = 10;
fireChance = 200;
}
// Load the specified sprite image
if (spriteName != null && !spriteName.isEmpty()) {
alienImage = ImageManager.loadImage(spriteName);
} else {
// Fallback to default sprites if none specified
switch (tier) {
case 1:
alienImage = ImageManager.loadImage("enemyTier1White.png");
break;
case 2:
alienImage = ImageManager.loadImage("enemyTier2Gold.png");
break;
case 3:
alienImage = ImageManager.loadImage("enemyTier3Gold.png");
break;
default:
alienImage = ImageManager.loadImage("enemyTier1White.png");
}
}
// flip the image vertically so alien faces downward
alienImage = ImageManager.flipImageVertically(alienImage);
dx = 0; // movement along x-axis controlled by swarm
dy = 0; // movement along y-axis when dropping down
}
/**
* Attempts to fire a bullet.
* Each alien independently rolls its chance to fire.
* @return true if this alien should fire this frame, false otherwise
*/
public boolean tryToFire() {
// Each alien rolls its own chance to fire
return fireRandom.nextInt(fireChance) == 0;
}
public void draw (Graphics2D g2) {
g2.drawImage(alienImage, x, y, width, height, null);
}
public void move(int direction, boolean dropDown) {
if (!panel.isVisible ()) return;
if (direction == 1) { // move left
x = x - 5;
}
else if (direction == 2) { // move right
x = x + 5;
}
if (dropDown) {
y = y + 20; // drop down one row
}
}
public void run () {
isRunning = true;
// The actual movement is controlled by AlienSwarm
// This thread just handles the drawing loop
try {
while (isRunning) {
// Drawing is handled by the swarm
sleep (50);
}
}
catch(InterruptedException e) {}
}
public Rectangle2D.Double getBoundingRectangle() {
return new Rectangle2D.Double (x, y, width, height);
}
public int getPointValue() {
return pointValue;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public void setX(int newX) {
x = newX;
}
public void setY(int newY) {
y = newY;
}
public void stopRunning() {
isRunning = false;
}
public int getTier() {
return tier;
}
public int getFireChance() {
return fireChance;
}
}