예제 #1
0
 public void update() {
   runTasks();
   if (networkManager != null) {
     long time = System.currentTimeMillis();
     float lastDelta2;
     delta = (time - lastTickTime) / 8.0F;
     lastTickTime = time;
     if (!lagging
         && networkManager.waitingForPingReturn
         && time - networkManager.lastPacketTime > 420) {
       lagging = true;
     }
     if (lagging) {
       lagMultiplier *= 0.85F;
       if (lagMultiplier < 0.01F) {
         lagMultiplier = 0.01F;
       }
     } else {
       if (lagMultiplier < 1.0F) {
         lagMultiplier += 0.05F;
         if (lagMultiplier >= 1.0F) {
           lagMultiplier = 1.0F;
         }
       }
     }
     if (delta > 120) {
       delta = 120;
     }
     delta *= lagMultiplier;
     errorTime *= lagMultiplier;
     lastTicks = ticks;
     ticks += delta;
     lastDelta = (float) (Math.floor(ticks) - Math.floor(lastTicks));
     lastTicks2 = ticks2;
     ticks2 += delta * 2;
     lastDelta2 = (float) (Math.floor(ticks2) - Math.floor(lastTicks2));
     if (globalAlpha < 1.0F) {
       globalAlpha += 0.0075F * delta;
       if (globalAlpha > 1.0F) {
         globalAlpha = 1.0F;
       }
     } else if (globalAlpha > 0.0F) {
       globalAlpha -= 0.0075F * delta;
       if (globalAlpha < 0.0F) {
         globalAlpha = 0.0F;
       }
     }
     if (qsm > 1.0F) {
       qsm -= 0.00004F * delta;
       if (qsm < 1.0F) {
         qsm = 1.0F;
       }
     } else if (qsm < MAX_QSM) {
       qsm += 0.00004F;
       if (qsm > MAX_QSM) {
         qsm = MAX_QSM;
       }
     }
     if (player != null) {
       if (!networkManager.waitingForPingReturn) {
         if (time - networkManager.lastPingSendTime > 250) {
           networkManager.lastPingSendTime = time;
           networkManager.ping();
         }
       }
       errorTime *= Math.pow(0.993, lastDelta);
       if (allowUserInput) {
         controller.update(this);
         float targetAngle = controller.getTargetAngle();
         targetAngle %= PI_2;
         if (targetAngle < 0) {
           targetAngle += PI_2;
         }
         if (targetAngle != lastSendAngle || lastSendAngleTime == 0) {
           if (time - lastSendAngleTime > 100) {
             lastSendAngle = targetAngle;
             networkManager.send(new MessageSetAngle(targetAngle));
           }
         }
         player.accelerating = controller.shouldAccelerate();
         if (time - lastAccelerateUpdateTime > 150) {
           if (player.accelerating != player.wasAccelerating) {
             lastAccelerateUpdateTime = time;
             networkManager.send(new MessageAccelerate(player.accelerating));
             player.wasAccelerating = player.accelerating;
           }
         }
       }
       Iterator<Entity> entityIter = entityIterator();
       while (entityIter.hasNext()) {
         Entity entity = entityIter.next();
         if (entity.updateBase(delta, lastDelta, lastDelta2)) {
           entityIter.remove();
         }
       }
     }
   } else {
     ticks++;
   }
 }
예제 #2
0
  @Override
  public void run() {
    double delta = 0;
    long previousTime = System.nanoTime();
    long timer = System.currentTimeMillis();
    int ups = 0;
    double nanoUpdates = 1000000000.0 / 30.0;

    GL11.glEnable(GL11.GL_TEXTURE_2D);
    GL11.glShadeModel(GL11.GL_SMOOTH);
    GL11.glDisable(GL11.GL_DEPTH_TEST);
    GL11.glDisable(GL11.GL_LIGHTING);

    GL11.glClearColor(0.0F, 0.0F, 0.0F, 0.0F);
    GL11.glClearDepth(1);

    GL11.glEnable(GL11.GL_BLEND);
    GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

    setupDisplay();

    boolean doResize = false;

    while (!Display.isCloseRequested()) {
      if (Display.wasResized() && doResize) {
        setupDisplay();
      }
      doResize = true;

      long currentTime = System.nanoTime();
      double currentTickDelta = (currentTime - previousTime) / nanoUpdates;
      delta += currentTickDelta;
      frameDelta = (frameDelta + currentTickDelta) % 1.0;
      previousTime = currentTime;

      while (delta >= 1) {
        update();
        renderHandler.update();
        delta--;
        ups++;
      }

      GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
      GL11.glPushMatrix();

      GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);

      renderHandler.render();

      fps++;

      if (System.currentTimeMillis() - timer > 1000) {
        int bytesPerSecond = 0;
        int packetsPerSecond = 0;
        if (networkManager != null) {
          bytesPerSecond = networkManager.bytesPerSecond;
          packetsPerSecond = networkManager.packetsPerSecond;
          networkManager.bytesPerSecond = 0;
          networkManager.packetsPerSecond = 0;
        }
        Display.setTitle(
            "Slyther - FPS: "
                + fps
                + " - UPS: "
                + ups
                + " - BPS: "
                + bytesPerSecond
                + " - PPS: "
                + packetsPerSecond);
        fps = 0;

        timer += 1000;
        ups = 0;
      }

      GL11.glPopMatrix();
      Display.sync(60);
      Display.update();
    }
    if (networkManager != null && networkManager.isOpen()) {
      networkManager.closeConnection(ClientNetworkManager.SHUTDOWN_CODE, "");
    }
    try {
      ConfigHandler.INSTANCE.saveConfig(CONFIGURATION_FILE, configuration);
    } catch (IOException e) {
      Log.error("Failed to save config");
      Log.catching(e);
    }
    Display.destroy();
  }