Repository navigation
Expand file tree
/
Copy pathTankEnemy.java
More file actions
101 lines (79 loc) · 2.36 KB
/
Copy pathTankEnemy.java
File metadata and controls
101 lines (79 loc) · 2.36 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
package game;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;
public class TankEnemy extends GameObject {
// Inherited fields
/*
private ID id;
private double x;
private double y;
private double velX;
private double velY;
*/
private GameObject player;
private Handler handler;
private int health = 20;
public TankEnemy(ID id, double x, double y, Handler handler) {
super(id, x, y);
this.handler = handler;
Random random = new Random();
if (random.nextBoolean()){
if (random.nextInt(Window.frame.getWidth()) < Window.frame.getWidth() / 2){
this.x = 0;
} else {
this.x = Window.frame.getWidth() - 20;
}
} else {
if (random.nextInt(Window.frame.getHeight()) < Window.frame.getHeight() / 2){
this.y = 0;
} else {
this.y = Window.frame.getHeight() - 20;
}
}
for( int i =0; i < handler.object.size(); i++) {
if(handler.object.get(i).getId() == ID.Player) {
player = handler.object.get(i);}
}
velX = 2;
velY = 2;
}
public Rectangle getBounds() {
return new Rectangle((int) x, (int) y, 30, 30);
}
public void tick () {
x += velX;
y += velY;
float diffX = (float) (x - player.getX() - 16);
float diffY = (float) (y - player.getY() - 16);
float distance = (float) Math.sqrt( (x - player.getX())*(x-player.getX()) + (y-player.getY())*(y-player.getY()));
//perpendicular distance
velX = ((-1/distance) * diffX * 2) ;
velY = ((-1/distance) * diffY * 2);
// if(y <= 0 || y >= Game.HEIGHT - 32) {velY *= -1;}
// if(x <= 0 || x >= Game.WIDTH - 32) {velX *= -1;}
}
public void render(Graphics g) {
g.setColor(Color.green);
g.fillOval( (int) x, (int) y, 30, 30);
g.setColor(Color.blue);
}
public int getHealth(){
return health;
}
@Override
public void setHealth(int damage){
health -= damage;
checkDeath();
}
public void checkDeath(){
if (health <= 0){
handler.removeObject(this);
HUD.score += 5;
}
}
// public void accountForCollision(GameObject obj) {
// HUD.health -= 50;
// }
}