-
-
Save ChazAttack73/bbb7dc6f5c29b2824d5a to your computer and use it in GitHub Desktop.
invoke function from js-constructors.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @method invoke | |
* | |
* Allows the spellcaster to cast spells. | |
* The first parameter should either be a `Spell` or `DamageSpell`. | |
* If it is a `DamageSpell`, the second parameter should be a `Spellcaster`. | |
* The function should return `false` if the above conditions are not satisfied. | |
* | |
* You should use `instanceof` to check for these conditions. | |
* | |
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof | |
* | |
* Next check if the spellcaster has enough mana to cast the spell. | |
* If it can cast a spell, it should lose mana equal to the spell's cost. | |
* If there is not enough mana, return `false`. | |
* | |
* If there is enough mana to cast the spell, return `true`. | |
* In addition, if it is a `DamageSpell` reduce the target's health by the spell's damage value. | |
* | |
* Use functions you've previously created: (`inflictDamage`, `spendMana`) | |
* to help you with this. | |
* | |
* @param {(Spell|DamageSpell)} spell The spell to be cast. | |
* @param {Spellcaster} target The spell target to be inflicted. | |
* @return {boolean} Whether the spell was successfully cast. | |
*/ | |
Spellcaster.prototype.invoke = function(spell, target) { | |
if ( spell instanceof Spell ) { | |
if ( this.mana >= spell.cost ) { | |
if ( spell instanceof DamageSpell ) { | |
if ( target instanceof Spellcaster ) { | |
target.inflictDamage( spell.damage ) | |
} else { | |
return false; | |
} | |
} | |
return this.spendMana(spell.cost); | |
} else { | |
return false; | |
} | |
} else { | |
return false; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment