77
88 Each constructor function has unique properties and methods that are defined in their block comments below:
99*/
10-
10+
1111/*
1212 === GameObject ===
1313 * createdAt
3232 * should inherit destroy() from GameObject through CharacterStats
3333 * should inherit takeDamage() from CharacterStats
3434*/
35-
35+
3636/*
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- */
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+ */
4141
4242// Test you work by un-commenting these 3 objects and the list of console logs below:
4343
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- */
44+ function GameObject ( attr ) {
45+ this . createdAt = attr . createdAt ;
46+ this . name = attr . name ;
47+ this . dimensions = attr . dimensions ;
48+ }
49+ GameObject . prototype . destroy = function ( ) {
50+ return `${ this . name } was removed from the game.` ;
51+ }
52+
53+ function CharacterStats ( attr ) {
54+ this . healthPoints = attr . healthPoints ;
55+ }
56+ CharacterStats . prototype = Object . create ( GameObject . prototype ) ;
57+ CharacterStats . prototype . takeDamage = function ( ) {
58+ return `${ this . name } took damage.` ;
59+ }
60+
61+ function Humanoid ( attr ) {
62+ this . team = attr . team ;
63+ this . weapons = attr . weapons ;
64+ this . language = attr . language ;
65+ GameObject . call ( this , attr ) ;
66+ CharacterStats . call ( this , attr ) ;
67+ }
68+ Humanoid . prototype = Object . create ( CharacterStats . prototype )
69+
70+ Humanoid . prototype . greet = function ( ) {
71+ return `${ this . name } offers a greeting in ${ this . language } .`
72+ }
73+
74+
75+ const mage = new Humanoid ( {
76+ createdAt : new Date ( ) ,
77+ dimensions : {
78+ length : 2 ,
79+ width : 1 ,
80+ height : 1
81+ } ,
82+ healthPoints : 5 ,
83+ name : "Bruce" ,
84+ team : "Mage Guild" ,
85+ weapons : [ "Staff of Shamalama" ] ,
86+ language : "Common Tongue"
87+ } ) ;
88+
89+ const swordsman = new Humanoid ( {
90+ createdAt : new Date ( ) ,
91+ dimensions : {
92+ length : 2 ,
93+ width : 2 ,
94+ height : 2
95+ } ,
96+ healthPoints : 15 ,
97+ name : "Sir Mustachio" ,
98+ team : "The Round Table" ,
99+ weapons : [ "Giant Sword" , "Shield" ] ,
100+ language : "Common Tongue"
101+ } ) ;
102+
103+ const archer = new Humanoid ( {
104+ createdAt : new Date ( ) ,
105+ dimensions : {
106+ length : 1 ,
107+ width : 2 ,
108+ height : 4
109+ } ,
110+ healthPoints : 10 ,
111+ name : "Lilith" ,
112+ team : "Forest Kingdom" ,
113+ weapons : [ "Bow" , "Dagger" ] ,
114+ language : "Elvish"
115+ } ) ;
116+
117+ console . log ( mage . createdAt ) ; // Today's date
118+ console . log ( archer . dimensions ) ; // { length: 1, width: 2, height: 4 }
119+ console . log ( swordsman . healthPoints ) ; // 15
120+ console . log ( mage . name ) ; // Bruce
121+ console . log ( swordsman . team ) ; // The Round Table
122+ console . log ( mage . weapons ) ; // Staff of Shamalama
123+ console . log ( archer . language ) ; // Elvish
124+ console . log ( archer . greet ( ) ) ; // Lilith offers a greeting in Elvish.
125+ console . log ( mage . takeDamage ( ) ) ; // Bruce took damage.
126+ console . log ( swordsman . destroy ( ) ) ; // Sir Mustachio was removed from the game.
127+
128+ // Stretch task:
129+ // * Create Villain and Hero constructor functions that inherit from the Humanoid constructor function.
130+
131+ function Villain ( attr ) {
132+ this . lair = attr . lair ;
133+ this . victimCount = attr . victimCount ;
134+ this . adjective = attr . adjective ;
135+ Humanoid . call ( this , attr ) ;
136+ }
137+
138+ Villain . prototype = Object . create ( Humanoid . prototype ) ;
139+ Villain . prototype . evile = function ( ) {
140+ return `${ this . name } the ${ this . adjective } murders ${ this . victimCount } babies in ${ this . lair } .`
141+ }
142+
143+ const baddie = new Villain ( {
144+ createdAt : new Date ( ) ,
145+ healthPoints : 10 ,
146+ name : "Milton" ,
147+ team : "Der" ,
148+ weapons : [ "Axe of Hell" ] ,
149+ language : "Stthmgol" ,
150+
151+ adjective : "Mad" ,
152+ victimCount : 1741 ,
153+ lair : "The Village of the Damned"
154+ } )
155+
156+ console . log ( baddie . evile ( ) ) ; // Bruce took damage.
157+
106158
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!
159+ // * 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;
160+ // * Create two new objects, one a villain and one a hero and fight it out with methods!
0 commit comments