Example #1
0
  /**
   * Updates the player based on the controls.
   *
   * @param time
   */
  public void update(double time) {
    double x = location.getX(), y = location.getY(), angle = bodyAngle;
    if (Input.getKey(Keyboard.KEY_W)) {
      location = new Vector2d(location, bodyAngle, time * DRIVE_SPEED_FOREWARD);
    }
    if (Input.getKey(Keyboard.KEY_A)) {
      bodyAngle -= TURNING_SPEED * time;
    }
    if (Input.getKey(Keyboard.KEY_S)) {
      location = new Vector2d(location, 180 + bodyAngle, time * DRIVE_SPEED_BACKWARD);
    }
    if (Input.getKey(Keyboard.KEY_D)) {
      bodyAngle += TURNING_SPEED * time;
    }

    if (Collision.isPlayerIntersectingMap(this)) {
      location = new Vector2d(x, y);
      bodyAngle = angle;
    }

    // gunAngle = new Vector2d(Input.getMousePosition()., y)

    Vector2d mouseLoc =
        Vector2d.add(
            Input.getMousePosition(),
            new Vector2d(location.getX() - Game.WIDTH / 2, location.getY() - Game.HEIGHT / 2));
    Vector2d relative = Vector2d.add(location.inverse(), mouseLoc);

    double mouseAngle = Math.toDegrees(Math.atan2(relative.getY(), relative.getX()));

    gunAngle = mouseAngle;

    if (Collision.isPlayerIntersectingBullet()) {
      kill();
    }

    if (Input.getKey(Keyboard.KEY_SPACE)
        && lastFireTime + FIRE_DELAY_MILS < System.currentTimeMillis()) {
      Game.getWorld()
          .getBullets()
          .add(
              new Bullet(
                      new Vector2d(
                          location, gunAngle, TankModel.GUN_LENGTH + TankModel.GUN_OFFSET_LENGTH),
                      gunAngle,
                      BULLET_SPEED)
                  .removeFromPlayer());
      lastFireTime = System.currentTimeMillis();
    }

    while (gunAngle < 0) {
      gunAngle += 360;
    }
    while (bodyAngle < 0) {
      bodyAngle += 360;
    }

    gunAngle %= 360;
    bodyAngle %= 360;
  }
Example #2
0
 public int getMapLocationY() {
   return (int) (location.getY() / Map.BLOCK_SIZE);
 }