From d9cc0d0ce609cd00664c26f4156049e34fb143a4 Mon Sep 17 00:00:00 2001 From: thingsastray Date: Sat, 21 Nov 2015 16:35:58 -1000 Subject: [PATCH] finished --- constructors.js | 69 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 66 insertions(+), 3 deletions(-) diff --git a/constructors.js b/constructors.js index d0bf11a..a36908d 100644 --- a/constructors.js +++ b/constructors.js @@ -10,6 +10,7 @@ * @property {string} description * @method printDetails */ + /** * Returns a string of all of the spell's details. @@ -18,6 +19,16 @@ * @name getDetails * @return {string} details containing all of the spells information. */ +function Spell (name, cost, description){ + this.name = name; + this.cost = cost; + this.description = description; +} + +Spell.prototype.getDetails = function(){ + return (this.name + " " + this.cost + " " + this.description); +}; + /** * A spell that deals damage. @@ -43,7 +54,16 @@ * @property {number} damage * @property {string} description */ +function DamageSpell (name, cost, damage, description){ + this.damage = damage; + Spell.call(this, name, cost, description); +} +DamageSpell.prototype = Object.create(Spell.prototype, { + constructor: { + value: DamageSpell + } +}); /** * Now that you've created some spells, let's create * `Spellcaster` objects that can use them! @@ -60,7 +80,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,7 +96,15 @@ * * @param {number} damage Amount of damage to deal to the spellcaster */ - +Spellcaster.prototype.inflictDamage = function(damage) { + if(this.health > damage){ + this.health -= damage; + } else { + this.health = 0; + this.isAlive = false; + } + +}; /** * @method spendMana * @@ -81,7 +114,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){ + return false; + }else{ + this.mana -= cost; + return true; + } +}; /** * @method invoke * @@ -108,3 +148,26 @@ * @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){ + if(target instanceof Spellcaster){ + if(!(this.spendMana(spell.cost))){ + return false; + } + target.inflictDamage(spell.damage); + console.log(target.name + " was damged by " + spell.name + " for " + spell.damage + "."); + return true; + } + return false; + } else { + if(!(this.spendMana(spell.cost))){ + return false; + } + return true; + } + +}; \ No newline at end of file