コード例 #1
0
ファイル: Projectile.java プロジェクト: TeamEandN/Worms-part2
  /**
   * sets the worm overlapping with the current Projectile
   *
   * @param position the position at which this projectile currenlty is
   * @effect if the distance between this projectile and a worm is smaller than the sum of the
   *     radius of the worm and the radius of the projectile |for each worm in
   *     this.getWorld().getAllWorm(): | if (position.calculateDistance(worm.getPosition()) <
   *     worm.getRadius()+ this.getRadius()) | then this.setOverlappingWorm(worm)
   */
  public void setOverlapsWorm(Position position) {
    List<Worm> worms = this.getWorld().getAllWorms();
    for (Worm worm : worms) {

      if (position.calculateDistance(worm.getPosition()) < worm.getRadius() + this.getRadius()) {
        this.setOverlappingWorm(worm);
      }
    }
  }
コード例 #2
0
  /**
   * Changes the positions of the projectile as a result of a jump from the current position.
   *
   * @post If the projectile hits a worm this worm loses some hit points and the projectile is
   *     removed from the world and set non active. | if ((Math.sqrt(Math.pow(w.getXpos()-tempXpos,
   *     2)+Math.pow(w.getYpos()-tempYpos, 2))< maxDistance)) | while (this.getActive == true) |
   *     then (new.setHitPoints(worm.getHitPoints-this.getDamage()) | then (world.getProjectile() ==
   *     null) | then (new.getActive == false))
   * @post If the projectile doesn't hit a worm, the projectile will be deleted when it leaves the
   *     map or when it hits impassable terrain. | if !((Math.sqrt(Math.pow(w.getXpos()-tempXpos,
   *     2)+Math.pow(w.getYpos()-tempYpos, 2))< maxDistance)) | then ( if
   *     ((isOutOfTheMap(tempXpos,tempYpos))) | then ((world.getProjectile() == null) | then
   *     (new.getActive == false)) | (else if (world.isImpassable(tempXpos, tempYpos,
   *     this.getRadius()))) | then ((world.getProjectile() == null) | then (new.getActive ==
   *     false)))
   * @throws IllegalStateException If the projectile can't jump the exception is thrown. |!
   *     canJump()
   */
  @Raw
  public void jump(Double timeStep) throws IllegalStateException {
    if (this.canJump()) {
      World world = this.getWorld();
      double tempXpos = this.getXpos();
      double tempYpos = this.getYpos();
      double t = 0;
      while ((world.isPassable(tempXpos, tempYpos, this.getRadius()))) {

        tempXpos = this.jumpStep(t)[0];
        tempYpos = this.jumpStep(t)[1];

        Collection<Worm> collection = (world.getWorms());

        for (Worm w : collection) {
          Worm overlappingWorm = null;
          double maxDistance = this.getRadius() + w.getRadius();

          if (!(w == world.getCurrentWorm())
              && (Math.sqrt(
                      Math.pow(w.getXpos() - tempXpos, 2) + Math.pow(w.getYpos() - tempYpos, 2))
                  < maxDistance)) {

            overlappingWorm = w;
            while ((this.getActive() == true)) {
              overlappingWorm.setHitPoints(overlappingWorm.getHitPoints() - this.getDamage());
              this.deleteProjectile(world);
              this.setActive(false);
            }

          } else {
            overlappingWorm = null;
            if ((isOutOfTheMap(tempXpos, tempYpos))) {
              this.deleteProjectile(world);
              this.setActive(false);
            } else if (((world.isImpassable(tempXpos, tempYpos, this.getRadius())))
                && (this.getActive() == true)) {
              this.deleteProjectile(world);
              this.setActive(false);
            }
          }
        }
        t += timeStep;
      }
    } else {
      throw new IllegalStateException();
    }
  }
コード例 #3
0
 /**
  * Sets the radius of the worm who shot a projectile.
  *
  * @param worm The worm that shot the projectile.
  */
 @Raw
 private void setRadiusWorm(Worm worm) {
   this.wormRadius = worm.getRadius();
 }