1
0

Add send and receive move methods

This commit is contained in:
Victor 2014-03-16 14:56:26 +02:00
parent 571b5f23bf
commit 061c7647c3
2 changed files with 24 additions and 1 deletions

View File

@ -8,7 +8,8 @@ public interface NetworkListener {
public static final int public static final int
ON_CONNECT = 1, ON_CONNECT = 1,
ON_SEED_RECEIVED = 2; ON_SEED_RECEIVED = 2,
ON_MOVE_RECEIVED = 3;
public void onStatusChanged(int status, Object data); public void onStatusChanged(int status, Object data);
} }

View File

@ -34,6 +34,9 @@ public class SocketHelper extends Thread {
case NetworkListener.ON_SEED_RECEIVED: case NetworkListener.ON_SEED_RECEIVED:
listener.onStatusChanged(status, receiveSeed()); listener.onStatusChanged(status, receiveSeed());
break; break;
case NetworkListener.ON_MOVE_RECEIVED:
listener.onStatusChanged(status, receiveMove());
break;
} }
} catch (IOException ex) { } } catch (IOException ex) { }
@ -54,6 +57,25 @@ public class SocketHelper extends Thread {
return dis.readLong(); return dis.readLong();
} }
public void sendMove(TurretInfo info) {
try {
dos.writeInt(NetworkListener.ON_MOVE_RECEIVED);
dos.writeDouble(info.barrelAngle);
dos.writeDouble(info.shotPower);
dos.writeInt(info.barrelX);
dos.writeInt(info.barrelY);
} catch (IOException ex) { }
}
private TurretInfo receiveMove() throws IOException {
TurretInfo t = new TurretInfo();
t.barrelAngle = dis.readDouble();
t.shotPower = dis.readDouble();
t.barrelX = dis.readInt();
t.barrelY = dis.readInt();
return t;
}
public void close() throws IOException { public void close() throws IOException {
if (dis != null) dis.close(); if (dis != null) dis.close();
if (dos != null) dos.close(); if (dos != null) dos.close();