Example #1
0
 /**
  * Sets the data
  *
  * @param buffer Bytebuffer to put data into
  */
 public void get(ByteBuffer buffer) {
   buffer.putInt((int) world.x);
   buffer.putInt((int) world.y);
   buffer.putDouble(radians);
   buffer.put((byte) (moving ? 1 : 0));
   for (Bullet b : gun.getBullets()) {
     if (b != null) {
       buffer.putInt((int) b.getWorld().x);
       buffer.putInt((int) b.getWorld().y);
       buffer.putDouble(b.getAngle());
     } else {
       buffer.putInt(-1);
       buffer.putInt(-1);
       buffer.putDouble(-1);
     }
   }
 }
Example #2
0
  void turnOnBulletReceiver() throws IOException {
    Global.log("Turning on bullet receiver...");
    while (true) {
      if (isDisposed()) throw new IOException("Client game disposed");
      Global.log("Connecting to port " + (Global.bulletPort()) + "...");
      try {
        bulletStream = new ClientByteStream(ip, Global.bulletPort(), Bullet.bufferSize());
        break;
      } catch (IOException ex) {
      }
    }
    Global.log("Connected!");
    timers.add(
        new FixedTimer(
            new FixedTask() {
              public boolean fixedRate() {
                return true;
              }

              public float FPS() {
                return Global.ReceiveFPS * 20;
              }

              public void run() {
                byte[] data = null;
                try {
                  data = bulletStream.read();
                } catch (IOException ex) {
                  System.err.println("Bullet receiver error: " + ex);
                  Global.onException();
                  stop();
                  return;
                }
                if (data == null) return;
                Bullet toSpawn = Bullet.fromBytes(data);
                Ship find = cShip.get(toSpawn.getFill());
                if (find == null) {
                  for (Ship s : turrets) {
                    if (s.fill.equals(toSpawn.getFill())) {
                      find = s;
                      break;
                    }
                  }
                }
                if (find == null) return;
                find.getBulletSet().add(toSpawn);
              }
            }));
  }