Guest User

Untitled

a guest
Oct 10th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Fire(context, sprite, controlButtons, controlButtonsIds, monsters, player, isPlayer, posX, posY) {
  2.     this.context = context;
  3.     this.sprite = sprite;
  4.     this.monsters = monsters;
  5.     this.player = player;
  6.     this.controlButtons = controlButtons;
  7.     this.controlButtonsIds = controlButtonsIds;
  8.    
  9.     this.isSoundPlayed = false;
  10.     this.isPlayer = isPlayer;
  11.     this.isExploded = false;
  12.     this.isRemoved = false;
  13.     this.wait = 0;
  14.    
  15.     if(isPlayer) {
  16.         this.srcX = 151;
  17.         this.srcY = 0;
  18.         this.speed = 7;
  19.     } else {
  20.         this.srcX = 101;
  21.         this.srcY = 0;
  22.         this.speed = 2 + Math.round(Math.random() * 4);
  23.     }
  24.    
  25.     this.width = 20;
  26.     this.height = 20;
  27.     this.offsetX = posX;
  28.     this.offsetY = posY;
  29. }
  30.  
  31. Fire.prototype.draw = function() {
  32.     this.update();
  33.    
  34.     if(!this.isRemoved && !this.player.isDead) {
  35.         this.context.drawImage(this.sprite, this.srcX, this.srcY, this.width, this.height, this.offsetX, this.offsetY, this.width, this.height);
  36.     }
  37. };
  38.  
  39. Fire.prototype.update = function() {
  40.     if(this.wait >= 0 && !this.isExploded) {
  41.         if(this.isPlayer) {
  42.             this.offsetY -= this.speed;
  43.            
  44.             for(var i = 0; i < this.monsters.length; i++) {
  45.                 if(this.offsetX <= this.monsters[i].offsetX + this.monsters[i].width && this.offsetX + this.width >= this.monsters[i].offsetX &&
  46.                    this.offsetY <= this.monsters[i].offsetY + this.monsters[i].height && this.offsetY + this.height >= this.monsters[i].offsetY && !this.isExploded
  47.                    && !this.monsters[i].isDead) {
  48.                     this.isExploded = true;
  49.                     this.wait = 35;
  50.                    
  51.                     var health = this.monsters[i].getHealth() - this.player.getFireDamage();
  52.                     this.monsters[i].setHealth(health);
  53.                 }
  54.             }
  55.         } else {
  56.             this.offsetY += this.speed;
  57.        
  58.             if(this.offsetX <= this.player.offsetX + this.player.width && this.offsetX + this.width >= this.player.offsetX &&
  59.                this.offsetY <= this.player.offsetY + this.player.height && this.offsetY + this.height >= this.player.offsetY && !this.isExploded
  60.                && !this.player.isDead) {
  61.                 this.isExploded = true;
  62.                 this.wait = 35;
  63.                
  64.                 var health = this.player.getHealth() - 1;
  65.                 this.player.setHealth(health);
  66.             }
  67.         }
  68.     }
  69.    
  70.     if(!this.isSoundPlayed && this.controlButtons[this.controlButtonsIds['sound']].isChecked) {
  71.         this.isSoundPlayed = true;
  72.         var fireSound = new Audio('sounds/fire.ogg');
  73.         fireSound.volume = 0.2;
  74.         fireSound.currentTime = 0;
  75.         fireSound.play();
  76.     }
  77.  
  78.     if(this.isExploded && this.wait > 0) {
  79.         this.height = 30;
  80.         this.width = 30;
  81.        
  82.         if(this.isPlayer) {
  83.             this.srcX = 171;
  84.             this.srcY = 0;
  85.         } else {
  86.             this.srcX = 121;
  87.             this.srcY = 0;
  88.         }
  89.        
  90.        
  91.         this.wait--;
  92.     }
  93.    
  94.     if(this.isExploded && this.wait <= 0) {
  95.         this.isRemoved = true;
  96.     }
  97. };
Add Comment
Please, Sign In to add comment