-
Notifications
You must be signed in to change notification settings - Fork 0
/
Battle_Player.java
307 lines (250 loc) · 7.11 KB
/
Battle_Player.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Polygon;
import javax.swing.ImageIcon;
public class Battle_Player {
private int q, r, mx, my, health, maxHealth, CDR, strength, hx, hy;
private double angle, speed, recoverCD;
private static double x, y;
private double hpAngle;
private ImageIcon[] down, left, right, up;
private boolean moving;
public Battle_Player(int health) {
q = 0;
r = 0;
y = Hextile.n_padding
+ (Hextile.screen_y - Hextile.n_padding - Hextile.s_padding)
/ 2;
x = Hextile.h_padding + (Hextile.screen_x - Hextile.h_padding * 2) / 2;
angle = 0;
speed = 60;
mx = (int) x;
my = (int) y;
hx = 1220;
hy = 60;
recoverCD = 2;
this.maxHealth = health;
this.health = health;
moving = true;
// designates the images
down = new ImageIcon[2];
down[0] = new ImageIcon("lvlimages/down.gif");
down[1] = new ImageIcon("lvlimages/StarchildDOWN.png");
left = new ImageIcon[2];
left[0] = new ImageIcon("lvlimages/left.gif");
left[1] = new ImageIcon("lvlimages/StarchildLEFT.png");
right = new ImageIcon[2];
right[0] = new ImageIcon("lvlimages/right.gif");
right[1] = new ImageIcon("lvlimages/StarchildRIGHT.png");
up = new ImageIcon[2];
up[0] = new ImageIcon("lvlimages/up.gif");
up[1] = new ImageIcon("lvlimages/StarchildUP.png");
}
// Updates the player object position and status
public void update(MouseStatus mouse, Hextile[][] hextiles, double delta) {
moving = true;
if (mouse.isPressed() && my != mouse.getMy() && mx != mouse.getMx()) {
mx = mouse.getMx();
my = mouse.getMy();
angle = angleCal((int) x, (int) y, mx, my);
}
double newx = x + Math.cos(angle) * speed * delta
* (1 - 0.5 * Math.abs(Math.sin(angle)));
double newy = y + Math.sin(angle) * speed * delta
* (1 - 0.5 * Math.abs(Math.sin(angle)));
double[] alternatePath;
// If the point is not in the big hexagon
if (!Hextile.getBigContainHex().contains((int) newx, (int) newy)) {
alternatePath = alternateRouteCal(delta);
if (alternatePath != null) {
if (1.5 <= Math.sqrt(Math.pow(mx - x, 2)
+ Math.pow((my - y) / 2.0, 2))) {
x = alternatePath[0];
y = alternatePath[1];
updateTilePos(hextiles);
} else {
x = mx;
y = my;
updateTilePos(hextiles);
moving = false;
}
} else {
moving = false;
}
} else {
if (1.5 <= Math.sqrt(Math.pow(mx - x, 2)
+ Math.pow((my - y) / 2.0, 2))) {
x = newx;
y = newy;
updateTilePos(hextiles);
} else {
x = mx;
y = my;
updateTilePos(hextiles);
moving = false;
}
}
hpAngle = (hpAngle + 0.01) % (Math.PI * 2);
if (recoverCD > 0) {
recoverCD -= delta / 3;
}
// System.out.println(recoverCD);
if (recoverCD <= 0) {
if (collisionCal(EnemyHandler.getList(1), EnemyHandler.getList(2),
EnemyHandler.getList(3), ProjectileHandler.getEShots())) {
startRecover();
health--;
EnemyHandler.removeInHex(q, r);
EnemyHandler.removeInHex(q + 1, r);
EnemyHandler.removeInHex(q + 1, r - 1);
EnemyHandler.removeInHex(q, r - 1);
EnemyHandler.removeInHex(q - 1, r);
EnemyHandler.removeInHex(q - 1, r + 1);
EnemyHandler.removeInHex(q, r + 1);
}
}
}
// Checks if it shares the same spot as an enemy or enemy projectile
private boolean collisionCal(int[][] list, int[][] list2, int[][] list3,
int[][] eShots) {
int[] qr = { q, r };
for (int i = 0; i < list.length; i++) {
if (list[i][0] == qr[0] && list[i][1] == qr[1])
return true;
}
for (int i = 0; i < list2.length; i++) {
if (list2[i][0] == qr[0] && list2[i][1] == qr[1])
return true;
}
for (int i = 0; i < list3.length; i++) {
if (list3[i][0] == qr[0] && list3[i][1] == qr[1])
return true;
}
for (int i = 0; i < eShots.length; i++) {
if (eShots[i][0] == qr[0] && eShots[i][1] == qr[1])
return true;
}
return false;
}
// Moving along a wall calculations
private double[] alternateRouteCal(double delta) {
double newx, newy;
double tAngle;
for (int i = 1; i < 90; i += 1) {
for (int j = 0; j < 2; j++) {
if (j == 0) {
tAngle = angle + Math.PI / 2 * i / 90;
} else {
tAngle = angle - Math.PI / 2 * i / 90;
}
newx = x + Math.cos(tAngle) * speed * delta;
newy = y + Math.sin(tAngle) * speed * delta;
if (Hextile.getBigContainHex().contains(newx, newy)) {
double[] xy = new double[2];
xy[0] = newx;
xy[1] = newy;
angle = angleCal((int) xy[0], (int) xy[1], mx, my);
return xy;
}
}
}
return null;
}
public void startRecover() {
recoverCD = 1;
}
public int getQ() {
return q;
}
public int getR() {
return r;
}
// Finds the angle the player's facing
private double angleCal(int x, int y, int mx, int my) {
double theta;
theta = angle = Math.atan2(my - y, mx - x);
return theta;
}
// Updates the tile position of the player
private void updateTilePos(Hextile[][] hextiles) {
int[] pos;
pos = Hextile.hexContainCal(hextiles, (int) x, (int) y);
if (pos != null) {
q = pos[0];
r = pos[1];
}
}
// draws the player
public void draw(Graphics2D g) {
int stance = 1;
if (moving == true) {
stance = 0;
}
angle = (angle + Math.PI * 2) % (Math.PI * 2);
g.drawOval((int) x - 10, (int) y - 5, 20, 10);
g.drawLine((int) x, (int) y, (int) (x + Math.cos(angle) * 10),
(int) (y + Math.sin(angle) * 5));
if ((int) (recoverCD * 10) % 2 == 1 || recoverCD <= 0) {
// Player debug image
if (angle < Math.PI / 4 || angle > Math.PI * 7 / 4) {
g.drawImage(right[stance].getImage(), (int) x - 19,
(int) y - 48, null);
} else if (angle < Math.PI * 3 / 4) {
g.drawImage(down[stance].getImage(), (int) x - 19,
(int) y - 48, null);
} else if (angle < Math.PI * 5 / 4) {
g.drawImage(left[stance].getImage(), (int) x - 19,
(int) y - 48, null);
} else {
g.drawImage(up[stance].getImage(), (int) x - 19, (int) y - 48,
null);
}
}
if (recoverCD >= 0) {
Hextile.drawShakeHex(g);
}
}
// RECURSION DRAWS HEALTH BAR THING
public void drawhealth(Graphics2D g, int lvl, int current) {
if (lvl <= 0)
return;
int radius = 10 * lvl, sides = lvl + 2;
double tAngle;
int[] xverts = new int[sides], yverts = new int[sides];
for (int i = 0; i < sides; i++) {
if (lvl % 2 == 0) {
tAngle = Math.PI * 2 / (sides) * i + hpAngle;
} else {
tAngle = Math.PI * 2 / (sides) * i - hpAngle;
}
xverts[i] = (int) (Math.cos(tAngle) * radius) + hx;
yverts[i] = (int) (Math.sin(tAngle) * radius) + hy;
}
if (lvl > current) {
g.setColor(Color.lightGray);
g.drawPolygon(new Polygon(xverts, yverts, sides));
} else if (lvl == current) {
g.setColor(Color.white);
g.fillPolygon(new Polygon(xverts, yverts, sides));
} else {
g.setColor(Color.gray);
g.drawPolygon(new Polygon(xverts, yverts, sides));
}
drawhealth(g, lvl - 1, current);
}
public int getMaxHealth() {
return maxHealth;
}
public int getHealth() {
// TODO Auto-generated method stub
return health;
}
public static int getY() {
// TODO Auto-generated method stub
return (int) y;
}
public static int getX() {
// TODO Auto-generated method stub
return (int) x;
}
}