Skip to content
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
61 changes: 61 additions & 0 deletions constructors.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@
* @method printDetails
*/

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;
};

/**
* 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.
Expand Down Expand Up @@ -44,6 +54,13 @@
* @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!
Expand All @@ -61,6 +78,13 @@
* @method invoke
*/

function Spellcaster(name,health,mana){
this.name = name;
this.health = health;
this.mana = mana;
this.isAlive = true;
}

/**
* @method inflictDamage
*
Expand All @@ -72,6 +96,14 @@
* @param {number} damage Amount of damage to deal to the spellcaster
*/

Spellcaster.prototype.inflictDamage = function(damage){
this.health -= damage;
if(this.health <= 0){
this.isAlive = false;
this.health = 0;
}
};

/**
* @method spendMana
*
Expand All @@ -82,6 +114,15 @@
* @return {boolean} success Whether mana was successfully spent.
*/

Spellcaster.prototype.spendMana = function(cost){
if(this.mana >= cost) {
this.mana = this.mana - cost;
return true;
} else {
return false;
}
};

/**
* @method invoke
*
Expand All @@ -108,3 +149,23 @@
* @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 === undefined || spell === null) {
return false;
}
if (spell instanceof DamageSpell && target instanceof Spellcaster) {
if(this.spendMana(spell.cost)) {
target.inflictDamage(spell.damage);
return true;
}
}
if (spell instanceof DamageSpell && !(target instanceof Spellcaster)) {
return false;
} else {
if(this.spendMana(spell.cost)){
return true;
}
}
return false;
};