Skip to content

Commit 8c3761b

Browse files
committed
prototypes
1 parent a3291d8 commit 8c3761b

1 file changed

Lines changed: 160 additions & 73 deletions

File tree

‎assignments/prototypes.js‎

Lines changed: 160 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
/*
2-
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.
2+
Object oriented design is commonly used in video games.
3+
For this part of the assignment you will be implementing several constructor functions with their correct inheritance hierarchy.
34
45
In this file you will be creating three constructor functions: GameObject, CharacterStats, Humanoid.
56
67
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.
78
89
Each constructor function has unique properties and methods that are defined in their block comments below:
910
*/
10-
11+
1112
/*
1213
=== GameObject ===
1314
* createdAt
@@ -32,79 +33,165 @@
3233
* should inherit destroy() from GameObject through CharacterStats
3334
* should inherit takeDamage() from CharacterStats
3435
*/
35-
36+
3637
/*
37-
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
38-
* Instances of Humanoid should have all of the same properties as CharacterStats and GameObject.
39-
* Instances of CharacterStats should have all of the same properties as GameObject.
40-
*/
38+
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
39+
* Instances of Humanoid should have all of the same properties as CharacterStats and GameObject.
40+
* Instances of CharacterStats should have all of the same properties as GameObject.
41+
*/
4142

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

44-
/*
45-
const mage = new Humanoid({
46-
createdAt: new Date(),
47-
dimensions: {
48-
length: 2,
49-
width: 1,
50-
height: 1,
51-
},
52-
healthPoints: 5,
53-
name: 'Bruce',
54-
team: 'Mage Guild',
55-
weapons: [
56-
'Staff of Shamalama',
57-
],
58-
language: 'Common Tongue',
59-
});
60-
61-
const swordsman = new Humanoid({
62-
createdAt: new Date(),
63-
dimensions: {
64-
length: 2,
65-
width: 2,
66-
height: 2,
67-
},
68-
healthPoints: 15,
69-
name: 'Sir Mustachio',
70-
team: 'The Round Table',
71-
weapons: [
72-
'Giant Sword',
73-
'Shield',
74-
],
75-
language: 'Common Tongue',
76-
});
77-
78-
const archer = new Humanoid({
79-
createdAt: new Date(),
80-
dimensions: {
81-
length: 1,
82-
width: 2,
83-
height: 4,
84-
},
85-
healthPoints: 10,
86-
name: 'Lilith',
87-
team: 'Forest Kingdom',
88-
weapons: [
89-
'Bow',
90-
'Dagger',
91-
],
92-
language: 'Elvish',
93-
});
94-
95-
console.log(mage.createdAt); // Today's date
96-
console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }
97-
console.log(swordsman.healthPoints); // 15
98-
console.log(mage.name); // Bruce
99-
console.log(swordsman.team); // The Round Table
100-
console.log(mage.weapons); // Staff of Shamalama
101-
console.log(archer.language); // Elvish
102-
console.log(archer.greet()); // Lilith offers a greeting in Elvish.
103-
console.log(mage.takeDamage()); // Bruce took damage.
104-
console.log(swordsman.destroy()); // Sir Mustachio was removed from the game.
105-
*/
45+
function GameObject(gameObj) {
46+
this.createdAt = gameObj.createdAt;
47+
this.name = gameObj.name;
48+
this.dimensions = gameObj.dimensions;
49+
}
50+
51+
GameObject.prototype.destroy = function() {
52+
return `${this.name} was removed from the game.`;
53+
};
54+
55+
function CharacterStats(statsObj) {
56+
GameObject.call(this, statsObj);
57+
this.healthPoints = statsObj.healthPoints;
58+
}
59+
60+
CharacterStats.prototype = Object.create(GameObject.prototype);
61+
62+
CharacterStats.prototype.takeDamage = function() {
63+
return `${this.name} took damage.`;
64+
};
65+
66+
function Humanoid(humanoidObj) {
67+
CharacterStats.call(this, humanoidObj);
68+
this.team = humanoidObj.team;
69+
this.weapons = humanoidObj.weapons;
70+
this.language = humanoidObj.language;
71+
}
72+
73+
Humanoid.prototype = Object.create(CharacterStats.prototype);
74+
75+
Humanoid.prototype.greet = function() {
76+
return `${this.name} offers a greeting in ${this.language}.`;
77+
};
78+
79+
const mage = new Humanoid({
80+
createdAt: new Date(),
81+
dimensions: {
82+
length: 2,
83+
width: 1,
84+
height: 1
85+
},
86+
healthPoints: 5,
87+
name: "Bruce",
88+
team: "Mage Guild",
89+
weapons: ["Staff of Shamalama"],
90+
language: "Common Tongue"
91+
});
92+
93+
const swordsman = new Humanoid({
94+
createdAt: new Date(),
95+
dimensions: {
96+
length: 2,
97+
width: 2,
98+
height: 2
99+
},
100+
healthPoints: 15,
101+
name: "Sir Mustachio",
102+
team: "The Round Table",
103+
weapons: ["Giant Sword", "Shield"],
104+
language: "Common Tongue"
105+
});
106+
107+
const archer = new Humanoid({
108+
createdAt: new Date(),
109+
dimensions: {
110+
length: 1,
111+
width: 2,
112+
height: 4
113+
},
114+
healthPoints: 10,
115+
name: "Lilith",
116+
team: "Forest Kingdom",
117+
weapons: ["Bow", "Dagger"],
118+
language: "Elvish"
119+
});
120+
121+
console.log(mage.createdAt); // Today's date
122+
console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }
123+
console.log(swordsman.healthPoints); // 15
124+
console.log(mage.name); // Bruce
125+
console.log(swordsman.team); // The Round Table
126+
console.log(mage.weapons); // Staff of Shamalama
127+
console.log(archer.language); // Elvish
128+
console.log(archer.greet()); // Lilith offers a greeting in Elvish.
129+
console.log(mage.takeDamage()); // Bruce took damage.
130+
console.log(swordsman.destroy()); // Sir Mustachio was removed from the game.
131+
132+
// Stretch task:
133+
// * Create Villain and Hero constructor functions that inherit from the Humanoid constructor function.
134+
// * 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;
135+
// * Create two new objects, one a villain and one a hero and fight it out with methods!
136+
137+
function Hero(heroObj) {
138+
Humanoid.call(this, heroObj);
139+
this.dmg = heroObj.dmg;
140+
}
141+
142+
Hero.prototype = Object.create(Humanoid.prototype);
143+
144+
Hero.prototype.hit = function(enemy) {
145+
enemy.healthPoints = enemy.healthPoints - this.dmg;
146+
let leftHealthMessage = "";
147+
if (enemy.healthPoints <= 0) {
148+
enemy.healthPoints = 0;
149+
leftHealthMessage = "Now it's dead.";
150+
} else {
151+
leftHealthMessage = `Now ${enemy.name} has ${enemy.healthPoints} health.`;
152+
}
153+
return `${this.name} hit ${this.dmg} damage to ${enemy.name}. ${leftHealthMessage}`;
154+
};
155+
156+
function Villain(villainObj) {
157+
Hero.call(this, villainObj);
158+
}
159+
160+
Villain.prototype = Object.create(Hero.prototype);
161+
162+
const pudge = new Hero({
163+
createdAt: new Date(),
164+
dimensions: {
165+
length: 1,
166+
width: 2,
167+
height: 4
168+
},
169+
healthPoints: 100,
170+
name: "Pudge",
171+
team: "Radiant",
172+
weapons: ["Hook", "Dismember"],
173+
language: "Butcherish",
174+
dmg: 20
175+
});
176+
177+
const sniper = new Villain({
178+
createdAt: new Date(),
179+
dimensions: {
180+
length: 1,
181+
width: 2,
182+
height: 2
183+
},
184+
healthPoints: 75,
185+
name: "Sniper",
186+
team: "Dire",
187+
weapons: ["Rifle", "Sharapnel"],
188+
language: "Sniperish",
189+
dmg: 30
190+
});
106191

107-
// Stretch task:
108-
// * Create Villain and Hero constructor functions that inherit from the Humanoid constructor function.
109-
// * 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;
110-
// * Create two new objects, one a villain and one a hero and fight it out with methods!
192+
console.log(sniper.hit(pudge));
193+
console.log(sniper.hit(pudge));
194+
console.log(pudge.hit(sniper));
195+
console.log(pudge.hit(sniper));
196+
console.log(sniper.hit(pudge));
197+
console.log(sniper.hit(pudge));

0 commit comments

Comments
 (0)