Exemple #1
0
 private void updatePosition(Projectile projectile) {
   double distance = projectile.getRadius() * Context.TICK_DELAY / projectile.getLifeTime();
   double angle = projectile.getAngle() * Math.PI / 180;
   double y = distance * Math.sin(angle);
   double x = distance * Math.cos(angle);
   projectile.setxStart(projectile.getxStart() + x);
   projectile.setyStart(projectile.getyStart() + y);
 }
Exemple #2
0
  private synchronized void calculateInstantImpacts(
      Person shooter, Projectile projectile, Room room) {

    double xStart = projectile.getxStart();
    double yStart = projectile.getyStart();

    Person closestPerson = null;
    Point closestPoint = null;

    for (AbstractZone zone : room.getMap().getZones()) {
      if (zone.isShootable()) {
        continue;
      }
      Bounds zoneBounds = GeometryService.getRectangle(zone);
      Point[] zoneIntersection =
          LineService.lineBoundsIntersections(
              new Line(
                  new Point(shooter.getX(), shooter.getY()),
                  new Point(projectile.getxEnd(), projectile.getyEnd())),
              zoneBounds);
      Point closest =
          isMoreClose(
              (int) projectile.getxStart(),
              (int) projectile.getyStart(),
              new Point(projectile.getxEnd(), projectile.getyEnd()),
              zoneIntersection);
      if (closest != null) {
        closestPoint = closest;
        projectile.setxEnd((int) closest.getX());
        projectile.setyEnd((int) closest.getY());
      }
    }

    for (Person person : room.getPersons().values()) {
      Point linePointB = new Point((double) projectile.getxEnd(), (double) projectile.getyEnd());
      Point[] intersectionPoints =
          LineService.lineIntersectCircle(
              new Line(new Point(xStart, yStart), linePointB),
              new Circle(person.getX(), person.getY(), PersonWebSocketEndpoint.PERSON_RADIUS));

      if (shooter == person) {
        projectile.setxStart((int) intersectionPoints[0].getX());
        projectile.setyStart((int) intersectionPoints[0].getY());
        continue;
      }

      if (intersectionPoints != null && intersectionPoints.length > 0) {
        Point closest =
            isMoreClose(
                (int) shooter.getX(), (int) shooter.getY(), closestPoint, intersectionPoints);
        if (closest != null) {
          if (projectile.isPiercing()) {
            onDamage(shooter, projectile.getDamage(), person, room);
          } else {
            closestPoint = closest;
            closestPerson = person;
          }
        }
      }
    }

    if (closestPerson != null) {
      onDamage(shooter, projectile.getDamage(), closestPerson, room);
    }
    if (closestPoint != null) {
      projectile.setxEnd((int) closestPoint.getX());
      projectile.setyEnd((int) closestPoint.getY());
    }
  }