//class? Tankimport java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.Random;public class Tank {
int id;
public static final int XSPEED = 5;
public static final int YSPEED = 5;
public static final int WIDTH = 30;
public static final int HEIGHT = 30;
private boolean live = true;
private static Random r = new Random();//定义为静态的, 大家伙都来用这一个
TankClient tc;
boolean good; int x,y;
boolean BU, BD, BR, BL;
Direction dir = Direction.STOP;
Direction ptDir = Direction.D;
private int step = r.nextInt(12) + 3;
public Tank(int x, int y, boolean good) {
this.x = x;
this.y = y;
this.good = good;
}
public Tank(int x, int y, boolean good, Direction dir,TankClient tc){
this(x,y,good);
this.dir = dir;
this.tc = tc;
} public void draw(Graphics g){
if(!live){
if(!good){
tc.tanks.remove(this);
}
}
Color c = g.getColor();
if(good)g.setColor(Color.red);
else g.setColor(Color.BLUE);
g.fillOval(x, y, WIDTH, HEIGHT);
g.drawString("id:"+ id, x, y-10);
g.setColor(c);
switch(ptDir){
case L:
g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x, y + Tank.HEIGHT/2);
break;
case LU:
g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x, y);
break;
case U:
g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH/2, y);
break;
case RU:
g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH,y);
break;
case R:
g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH, y + Tank.HEIGHT/2);
break;
case RD:
g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH, y + Tank.HEIGHT);
break;
case D:
g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH/2, y + Tank.HEIGHT);
break;
case LD:
g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x, y + Tank.HEIGHT);
break;
}
move();
}
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;
case STOP:
break;
}
if(dir != Direction.STOP){
ptDir = dir;
}
if(x<0) x = 0;
if(y<30) y = 30;
if(x + Tank.WIDTH > TankClient.GAME_WIDTH) x = TankClient.GAME_WIDTH - Tank.WIDTH;
if(y + Tank.HEIGHT > TankClient.GAME_HEIGHT) y = TankClient.GAME_HEIGHT - Tank.HEIGHT;
}
public void keyPressed(KeyEvent e){
int key = e.getKeyCode(); switch(key){
case KeyEvent.VK_RIGHT:
BR = true;
break;
case KeyEvent.VK_LEFT:
BL = true;
break;
case KeyEvent.VK_UP:
BU = true;
break;
case KeyEvent.VK_DOWN:
BD = true;
break;
}
locateDirection();
}
void locateDirection(){
Direction oldDir = this.dir;
if(BL && !BU && !BR && !BD) dir = Direction.L;
else if(BL && BU && !BR && !BD) dir = Direction.LU;
else if(!BL && BU && !BR && !BD) dir = Direction.U;
else if(!BL && BU && BR && !BD) dir = Direction.RU;
else if(!BL && !BU && BR && !BD) dir = Direction.R;
else if(!BL && !BU && BR && BD) dir = Direction.RD;
else if(!BL && !BU && !BR && BD) dir = Direction.D;
else if(BL && !BU && !BR && BD) dir = Direction.LD;
else if(!BL && !BU && !BR && !BD) dir = Direction.STOP;
if(dir != oldDir){
TankMoveMsg msg = new TankMoveMsg(id,x,y,dir,ptDir);
tc.nc.send(msg);
}
} public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
switch(key){
case KeyEvent.VK_CONTROL:
fire();
break;
case KeyEvent.VK_LEFT:
BL = false;
break;
case KeyEvent.VK_RIGHT:
BR = false;
break;
case KeyEvent.VK_UP:
BU = false;
break;
case KeyEvent.VK_DOWN:
BD = false;
break;
}
locateDirection();
}
public Missile fire(){
if(!live) return null;
int x = this.x + Tank.WIDTH/2 - Missile.WIDTH/2;
int y = this.y + Tank.HEIGHT/2 - Missile.HEIGHT/2;
Missile m = new Missile(id,x,y,this.good,this.ptDir,this.tc);
tc.missile.add(m);
MissileNewMsg msg = new MissileNewMsg(m);
tc.nc.send(msg);
return m;
}
public Rectangle getRect(){;
return new Rectangle(x,y,WIDTH,HEIGHT);
}
public boolean isLive() {
return live;
} public void setLive(boolean live) {
this.live = live;
}
}//class Missileimport java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.List;public class Missile {
public static final int XSPEED = 10;
public static final int YSPEED = 10; public static final int WIDTH = 10;
public static final int HEIGHT = 10;
private static int ID = 1;
int x,y;
Direction dir;
private TankClient tc;
int tankId;
int id;
boolean live = true; boolean good;
public Missile(int tankId,int x, int y,boolean good, Direction dir) {
this.tankId = tankId;
this.x = x;
this.y = y;
this.good = good;
this.dir = dir;
this.id = ID++;
}
public Missile(int tankId,int x, int y, boolean good, Direction dir, TankClient tc) {
this.tankId = tankId;
this.x = x;
this.y = y;
this.good = good;
this.dir = dir;
this.tc = tc;
}
public void draw(Graphics g){
if(!live){
tc.missile.remove(this);
return;
}
Color c = g.getColor();
g.setColor(Color.BLACK);
g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(c);
move();
}
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,WIDTH,HEIGHT);
}
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;
}
public boolean hitTanks(List<Tank> tanks){
for(int i = 0; i<tanks.size(); i++){
if(this.hitTank(tanks.get(i))){
return true;
}
}
return false;
}
public boolean isLive() {
return live;
}
}//class? Explodeimport java.awt.Color;
import java.awt.Graphics;
public class Explode {
int x,y;
private boolean live = true;
private TankClient tc;
private int[] diameter = {4,7,12,18,26,32,49,30,14,6};
int step = 0;
public Explode(int x, int y, TankClient tc){
this.x = x;
this.y = y;
this.tc = tc;
}
public void draw(Graphics g){
if(!live){
tc.explodes.remove(this);
return;
}
Color c = g.getColor();
g.setColor(Color.black);
g.fillOval(x, y, diameter[step], diameter[step]);
g.setColor(c);
step++;
if(step == diameter.length){
live = false;
step = 0;
return;
}
}}
//class enumpublic enum Direction {
U,RU,R,RD,D,LD,L,LU,STOP
}
//class? Msgimport java.io.DataInputStream;
import java.net.DatagramSocket;
public interface Msg {
public static final int TANK_NEW_MSG = 1; public static final int TANK__MOVE_MSG = 2;
public static final int MISSILE_NEW_MSG = 3;
public static final int TANK_DEAD_MSG = 4;
public static final int MISSILE_DEAD_MSG = 5;
public void send(DatagramSocket ds, String IP, int udpPort);
public void parse(DataInputStream dis);
}
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.Random;public class Tank {
int id;
public static final int XSPEED = 5;
public static final int YSPEED = 5;
public static final int WIDTH = 30;
public static final int HEIGHT = 30;
private boolean live = true;
private static Random r = new Random();//定义为静态的, 大家伙都来用这一个
TankClient tc;
boolean good; int x,y;
boolean BU, BD, BR, BL;
Direction dir = Direction.STOP;
Direction ptDir = Direction.D;
private int step = r.nextInt(12) + 3;
public Tank(int x, int y, boolean good) {
this.x = x;
this.y = y;
this.good = good;
}
public Tank(int x, int y, boolean good, Direction dir,TankClient tc){
this(x,y,good);
this.dir = dir;
this.tc = tc;
} public void draw(Graphics g){
if(!live){
if(!good){
tc.tanks.remove(this);
}
}
Color c = g.getColor();
if(good)g.setColor(Color.red);
else g.setColor(Color.BLUE);
g.fillOval(x, y, WIDTH, HEIGHT);
g.drawString("id:"+ id, x, y-10);
g.setColor(c);
switch(ptDir){
case L:
g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x, y + Tank.HEIGHT/2);
break;
case LU:
g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x, y);
break;
case U:
g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH/2, y);
break;
case RU:
g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH,y);
break;
case R:
g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH, y + Tank.HEIGHT/2);
break;
case RD:
g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH, y + Tank.HEIGHT);
break;
case D:
g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH/2, y + Tank.HEIGHT);
break;
case LD:
g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x, y + Tank.HEIGHT);
break;
}
move();
}
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;
case STOP:
break;
}
if(dir != Direction.STOP){
ptDir = dir;
}
if(x<0) x = 0;
if(y<30) y = 30;
if(x + Tank.WIDTH > TankClient.GAME_WIDTH) x = TankClient.GAME_WIDTH - Tank.WIDTH;
if(y + Tank.HEIGHT > TankClient.GAME_HEIGHT) y = TankClient.GAME_HEIGHT - Tank.HEIGHT;
}
public void keyPressed(KeyEvent e){
int key = e.getKeyCode(); switch(key){
case KeyEvent.VK_RIGHT:
BR = true;
break;
case KeyEvent.VK_LEFT:
BL = true;
break;
case KeyEvent.VK_UP:
BU = true;
break;
case KeyEvent.VK_DOWN:
BD = true;
break;
}
locateDirection();
}
void locateDirection(){
Direction oldDir = this.dir;
if(BL && !BU && !BR && !BD) dir = Direction.L;
else if(BL && BU && !BR && !BD) dir = Direction.LU;
else if(!BL && BU && !BR && !BD) dir = Direction.U;
else if(!BL && BU && BR && !BD) dir = Direction.RU;
else if(!BL && !BU && BR && !BD) dir = Direction.R;
else if(!BL && !BU && BR && BD) dir = Direction.RD;
else if(!BL && !BU && !BR && BD) dir = Direction.D;
else if(BL && !BU && !BR && BD) dir = Direction.LD;
else if(!BL && !BU && !BR && !BD) dir = Direction.STOP;
if(dir != oldDir){
TankMoveMsg msg = new TankMoveMsg(id,x,y,dir,ptDir);
tc.nc.send(msg);
}
} public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
switch(key){
case KeyEvent.VK_CONTROL:
fire();
break;
case KeyEvent.VK_LEFT:
BL = false;
break;
case KeyEvent.VK_RIGHT:
BR = false;
break;
case KeyEvent.VK_UP:
BU = false;
break;
case KeyEvent.VK_DOWN:
BD = false;
break;
}
locateDirection();
}
public Missile fire(){
if(!live) return null;
int x = this.x + Tank.WIDTH/2 - Missile.WIDTH/2;
int y = this.y + Tank.HEIGHT/2 - Missile.HEIGHT/2;
Missile m = new Missile(id,x,y,this.good,this.ptDir,this.tc);
tc.missile.add(m);
MissileNewMsg msg = new MissileNewMsg(m);
tc.nc.send(msg);
return m;
}
public Rectangle getRect(){;
return new Rectangle(x,y,WIDTH,HEIGHT);
}
public boolean isLive() {
return live;
} public void setLive(boolean live) {
this.live = live;
}
}//class Missileimport java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.List;public class Missile {
public static final int XSPEED = 10;
public static final int YSPEED = 10; public static final int WIDTH = 10;
public static final int HEIGHT = 10;
private static int ID = 1;
int x,y;
Direction dir;
private TankClient tc;
int tankId;
int id;
boolean live = true; boolean good;
public Missile(int tankId,int x, int y,boolean good, Direction dir) {
this.tankId = tankId;
this.x = x;
this.y = y;
this.good = good;
this.dir = dir;
this.id = ID++;
}
public Missile(int tankId,int x, int y, boolean good, Direction dir, TankClient tc) {
this.tankId = tankId;
this.x = x;
this.y = y;
this.good = good;
this.dir = dir;
this.tc = tc;
}
public void draw(Graphics g){
if(!live){
tc.missile.remove(this);
return;
}
Color c = g.getColor();
g.setColor(Color.BLACK);
g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(c);
move();
}
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,WIDTH,HEIGHT);
}
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;
}
public boolean hitTanks(List<Tank> tanks){
for(int i = 0; i<tanks.size(); i++){
if(this.hitTank(tanks.get(i))){
return true;
}
}
return false;
}
public boolean isLive() {
return live;
}
}//class? Explodeimport java.awt.Color;
import java.awt.Graphics;
public class Explode {
int x,y;
private boolean live = true;
private TankClient tc;
private int[] diameter = {4,7,12,18,26,32,49,30,14,6};
int step = 0;
public Explode(int x, int y, TankClient tc){
this.x = x;
this.y = y;
this.tc = tc;
}
public void draw(Graphics g){
if(!live){
tc.explodes.remove(this);
return;
}
Color c = g.getColor();
g.setColor(Color.black);
g.fillOval(x, y, diameter[step], diameter[step]);
g.setColor(c);
step++;
if(step == diameter.length){
live = false;
step = 0;
return;
}
}}
//class enumpublic enum Direction {
U,RU,R,RD,D,LD,L,LU,STOP
}
//class? Msgimport java.io.DataInputStream;
import java.net.DatagramSocket;
public interface Msg {
public static final int TANK_NEW_MSG = 1; public static final int TANK__MOVE_MSG = 2;
public static final int MISSILE_NEW_MSG = 3;
public static final int TANK_DEAD_MSG = 4;
public static final int MISSILE_DEAD_MSG = 5;
public void send(DatagramSocket ds, String IP, int udpPort);
public void parse(DataInputStream dis);
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;public class NetClient {
TankClient tc;
private int udpPort;
String IP;
DatagramSocket ds = null;
public NetClient(TankClient tc){
this.tc = tc;
}
public void connect(String IP, int port){
this.IP = IP;
try {
ds = new DatagramSocket(udpPort);
} catch (SocketException e) {
e.printStackTrace();
}
Socket s = null;
try {
s = new Socket(IP,port);
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeInt(udpPort);
DataInputStream dis = new DataInputStream(s.getInputStream());
int id = dis.readInt();
tc.myTank.id = id;
if(id%2 == 0){
tc.myTank.good = false;
}else{
tc.myTank.good = true;
}
System.out.println("Connected to Server");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(s != null){
try {
s.close();
s = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
TankNewMsg msg = new TankNewMsg(tc.myTank);
send(msg);
new Thread(new UDPRecvThread()).start();
}
public void send(Msg msg){
msg.send(ds, IP, TankServer.UDP_PORT);
}
private class UDPRecvThread implements Runnable{
byte[] buf = new byte[1024];
public void run() {
while(ds != null){
DatagramPacket dp = new DatagramPacket(buf, buf.length);
try {
ds.receive(dp);
parse(dp);
System.out.println("a package receive from server");
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void parse(DatagramPacket dp) {
ByteArrayInputStream bais = new ByteArrayInputStream(buf,0,dp.getLength());
DataInputStream dis = new DataInputStream(bais);
int msgType = 0;
try {
msgType = dis.readInt();
} catch (IOException e) {
e.printStackTrace();
}
Msg msg = null;
switch(msgType){
case Msg.TANK_NEW_MSG:
msg = new TankNewMsg(NetClient.this.tc);
msg.parse(dis);
break;
case Msg.TANK__MOVE_MSG:
msg = new TankMoveMsg(NetClient.this.tc);
msg.parse(dis);
break;
case Msg.MISSILE_NEW_MSG:
msg = new MissileNewMsg(NetClient.this.tc);
msg.parse(dis);
break;
case Msg.TANK_DEAD_MSG:
msg = new TankDeadMsg(NetClient.this.tc);
msg.parse(dis);
break;
case Msg.MISSILE_DEAD_MSG:
msg = new MissileDeadMsg(NetClient.this.tc);
msg.parse(dis);
break;
}
}
}
public int getUdpPort() {
return udpPort;
} public void setUdpPort(int udpPort) {
this.udpPort = udpPort;
}
}//class ?MissileNewMsgimport 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 MissileNewMsg implements Msg {
int msgType = Msg.MISSILE_NEW_MSG;
TankClient tc;
Missile m;
public MissileNewMsg(Missile m){
this.m = m;
}
public MissileNewMsg(TankClient tc){
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(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();
}
} 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();
Direction dir = Direction.values()[dis.readInt()];
boolean good = dis.readBoolean();
Missile m = new Missile(tankId,x,y,good,dir,tc);
m.id = id;
tc.missile.add(m);
} catch (IOException e) {
e.printStackTrace();
}
}}//class? MissileDeadMsgimport 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 MissileDeadMsg implements Msg {
int msgType = Msg.TANK_DEAD_MSG;
TankClient tc;
int tankId;
int id;
public MissileDeadMsg(int tankId,int id){
this.tankId = tankId;
this.id = id;
}
public MissileDeadMsg(TankClient tc){
this.tc = tc;
}
public void parse(DataInputStream dis) {
try {
int tankId = dis.readInt();
int id = dis.readInt();
if(tc.myTank.id == tankId){
return;
}
for(int i = 0; i<tc.missile.size(); i++){
Missile m = tc.missile.get(i);
if(m.tankId == tankId && m.id == id){
m.live = false;
tc.explodes.add(new Explode(m.x, m.y, tc));
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} } public void send(DatagramSocket ds, String IP, int udpPort) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try {
dos.writeInt(msgType);
dos.writeInt(tankId);
dos.writeInt(id);
} 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();
} }
}//class TankNewMsgimport 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 TankNewMsg implements Msg{
int msgType = Msg.TANK_NEW_MSG;
Tank tank;
TankClient tc;
public TankNewMsg(Tank tank){
this.tank = tank;
}
public TankNewMsg(TankClient tc){
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(tank.id);
dos.writeInt(tank.x);
dos.writeInt(tank.y);
dos.writeInt(tank.dir.ordinal());
dos.writeBoolean(tank.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();
}
}
public void parse(DataInputStream dis) {
try {
int id = dis.readInt();
if(tc.myTank.id == id){
return;
}
int x = dis.readInt();
int y = dis.readInt();
Direction dir = Direction.values()[dis.readInt()];
boolean good = dis.readBoolean();
//System.out.println("id: " + id + "-x: " + x + "-y: " + y + "-dir: " +dir+ "-good " +good);
boolean exist = false;
for(int i = 0; i<tc.tanks.size(); i++){
Tank t = tc.tanks.get(i);
if(t.id == id){
exist = true;
break;
}
}
if(!exist){
TankNewMsg tnMsg = new TankNewMsg(tc.myTank);
tc.nc.send(tnMsg);
Tank t = new Tank(x, y, good, dir, tc);
t.id = id;
tc.tanks.add(t);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}//class ?TankDeadMsgimport 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 TankDeadMsg implements Msg {
int msgType = Msg.TANK_DEAD_MSG;
TankClient tc;
int id;
public TankDeadMsg(int id){
this.id = id;
}
public TankDeadMsg(TankClient tc){
this.tc = tc;
}
public void parse(DataInputStream dis) {
try {
int id = dis.readInt();
if(tc.myTank.id == id){
return;
}
for(int i = 0; i<tc.tanks.size(); i++){
Tank t = tc.tanks.get(i);
if(t.id == id){
t.setLive(false);;
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} } public void send(DatagramSocket ds, String IP, int udpPort) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try {
dos.writeInt(msgType);
dos.writeInt(id);
} 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();
} }
}
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 TankMoveMsg implements Msg{
int msgType = Msg.TANK__MOVE_MSG;
int x,y;
int id;
Direction ptDir;
Direction dir;
TankClient tc;
public TankMoveMsg(int id, int x, int y,Direction dir,Direction ptDir) {
this.id = id;
this.x = x;
this.y = y;
this.dir = dir;
this.ptDir = ptDir;
}
public TankMoveMsg(TankClient tc){
this.tc = tc;
}
public void parse(DataInputStream dis) {
try {
int id = dis.readInt();
if(tc.myTank.id == id){
return;
}
int x = dis.readInt();
int y = dis.readInt();
Direction dir = Direction.values()[dis.readInt()];
Direction ptDir = Direction.values()[dis.readInt()];
//System.out.println("id: " + id + "-x: " + x + "-y: " + y + "-dir: " +dir+ "-good " +good);
boolean exist = false;
for(int i = 0; i<tc.tanks.size(); i++){
Tank t = tc.tanks.get(i);
if(t.id == id){
t.x = x;
t.y = y;
t.dir = dir;
t.ptDir = ptDir;
exist = true;
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
} public void send(DatagramSocket ds, String IP, int udpPort) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try {
dos.writeInt(msgType);
dos.writeInt(id);
dos.writeInt(x);
dos.writeInt(y);
dos.writeInt(dir.ordinal());
dos.writeInt(ptDir.ordinal());
} 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();
}
}
}