-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTank.java
392 lines (315 loc) · 10.7 KB
/
Tank.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import java.awt.*;
class TankParticle extends Particle {
Color[] possibleColors = {Color.decode("#ED4245"), Color.decode("#FFA500"), Color.decode("#808080")};
static final int side = 20;
int x;
int y;
int speed;
double angle = WanshotModel.degreesToRadians(Math.random() * 360);
int opacity = 200;
Color color;
public TankParticle(int x, int y, Color tankColor, int speed) {
this.x = x;
this.y = y;
this.speed = speed;
int ind = (int)(Math.random() * (this.possibleColors.length + 1));
if (ind > 2) {
this.color = tankColor;
} else {
this.color = this.possibleColors[ind];
}
}
public void update() {
this.x += this.speed * Math.cos(this.angle) * WanshotModel.deltaTime;
this.y += this.speed * Math.sin(this.angle) * WanshotModel.deltaTime;
this.opacity -= 300 * WanshotModel.deltaTime;
this.speed /= 2;
if (this.opacity <= 0) {
super.delete = true;
return;
}
this.color = new Color(this.color.getRed(), this.color.getGreen(), this.color.getBlue(), this.opacity);
}
public void render(Graphics2D ctx) {
int randomBump = (int)(Math.random() * 10);
ctx.rotate(this.angle, this.x + TankParticle.side / 2, this.y + TankParticle.side / 2);
ctx.setColor(this.color);
Rectangle p = new Rectangle(this.x, this.y, TankParticle.side + randomBump, TankParticle.side + randomBump);
ctx.draw(p);
ctx.fill(p);
ctx.setTransform(WanshotView.oldTransform);
}
}
class Grave extends Particle {
static final int WIDTH = 10;
static final int HEIGHT = 30;
int opacity = 200;
Color color;
int x;
int y;
public Grave(int x, int y, Color color) {
this.x = x;
this.y = y;
this.color = color;
}
public void update() {
this.opacity -= WanshotModel.deltaTime / 20;
if (this.opacity <= 0) {
super.delete = true;
return;
}
this.color = new Color(this.color.getRed(), this.color.getGreen(), this.color.getBlue(), this.opacity);
}
public void render(Graphics2D ctx) {
double angleLeanRight = Math.PI / 4;
double angleLeanLeft = Math.PI / 2;
ctx.setColor(this.color);
ctx.rotate(angleLeanRight, this.x + Grave.WIDTH / 2, this.y + Grave.HEIGHT / 2);
Rectangle p1 = new Rectangle(this.x, this.y, Grave.WIDTH, Grave.HEIGHT);
ctx.draw(p1);
ctx.fill(p1);
ctx.rotate(angleLeanLeft, this.x + Grave.WIDTH / 2, this.y + Grave.HEIGHT / 2);
Rectangle p2 = new Rectangle(this.x, this.y, Grave.WIDTH, Grave.HEIGHT);
ctx.draw(p2);
ctx.fill(p2);
ctx.setTransform(WanshotView.oldTransform);
}
}
class Track extends Particle {
static final int WIDTH = 4;
static final int HEIGHT = 7;
int opacity = 200;
Color color = Color.decode("#7B3F00");
int x;
int y;
double angle;
public Track(int x, int y, double angle) {
this.x = x;
this.y = y;
this.angle = angle;
}
public void update() {
this.opacity -= WanshotModel.deltaTime;
if (this.opacity <= 0) {
super.delete = true;
return;
}
this.color = new Color(this.color.getRed(), this.color.getGreen(), this.color.getBlue(), this.opacity);
}
public void render(Graphics2D ctx) {
ctx.rotate(this.angle, this.x + Track.WIDTH / 2, this.y + Track.HEIGHT / 2);
ctx.setColor(this.color);
Rectangle p1 = new Rectangle(this.x, (this.y) - Track.HEIGHT, Track.WIDTH, Track.HEIGHT);
ctx.draw(p1);
ctx.fill(p1);
Rectangle p2 = new Rectangle(this.x, (this.y) + Track.HEIGHT, Track.WIDTH, Track.HEIGHT);
ctx.draw(p2);
ctx.fill(p2);
ctx.setTransform(WanshotView.oldTransform); }
}
public class Tank extends Parallelogram {
//TANK STATIC INFO
static final int WIDTH = 43;
static final int HEIGHT = 33;
static final int turretBaseSide = 19;
static final int turretNozzleWidth = 21;
static final int turretNozzleHeight = 10;
//TANK INSTANCE INFO
double x;
double y;
double centerX;
double centerY;
double angle;
double turretAngle;
double xInc;
double yInc;
double rotationInc;
double speed;
boolean delete = false;
int stun;
int stunCount = 0;
int shellCap;
int shellShot = 0;
int shellCooldown;
int shellCooldownCount = 0;
int trackCooldown = -5;
int trackCooldownCount = 0;
Color color;
Color turretColor;
Color sideColor;
public Tank(
double x,
double y,
double angle,
double speed,
double rotationSpeed,
int stun,
int shellCap,
int shellCooldown,
Color color,
Color turretColor,
Color sideColor
) {
super((int)x, (int)y, Tank.WIDTH, Tank.HEIGHT, angle);
this.x = x;
this.y = y;
this.centerX = this.x + Tank.WIDTH / 2;
this.centerY = this.y + Tank.HEIGHT / 2;
this.angle = angle;
this.turretAngle = angle;
this.speed = speed;
this.rotationInc = rotationSpeed * WanshotModel.deltaTime;
this.stun = stun;
this.shellCap = shellCap;
this.shellCooldown = shellCooldown;
this.color = color;
this.turretColor = turretColor;
this.sideColor = sideColor;
}
//this only finalizes information
//actual updating of coords happens differently for player and bot
//Player && Bot will update x and y directly, not caring for collisions
//This will update center x and deal with collisions seperately
public void tankWithTile() {
for (int i = 0; i < WanshotModel.tiles.size(); i++) {
Tile tile = WanshotModel.tiles.get(i);
//colliding with this tile
if (super.sat_parallelogram(tile)) {
this.x += super.getNormal().x * super.getCollisionDepth() / 2;
this.y += super.getNormal().y * super.getCollisionDepth() / 2;
}
}
}
public void tankWithTank() {
for (int i = 0; i < WanshotModel.tanks.size(); i++) {
Tank tank = WanshotModel.tanks.get(i);
//colliding with tank not self
if (System.identityHashCode(this) != System.identityHashCode(tank)) {
if (super.sat_parallelogram(tank)) {
this.x += super.getNormal().x * super.getCollisionDepth() / 2;
this.y += super.getNormal().y * super.getCollisionDepth() / 2;
tank.x -= super.getNormal().x * super.getCollisionDepth() / 2;
tank.y -= super.getNormal().y * super.getCollisionDepth() / 2;
}
}
}
}
public void createTrack() {
Track p = new Track((int)this.centerX - Track.WIDTH / 2, (int)this.centerY - Track.HEIGHT, this.angle);
WanshotModel.particles.add(p);
}
public void createExplosion() {
Grave p = new Grave((int)this.centerX - Grave.WIDTH / 2, (int)this.centerY - Grave.HEIGHT / 2, this.color);
WanshotModel.particles.add(p);
for (int i = 0; i < 50; i++) {
TankParticle tp = new TankParticle((int)this.centerX - TankParticle.side / 2, (int)this.centerY - TankParticle.side / 2, this.color, this.getExplosionType());
WanshotModel.particles.add(tp);
}
WanshotMain.playSound("tankDeath.wav");
if (this instanceof Player) {
WanshotMain.playSound("playerDeath.wav");
} else {
WanshotMain.playSound("enemyDeath.wav");
}
}
public int getExplosionType() {
if (this instanceof BrownTank) {
return 1800;
}
if (this instanceof GreyTank) {
return 2200;
}
if (this instanceof YellowTank) {
return 2600;
}
return 3000;
}
public boolean canShoot() {
return this.shellShot < this.shellCap && this.shellCooldownCount == 0;
}
public void shoot(int shellType) {
if (this.canShoot()) {
double initialBoostX = (20 * Math.cos(this.turretAngle));
double initialBoostY = (20 * Math.sin(this.turretAngle));
Shell shell = new Shell(this.centerX - Shell.WIDTH / 2 + initialBoostX, this.centerY - Shell.HEIGHT / 2 + initialBoostY, this.turretAngle, shellType, this);
WanshotModel.shells.add(shell);
this.stunCount = this.stun;
this.shellCooldownCount = this.shellCooldown;
this.shellShot++;
}
}
public void update() {
super.update(this.x, this.y, this.angle);
if (this.shellCooldownCount < 0) {
this.shellCooldownCount++;
}
//only create tracks for tanks that move
if (!(this instanceof BrownTank) && !(this instanceof GreenTank)) {
if (this.trackCooldownCount < 0) {
this.trackCooldownCount++;
} else {
this.trackCooldownCount = this.trackCooldown;
this.createTrack();
}
}
if (this.stunCount < 0) {
this.xInc = 0;
this.yInc = 0;
this.stunCount++;
} else {
this.xInc = this.speed * Math.cos(this.angle) * WanshotModel.deltaTime;
this.yInc = this.speed * Math.sin(this.angle) * WanshotModel.deltaTime;
}
//Update Center
this.centerX = this.x + Tank.WIDTH / 2;
this.centerY = this.y + Tank.HEIGHT / 2;
//Modulo rotations
this.angle %= 2 * Math.PI;
this.turretAngle %= 2 * Math.PI;
if (this.angle < 0) {
this.angle = 2 * Math.PI - Math.abs(this.angle);
}
if (this.turretAngle < 0) {
this.turretAngle = 2 * Math.PI - Math.abs(this.turretAngle);
}
//Collisions
this.tankWithTile();
this.tankWithTank();
}
public void render(Graphics2D ctx) {
ctx.rotate(this.angle, ((int)this.x - 5) + Tank.WIDTH / 2, ((int)this.y + (Tank.HEIGHT / 7)) + Tank.HEIGHT / 2);
ctx.setColor(WanshotView.SHADOW);
Rectangle shadow = new Rectangle((int)this.x - 5, (int)this.y + (Tank.HEIGHT / 7), Tank.WIDTH, Tank.HEIGHT);
ctx.fill(shadow);
ctx.setTransform(WanshotView.oldTransform);
ctx.rotate(this.angle, this.centerX, this.centerY);
Rectangle body = new Rectangle((int)this.x, (int)this.y, Tank.WIDTH, Tank.HEIGHT);
ctx.setColor(this.color);
ctx.draw(body);
ctx.fill(body);
ctx.setColor(this.sideColor);
Rectangle leftWheel = new Rectangle((int)this.x, (int)this.y, Tank.WIDTH, Tank.HEIGHT / 5);
Rectangle rightWheel = new Rectangle((int)this.x, (int)this.y + (Tank.HEIGHT - 7), Tank.WIDTH, Tank.HEIGHT / 5);
ctx.draw(leftWheel);
ctx.fill(leftWheel);
ctx.draw(rightWheel);
ctx.fill(rightWheel);
//reset rotation
ctx.setTransform(WanshotView.oldTransform);
ctx.rotate(this.turretAngle, this.centerX, this.centerY);
Rectangle turretBaseOutline = new Rectangle((int)this.centerX - (Tank.turretBaseSide / 2), (int)this.centerY - (Tank.turretBaseSide / 2), Tank.turretBaseSide, Tank.turretBaseSide);
Rectangle turretBase = new Rectangle((int)this.centerX - ((Tank.turretBaseSide / 2)) + 2, (int)this.centerY - ((Tank.turretBaseSide / 2)) + 2, Tank.turretBaseSide - 2, Tank.turretBaseSide - 2);
ctx.setStroke(new BasicStroke(2));
ctx.setColor(Color.BLACK);
ctx.draw(turretBaseOutline);
ctx.setColor(this.turretColor);
ctx.fill(turretBase);
Rectangle turretNozzleOutline = new Rectangle((int)this.centerX + Tank.turretBaseSide / 2, (int)this.centerY - (Tank.turretNozzleHeight / 2), Tank.turretNozzleWidth, Tank.turretNozzleHeight);
Rectangle turretNozzle = new Rectangle((int)this.centerX + (Tank.turretBaseSide / 2) + 1, (int)this.centerY - (Tank.turretNozzleHeight / 2) + 1, Tank.turretNozzleWidth - 1, Tank.turretNozzleHeight - 1);
ctx.setColor(Color.BLACK);
ctx.draw(turretNozzleOutline);
ctx.setColor(this.turretColor);
ctx.fill(turretNozzle);
ctx.setTransform(WanshotView.oldTransform);
}
}