-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTank.java
174 lines (138 loc) · 3.46 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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Tank
{
private String imagePath;
public Bullet bullet = null;
public ImageIcon tankImage;
public String type = "1";
public int direction = 1;
public Transform transform = new Transform(0, 0);
public int score = 0;
public int hp = 5;
public BrickGenerator br;
public boolean loaded = true;
public TankListener tankListener;
// draft
public void shoot(){
loaded = false;
switch(direction){
case 0:
bullet = new Bullet(transform.x + 40, transform.y + 20, 0);
break;
case 1:
bullet = new Bullet(transform.x + 20, transform.y, 1);
break;
case 2:
bullet = new Bullet(transform.x, transform.y + 20, 2);
break;
case 3:
bullet = new Bullet(transform.x + 20, transform.y + 40, 3);
break;
}
}
public void DrawColRectangle(Graphics g){
g.setColor(Color.blue);
if(direction == 0)
{
g.fillRect(transform.x + 50, transform.y + 5, 5, 40);
}
else if (direction == 1)
{
g.fillRect(transform.x + 5, transform.y - 5, 40, 5);
}
else if (direction == 2)
{
g.fillRect(transform.x - 5, transform.y + 5, 5, 40);
}
else if (direction == 3)
{
g.fillRect(transform.x + 5, transform.y + 50, 40, 5);
}
//g.fillRect(660, 0, 140, 600);
}
public Rectangle colRectangle(){
if(direction == 0)
{
return new Rectangle(transform.x + 50, transform.y + 5, 5, 40);
}
else if (direction == 1)
{
return new Rectangle(transform.x + 5, transform.y - 5, 40, 5);
}
else if (direction == 2)
{
return new Rectangle(transform.x - 5, transform.y + 5, 5, 40);
}
else if (direction == 3)
{
return new Rectangle(transform.x + 5, transform.y + 50, 40, 5);
}
return new Rectangle(100000,100000,1,1);
}
public void shooting(Graphics g){
if(bullet != null)
{
bullet.move();
bullet.draw(g);
if(br.checkCollision(bullet.getX(), bullet.getY()))
{
bullet = null;
loaded = true;
}
if(bullet.transform.y < 1
|| bullet.transform.y > 580
|| bullet.transform.x < 1
|| bullet.transform.x > 630)
{
bullet = null;
loaded = true;
}
}
}
public void Move(int dir, int speed){
if (direction == dir){
switch(direction){
case 0:
if(transform.x < 600 && !br.isColliding(colRectangle())) //Main.Screen.x)
transform.x += speed;
break;
case 1:
if(transform.y > speed - 5 && !br.isColliding(colRectangle()))
transform.y -= speed;
break;
case 2:
if(transform.x > speed - 5 && !br.isColliding(colRectangle()))
transform.x -= speed;
break;
case 3:
if(transform.y < Main.Screen.y - 80 && !br.isColliding(colRectangle()))
transform.y += speed;
break;
}
} else {
direction = dir;
}
}
public void updateDir(){
String imgPath = imagePath + type + "\\" + String.valueOf(direction) + ".png";
tankImage = new ImageIcon(imgPath);
}
public Tank(String imgPath, int dir, int x, int y, int hp,
String type, BrickGenerator br,
int shootKey, int MoveKeys[])
{
imagePath = imgPath;
direction = dir;
this.br = br;
this.type = type;
this.hp = hp;
transform.setPos(x, y);
//ListenThread thread = new ListenThread(shootKey, MoveKeys, this);
//thread.start();
//tankListener = thread.tankListener();
tankListener = new TankListener(shootKey, MoveKeys, this);
updateDir();
}
}