-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMissile.java
More file actions
135 lines (117 loc) · 3.72 KB
/
Copy pathMissile.java
File metadata and controls
135 lines (117 loc) · 3.72 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
package com.Tank;
import java.awt.*;
import java.util.List;
/**
* 一个类的注释
* @author yandan
*
*/
public class Missile {
private static final int XSPEED = 20;
private static final int YSPEED = 20;
private static final int MISSILEWIDTH = 10 ;
private static final int MISSILEHEIGHT = 10;
int x , y;
private boolean live = true;
private boolean good;
Tank.Direction dir;
private TankClient tc;
public Missile(int x, int y, boolean good,Tank.Direction dir) {
this.x = x;
this.y = y;
this.dir = dir;
this.good=good;
}
public Missile(int x,int y,boolean good,Tank.Direction dir,TankClient tc) {
this(x,y,good,dir);
this.tc = tc;
}
public void draw(Graphics g) {
if(!live) {
tc.missiles.remove(this);
return;
}
Color c = g.getColor();
if(good)
g.setColor(Color.RED);
else
g.setColor(Color.BLACK);
g.fillOval(x-MISSILEWIDTH/2, y-MISSILEHEIGHT/2, MISSILEWIDTH,MISSILEHEIGHT);
g.setColor(c);
move();
}
private void move() {
switch (dir) {
case L:
x-=XSPEED;
break;
case LU:
x-=XSPEED;
y-=YSPEED;
break;
case U:
y-=YSPEED;
break;
case RU:
x+=XSPEED;
y-=YSPEED;
break;
case R:
x+=XSPEED;
break;
case RD:
x+=XSPEED;
y+=YSPEED;
break;
case D:
y+=YSPEED;
break;
case LD:
x-=XSPEED;
y+=YSPEED;
break;
}
if(x<0 || y<0 || x> TankClient.GAME_WIDTH || y>TankClient.GAME_HEIGHT) {
live = false;
}
}
public Rectangle getRect() {
return new Rectangle(x,y,MISSILEWIDTH,MISSILEHEIGHT);
}
//碰撞有待完善,这个只是两个矩形的碰撞
public boolean hitTank(Tank t) {
if(this.getRect().intersects(t.getRect()) && t.isLive() && this.good!=t.isGood() && this.live) {
if(t.isGood()) {
t.setLife(t.getLife() - 20);
if(t.getLife()<=0)
t.setLive(false);
}
else {
t.setLive(false);
}
this.live=false;
Explode e = new Explode(x,y,tc);
tc.explodes.add(e);
return true;
}
return false;
}
public boolean hitTanks(List<Tank> tanks) {
for(int i=0; i<tanks.size(); i++) {
if(hitTank(tanks.get(i)))
//tanks.remove(i);
return true;
}
return false;
}
public boolean isLive() {
return live;
}
public boolean hitWall(Wall w) {
if(this.live && this.getRect().intersects(w.getRect())) {
this.live = false;
return true;
}
return false;
}
}