-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMissile.java
More file actions
199 lines (174 loc) · 3.57 KB
/
Copy pathMissile.java
File metadata and controls
199 lines (174 loc) · 3.57 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
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
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.List;
/**
* 代表子弹的类
* @author kannshanlei
*
*/
public class Missile {
/**
* 子弹x方向的速度
*/
public static final int XSPEED = 10;
/**
* 子弹y方向的速度
*/
public static final int YSPEED = 10;
/**
* 子弹的宽度
*/
public static final int WIDTH = 10;
/**
* 子弹的高度
*/
public static final int HEIGHT = 10;
private static int ID = 1;
TankClient tc;
int tankId;
int id;
int x, y;
Dir dir = Dir.R;
boolean live = true;
boolean good;
/**
* 根据位置等属性构造子弹
* @param tankId 所属坦克的id号(用于网络版)
* @param x 子弹产生的x坐标
* @param y 子弹产生的y坐标
* @param good 子弹的立场是好还是坏
* @param dir 子弹的方向
* @see Dir
*/
public Missile(int tankId, int x, int y, boolean good, Dir dir) {
this.tankId = tankId;
this.x = x;
this.y = y;
this.good = good;
this.dir = dir;
this.id = ID++;
}
/**
* 根据位置和TankClient构建子弹
* @param tankId
* @param x
* @param y
* @param good
* @param dir
* @param tc 子弹构建的场所
* @see TankClient
*/
public Missile(int tankId, int x, int y, boolean good, Dir dir,
TankClient tc) {
this(tankId, x, y, good, dir);
this.tc = tc;
}
/**
* 画出子弹
* @param g 画笔
*/
public void draw(Graphics g) {
if (!live) {
tc.missiles.remove(this);
return;
}
Color c = g.getColor();
g.setColor(Color.BLACK);
g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(c);
move();
}
private void move() {
switch (dir) {
case L:
x -= XSPEED;
break;
case U:
y -= YSPEED;
break;
case R:
x += XSPEED;
break;
case D:
y += YSPEED;
break;
case STOP:
break;
}
if (x < 0 || y < 0 || x > TankClient.GAME_WIDTH
|| y > TankClient.GAME_HEIGHT) {
live = false;
}
}
/**
* 取得子弹的外切方形
* @return 子弹的外切Rectangle
*/
public Rectangle getRect() {
return new Rectangle(x, y, WIDTH, HEIGHT);
}
/**
* 检测子弹是否撞到坦克
* @param t 被检测的坦克
* @return 如果撞到返回true,否则返回false
*/
public boolean hitTank(Tank t) {
if (this.live && t.isLive() && this.good != t.good
&& this.getRect().intersects(t.getRect())) {
this.live = false;
t.setLive(false);
tc.explodes.add(new Explode(x, y, tc));
return true;
}
return false;
}
/**
* 检测是否撞到一系列坦克中的一个
* @param tanks 被检测的坦克序列
* @return 如果撞到其中一个,返回true,否则返回false
*/
public boolean hitTanks(List<Tank> tanks) {
for (int i = 0; i < tanks.size(); i++) {
if (this.hitTank(tanks.get(i))) {
return true;
}
}
return false;
}
/**
* 检测是否击中老窝,击中返回true
* @param home 老窝
* @return
*/
public boolean hitHome(Home home){
if(this.live&&home.live&&this.getRect().intersects(home.getRect())){
this.live = false;
home.setLive(false);
tc.explodes.add(new Explode(x, y, tc));
return true;
}
return false;
}
public boolean hitWall(Wall w) {
if (this.live &&w.live && this.getRect().intersects(w.getRect())) {
this.live = false;
w.live = false;
return true;
}
return false;
}
/**
* 检测两个子弹是否相撞
* @param m 被检测的子弹
* @return
*/
public boolean hitMissile(Missile m) {
if (m.id!=this.id&&this.live &&m.live && this.getRect().intersects(m.getRect())) {
this.live = false;
m.live = false;
return true;
}
return false;
}
}