コード例 #1
0
 private double distanceTo(Actor opponent) {
   double actorToMoveX = opponent.getAvatar().getTranslateX();
   double actorToMoveY = opponent.getAvatar().getTranslateY();
   double currentX = getAvatar().getTranslateX();
   double currentY = getAvatar().getTranslateY();
   double deltaX = actorToMoveX - currentX;
   double deltaY = actorToMoveY - currentY;
   double calculatedDistance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
   return calculatedDistance;
 }
コード例 #2
0
  /**
   * processes a single round of combat between two Actor objects: the <b>attacker</b> is this
   * object; the <b>defender</b> is received as an argument. This method is called by the
   * <b>attacker</b> <i>Actor</i> object. This <b>attacker</b> <i>Actor</i> object chooses another
   * <i>Actor</i> object as the <b>defender</b> by sending a reference-to the second <i>Actor</i>
   * object. When program execution arrives in <i>combatRound</i>, the method will have access to 2
   * sets of <i>Actor</i> attributes (a.k.a. instance fields). In particular, this method will need
   * to use <i>health</i> and <i>strength</i> to process a single round of combat. As an outcome of
   * the single round, both <i>Actor</i> objects: the <b>attacker</b> and the <b>defender</b> are
   * likely to loose some <i>health</i> value, but the <i>Actor</i> object with less <i>strength</i>
   * will likely incur more damage to their <i>health</i>. You access the <b>attacker</b> instance
   * fields (such as <i>health</i> using <i>this.health</i> and the <b>defender</b> instance fields
   * using <i>defender.health</i>. Of course, <i>defender</i> is the name of the stack-oriented
   * reference-to variable that is sent to the method.
   *
   * @param defender a reference to a different <i>Actor</i> object that will engage in combat with
   *     this <i>Actor</i> object.
   * @return <i>health</i> of the <i>Actor</i> following the combat round.
   */
  public double combatRound(Actor defender) {
    final double MAX_COMBAT_HEALTH_REDUCTION_OF_LOOSER =
        10.0; // health ranges 0.0 to 100.0, thus could loose 0.0 to 10.0
    final double MAX_COMBAT_HEALTH_REDUCTION_OF_WINNER = 3.0; // could loose 0.0 to 3.0
    double healthAdjustmentOfLooser =
        -(Math.random() * MAX_COMBAT_HEALTH_REDUCTION_OF_LOOSER)
            - 1.0; // looser looses at least 1.0
    double healthAdjustmentOfWinner =
        -(Math.random() * MAX_COMBAT_HEALTH_REDUCTION_OF_WINNER) + 1.0; // winner gains at least 1.0

    double proportionHitPoints =
        getHitPoints() / (getHitPoints() + defender.getHitPoints()); // between 0.0 and 1.0
    if (Math.random() > proportionHitPoints) {
      adjustHealth(healthAdjustmentOfLooser);
      defender.adjustHealth(healthAdjustmentOfWinner);
    } else {
      defender.adjustHealth(healthAdjustmentOfLooser);
      adjustHealth(healthAdjustmentOfWinner);
    }
    return getHealth();
  } // end combatRound()
コード例 #3
0
 public double getHitPoints() {
   return getStrength() + getHealth() * .5 * Math.random();
 }
コード例 #4
0
 /**
  * <i>Actor</i> regain health on each cycle of the simulation (and loose health in battles handled
  * by other code).
  */
 public void gameCycleHealthGain() {
   final double MAX_CYCLE_HEALTH_GAIN = 2.0;
   adjustHealth(Math.random() * MAX_CYCLE_HEALTH_GAIN);
 }