Skip to content
Open
Show file tree
Hide file tree
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
257 changes: 250 additions & 7 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/*
Object oriented design is commonly used in video games. For this part of the assignment you will be implementing several constructor functions with their correct inheritance hierarchy.
Object oriented design is commonly used in video games.
For this part of the assignment you will be implementing several constructor functions with their correct inheritance hierarchy.

In this file you will be creating three constructor functions: GameObject, CharacterStats, Humanoid.

At the bottom of this file are 3 objects that all end up inheriting from Humanoid. Use the objects at the bottom of the page to test your constructor functions.
At the bottom of this file are 3 objects that all end up inheriting from Humanoid.
Use the objects at the bottom of the page to test your constructor functions.

Each constructor function has unique properties and methods that are defined in their block comments below:
*/
Expand All @@ -15,13 +17,27 @@
* dimensions (These represent the character's size in the video game)
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
*/
function GameObject(gObjAttrs)
{
this.createdAt = gObjAttrs.createdAt;
this.name = gObjAttrs.name;
this.dimensions = gObjAttrs.dimensions;
}
GameObject.prototype.destroy = function() {return `${this.name} was removed from the game`;};

/*
=== CharacterStats ===
* healthPoints
* takeDamage() // prototype method -> returns the string '<object name> took damage.'
* should inherit destroy() from GameObject's prototype
*/
function CharacterStats(charStatAttrs)
{
GameObject.call(this, charStatAttrs);
this.healthPoints = charStatAttrs.healthPoints;
}
CharacterStats.prototype = Object.create(GameObject.prototype);
CharacterStats.prototype.takeDamage = function () {return `${this.name} took damage`;};

/*
=== Humanoid (Having an appearance or character resembling that of a human.) ===
Expand All @@ -32,7 +48,16 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

function Humanoid(hmndAttrs)
{
CharacterStats.call(this, hmndAttrs);
this.team = hmndAttrs.team;
this.weapons = hmndAttrs.weapons;
this.language = hmndAttrs.language;
}
Humanoid.prototype = Object.create(CharacterStats.prototype);
Humanoid.prototype.greet = function() {return `${this.name} offers a greeting in ${this.language}`;};

/*
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
* Instances of Humanoid should have all of the same properties as CharacterStats and GameObject.
Expand All @@ -41,7 +66,7 @@

// Test you work by un-commenting these 3 objects and the list of console logs below:

/*

const mage = new Humanoid({
createdAt: new Date(),
dimensions: {
Expand Down Expand Up @@ -102,9 +127,227 @@
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.
*/


// Stretch task:
// * Create Villain and Hero constructor functions that inherit from the Humanoid constructor function.
// * Give the Hero and Villains different methods that could be used to remove health points from objects which could result in destruction if health gets to 0 or drops below 0;
// * Create two new objects, one a villain and one a hero and fight it out with methods!
// * Give the Hero and Villains different methods that could be used to
// remove health points from objects which could result in destruction if health gets to 0 or drops below 0;
// * Create two new objects, one a villain and one a hero and fight it out with methods!



function Villain(vlnAttrs)
{
Humanoid.call(this, vlnAttrs);
this.xp = vlnAttrs.xp;
this.AC = vlnAttrs.AC;
this.hitBon = vlnAttrs.hitBon;
this.dmgBon = vlnAttrs.dmgBon;
this.dmgDice = vlnAttrs.dmgDice;
}
Villain.prototype = Object.create(Humanoid.prototype);
Villain.prototype.attack = function(heroObj)
{
let dmgCalc = function()
{
let dmg = 0;
for(let i=0; i < this.dmgDice; i++)
{
dmg +=Math.floor(1 + Math.random()*6);
}
dmg += this.dmgBon;
return dmg;
}

let attResolve = function()
{
let hitRoll = this.hitBon + Math.floor(1+Math.random()*19);
// console.log(hitRoll);
if (hitRoll >= heroObj.AC)
{
let dmg = dmgCalc.call(this);
heroObj.healthPoints -= dmg;
if (heroObj.healthPoints <= 0)
{
console.log(`${this.name} slayed the hero!\n`);
console.log(`hero hp: ${heroObj.healthPoints}\n`);
console.log(heroObj.destroy());
}
else
{
console.log(`${this.name} hit the hero for ${dmg}!\n`);
console.log(`hero hp: ${heroObj.healthPoints}\n`);
}
}
else
{
console.log(`${this.name} missed the hero!\n`);
}
}
attResolve.call(this);
}

console.log("\n\n*******************************\n Greentooth Vs Dragon!\n\n");

function Hero(heroAttrs)
{
Humanoid.call(this, heroAttrs);
this.xpTot = 0;
this.level = 1;
this.strength = heroAttrs.strength;
this.agility = heroAttrs.agility;
this.intellect = heroAttrs.intellect;
this.gold = heroAttrs.gold;
this.AC = heroAttrs.AC;
this.weapons = heroAttrs.weapons;
this.limit = 0;
this.hitBon = heroAttrs.hitBon;
this.dmgDice = heroAttrs.dmgDice;
this.dmgBon = heroAttrs.dmgBon;
}
Hero.prototype = Object.create(Humanoid.prototype);
Hero.prototype.mainAttack = function(vlnObj)
{
let limitString = ""
{
let dmgCalc = function()
{
let dmg = 0;

if(this.limit < 4)
{
for(let i=0; i <this.dmgDice; i++)
{
dmg +=Math.floor(1 + Math.random()*6);
}
this.limit +=1;
}
else
{
dmg += this.dmgDice*6; limit = 0;
limitString = "with a limit attack ";
}

dmg += this.dmgBon;

return dmg;
}

let attResolve = function()
{

let hitRoll = this.hitBon + Math.floor(1+Math.random()*19);
// console.log(hitRoll);
if (hitRoll >= vlnObj.AC)
{
let dmg = dmgCalc.call(this);
vlnObj.healthPoints -= dmg;
if (vlnObj.healthPoints <= 0)
{
console.log(`${this.name} slayed the ${vlnObj.name}!\n`);
console.log(`${vlnObj.name} hp: ${vlnObj.healthPoints}\n`);
console.log(vlnObj.destroy());
}
else
{
console.log(`${this.name} hit the ${vlnObj.name} ${limitString}for ${dmg}!\n`);
console.log(`${vlnObj.name} hp: ${vlnObj.healthPoints}\n`);
}
}
else
{
console.log(`${this.name} missed the ${vlnObj.name}!\n`);
}
}
attResolve.call(this);
}
}

let dragon = new Villain(
{
createdAt: new Date(),
dimensions: {
length: 5,
width: 3,
height: 4
},
healthPoints: 150,
name: "Dragon",
team: "Dragonkin",
weapons: ["Claw", "Bite", "Breath"],
hitBon: 8,
dmgDice: 2,
dmgBon: 3,
AC: 14,
language: "Draconic",
xp: 1000
}
);

let greenTooth = new Hero(
{
createdAt: new Date(),
strength: 5,
agility: 3,
intellect: 2,
gold: 0,
AC: 16,
dimensions:
{
length: 1,
width: 2,
height: 4
},
healthPoints: 35,
name: "Greentooth",
team: "Small Town Bumpkins",
weapons: ["Dagger", "Fist"],
language: "Common",
hitBon: 9,
dmgDice: 5,
dmgBon: 5
});

//fight!
// let greenDragToggle = 0;
// while ((greenTooth.healthPoints > 0) && (dragon.healthPoints > 0))
// {
// if (greenDragToggle === 0)
// {
// greenTooth.mainAttack(dragon);
// }
// else
// {
// dragon.attack(greenTooth)
// }
// greenDragToggle === 0 ? greenDragToggle = 1 : greenDragToggle = 0;
// }

let greenDragToggle = 0;
function fight()
{
let attWait = setInterval(() => {
if (greenTooth.healthPoints > 0 && dragon.healthPoints > 0)
{
if (greenDragToggle === 0)
{
greenTooth.mainAttack.call(greenTooth, dragon);
console.log('-----------------------\n');
}
else
{
dragon.attack.call(dragon, greenTooth);
console.log('-----------------------\n');
}
greenDragToggle === 0 ? greenDragToggle = 1 : greenDragToggle = 0;
}
else
{
if (greenTooth.healthPoints <= 0) {console.log("Game Over");}
else {console.log("Victory!");}
clearInterval(attWait);
}
}, 1000);
}
fight();
46 changes: 40 additions & 6 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -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. 'this' binds to the window when in the global context.
* 2. Methods called by '.' bind 'this' to the object left of the dot.
* 3. When using a constructor function, 'this' binds to the specific instance of the obj.
* 4. Using '.bind', 'call', or '.apply' explicitly binds 'this' to the object paramater of those methods.
*
* write out a code example of each explanation above
*/
Expand All @@ -13,14 +13,48 @@

// code example for Window Binding

console.log(this.innerWidth); //test using index.html and live extension, check console.

// Principle 2

// code example for Implicit Binding

const myObj =
{
name: "blah",
sayName: function() {console.log(`Hello, my name is ${this.name}.`);}
}
myObj.sayName();

// Principle 3

// code example for New Binding

function Person(personAttrs)
{
this.name = personAttrs.name;
this.age = personAttrs.age;
this.speak = function() {console.log(`Hello, my name is ${this.name}, and I'm ${this.age} years old.`)}
}

const guy = new Person(
{
name: "Guy",
age: 25
}
);
guy.speak();

// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding

const gil = new Person(
{
name: "Gil",
age: 30
}
);

guy.speak.call(gil);
gil.speak.apply(guy);