Exemplo n.º 1
0
  /**
   * Performs a gun shot and deals damage if necessary
   *
   * @param player the player who is shooting
   * @param intersectionInfoList any intersections along the bullet path
   */
  public void handleGunShot(Player player, ArrayList<EntityIntersectionInfo> intersectionInfoList) {

    if (intersectionInfoList != null) {

      // Find nearest intersection to the player
      Entity nearestIntersectingEntity = null;
      float nearestIntersectionDistance = 0.0f;

      for (EntityIntersectionInfo info : intersectionInfoList) {

        // Ignore intersections with players on your team
        if (info.getEntity() instanceof Player) {

          Player _player = (Player) info.getEntity();

          if (player.getTeam().getID() == _player.getTeam().getID()) continue;
        }

        // Check intersection points
        for (Vector2f p : info.getIntersectionPoints()) {

          // Calculate distance between intersection point and player
          Vector2f dif = new Vector2f();
          Vector2f.sub(player.getPosition(), p, dif);

          float len = dif.length();

          // Update nearest intersection entity
          if (nearestIntersectionDistance == 0.0f || len < nearestIntersectionDistance) {

            nearestIntersectionDistance = len;
            nearestIntersectingEntity = info.getEntity();
          }
        }
      }

      // Check if nearest intersecting entity is player and do damage if so
      if (nearestIntersectingEntity != null) {

        if (nearestIntersectingEntity instanceof Player) {
          Player hitPlayer = (Player) nearestIntersectingEntity;

          // System.out.println("hit player");

          if (hitPlayer.getTeam().getID() != player.getTeam().getID()) {

            // TODO: move damage into method or constant
            hitPlayer.doDamage(player.getWeaponDamage());
          }
        }
      }
    }
  }
Exemplo n.º 2
0
 public static void truncate(Vector2f vector, float max) {
   float length = vector.length();
   if (length == 0) return;
   float i = max / length;
   vector.scale(i < 1. ? 1.f : i);
 }