コード例 #1
0
ファイル: Battle.java プロジェクト: gcherr/Adventure
  public Battle(Player p, Monster m) {
    while (p.getHealth() < 0 && m.getHealth() < 0) {
      System.out.println(">What will you do?");
      String s = scan.next();

      if (s.equals("fight")) {
        p.attack(m);
        p.takeDamage(m.getDamage());
        m.attack(p);
        m.takeDamage(p.getDamage());
      }
    }
  }
コード例 #2
0
  public void actionPerformed(ActionEvent a) {
    Object source = a.getSource();

    if (scene == OVERWORLD) {
      if (source == moveTimer) {
        if (moveUp && !moveDown) player.moveUp();
        else if (!moveUp && moveDown) player.moveDown();

        if (moveLeft && !moveRight) player.moveLeft();
        else if (!moveLeft && moveRight) player.moveRight();
      }

      if (source == itemsButton) itemsButton.setText("ITEMS!");
      else if (source == equipButton) equipButton.setText("EQUIPMENT!");
      else if (source == skillsButton) skillsButton.setText("SKILLS");
      else if (source == settingsButton) settingsButton.setText("SETTINGS");
      else if (source == statusButton) statusButton.setText("STATUS");
      else if (source == dataButton) dataButton.setText("DATA");
    }
    if (scene == BATTLE) {
      if (source == moveTimer) {
        // Spell Movement
        for (int spellNum = 0; spellNum < spellsThrown.size(); spellNum++) {
          Spell spell = spellsThrown.get(spellNum);
          spell.move();
          if (spell.spellHit()) {
            enemies.get(spell.getTarget()).takeDamage(spell.getDamage());
            enemies.get(spell.getTarget()).statusPresent(spell.getEffect());
            spellsThrown.remove(spellNum);
            checkEnemyPresence();
          }
        }

        for (int enemyLoop = 0; enemyLoop < enemies.size(); enemyLoop++) {
          Enemy enemy = enemies.get(enemyLoop);
          for (int spellNum = 0; spellNum < enemy.spellsThrown.size(); spellNum++) {
            Spell spell = enemy.spellsThrown.get(spellNum);
            spell.move();
            if (spell.spellHit()) {
              player.takeDamage(spell.getDamage());
              enemy.spellsThrown.remove(spellNum);
              checkAllyPresence();
            }
          }
        }
      }
      if (source == staminaTimer) {
        // Stamina Regeneration
        player.staminaGain(16);
        // Enemy Attacks
        int enemyLoop;
        for (enemyLoop = 0; enemyLoop < enemies.size(); enemyLoop++) {
          enemies.get(enemyLoop).attack(player.centerX(), player.centerY(), 12);
        }
      }
    }
    repaint();

    // To check collision
    if (scene == OVERWORLD) {
      for (int loop = 0; loop < enemies.size(); loop++) {
        Enemy enemy = enemies.get(loop);
        if (enemy.hitbox().intersects(player.hitbox())) {
          player.combatChange(true);
          enemy.combatChange(true);
          battleBegin();
        }
      }
    }
  }
コード例 #3
0
ファイル: Level.java プロジェクト: Cambis/SWEN222_Game
 /**
  * Handles a damage packet from the server. The method then gives the appropriate amount of damage
  * to the appropriate player.
  *
  * @param username - username of player affected
  * @param damage - amount of damage to deal
  */
 public void handleDamage(String username, double damage) {
   Player player = getPlayer(username);
   if (player.isAlive()) player.takeDamage(damage);
   else if (!player.isAlive()) game.getRenderer().deleteModel(username);
 }
コード例 #4
0
ファイル: PhysObject.java プロジェクト: aaronmjacobs/Inertia
  /**
   * Handles hard physical collisions
   *
   * @param other The object to check collisions against
   * @return 1 if there was a collision, -1 otherwise
   */
  protected int handleCollisions(PhysObject other) {
    int code = -1;

    Vec2 newPosition = position.add(velocity.multiply(Globals.timeStep));

    // If the new coords cause a collision
    if (this.circularIntersects(other, newPosition)) {
      code = 1;
      Vec2 fromOther = this.position.subtract(other.position);
      double massRatio = other.mass / this.mass;

      // If they were to our left and heading to the right
      // or if they were to our right and heading to the left
      if ((fromOther.x > 0 && other.velocity.x > 0) || (fromOther.x < 0 && other.velocity.x < 0)) {
        // Add their x velocity to ours

        // If we are moving in the same direction, don't apply the ratio
        if ((this.velocity.x > 0 && other.velocity.x > 0)
            || (this.velocity.x < 0 && other.velocity.x < 0)) {
          this.velocity.x += other.velocity.x;
        } else
        // If we are moving in different directions, apply the ratio
        {
          this.velocity.x += other.velocity.x * massRatio;
        }
      }
      // If they were to our left and heading to the left
      // or if they were to our right and heading to the right
      else {
        // Bounce
        this.velocity.x = -(this.velocity.x / 4);
      }

      // If they were above us and heading down
      // or if they were below us and heading up
      if ((fromOther.y > 0 && other.velocity.y > 0) || (fromOther.y < 0 && other.velocity.y < 0)) {
        // Add their x velocity to ours

        // If we are moving in the same direction, don't apply the ratio
        if ((this.velocity.y > 0 && other.velocity.y > 0)
            || (this.velocity.y < 0 && other.velocity.y < 0)) {
          this.velocity.y += other.velocity.y;
        } else
        // If we are moving in different directions, apply the ratio
        {
          this.velocity.y += other.velocity.y * massRatio;
        }
      }
      // If they were above us and heading up
      // or if they were below us and heading down
      else {
        // Bounce
        this.velocity.y = -(this.velocity.y / 4);
      }

      if (this.getClass() == Player.class) {
        ((Player) this).takeDamage(other.mass);
      } else if (this.getClass() == Enemy.class) {
        ((Enemy) this).takeDamage(other.mass);
      }
    }

    return code;
  }
コード例 #5
0
ファイル: Monster.java プロジェクト: Jillbore/grid-hack
 // The damage taken is random
 public void attack(GridActor target) {
   Player.takeDamage(1);
 }
コード例 #6
0
  /**
   * Method to actually do the calculations of the attack. First grab the dice that were used, do
   * the computations against the operations presented and then deal out damage. Then if an opponent
   * dies, generate a new opponent or if there is none go to the reward screen.
   *
   * @param view view of the application
   */
  public void attackClicked(View view) {
    currentElement = "Light";
    // Generate all of the stuff we need to calculate damage
    // Create the list of the dice in the first row
    ArrayList<Die> firstRow = new ArrayList<Die>();
    // Create the list of dice in the second row
    ArrayList<Die> secondRow = new ArrayList<Die>();
    // Find which dice was used in the first row first position
    ImageButton firstRowFirstDice = (ImageButton) findViewById(R.id.firstRowFirstDice);
    // Find which dice was used in the first row second position
    ImageButton firstRowSecondDice = (ImageButton) findViewById(R.id.firstRowSecondDice);
    // Find which dice was used in the second row first slot
    ImageButton secondRowFirstDice = (ImageButton) findViewById(R.id.secondRowFirstDice);
    // Find which dice was used in the second row second slot
    ImageButton secondRowSecondDice = (ImageButton) findViewById(R.id.secondRowSecondDice);
    if ((firstRowFirstDice.getTag() == null)
        || (firstRowSecondDice.getTag() == null)
        || (secondRowFirstDice.getTag() == null)
        || (secondRowSecondDice.getTag() == null)) {
      Intent healthLow = new Intent(this, HealthTooLowActivity.class);
      healthLow.putExtra("player", player);
      healthLow.putExtra("dice", true);
      startActivityForResult(healthLow, 200);
    } else {

      // Get the value of the dice as a string
      String frfd = firstRowFirstDice.getTag().toString();
      String frsd = firstRowSecondDice.getTag().toString();
      String srfd = secondRowFirstDice.getTag().toString();
      String srsd = secondRowSecondDice.getTag().toString();
      // Find the text view for the first answer
      TextView firstRowAns = (TextView) findViewById(R.id.firstRowFirstAns);
      // Find the text view for the second answer
      TextView secondRowAns = (TextView) findViewById(R.id.secondRowAns);
      TextView firstRowSecondOp = (TextView) findViewById(R.id.firstRowSecondOp);
      TextView secondRowSecondOp = (TextView) findViewById(R.id.secondRowSecondOp);
      boolean isDead = false;
      diceUsed = player.getDiceUsed();
      playerProgressBar = (ProgressBar) findViewById(R.id.playerProgressBar);
      opProgressBar = (ProgressBar) findViewById(R.id.oppProgressBar);
      generateOps = false;
      TextView firstRowFirstOp = (TextView) findViewById(R.id.firstRowFirstOp);
      TextView secondRowFirstOp = (TextView) findViewById(R.id.secondRowFirstOp);
      int firstRowFirstDiceRoll = 0;
      int firstRowSecondDiceRoll = 0;
      int secondRowFirstDiceRoll = 0;
      int secondRowSecondDiceRoll = 0;

      // Loop through the dice used array list to figure out which dice was used in which row
      for (int i = 0; i < diceUsed.size(); i++) {
        // If the dice selected equals the string value of the first row first position dice and the
        // first row array list is less than 2, add it to the first row list
        if (diceUsed.get(i).getDiceType().equals(frfd) && firstRow.size() < 2) {
          firstRow.add(diceUsed.get(i));
          // Else if the dice value is equal to the first row second position and the first row
          // array list is less than 2, add it to the first row list
        } else if (diceUsed.get(i).getDiceType().equals(frsd) && firstRow.size() < 2) {
          firstRow.add(diceUsed.get(i));
          // Else if the dice value is equal to the second row first dice and that list is less than
          // two, add it to the second row list
        } else if (diceUsed.get(i).getDiceType().equals(srfd) && secondRow.size() < 2) {
          secondRow.add(diceUsed.get(i));
          // Else if the dice value is equal to the second row second dice and that list is less
          // than two, add it to the second row list
        } else if (diceUsed.get(i).getDiceType().equals(srsd) && secondRow.size() < 2) {
          secondRow.add(diceUsed.get(i));
        }
      }
      int firstRowTotal = 0;
      int secondRowTotal = 0;

      // For every dice in the first row list, have them roll dice and add it to the total
      for (Die d : firstRow) {
        if (firstRow.indexOf(d) == 0) {
          firstRowFirstDiceRoll = d.rollDice();
          firstRowTotal += firstRowFirstDiceRoll;
        } else {
          if (firstRowFirstOp.getText().equals("+")) {
            firstRowSecondDiceRoll = d.rollDice();
            firstRowTotal += firstRowSecondDiceRoll;
          } else if (firstRowFirstOp.getText().equals("-")) {
            firstRowSecondDiceRoll = d.rollDice();
            firstRowTotal = firstRowTotal - firstRowSecondDiceRoll;

          } else if (firstRowFirstOp.getText().equals("*")) {
            firstRowSecondDiceRoll = d.rollDice();
            firstRowTotal = firstRowTotal * firstRowSecondDiceRoll;
          } else {
            firstRowSecondDiceRoll = d.rollDice();
            firstRowTotal = firstRowTotal / firstRowSecondDiceRoll;
          }
        }
      }
      // For every dice in the second row list, have them roll dice and add it to the total
      for (Die d : secondRow) {
        if (secondRow.indexOf(d) == 0) {
          secondRowFirstDiceRoll = d.rollDice();
          secondRowTotal += secondRowFirstDiceRoll;
        } else {
          if (secondRowFirstOp.getText().equals("+")) {
            secondRowSecondDiceRoll = d.rollDice();
            secondRowTotal += secondRowSecondDiceRoll;
          } else if (secondRowFirstOp.getText().equals("-")) {
            secondRowSecondDiceRoll = d.rollDice();
            secondRowTotal = secondRowTotal - secondRowSecondDiceRoll;

          } else if (secondRowFirstOp.getText().equals("*")) {
            secondRowSecondDiceRoll = d.rollDice();
            secondRowTotal = secondRowTotal * secondRowSecondDiceRoll;
          } else {
            secondRowSecondDiceRoll = d.rollDice();
            secondRowTotal = secondRowTotal / secondRowSecondDiceRoll;
          }
        }
      }
      // If the selected element is fire, add + 1 to each row
      if (currentElement.equals("Fire")) {
        firstRowTotal++;
        secondRowTotal++;
      }
      Log.i("firstrowdm", "first row damage : " + firstRowTotal);
      Log.i("firstrowdmg", "second rwo damage : " + secondRowTotal);
      if (firstRowTotal < 0) {
        firstRowTotal = 0;
      }
      if (secondRowTotal < 0) {
        secondRowTotal = 0;
      }
      this.player.updateMaxSingleDamage(firstRowTotal);
      this.player.updateMaxSingleDamage(secondRowTotal);
      this.player.updateMaxTotalDamage(firstRowTotal + secondRowTotal);
      int firstRowAttack = 0;
      int secondRowAttack = 0;
      String firstRowUser = "";
      String secondRowUser = "";
      double totalRolls = 0;
      double totalHits = 0;
      boolean isDeadOnFirstRoll = false;
      // Now check damage calulations
      // If the first row total from the dice is greater or equal to the answer
      if (firstRowTotal >= Integer.parseInt(firstRowAns.getText().toString())) {
        // do attack,
        firstRowAttack = firstRowTotal - Integer.parseInt(firstRowAns.getText().toString());
        currOpponet.takeDamage((int) (firstRowAttack * player.getDamageRate()));
        firstRowUser = "******";
        // recalc health,
        int gj2 = currOpponet.getCurrentHealth();
        opHealth.setText(currOpponet.getName() + " HP: " + currOpponet.getCurrentHealth());
        opProgressBar.setProgress(currOpponet.getPercentHealthLeft());
        this.player.updateTotalHits();
        this.player.updateTotalRolls();
        totalRolls++;
        totalHits++;
        // Check op health if dead rewards screen,
        if (currOpponet.getCurrentHealth() <= 0) {
          player.swapDiceBackToInv();
          opponents.remove(0);
          generateOps = true;
          isDeadOnFirstRoll = true;
          isDead = true;
          this.player.updateHighestAcc(totalHits / totalRolls * 100);
          generateResults(
              firstRowFirstDiceRoll,
              firstRowSecondDiceRoll,
              secondRowFirstDiceRoll,
              secondRowSecondDiceRoll,
              firstRowFirstOp.getText().toString(),
              secondRowFirstOp.getText().toString(),
              firstRowSecondOp.getText().toString(),
              secondRowSecondOp.getText().toString(),
              firstRowAns.getText().toString(),
              secondRowAns.getText().toString(),
              firstRowAttack,
              firstRowUser,
              secondRowAttack,
              secondRowUser,
              isDeadOnFirstRoll,
              isDead,
              currOpponet.getAttack());
          generateNextOpponent();
        }
        // Else if it is less than the answer
      } else {
        // do enemy attack,
        firstRowAttack = Integer.parseInt(firstRowAns.getText().toString()) - firstRowTotal;
        player.takeDamage(firstRowAttack + currOpponet.getAttack());
        firstRowUser = "******";
        // recalc health,
        playerHealth.setText("Player Health: " + player.getCurrentHealth());
        int d = player.getPercentHealthLeft();
        playerProgressBar.setProgress(player.getPercentHealthLeft());
        this.player.updateTotalRolls();
        totalRolls++;
      }

      // IF the second row total from the dice is greater or equal to the answer
      if ((secondRowTotal >= Integer.parseInt(secondRowAns.getText().toString()))
          && !(currOpponet.getCurrentHealth() < 0)
          && !(isDead)) {
        // do attack,
        secondRowAttack = secondRowTotal - Integer.parseInt(secondRowAns.getText().toString());
        currOpponet.takeDamage((int) (secondRowAttack * player.getDamageRate()));
        secondRowUser = "******";
        // recalc health,
        int o1 = currOpponet.getCurrentHealth();
        opHealth.setText(currOpponet.getName() + " HP: " + currOpponet.getCurrentHealth());
        opProgressBar.setProgress(currOpponet.getPercentHealthLeft());
        this.player.updateTotalHits();
        this.player.updateTotalRolls();
        totalRolls++;
        totalHits++;
        // Check op health if dead rewards screen,
        if (currOpponet.getCurrentHealth() <= 0) {
          player.swapDiceBackToInv();
          if (opponents.size() > 0) opponents.remove(0);
          generateOps = true;
          isDead = true;
          player.swapDiceBackToInv();
          generateResults(
              firstRowFirstDiceRoll,
              firstRowSecondDiceRoll,
              secondRowFirstDiceRoll,
              secondRowSecondDiceRoll,
              firstRowFirstOp.getText().toString(),
              secondRowFirstOp.getText().toString(),
              firstRowSecondOp.getText().toString(),
              secondRowSecondOp.getText().toString(),
              firstRowAns.getText().toString(),
              secondRowAns.getText().toString(),
              firstRowAttack,
              firstRowUser,
              secondRowAttack,
              secondRowUser,
              false,
              isDead,
              currOpponet.getAttack());
          generateNextOpponent();
        }
        // Else if it is less than the answer
      } else if (!(currOpponet.getCurrentHealth() < 0) && !(isDead)) {
        // do enemy attack,
        secondRowAttack = Integer.parseInt(secondRowAns.getText().toString()) - secondRowTotal;
        player.takeDamage(secondRowAttack + currOpponet.getAttack());
        secondRowUser = "******";
        // recalc health,
        playerHealth.setText("Player Health: " + player.getCurrentHealth());
        playerProgressBar.setProgress(player.getPercentHealthLeft());
        this.player.updateTotalRolls();
        totalRolls++;
      }

      // If at this point we have not gotten pushed to the rewards screen, lets see if the player is
      // dead
      if (player.getCurrentHealth() <= 0) {
        player.swapDiceBackToInv();
        generateResults(
            firstRowFirstDiceRoll,
            firstRowSecondDiceRoll,
            secondRowFirstDiceRoll,
            secondRowSecondDiceRoll,
            firstRowFirstOp.getText().toString(),
            secondRowFirstOp.getText().toString(),
            firstRowSecondOp.getText().toString(),
            secondRowSecondOp.getText().toString(),
            firstRowAns.getText().toString(),
            secondRowAns.getText().toString(),
            firstRowAttack,
            firstRowUser,
            secondRowAttack,
            secondRowUser,
            false,
            false,
            currOpponet.getAttack());
        generateDeathScreen();
      }
      // Else the player is alive and so is the monster, so reset the dice locations to null
      // Generate new answers
      // Generate new operations
      // Swap the dice from the players usedDice array to the inventory and keep fighting
      else if (generateOps == false) {
        // reset operations and dice
        player.swapDiceBackToInv();
        generateResults(
            firstRowFirstDiceRoll,
            firstRowSecondDiceRoll,
            secondRowFirstDiceRoll,
            secondRowSecondDiceRoll,
            firstRowFirstOp.getText().toString(),
            secondRowFirstOp.getText().toString(),
            firstRowSecondOp.getText().toString(),
            secondRowSecondOp.getText().toString(),
            firstRowAns.getText().toString(),
            secondRowAns.getText().toString(),
            firstRowAttack,
            firstRowUser,
            secondRowAttack,
            secondRowUser,
            false,
            false,
            currOpponet.getAttack());
        resetDicePics();
        generateOperations(currOpponet.getOp());
        generateAns(level);

        ImageButton frfdTag = (ImageButton) findViewById(R.id.firstRowFirstDice);
        frfdTag.setTag(null);
        ImageButton frsdTag = (ImageButton) findViewById(R.id.firstRowSecondDice);
        frsdTag.setTag(null);
        ImageButton srfdTag = (ImageButton) findViewById(R.id.secondRowFirstDice);
        srfdTag.setTag(null);
        ImageButton srsdTag = (ImageButton) findViewById(R.id.secondRowSecondDice);
        srsdTag.setTag(null);
      }
    }
  }