📄 newitemmsg.java
字号:
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;
public class NewItemMsg implements Msg {
int msgType = Msg.NEW_ITEM_MSG;
TankClient tc;
private Item item;
public NewItemMsg(TankClient tc,Item item) {
this.item=item;
this.tc = tc;
}
public void send(DatagramSocket ds, String IP, int udpPort) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try {
dos.writeInt(msgType);
dos.writeInt(tc.tank.getTankId());
dos.writeInt(item.x);
dos.writeInt(item.y);
dos.writeInt(item.style);
dos.writeBoolean(item.live);
dos.writeInt(item.step);
} 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();
}
}
public void parse(DataInputStream dis) {
System.out.println("客户端接收一份服务端发来NewItemMsg的数据!");
try {
int tankId = dis.readInt();
if(tankId==tc.tank.getTankId()) return;
int x= dis.readInt();
int y = dis.readInt();
int style = dis.readInt();
boolean live = dis.readBoolean();
int step = dis.readInt();
Item item = new Item(x,y,tc);
item.style=style;
item.step=step;
item.live=live;
tc.items.add(item);
} catch (IOException e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -