⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 autotanknewmsg.java

📁 基于尚学堂坦克大战的基础上开发,支持多人对战,在线聊天,坦克大战网络版.
💻 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 AutoTankNewMsg implements Msg {

	int msgType = Msg.AUTANK_NEW_MSG;

	Tank tank;

	TankClient tc;

	private int count = 0;

	public AutoTankNewMsg(Tank tank, TankClient tc, int count) {
		this.tank = tank;
		this.tc = tc;
		this.count = count;
	}

	public void send(DatagramSocket ds, String IP, int udpPort) {

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(baos);
		try {
			dos.writeInt(msgType);
			if (count == 0) {
				dos.writeInt(count);
			} else {
				dos.writeInt(-1);
			}
			System.out.println("发送前");
			for (int i = 0; i < tc.badTanks.size(); i++) {
				Tank badTank = tc.badTanks.get(i);
				dos.writeInt(tank.getTankId());
				dos.writeInt(tank.x);
				dos.writeInt(tank.y);
				dos.writeInt(tank.dir.ordinal());
				dos.writeBoolean(tank.good);
				dos.writeBoolean(tank.isAuto());
				System.out.println("发送第" + i + "个坏蛋包!!");
			}
			System.out.println("发送的size:" + tc.badTanks.size());

			byte[] buf = baos.toByteArray();

			DatagramPacket dp = new DatagramPacket(buf, buf.length,
					new InetSocketAddress(IP, udpPort));
			ds.send(dp);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void parse(DataInputStream dis) {
		try {

			if (tc.badTanks.size() > 0) {
				return;
			}
			int size = dis.readInt();
			if (size == -1)
				return;

			System.out.println("接收的size:" + size);
			for (int i = 0; i < size; i++) {

				int id = dis.readInt();
				System.out.println(id);
				if (tc.badTanks.size() > 0) {
					return;
				}

				int x = dis.readInt();
				int y = dis.readInt();
				Direction dir = Direction.values()[dis.readInt()];
				boolean good = dis.readBoolean();
				boolean auto = dis.readBoolean();
				// System.out.println("id:" + id + "-x:" + x + "-y:" + y +
				// "-dir:"
				// + dir + "-good:" + good);
				Tank newbadtank = new Tank(x, y, id, false, true, dir, tc);
				System.out.println(id);
				newbadtank.setTankId(id);
				tc.badTanks.add(newbadtank);
			}

		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -