Skip to content

all tests pass #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions constructors.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
* @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.
Expand All @@ -18,6 +22,11 @@
* @name getDetails
* @return {string} details containing all of the spells information.
*/
this.getDetails = function () {
return this.name + ', ' + this.cost + ', ' + this.description;
};
}


/**
* A spell that deals damage.
Expand All @@ -43,6 +52,16 @@
* @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, {
constructor: {
value: Spell
}
});

/**
* Now that you've created some spells, let's create
Expand All @@ -60,6 +79,12 @@
* @method spendMana
* @method invoke
*/
function Spellcaster (name, health, mana) {
this.name = name;
this.health = health;
this.mana = mana;
this.isAlive = true;
}

/**
* @method inflictDamage
Expand All @@ -71,6 +96,13 @@
*
* @param {number} damage Amount of damage to deal to the spellcaster
*/
Spellcaster.prototype.inflictDamage = function (damage) {
this.health = this.health - damage;
if (this.health <= 0) {
this.health = 0;
this.isAlive = false;
}
};

/**
* @method spendMana
Expand All @@ -81,6 +113,15 @@
* @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;
}
if (this.mana >= cost) {
this.mana = this.mana - cost;
return true;
}
};

/**
* @method invoke
Expand Down Expand Up @@ -108,3 +149,47 @@
* @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);
return true;
}
return false;
} else {
if (this.spendMana(spell.cost) === false) {
return false;
}
return true;
}
};
// if (this.spendMana(spell.cost) === false) {
// return false;
// } else {
// this.spendMana(spell.cost);
// return true;
// }


// if (spell instanceof DamageSpell) {
// if (this.spendMana(spell.cost) === false) {
// return false;
// } else {
// this.mana = this.mana - spell.cost;
// target.health = target.health - spell.damage;
// return true;
// }
// } else {
// return false;
// }
// }

// return true;