Skip to content

Instantly share code, notes, and snippets.

@JoeKarlsson
Created December 11, 2015 00:21
Show Gist options
  • Save JoeKarlsson/33a5593669c83bc7565c to your computer and use it in GitHub Desktop.
Save JoeKarlsson/33a5593669c83bc7565c to your computer and use it in GitHub Desktop.
invoke function from js-constructors.js
/**
* @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