Kame3

NPC

Nov 19th, 2019 (edited)
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2.     <head>
  3.         <script type="text/javascript">
  4.             function npc(name,hitpoints){
  5.                 this.name = name;
  6.                 this.hitpoints = hitpoints;
  7.             }
  8.  
  9.             function hero(name){
  10.                 npc.call(this,name,100);
  11.                 this.damage = 10;
  12.                 this.critical = false;
  13.             }
  14.  
  15.             // hero.prototype = Object.create(NPC.prototype);
  16.             // hero.prototype.constructor = hero;
  17.  
  18.             hero.prototype.attack = function(otherHero, attackerIndex, defenderIndex, arrayLength){
  19.                 // if(otherHero == undefined){
  20.                 //  console.log("Attacker index: " + attackerIndex + " Defender index: " + defenderIndex + " Array Length: "+ arrayLength);
  21.                 // } else {
  22.  
  23.                     otherHero.hitpoints -= this.critical ? this.damage*1.5 : this.damage;
  24.                     console.log(this.name + " attacker " + otherHero.name + " with " + (this.critical ? this.damage*1.5 : this.damage) + " dmg");
  25.  
  26.                     if(this.critical && otherHero instanceof hero){
  27.                         otherHero.critical = true;
  28.                         this.critical = false;
  29.                     }  
  30.                 // }
  31.             };
  32.  
  33.             npc.prototype.status = function(){
  34.                 console.log("name: " + this.name + " hitpoits: " + this.hitpoits);
  35.             };
  36.  
  37.             function randomNumber(min,max,notThis){
  38.                 min = min || 0;
  39.                 var randm = Math.round(Math.random()*max-1);
  40.                 if(randm === notThis || randm < 0 || randm < min || randm > max){
  41.                     return randomNumber(min,max,notThis)
  42.                 }
  43.                 return randm;
  44.             }
  45.  
  46.             var poleObj = [];
  47.             var randomNu = randomNumber(10,50,-1); //random brojki od 10 - 50
  48.             console.log(randomNu);
  49.  
  50.             for(var i =0;i<randomNu;i++){
  51.                 if(randomNumber(0,2,-1) == 0){
  52.                     poleObj.push(new hero("hero_" + i));
  53.                 } else {
  54.                     poleObj.push(new npc("npc_" + i, 100));
  55.                 }
  56.             }
  57.             console.log(poleObj);
  58.  
  59.             poleObj[randomNumber(0,poleObj.length,-1)].critical = true;
  60.  
  61.             while(poleObj.length > 1){
  62.                 var attackerIndex = randomNumber(0,poleObj.length, -1);
  63.                 var defenderIndex = randomNumber(0,poleObj.length, attackerIndex);
  64.                 if(poleObj[attackerIndex] instanceof hero){
  65.                     poleObj[attackerIndex].attack(poleObj[defenderIndex], attackerIndex, defenderIndex, poleObj.length);
  66.                     if(poleObj[defenderIndex].hitpoits <= 0){
  67.                         poleObj.splice(defenderIndex,1);
  68.                     }
  69.                 }
  70.             }
  71.  
  72.             console.log("The winner is: " + poleObj[0].name + " and has: " + poleObj[0].hitpoits + " hitpoints.");
  73.  
  74.  
  75.         </script>
  76.     </head>
  77.     <body>
  78.     </body>
  79. </html>
Add Comment
Please, Sign In to add comment