Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Fire(context, sprite, controlButtons, controlButtonsIds, monsters, player, isPlayer, posX, posY) {
- this.context = context;
- this.sprite = sprite;
- this.monsters = monsters;
- this.player = player;
- this.controlButtons = controlButtons;
- this.controlButtonsIds = controlButtonsIds;
- this.isSoundPlayed = false;
- this.isPlayer = isPlayer;
- this.isExploded = false;
- this.isRemoved = false;
- this.wait = 0;
- if(isPlayer) {
- this.srcX = 151;
- this.srcY = 0;
- this.speed = 7;
- } else {
- this.srcX = 101;
- this.srcY = 0;
- this.speed = 2 + Math.round(Math.random() * 4);
- }
- this.width = 20;
- this.height = 20;
- this.offsetX = posX;
- this.offsetY = posY;
- }
- Fire.prototype.draw = function() {
- this.update();
- if(!this.isRemoved && !this.player.isDead) {
- this.context.drawImage(this.sprite, this.srcX, this.srcY, this.width, this.height, this.offsetX, this.offsetY, this.width, this.height);
- }
- };
- Fire.prototype.update = function() {
- if(this.wait >= 0 && !this.isExploded) {
- if(this.isPlayer) {
- this.offsetY -= this.speed;
- for(var i = 0; i < this.monsters.length; i++) {
- if(this.offsetX <= this.monsters[i].offsetX + this.monsters[i].width && this.offsetX + this.width >= this.monsters[i].offsetX &&
- this.offsetY <= this.monsters[i].offsetY + this.monsters[i].height && this.offsetY + this.height >= this.monsters[i].offsetY && !this.isExploded
- && !this.monsters[i].isDead) {
- this.isExploded = true;
- this.wait = 35;
- var health = this.monsters[i].getHealth() - this.player.getFireDamage();
- this.monsters[i].setHealth(health);
- }
- }
- } else {
- this.offsetY += this.speed;
- if(this.offsetX <= this.player.offsetX + this.player.width && this.offsetX + this.width >= this.player.offsetX &&
- this.offsetY <= this.player.offsetY + this.player.height && this.offsetY + this.height >= this.player.offsetY && !this.isExploded
- && !this.player.isDead) {
- this.isExploded = true;
- this.wait = 35;
- var health = this.player.getHealth() - 1;
- this.player.setHealth(health);
- }
- }
- }
- if(!this.isSoundPlayed && this.controlButtons[this.controlButtonsIds['sound']].isChecked) {
- this.isSoundPlayed = true;
- var fireSound = new Audio('sounds/fire.ogg');
- fireSound.volume = 0.2;
- fireSound.currentTime = 0;
- fireSound.play();
- }
- if(this.isExploded && this.wait > 0) {
- this.height = 30;
- this.width = 30;
- if(this.isPlayer) {
- this.srcX = 171;
- this.srcY = 0;
- } else {
- this.srcX = 121;
- this.srcY = 0;
- }
- this.wait--;
- }
- if(this.isExploded && this.wait <= 0) {
- this.isRemoved = true;
- }
- };
Add Comment
Please, Sign In to add comment