Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <script type="text/javascript">
- function npc(name,hitpoints){
- this.name = name;
- this.hitpoints = hitpoints;
- }
- function hero(name){
- npc.call(this,name,100);
- this.damage = 10;
- this.critical = false;
- }
- // hero.prototype = Object.create(NPC.prototype);
- // hero.prototype.constructor = hero;
- hero.prototype.attack = function(otherHero, attackerIndex, defenderIndex, arrayLength){
- // if(otherHero == undefined){
- // console.log("Attacker index: " + attackerIndex + " Defender index: " + defenderIndex + " Array Length: "+ arrayLength);
- // } else {
- otherHero.hitpoints -= this.critical ? this.damage*1.5 : this.damage;
- console.log(this.name + " attacker " + otherHero.name + " with " + (this.critical ? this.damage*1.5 : this.damage) + " dmg");
- if(this.critical && otherHero instanceof hero){
- otherHero.critical = true;
- this.critical = false;
- }
- // }
- };
- npc.prototype.status = function(){
- console.log("name: " + this.name + " hitpoits: " + this.hitpoits);
- };
- function randomNumber(min,max,notThis){
- min = min || 0;
- var randm = Math.round(Math.random()*max-1);
- if(randm === notThis || randm < 0 || randm < min || randm > max){
- return randomNumber(min,max,notThis)
- }
- return randm;
- }
- var poleObj = [];
- var randomNu = randomNumber(10,50,-1); //random brojki od 10 - 50
- console.log(randomNu);
- for(var i =0;i<randomNu;i++){
- if(randomNumber(0,2,-1) == 0){
- poleObj.push(new hero("hero_" + i));
- } else {
- poleObj.push(new npc("npc_" + i, 100));
- }
- }
- console.log(poleObj);
- poleObj[randomNumber(0,poleObj.length,-1)].critical = true;
- while(poleObj.length > 1){
- var attackerIndex = randomNumber(0,poleObj.length, -1);
- var defenderIndex = randomNumber(0,poleObj.length, attackerIndex);
- if(poleObj[attackerIndex] instanceof hero){
- poleObj[attackerIndex].attack(poleObj[defenderIndex], attackerIndex, defenderIndex, poleObj.length);
- if(poleObj[defenderIndex].hitpoits <= 0){
- poleObj.splice(defenderIndex,1);
- }
- }
- }
- console.log("The winner is: " + poleObj[0].name + " and has: " + poleObj[0].hitpoits + " hitpoints.");
- </script>
- </head>
- <body>
- </body>
- </html>
Add Comment
Please, Sign In to add comment