|
14 | 14 | * dimensions |
15 | 15 | * destroy() // prototype method -> returns the string 'Object was removed from the game.' |
16 | 16 | */ |
| 17 | +function GameObject(gameObjAttr) { |
| 18 | + this.createdAt = gameObjAttr.createdAt; |
| 19 | + this.dimensions = gameObjAttr.dimensions; |
| 20 | +} |
| 21 | + |
| 22 | +GameObject.prototype.destroy = function(){ |
| 23 | + return 'Object was removed from the game'; |
| 24 | +} |
| 25 | + |
17 | 26 |
|
18 | 27 | /* |
19 | 28 | === CharacterStats === |
|
23 | 32 | * should inherit destroy() from GameObject's prototype |
24 | 33 | */ |
25 | 34 |
|
| 35 | +function CharacterStats(charStatsAttr){ |
| 36 | + GameObject.call(this,charStatsAttr) |
| 37 | + this.hp = charStatsAttr.hp; |
| 38 | + this.name = charStatsAttr.name; |
| 39 | +} |
| 40 | + |
| 41 | +CharacterStats.prototype = Object.create(GameObject.prototype) |
| 42 | + |
| 43 | +CharacterStats.prototype.takeDamage = function(){ |
| 44 | + return `$this.name took damage`; |
| 45 | +} |
26 | 46 | /* |
27 | 47 | === Humanoid === |
28 | 48 | * faction |
|
33 | 53 | * should inherit takeDamage() from CharacterStats |
34 | 54 | */ |
35 | 55 |
|
| 56 | +function Humanoid(humanAttr){ |
| 57 | + CharacterStats.call(this,humanAttr); |
| 58 | + this.faction = humanAttr.faction; |
| 59 | + this.weapons = humanAttr.weapons; |
| 60 | + this.language = humanAttr.language; |
| 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: Humanoid -> CharacterStats -> GameObject |
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 | | -/* |
45 | 77 | const mage = new Humanoid({ |
46 | 78 | createdAt: new Date(), |
47 | 79 | dimensions: { |
|
102 | 134 | console.log(archer.greet()); // Lilith offers a greeting in Elvish. |
103 | 135 | console.log(mage.takeDamage()); // Bruce took damage. |
104 | 136 | console.log(swordsman.destroy()); // Sir Mustachio was removed from the game. |
105 | | -*/ |
| 137 | + |
106 | 138 |
|
107 | 139 | // Stretch task: |
108 | 140 | // * Create Villian and Hero classes that inherit from the Humanoid class. |
|
0 commit comments