-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameEnemy.java
More file actions
195 lines (163 loc) · 3.56 KB
/
GameEnemy.java
File metadata and controls
195 lines (163 loc) · 3.56 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
// Alexandra Qin
package DoodleFall;
import java.awt.*;
public class GameEnemy
{
// intitalize enemy move speed
private int move_speed = GC.ENEMY_MOVE_SPEED;
private int x_pos_left;
private int x_pos_right;
private int y_pos_up;
private int y_pos_down;
private Image objectPic;
private boolean moving;
private boolean moving_left;
private boolean moving_right;
private boolean moving_up;
private boolean moving_down;
private int moveNum;
private static Component parent;
public GameEnemy()
{
this(parent);
}
public GameEnemy(Component parent)
{
// set random position for enemy
x_pos_left = (int)(Math.random() * (GC.APPLET_WIDTH - 30));
x_pos_right = x_pos_left + GC.ENEMY_WIDTH;
y_pos_up = (int)(80 + Math.random() * (GC.APPLET_HEIGHT - 80));
y_pos_down = y_pos_up + GC.ENEMY_HEIGHT;
}
public void setImages (Image objectPic)
{
this.objectPic = objectPic;
}
public int getXPosLeft()
{
return x_pos_left;
}
public int getXPosRight()
{
return x_pos_right;
}
public int getYPosUp()
{
return y_pos_up;
}
public int getYPosDown()
{
return y_pos_down;
}
public void resetEnemy()
{
// resets random position for enemy
x_pos_left = (int)(Math.random() * (GC.APPLET_WIDTH - 30));
x_pos_right = x_pos_left + GC.ENEMY_WIDTH;
y_pos_up = (int)(80 + Math.random() * (GC.APPLET_HEIGHT - 80));
y_pos_down = y_pos_up + GC.ENEMY_HEIGHT;
}
public void resetMovingEnemy()
{
// resets random position for moving enemy
x_pos_left = (int)(Math.random() * (GC.APPLET_WIDTH - 30));
x_pos_right = x_pos_left + GC.ENEMY_WIDTH;
y_pos_up = (int)(80 + Math.random() * (GC.APPLET_HEIGHT - 80));
y_pos_down = y_pos_up + GC.ENEMY_HEIGHT;
moving = true;
moving_left = false;
moving_right = false;
moving_up = false;
moving_down = false;
// randomly determines the direction of enemy movement
moveNum = (int)(Math.random() * 4);
if (moveNum == 0)
moving_left = true;
if (moveNum == 1)
moving_right = true;
if (moveNum == 2)
moving_up = true;
if (moveNum == 3)
moving_down = true;
}
public boolean isMoving()
{
return moving;
}
public void setMoveSpeed(int speed)
{
this.move_speed = speed;
}
public void enemyMoveLeft(boolean value)
{
moving_left = value;
}
public void enemyMoveRight(boolean value)
{
moving_right = value;
}
public void enemyMoveUp(boolean value)
{
moving_up = value;
}
public void enemyMoveDown(boolean value)
{
moving_down = value;
}
// determines enemy's behavior if moving
public void enemyMove()
{
if(moving_left)
{
x_pos_left -= move_speed;
x_pos_right -= move_speed;
}
else if(moving_right)
{
x_pos_left += move_speed;
x_pos_right += move_speed;
}
else if(moving_down)
{
y_pos_up += move_speed;
y_pos_down += move_speed;
}
else if(moving_up)
{
y_pos_up -= move_speed;
y_pos_down -= move_speed;
}
}
// if enemy out of bounds down
public boolean isOutDown()
{
return (y_pos_up > GC.APPLET_HEIGHT);
}
// if enemy out of bounds up
public boolean isOutUp()
{
return (y_pos_down < 0);
}
// if enemy out of bounds left
public boolean isOutLeft()
{
return (x_pos_right < 0);
}
// if enemy out of bounds right
public boolean isOutRight()
{
return (x_pos_left > GC.APPLET_WIDTH);
}
// resets moving enemy if out of bounds
public void resetOutEnemy(int x, int y)
{
x_pos_left = x;
x_pos_right = x + GC.ENEMY_WIDTH;
y_pos_up = y;
y_pos_down = y + GC.ENEMY_HEIGHT;
}
public void paintEnemy(Graphics g)
{
g.drawImage(objectPic, x_pos_left, y_pos_up, parent);
}
}