|
14 | 14 | * dimensions |
15 | 15 | * destroy() // prototype method -> returns the string: 'Object was removed from the game.' |
16 | 16 | */ |
| 17 | +function GameObject(attributes) { |
| 18 | + this.createdAt = attributes.createdAt; |
| 19 | + this.dimensions = attributes.dimensions; |
| 20 | +} |
| 21 | + |
| 22 | +GameObject.prototype.destroy = function() { |
| 23 | + return `Object was removed from the game.`; |
| 24 | +} |
17 | 25 |
|
18 | 26 | /* |
19 | 27 | === CharacterStats === |
|
22 | 30 | * takeDamage() // prototype method -> returns the string '<object name> took damage.' |
23 | 31 | * should inherit destroy() from GameObject's prototype |
24 | 32 | */ |
| 33 | +function CharacterStats(charAttributes) { |
| 34 | + this.hp = charAttributes.hp; |
| 35 | + this.name = charAttributes.name; |
| 36 | + GameObject.call(this, charAttributes); |
| 37 | +} |
| 38 | + |
| 39 | +CharacterStats.prototype = Object.create(GameObject.prototype); |
| 40 | + |
| 41 | +CharacterStats.prototype.takeDamage = function() { |
| 42 | + return `${this.name} took damage`; |
| 43 | +} |
| 44 | + |
25 | 45 |
|
26 | 46 | /* |
27 | 47 | === Humanoid === |
|
32 | 52 | * should inherit destroy() from GameObject through CharacterStats |
33 | 53 | * should inherit takeDamage() from CharacterStats |
34 | 54 | */ |
35 | | - |
| 55 | +function Humanoid(humanoidAttributes) { |
| 56 | + this.faction = humanoidAttributes.faction; |
| 57 | + this.weapons = humanoidAttributes.weapons; |
| 58 | + this.language = humanoidAttributes.language; |
| 59 | + CharacterStats.call(this,humanoidAttributes); |
| 60 | + |
| 61 | +} |
| 62 | + |
| 63 | +Humanoid.prototype = Object.create(CharacterStats.prototype); |
| 64 | + |
| 65 | +Humanoid.prototype.greet = function() { |
| 66 | + return `${this.name} offers a greeting in ${this.language}`; |
| 67 | +} |
| 68 | + |
36 | 69 | /* |
37 | 70 | * Inheritance chain: GameObject -> CharacterStats -> Humanoid |
38 | 71 | * Instances of Humanoid should have all of the same properties as CharacterStats and GameObject. |
|
41 | 74 |
|
42 | 75 | // Test you work by uncommenting these 3 objects and the list of console logs below: |
43 | 76 |
|
44 | | -/* |
| 77 | + |
45 | 78 | const mage = new Humanoid({ |
46 | 79 | createdAt: new Date(), |
47 | 80 | dimensions: { |
|
92 | 125 | language: 'Elvish', |
93 | 126 | }); |
94 | 127 |
|
95 | | - console.log(mage.createdAt); // Today's date |
96 | | - console.log(archer.dimensions); // { length: 1, width: 2, height: 4 } |
97 | | - console.log(swordsman.hp); // 15 |
98 | | - console.log(mage.name); // Bruce |
99 | | - console.log(swordsman.faction); // 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 | | -*/ |
| 128 | + console.log("1.... " +mage.createdAt); // Today's date |
| 129 | + console.log("2.... "+archer.dimensions); // { length: 1, width: 2, height: 4 } |
| 130 | + console.log("3.... "+swordsman.hp); // 15 |
| 131 | + console.log("4.... "+mage.name); // Bruce |
| 132 | + console.log("5.." +swordsman.faction); // The Round Table |
| 133 | + console.log("6... "+mage.weapons); // Staff of Shamalama |
| 134 | + console.log("7..." +archer.language); // Elvish |
| 135 | + console.log("8..." +archer.greet()); // Lilith offers a greeting in Elvish. |
| 136 | + console.log("9..." +mage.takeDamage()); // Bruce took damage. |
| 137 | + console.log("10.."+swordsman.destroy()); // Sir Mustachio was removed from the game. |
| 138 | + |
106 | 139 |
|
107 | 140 | // Stretch task: |
108 | 141 | // * Create Villian and Hero constructor functions that inherit from the Humanoid constructor function. |
|
0 commit comments