-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemy.java
More file actions
162 lines (133 loc) · 3.63 KB
/
Copy pathEnemy.java
File metadata and controls
162 lines (133 loc) · 3.63 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
// Yousef Al-Wahami
// Enemy.java
// Consists of the Enemy class and the behaviour of an Enemy (speed, hp, type, pathways + destinations, animations)
import java.io.File;
import java.io.IOException;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
public class Enemy {
private int vx,vy; //x and y of rect, velocity of x and y
public int x,y, hp, payout; //payout is money given for each kill
private int [][]path;
private int leg;
private int speed;
private int type;
private double ang=-90;
private int dx, dy; //direction vectors
//used for animation
private BufferedImage[]pics;
private int frame;
/*
*idea is
*have map[[pos1,pos2][pos1,pos2]]
*map[i][j]
*i = map
*j = position
*
*every map is different and therefore has different movement positions.
*/
// dx,dy - used to let the enemy start before the starting spot
public Enemy(int pnum, int dx, int dy, int t) {
path = Util.fullPath[pnum];
x = path[1][0] + dx;
y = path[1][1] + dy;
leg = 1;
type = t;
//payout, speed, and hp is determined by specific towers
if(type==0){
speed=4;
hp=8;
payout=5;
}
else if(type==1){
speed=8;
hp=6;
payout=10;
}
else if(type==2){
speed=2;
hp=14;
payout=15;
}
else if(type==3){
speed=6;
hp=25;
payout=25;
}
try {
//in order to rotate images, we must use BufferedImage
pics = new BufferedImage[2];
for(int i = 0; i<2; i++){
//t is type, i is frame number
pics[i] = ImageIO.read(new File("Animations/enemy"+t+"_"+i+".png"));
}
}
catch (IOException e) {
System.out.println(e);
}
}
public void move(){
int [] from = path[leg]; //from destination
int [] to = path[leg+1]; //to destination
// dx, unique to speed
if(to[0] > from[0]){
dx = speed;
}
else if(to[0] < from[0]){
dx = -speed;
}
else{
dx = 0;
}
// dy, unique to speed
if(to[1] > from[1]){
dy = speed;
}
else if(to[1] < from[1]){
dy = -speed;
}
else{
dy = 0;
}
x+=dx;
y+=dy;
if(Util.dist(new int[]{x,y}, to) < speed){
leg+=1;
}
}
public Rectangle getRect(){
return new Rectangle(x,y,40,40);
}
public void draw(Graphics g){
frame++;
//handles which direction the animations face/turn
if(dx>0 && dy==0){
ang=90;
}
else if(dx<0 && dy==0){
ang=-90;
}
else if(dx==0 && dy>0){
ang=180;
}
else if(dx==0 && dy<0){
ang=0;
}
//handles the image rotation
AffineTransform rot = new AffineTransform();
rot.rotate((Math.toRadians(ang)),20,20); // 75,84 is the center of my Image, this is the point of rotation.
AffineTransformOp rotOp = new AffineTransformOp(rot, AffineTransformOp.TYPE_BILINEAR); // The options are: TYPE_BICUBIC, TYPE_BILINEAR, TYPE_NEAREST_NEIGHBOR
Graphics2D g2D = (Graphics2D)g; // NEAREST_NEIGHBOR is fastest but lowest quality
g2D.drawImage(pics[frame / 5 % pics.length],rotOp, x , y);
//health bar follows enemy around
g.setColor(Color.GREEN);
g.fillRect(x,y+40,hp*3,10);
}
public static void main(String[] args) {
Game frame = new Game();
}
}