Luninariel

Monsters - Attack

Dec 6th, 2018
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. package edu.missouriwestern.cpozo.csc254.monsters;
  2.  
  3.  
  4. import java.time.LocalTime;
  5.  
  6. public class Attack {
  7.  
  8. /**
  9. *
  10. * An attack requires both the attacker and the defender to be alive.
  11. * If one or both are dead, then a "No Attack" message is returned.
  12. *
  13. * A random number between 0.0 <= 0.0 < 1.0. If this number is less than the attacker's aggressiveness, then
  14. * an attack occurs.
  15. * If the attack occurs the strength of the attack is calculated using the strength and stamina of the attacker.
  16. * If no attack occurs the ccreature's attack message is returned.
  17. *
  18. */
  19.  
  20. public static boolean isNight(){
  21. boolean night = false;
  22. LocalTime time = LocalTime.now();
  23. int currentHour = time.getHour();
  24. night = (currentHour <6 || currentHour > 18);
  25. return night;
  26. }
  27.  
  28. private static String normalAttack(Entity attacker, Entity defender){
  29. String message = "Nothing So far";
  30. if(attacker.isDead() || defender.isDead()){
  31. message = "One or both players are dead. No attack";
  32. }else {
  33. boolean attack = Math.random() < attacker.getAggressiveness();
  34. if (attack) {
  35. double force = attacker.getStrength() * Math.random();
  36. double damage = force / defender.getStamina();
  37. defender.subtractHealth(damage);
  38. message = String.format("%s %d %s with %d%% force and doing %d%% damage",
  39. attacker.symbol, attacker.getId(), attacker.getAttackMessage(), (int)Math.round(force*100),
  40. (int)Math.round(damage*100));
  41. }else{
  42. message = String.format("%s %d %s", attacker.symbol,attacker.getId(), attacker.getPassiveMessage());
  43. }
  44. }
  45. return message;
  46. }
  47.  
  48.  
  49.  
  50.  
  51. public static String attack(Entity attacker, Entity defender) {
  52.  
  53. if(isNight()==true && (defender instanceof Nocturnal)){
  54. if(Math.random()>0.5){
  55. return normalAttack(attacker,defender);
  56. }
  57. else
  58. return String.format("%s %d has hidden", defender.getSymbol(),defender.getId());
  59. }
  60.  
  61. if (isNight()==false && ( attacker instanceof Nocturnal)){
  62. return String.format("%s",attacker.getPassiveMessage());
  63. }
  64. if(isNight()==true){
  65. return normalAttack(attacker, defender);
  66. }
  67. return normalAttack(attacker, defender);
  68. }
  69.  
  70. /**
  71. * This method has player1 attack player 2, and then player 2 attack player 1.
  72. *
  73. * @param player1
  74. * @param player2
  75. * @param round This is an integer that indicates round number. It is normally an incremented variable.
  76. *
  77. * There is no return value
  78. */
  79.  
  80. public static void round(Entity player1, Entity player2, int round){
  81. System.out.println("\nRound " + round);
  82. System.out.println(Attack.attack(player1, player2));
  83. System.out.println(Attack.attack(player2, player1));
  84. }
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment