From af0f7efca2de0a06db40cce04288b424e4fe9062 Mon Sep 17 00:00:00 2001 From: Bryce Date: Thu, 26 May 2016 15:01:44 -1000 Subject: [PATCH 1/3] Added 'Spell' constructor. --- constructors.js | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/constructors.js b/constructors.js index d0bf11a..381f944 100644 --- a/constructors.js +++ b/constructors.js @@ -10,7 +10,22 @@ * @property {string} description * @method printDetails */ - + function Spell (name, cost, description) { + if (typeof(name) === 'string') { + this.name = name; + } else { + throw new TypeError('Spell name must be a string.'); + } + if (typeof(cost) === 'number') { + this.cost = cost; + } else { + throw new TypeError('Spell cost must be a number.'); + } + if (typeof(description) === 'string') { + this.description = description; + } else { + throw new TypeError('Spell description must be a string.'); + } /** * 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,7 +33,10 @@ * @name getDetails * @return {string} details containing all of the spells information. */ - + this.getDetails = function () { + return 'Spell Name: ' + name + ', Spell Cost: ' + cost + ', Spell Description: ' + description; + }; + } /** * A spell that deals damage. * We want to keep this code DRY (Don't Repeat Yourself). From 1d6f75354b8222505d5a7f5c364196d1cb4b655f Mon Sep 17 00:00:00 2001 From: Bryce Date: Thu, 26 May 2016 16:00:46 -1000 Subject: [PATCH 2/3] Added 'Spellcaster' constructor and completed methods up to invoke. --- constructors.js | 89 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 67 insertions(+), 22 deletions(-) diff --git a/constructors.js b/constructors.js index 381f944..751dab9 100644 --- a/constructors.js +++ b/constructors.js @@ -10,22 +10,23 @@ * @property {string} description * @method printDetails */ - function Spell (name, cost, description) { - if (typeof(name) === 'string') { - this.name = name; - } else { - throw new TypeError('Spell name must be a string.'); - } - if (typeof(cost) === 'number') { - this.cost = cost; - } else { - throw new TypeError('Spell cost must be a number.'); - } - if (typeof(description) === 'string') { - this.description = description; - } else { - throw new TypeError('Spell description must be a string.'); - } +function Spell (name, cost, description) { + if (typeof(name) === 'string') { + this.name = name; + } else { + throw new TypeError('Spell name must be a string.'); + } + if (typeof(cost) === 'number') { + this.cost = cost; + } else { + throw new TypeError('Spell cost must be a number.'); + } + if (typeof(description) === 'string') { + this.description = description; + } else { + throw new TypeError('Spell description must be a string.'); + } +} /** * 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. @@ -33,10 +34,9 @@ * @name getDetails * @return {string} details containing all of the spells information. */ - this.getDetails = function () { - return 'Spell Name: ' + name + ', Spell Cost: ' + cost + ', Spell Description: ' + description; - }; - } +Spell.prototype.getDetails = function () { + return 'Spell Name: ' + this.name + ', Spell Cost: ' + this.cost + ', Spell Description: ' + this.description; + }; /** * A spell that deals damage. * We want to keep this code DRY (Don't Repeat Yourself). @@ -62,6 +62,12 @@ * @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 * `Spellcaster` objects that can use them! @@ -79,6 +85,24 @@ * @method invoke */ +function Spellcaster (name, health, mana) { + if (typeof(name) === 'string') { + this.name = name; + } else { + throw new TypeError('Name must be a string.'); + } + if (typeof(health) === 'number') { + this.health = health; + } else { + throw new TypeError('Health must be a number.'); + } + if (typeof(mana) === 'number') { + this.mana = mana; + } else { + throw new TypeError('Mana must be a number.'); + } +} +Spellcaster.prototype.isAlive = true; /** * @method inflictDamage * @@ -89,7 +113,17 @@ * * @param {number} damage Amount of damage to deal to the spellcaster */ - +Spellcaster.prototype.inflictDamage = function (damage) { + if (typeof(damage) === 'number') { + this.health -= damage; + if (this.health <= 0) { + this.health = 0; + this.isAlive = false; + } + } else { + throw new TypeError('Damage must be a number.'); + } +}; /** * @method spendMana * @@ -99,7 +133,17 @@ * @param {number} cost The amount of mana to spend. * @return {boolean} success Whether mana was successfully spent. */ - +Spellcaster.prototype.spendMana = function (cost) { + if (typeof(cost) === 'number') { + if (this.mana >= cost) { + this.mana -= cost; + return true; + } else { + return false; + } + } else { + throw new TypeError('Cost must be a number.'); +} /** * @method invoke * @@ -126,3 +170,4 @@ * @param {Spellcaster} target The spell target to be inflicted. * @return {boolean} Whether the spell was successfully cast. */ +} From 6ae1707ff9847fb46db7bf13e5c15ad5d0dcf1c9 Mon Sep 17 00:00:00 2001 From: Bryce Date: Thu, 26 May 2016 16:45:49 -1000 Subject: [PATCH 3/3] Completed invoke method. --- constructors.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/constructors.js b/constructors.js index 751dab9..430ac66 100644 --- a/constructors.js +++ b/constructors.js @@ -143,7 +143,8 @@ Spellcaster.prototype.spendMana = function (cost) { } } else { throw new TypeError('Cost must be a number.'); -} + } +}; /** * @method invoke * @@ -170,4 +171,15 @@ Spellcaster.prototype.spendMana = function (cost) { * @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 && !(spell instanceof DamageSpell)) { + return this.spendMana(spell.cost); + } + if (spell instanceof DamageSpell && target instanceof Spellcaster) { + if(this.spendMana(spell.cost)) { + target.inflictDamage(spell.damage); + return true; + } + } + return false; +};