Skip to content
Closed
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
124 changes: 116 additions & 8 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
Object oriented design is commonly used in video games. For this part of the assignment
you will be implementing several classes with their correct inheritance heirarchy.
you will be implementing several prototypes with their correct inheritance heirarchy.

In this file you will be creating three classes: GameObject, CharacterStats, Humanoid.
In this file you will be creating three prototypes: GameObject, CharacterStats, Humanoid.
At the bottom of this file are 3 objects that all inherit from Humanoid. Use the objects at the bottom of the page to test your classes.

Each class has unique properites and methods that are defined in their block comments below:
Each prototype has unique properites and methods that are defined in their block comments below:
*/

/*
Expand All @@ -15,6 +15,14 @@
* destroy() // prototype method -> returns the string 'Object was removed from the game.'
*/

const GameObject = function (parameter) {
this.createdAt = parameter.createdAt;
this.dimensions = parameter.dimensions;
this.destroy = function() {
return `${this.name} was removed from the game.`;
}
}

/*
=== CharacterStats ===
* hp
Expand All @@ -23,6 +31,19 @@
* should inherit destroy() from GameObject's prototype
*/

const CharacterStats = function (parameter) {
this.hp = parameter.hp;
this.name = parameter.name;
GameObject.call(this, parameter);
this.status = function() {
if (this.hp > 0) return `${this.name} has ${this.hp} hit points left.`;
else return `${this.name} is dead!`;
}
this.takeDamage = function() {
return `${this.name} took damage.`;
}
}
CharacterStats.prototype = Object.create(GameObject.prototype);
/*
=== Humanoid ===
* faction
Expand All @@ -32,7 +53,24 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

const Humanoid = function (parameter) {
this.faction = parameter.faction;
this.weapons = parameter.weapons;
this.language = parameter.language;
CharacterStats.call(this, parameter);
this.greet = function () {
return `${this.name} offers a greeting in ${this.language}`;
}
this.harm = function () {
this.hp = this.hp - 5;
}
}

Humanoid.prototype = Object.create(CharacterStats.prototype);




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

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

/*

const mage = new Humanoid({
createdAt: new Date(),
dimensions: {
Expand Down Expand Up @@ -102,9 +140,79 @@
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 Villian and Hero classes that inherit from the Humanoid class.
// * Create Villian and Hero prototype that inherit from the Humanoid prototype.
// * Give the Hero and Villians 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 villian and one a hero and fight it out with methods!
// * Create two new objects, one a villian and one a hero and fight it out with methods!


const Hero = function (parameter) {
Humanoid.call(this, parameter)
this.smite = function () {
this.harm.call(target);
return `${this.name} righteously smites ${target.name} for 5 damage!`
}
}

Hero.prototype = Object.create(Humanoid.prototype);



const Villian = function (parameter) {
Humanoid.call(this, parameter)
this.bash = function () {
this.harm.call(target);
return `${this.name} brutally bashes ${target.name} for 5 damage!`
}
}

Villian.prototype = Object.create(Humanoid.prototype);

const Mr_Hero = new Hero({
createdAt: new Date(),
dimensions: {
length: 2,
width: 1,
height: 1,
},
hp: 15,
name: 'Mister Heroic',
faction: 'The Good Guys',
weapons: [
'Shiny sword',
],
language: 'Common Toungue',
});

const Mr_Villain = new Villain({
createdAt: new Date(),
dimensions: {
length: 2,
width: 1,
height: 1,
},
hp: 20,
name: 'Mister Villainous',
faction: 'The Bad Guys',
weapons: [
'Demonic Sword',
],
language: 'Orcish',
});

console.log(Mr_Villain.status());
console.log(Mr_Hero.status());
console.log(Mr_Hero.smite(Mr_Villain));
console.log(Mr_Villain.status());
console.log(Mr_Villain.bash(Mr_Hero));
console.log(Mr_Hero.status());
console.log(Mr_Hero.smite(Mr_Villain));
console.log(Mr_Villain.status());
console.log(Mr_Villain.bash(Mr_Hero));
console.log(Mr_Hero.status());
console.log(Mr_Hero.smite(Mr_Villain));
console.log(Mr_Villain.status());
console.log(Mr_Villain.bash(Mr_Hero));
console.log(Mr_Hero.status());
55 changes: 47 additions & 8 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,65 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. Window/Global Binding is when the this keyword doesn't have anywhere else to look so it looks to the window by default.
* 2. Implicit Binding is when the this keyword goes with the object to the left of the dot when called.
* 3. Explicit Binding binds the this keyword manually using 'call' 'bind' and 'apply'.
* 4. New Binding creates a new object and assigns the this keyword to an object.
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
let myobject = () => {
this.name = "Billy";
}
myobject();
console.log(window.name);



// Principle 2

// code example for Implicit Binding
let myObject = {
name: 'Anne',
color: 'red',
number: 11,
speak: function () {
return `I'm an object and my name is ${this.name} and my favorite color is ${this.color} and my favortie number is ${this.number}`
}
}
console.log(myObject.speak());


// Principle 3

// code example for New Binding
let person = function (haircolor, eyecolor, height) {
this.haircolor = haircolor;
this.eyecolor = eyecolor;
this.height = height;
this.sayHi = function () {
return `My haircolor is ${this.haircolor} and my eyecolor is ${this.eyecolor} and my height is ${this.height}`
}
}
let newGuy = new person('brown', 'blue', 5);
console.log(newGuy.sayHi());



// Principle 4

// code example for Explicit Binding
let human = {
name: 'Anne',
species: 'human',
speak: function () {
return `Hi, I'm ${this.name} and im a ${this.human}`
}
}

let newName = function () {
this.name = 'Sarah';
}

newName.call(human);
console.log(human.name);