diff --git a/constructors.js b/constructors.js index d0bf11a..12a0ead 100644 --- a/constructors.js +++ b/constructors.js @@ -10,7 +10,11 @@ * @property {string} description * @method printDetails */ - + function Spell(name, cost, description) { + this.name = name; + this.cost = cost; + this.description = description; + } /** * Returns a string of all of the spell's details. * The format doesn't matter, as long as it contains the spell name, cost, and description. @@ -18,6 +22,9 @@ * @name getDetails * @return {string} details containing all of the spells information. */ + Spell.prototype.getDetails = function () { + return "You cast " + this.name + " which cost " + this.cost + " and does " + this.description; + }; /** * A spell that deals damage. @@ -43,6 +50,11 @@ * @property {number} damage * @property {string} description */ + function DamageSpell(name, cost, damage, description) { + Spell.call(this, name, cost, description); + this.damage = damage; + } + DamageSpell.prototype = Object.create(Spell.prototype); /** * Now that you've created some spells, let's create @@ -60,6 +72,12 @@ * @method spendMana * @method invoke */ + function Spellcaster(name, health, mana) { + this.name = name; + this.health = health; + this.mana = mana; + this.isAlive = true; + } /** * @method inflictDamage @@ -71,6 +89,13 @@ * * @param {number} damage Amount of damage to deal to the spellcaster */ + Spellcaster.prototype.inflictDamage = function (damage) { + this.health -=damage; + if (this.health <= damage){ + this.health = 0; + this.isAlive = false; + } + }; /** * @method spendMana @@ -81,6 +106,14 @@ * @param {number} cost The amount of mana to spend. * @return {boolean} success Whether mana was successfully spent. */ + Spellcaster.prototype.spendMana = function (cost) { + if (this.mana >= cost){ + this.mana -=cost; + return true; + }else{ + return false; + } + }; /** * @method invoke @@ -108,3 +141,22 @@ * @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)){ + return false; + } + + if(spell instanceof DamageSpell && (!(target instanceof Spellcaster))){ + return false; + } + + if (this.mana >= spell.cost){ + this.spendMana(spell.cost); + if (spell instanceof DamageSpell){ + target.inflictDamage(spell.damage); + } + return true; + }else{ + return false; + } + };