From 01a2ca1e93ddcdde3f3bf146f1588a0679d4e2c7 Mon Sep 17 00:00:00 2001 From: Forrest-Bingham Date: Wed, 18 Sep 2019 17:22:32 -0600 Subject: [PATCH 1/2] JavaScrupt 3 with Stretch Goals --- assignments/prototypes.js | 202 +++++++++++++++++++++++++++++++++++++- assignments/this.js | 52 ++++++++-- 2 files changed, 245 insertions(+), 9 deletions(-) diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 5625c97cb..e7838d935 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -40,8 +40,149 @@ */ // Test you work by un-commenting these 3 objects and the list of console logs below: +function GameObject(object){ + this.createdAt = object.createdAt; + this.name = object.name; + this.dimensions = object.dimensions; +} + +GameObject.prototype.destroy = function(){ + return`${this.name} was removed from the game.`; + } + + +function CharacterStats(stats){ + this.healthPoints = stats.healthPoints +} + +CharacterStats.prototype = Object.create(GameObject.prototype); // Links CharacterStats to GameObject + +CharacterStats.prototype.takeDamage = function(){ // Take Damage Function + if(this.healthPoints <= 0) + { + GameObject.destroy(); + } + else{ + return`${this.name} took damage.`; + } + +} + +Humanoid.prototype = Object.create(CharacterStats.prototype); + +function Humanoid(player){ + CharacterStats.call(this, player); + this.createdAt = player.createdAt; + this.name = player.name, + this.weapons = player.weapons, + this.language = player.language, + this.dimensions = player.dimensions, + this.team = player.team, + this.damage = player.damage, + this.spellDamage = player.spellDamage, + this.spells = player.spells +} +Humanoid.prototype.greet = function(){ + return`${this.name} offers a greeting in ${this.language}`; +} + +Humanoid.prototype.attack = function(targetPlayer){ + + targetPlayer.healthPoints = targetPlayer.healthPoints - this.damage; + + if (targetPlayer.healthPoints <= 0) + { + GameObject.prototype.destroy(); + return`${targetPlayer.name} was killed by ${this.name}.`; + } + + return `${targetPlayer.name} took ${this.damage} damage from ${this.name} and now has ${targetPlayer.healthPoints} HP left`; +} + +Humanoid.prototype.castSpell = function(targetPlayer){ + + targetPlayer.healthPoints = targetPlayer.healthPoints - this.spellDamage; + + if (targetPlayer.healthPoints <= 0) + { + GameObject.prototype.destroy(); + return`${targetPlayer.name} was killed by ${this.name}'s ${this.spells}.`; + } + + return `${targetPlayer.name} took ${this.spellDamage} damage from ${this.name} and now has ${targetPlayer.healthPoints} HP left`; + +} + +Hero.prototype = Object.create(Humanoid.prototype); + +function Hero(player){ + CharacterStats.call(this, player); + this.createdAt = player.createdAt; + this.name = player.name, + this.weapons = player.weapons, + this.language = player.language, + this.dimensions = player.dimensions, + this.team = player.team, + this.spells = player.spells, + this.heals = player.heals, + this.damage = player.damage, + this.spellDamage = player.spellDamage +} + +Hero.prototype.heal = function(){ + this.healthPoints = this.healthPoints + this.heals; + return `${this.name} healed for ${this.heals} HP. Current HP is now ${this.healthPoints}`; +} + +Hero.prototype.attack = function(targetPlayer){ + targetPlayer.healthPoints = targetPlayer.healthPoints - this.damage; + + if (targetPlayer.healthPoints <= 0) + { + GameObject.prototype.destroy(); + return`${targetPlayer.name} was killed by ${this.name}'s ${this.weapons}.`; + } + + return `${targetPlayer.name} took ${this.damage} damage from ${this.name} and now has ${targetPlayer.healthPoints} HP left`; +} + +function Villain(player){ + CharacterStats.call(this, player); + this.createdAt = player.createdAt; + this.name = player.name, + this.weapons = player.weapons, + this.language = player.language, + this.dimensions = player.dimensions, + this.team = player.team, + this.spells = player.spells, + this.damage = player.damage, + this.spellDamage = player.spellDamage +} + +Villain.prototype.castSpell = function(targetPlayer){ + targetPlayer.healthPoints = targetPlayer.healthPoints - this.spellDamage; + + if (targetPlayer.healthPoints <= 0) + { + GameObject.prototype.destroy(); + return`${targetPlayer.name} was killed by $${this.name}'s ${this.spells}.`; + } + + return `${targetPlayer.name} took ${this.spellDamage} damage from ${this.spells} and now has ${targetPlayer.healthPoints} HP left`; +} + +Villain.prototype.attack = function(targetPlayer){ + + targetPlayer.healthPoints = targetPlayer.healthPoints - this.damage; + + if (targetPlayer.healthPoints <= 0) + { + GameObject.prototype.destroy(); + return`${targetPlayer.name} was killed by ${this.name}'s ${this.weapons}`; + } + return `${targetPlayer.name} took ${this.damage} damage from ${this.name} and now has ${targetPlayer.healthPoints} HP left`; +} -/* const mage = new Humanoid({ createdAt: new Date(), dimensions: { @@ -55,6 +196,9 @@ weapons: [ 'Staff of Shamalama', ], + damage: 2, + spells: 'Fireball', + spellDamage: 5, language: 'Common Tongue', }); @@ -72,6 +216,7 @@ 'Giant Sword', 'Shield', ], + damage: 4, language: 'Common Tongue', }); @@ -82,16 +227,52 @@ width: 2, height: 4, }, - healthPoints: 10, + healthPoints: 12, name: 'Lilith', team: 'Forest Kingdom', weapons: [ 'Bow', 'Dagger', ], + damage: 2, language: 'Elvish', }); + const paladin = new Hero({ + createdAt: new Date(), + dimensions: { + length: 1, + width: 2, + height: 4, + }, + healthPoints: 20, + name: 'Uther Lightbringer', + team: 'Lordaeron', + weapons:'Giant Hammer', + damage: 3, + spells: 'Holy Light', + heals: 6, + spellDamage: 3, + language: 'Common Tongue', + }); + + const deathKnight = new Villain({ + createdAt: new Date(), + dimensions: { + length: 2, + width: 3, + height: 4, + }, + healthPoints: 20, + name: 'Prince Arthas', + team: 'The Scourge', + weapons:'FrostMourne', + damage: 5, + spells: 'Death Coil', + spellDamage: 6, + language: 'Common Tongue', + }); + console.log(mage.createdAt); // Today's date console.log(archer.dimensions); // { length: 1, width: 2, height: 4 } console.log(swordsman.healthPoints); // 15 @@ -102,7 +283,22 @@ console.log(archer.greet()); // Lilith offers a greeting in Elvish. console.log(mage.takeDamage()); // Bruce took damage. console.log(swordsman.destroy()); // Sir Mustachio was removed from the game. -*/ + console.log(paladin.spells); //Holy Light + console.log(deathKnight.weapons); + console.log(mage.castSpell(archer)); + console.log(mage.castSpell(archer)); + console.log(archer.healthPoints); + console.log(mage.castSpell(archer)); + console.log(paladin.attack(deathKnight)); + console.log(paladin.heal()); + console.log(paladin.healthPoints); + console.log(deathKnight.attack(paladin)); + console.log(deathKnight.castSpell(paladin)); + console.log(deathKnight.attack(mage)); + + + + // Stretch task: // * Create Villain and Hero constructor functions that inherit from the Humanoid constructor function. diff --git a/assignments/this.js b/assignments/this.js index 969bbeeba..880621a85 100644 --- a/assignments/this.js +++ b/assignments/this.js @@ -1,10 +1,10 @@ -/* The for principles of "this"; +/* The four principles of "this"; * in your own words. explain the four principle for the "this" keyword below. * -* 1. -* 2. -* 3. -* 4. +* 1. if no other rules apply, the "This" keyword defaults to the window object (gloab object in Node) unless you use strict, which will give undefined. +* 2. implicit binding is automatic when function is invoked. refers to what is left of . +* 3. Explicit binding is when we can tell the JS engine to set "this" to a certain value with either Call, Apply, or Bind. +* 4. Bind - used to create a new function that is permanently bound to a "this" value. * * write out a code example of each explanation above */ @@ -13,14 +13,54 @@ // code example for Window Binding +//'use strict'; +/* function eat() +{ + console.log(this.food); +} +const food ="Apple"; + +eat();*/ //food is undefined. + // Principle 2 // code example for Implicit Binding +let basketballPlayer = { + name: 'Joe', + number: 2, + shoot: '3 points', + score: function(){ + console.log(this.shoot); + } + } + basketballPlayer.score(); + // Principle 3 // code example for New Binding +function thirsty(){ + console.log(`Takes a drink of ${this.drink}`); +} + +let person = { + name: 'Jerems', + drink: 'Dewski' +} + +drinks = thirsty.bind(person); +drinks(); + // Principle 4 -// code example for Explicit Binding \ No newline at end of file +// code example for Explicit Binding + +function fishing(){ + console.log(`You caught a ${this.caught}`); +} +let goFishing = { + caught: 'trout' +} + +fishing.call(goFishing); \ No newline at end of file From dd12492b30cd700e31677088c83423e8683909bb Mon Sep 17 00:00:00 2001 From: Forrest-Bingham Date: Wed, 18 Sep 2019 17:25:12 -0600 Subject: [PATCH 2/2] JavaSCRIPT 3 with stretch goals --- assignments/prototypes.js | 1 - 1 file changed, 1 deletion(-) diff --git a/assignments/prototypes.js b/assignments/prototypes.js index e7838d935..5e86d60c3 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -297,7 +297,6 @@ Villain.prototype.attack = function(targetPlayer){ console.log(deathKnight.attack(mage)); - // Stretch task: