Advertisement
Blagojche

Hero.js

Nov 20th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function NPC(name, hitpoints) {
  2.     this.name = name;
  3.     this.hitpoints = hitpoints;
  4. }
  5. function Hero(name) {
  6.     NPC.call(this, name, 100);
  7.     this.damage = 10;
  8.     this.critical = false;
  9. }
  10.  
  11. Hero.prototype.attack = function (defender) {
  12.  
  13.     if (defender instanceof Hero && defender.critical === true && this instanceof Hero) {
  14.         defender.hitpoints -= 15;
  15.  
  16.         if (defender.hitpoints > 0) {
  17.             defender.critical = true;
  18.             this.critical = false;
  19.         } else {
  20.             this.critical = false;
  21.         }
  22.  
  23.     } else {
  24.         defender.hitpoints -= 10;
  25.     }
  26.     console.log(this.name + " attacked " + defender.name + ". Defender hitpoints = " + defender.hitpoints);
  27. };
  28.  
  29. Hero.prototype.status = function () {
  30.     return this.name + " " + this.hitpoints;
  31. };
  32.  
  33. //Creating list of characters
  34. let N = Math.floor(Math.random() * 41) + 10;
  35. let characters = [];
  36. for (let i = 0; i < N; i++) {
  37.     let random = Math.floor(Math.random() * 2) + 1;
  38.     random === 1 ? characters.push(new Hero("Hero_" + i)) : characters.push(new NPC("NPC_" + i, 100));
  39. }
  40. //end of Creating list of characters
  41.  
  42. //Choosing random Hero player for critical damage
  43. while (true) {
  44.     let random = Math.floor(Math.random() * characters.length);
  45.     if (characters[random] instanceof Hero) {
  46.         characters[random].critical = true;
  47.         break;
  48.     }
  49. }
  50. console.log(characters);
  51. //end of Choosing random Hero player for critical damage
  52.  
  53. //Battle of titans
  54. let go = true;
  55. function battle(characters) {
  56.     while (go) {
  57.         for (let i = 0; i < characters.length; i++) {
  58.  
  59.             while (true) {
  60.                 var random = Math.floor(Math.random() * characters.length);
  61.                 if (random !== i) {
  62.                     break;
  63.                 }
  64.             }
  65.  
  66.             if (characters[i] instanceof Hero) {
  67.                 characters[i].attack(characters[random]);
  68.                 if (characters[random].hitpoints <= 0) {
  69.                     characters.splice(random, 1);
  70.                 }
  71.                 console.log(characters[i].status());
  72.             }
  73.  
  74.             if (characters.length === 1) {
  75.                 console.log("Winner: " + characters[0].name + ", hitpoints: " + characters[0].hitpoints + "!");
  76.                 go = false;
  77.                 break;
  78.             }
  79.         }
  80.     }
  81. }
  82. //end of Battle of titans
  83. battle(characters);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement