Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <meta http-equiv="X-UA-Compatible" content="ie=edge">
- <title>Zad01</title>
- <script>
- class NPC{
- constructor(name, hitpoints){
- this.name = name;
- this.hitpoints = hitpoints;
- }
- status(){
- console.log(`${this.name} has ${this.hitpoints} health!`);
- }
- }
- class Hero extends NPC{
- constructor(name){
- super();
- this.name = name;
- this.hitpoints = 100;
- this.attackDmg = 10;
- }
- attack(defender){
- defender.hitpoints -= this.attackDmg;
- if(defender instanceof Hero && this.attackDmg == 15){
- if(defender.hitpoints > 0){
- this.takeCritical();
- defender.giveCritical();
- }
- else{
- this.takeCritical();
- }
- }
- }
- giveCritical(){
- this.attackDmg = 15;
- }
- takeCritical(){
- this.attackDmg = 10;
- }
- }
- function startGame(){
- //inicialize
- var i = Math.floor(Math.random() * (50 - 10 + 1) ) + 10;
- var arrayClasses = [];
- while(i>0){
- tip = Math.floor(Math.random() * 2);
- if(tip == 0){
- var npc = new NPC(i,100);
- arrayClasses.push(npc);
- }
- else{
- var hero = new Hero(i);
- arrayClasses.push(hero);
- }
- i--;
- }
- //give crit to random hero
- var HeroGetsCrit = arrayClasses[Math.floor(Math.random() * arrayClasses.length)];
- while(!(HeroGetsCrit instanceof Hero)){
- HeroGetsCrit = arrayClasses[Math.floor(Math.random() * arrayClasses.length)];
- }
- HeroGetsCrit.giveCritical();
- //play game
- while(arrayClasses.length > 1){
- //debugger;
- var attacker = arrayClasses[Math.floor(Math.random() * arrayClasses.length)]
- if(attacker instanceof Hero){
- var defenderIndex = Math.floor(Math.random() * arrayClasses.length)
- var defender = arrayClasses[defenderIndex]
- while(defender == attacker){
- var defenderIndex = Math.floor(Math.random() * arrayClasses.length)
- var defender = arrayClasses[defenderIndex]
- }
- //attacker.status();
- //defender.status();
- attacker.attack(defender);
- if(defender.hitpoints <= 0){
- arrayClasses.splice(defenderIndex, 1);
- }
- }
- }
- console.log(`Winner: ${arrayClasses[0].name} ${arrayClasses[0].hitpoints}hp!`);
- }
- startGame();
- </script>
- </head>
- <body>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement