-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMissileNewMsg.java
97 lines (87 loc) · 2.17 KB
/
MissileNewMsg.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
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
/**
* 代表子弹产生的消息类
* @author kanshanlei
*
*/
public class MissileNewMsg implements Msg {
int msgType = Msg.MISSILE_NEW_MSG;
TankClient tc;
Missile m;
/**
* 根据子弹信息构建新的消息类
* @param m 产生消息的子弹
*/
public MissileNewMsg(Missile m) {
this.m = m;
}
/**
* 根据消息产生的场所构建新的消息
* @param tc
*/
public MissileNewMsg(TankClient tc) {
this.tc = tc;
}
/**
* 发送相关的消息
* @param ds 通过该socket发送数据
* @param IP 数据的目标IP
* @param udpPort 数据的目标端口
*/
public void send(DatagramSocket ds, String IP, int udpPort) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try {
dos.writeInt(msgType);
dos.writeInt(m.tankId);
dos.writeInt(m.id);
dos.writeInt(m.x);
dos.writeInt(m.y);
dos.writeInt(m.dir.ordinal());
dos.writeBoolean(m.good);
} catch (IOException e) {
e.printStackTrace();
}
byte[] buf = baos.toByteArray();
try {
DatagramPacket dp = new DatagramPacket(buf, buf.length,
new InetSocketAddress(IP, udpPort));
ds.send(dp);
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 分析接收到的消息数据
* @param dis 接收到的消息数据的输入流
*/
public void parse(DataInputStream dis) {
try {
int tankId = dis.readInt();
if (tankId == tc.myTank.id) {
return;
}
int id = dis.readInt();
int x = dis.readInt();
int y = dis.readInt();
Dir dir = Dir.values()[dis.readInt()];
boolean good = dis.readBoolean();
// System.out.println("id:" + id + "-x:" + x + "-y:" + y + "-dir:" +
// dir + "-good:" + good);
Missile m = new Missile(tankId, x, y, good, dir, tc);
m.id = id;
tc.missiles.add(m);
} catch (IOException e) {
e.printStackTrace();
}
}
}