Пример #1
0
  private void fire(Person person, Room room) {
    AbstractWeapon gun = person.getWeapon();
    person.setShotCooldown(System.currentTimeMillis() + gun.getShotTimeout());

    gun.setTotalClip(gun.getTotalClip() - 1);
    gun.setCurrentClip(gun.getCurrentClip() - 1);

    Integer id = gameContext.getProjectilesIds().getAndIncrement();
    Projectile[] projectilesBatch = new Projectile[gun.getBulletsPerShot()];
    for (int i = 0; i < projectilesBatch.length; i++) {
      Projectile projectile = getCompatibleProjectile(person);
      projectile.setId(i);
      projectilesBatch[i] = projectile;

      float angle = projectile.getAngle();
      if (angle == 90) {
        projectile.setxEnd((int) person.getX());
        int gunLimit = (int) (projectile.getRadius() + person.getY());
        projectile.setyEnd(gunLimit > room.getMap().getY() ? room.getMap().getY() : gunLimit);
      } else if (angle == 180) {
        projectile.setyEnd((int) person.getY());
        int gunLimit = (int) person.getX() - (int) projectile.getRadius();
        projectile.setyEnd(gunLimit > 0 ? gunLimit : 0);
      } else if (angle == 270) {
        projectile.setxEnd((int) person.getX());
        int gunLimit = (int) person.getY() - (int) projectile.getRadius();
        projectile.setyEnd(gunLimit > 0 ? gunLimit : 0);
      } else if (angle == 0) {
        projectile.setyEnd((int) person.getY());
        int gunLimit = (int) person.getX() + (int) projectile.getRadius();
        projectile.setyEnd(gunLimit > room.getMap().getX() ? room.getMap().getX() : gunLimit);
      } else {
        double y = projectile.getRadius() * Math.sin(angle * Math.PI / 180);
        double x = projectile.getRadius() * Math.cos(angle * Math.PI / 180);
        projectile.setxEnd((int) x + (int) person.getX());
        projectile.setyEnd((int) y + (int) person.getY());
      }
      if (projectile.isInstant()) {
        calculateInstantImpacts(person, projectile, room);
      }
    }

    room.getProjectiles().put(id, projectilesBatch);
  }
Пример #2
0
  private void onRocketLifecycle(Room room, Rocket rocket, Integer rocketBatchId) {
    updatePosition(rocket);

    Circle rocketCircle =
        new Circle(rocket.getxStart(), rocket.getyStart(), PersonWebSocketEndpoint.ROCKET_RADIUS);
    Explosion explosion = null;
    if (rocket.getxStart() <= 0
        || rocket.getxStart() >= room.getMap().getX()
        || rocket.getyStart() <= 0
        || rocket.getyStart() >= room.getMap().getY()) {
      explosion = new Explosion((int) rocket.getxStart(), (int) rocket.getyStart());
    }
    if (explosion == null) {
      for (AbstractZone zone : room.getMap().getZones()) {
        if (!zone.isShootable()) {
          Point[] point =
              CircleService.circleBoundsIntersection(
                  rocketCircle,
                  new Bounds(zone.getX(), zone.getY(), zone.getWidth(), zone.getHeight()));
          if (point.length > 0) {
            explosion = new Explosion((int) point[0].getX(), (int) point[0].getY());
            break;
          }
        }
      }
    }
    if (explosion == null) {
      for (Person person : room.getPersons().values()) {
        if (person.getId() != rocket.getPersonId()) {
          Circle personCircle =
              new Circle(person.getX(), person.getY(), PersonWebSocketEndpoint.PERSON_RADIUS);
          Point[] point = CircleService.circleCircleIntersection(rocketCircle, personCircle);
          if (point.length > 0) {
            explosion = new Explosion((int) point[0].getX(), (int) point[0].getY());
            onDamage(room.getPersons().get(rocket.getPersonId()), rocket.getDamage(), person, room);
            break;
          }
        }
      }
    }

    if (explosion != null) {
      explosion.setxEnd((int) explosion.getxStart());
      explosion.setyEnd((int) explosion.getyStart());
      int id = gameContext.getProjectilesIds().getAndIncrement();
      room.getProjectiles().put(id, new Projectile[] {explosion});

      Circle explosionCircle =
          new Circle(explosion.getxStart(), explosion.getyStart(), explosion.getRadius());
      Person shooter = room.getPersons().get(rocket.getPersonId());

      for (Person person : room.getPersons().values()) {
        double damageFactor =
            PointService.pointBelongToCircle(
                new Point(person.getX(), person.getY()), explosionCircle);
        if (damageFactor > -1) {
          double damage = explosion.getDamage() + damageFactor * explosion.getDamageFactor();
          onDamage(shooter, (int) damage, person, room);
        }
      }
      room.getProjectiles().remove(rocketBatchId);
    }
  }