|
7 | 7 | createdAt |
8 | 8 | dimensions |
9 | 9 | destroy() // prototype method -> returns the string 'Game object was removed from the game.' |
| 10 | +*/ |
| 11 | + |
| 12 | +function GameObject(options) { |
| 13 | + this.createdAt = options.createdAt; |
| 14 | + this.dimensions = options.dimensions; |
| 15 | +} |
| 16 | + |
| 17 | +GameObject.prototype.destroy = function destroy() { |
| 18 | + return 'Game object was removed from the game.'; |
| 19 | +}; |
10 | 20 |
|
| 21 | +/* |
11 | 22 | NPC |
12 | 23 | hp |
13 | 24 | name |
14 | 25 | takeDamage() // prototype method -> returns the string '<object name> took damage.' |
15 | 26 | // should inherit destroy() from GameObject's prototype |
| 27 | +*/ |
| 28 | +function NPC(options) { |
| 29 | + GameObject.call(this, options); |
| 30 | + this.hp = options.hp; |
| 31 | + this.name = options.name; |
| 32 | +} |
| 33 | + |
| 34 | +NPC.prototype = Object.create(GameObject.prototype); |
| 35 | +NPC.prototype.takeDamage = function takeDamage() { |
| 36 | + return (`${this.name} took damage.`); |
| 37 | +}; |
16 | 38 |
|
| 39 | +/* |
17 | 40 | Humanoid |
18 | 41 | faction |
19 | 42 | weapons |
|
27 | 50 | Instances of NPC should have all of the same properties as GameObject. |
28 | 51 |
|
29 | 52 | Example: |
| 53 | + */ |
30 | 54 |
|
31 | | - const hamsterHuey = new Humanoid({ |
32 | | - createdAt: new Date(), |
33 | | - dimensions: { |
34 | | - length: 2, |
35 | | - width: 1, |
36 | | - height: 1, |
37 | | - }, |
38 | | - hp: 5, |
39 | | - name: 'Hamster Huey', |
40 | | - faction: 'Gooey Kablooie', |
41 | | - weapons: [ |
42 | | - 'bubblegum', |
43 | | - ], |
44 | | - language: 'Hamsterish', |
45 | | - }); |
46 | | -
|
47 | | - hamsterHuey.greet(); // returns 'Hamster Huey offers a greeting in Hamsterish' |
48 | | - hamsterHuey.takeDamage(); // returns 'Hamster Huey took damage.' |
49 | | - hamsterHuey.destroy(); // returns 'Game object was removed from the game.' |
50 | | -*/ |
| 55 | +function Humanoid(options) { |
| 56 | + NPC.call(this, options); |
| 57 | + GameObject.call(this, options); |
| 58 | + this.faction = options.faction; |
| 59 | + this.weapons = options.weapons; |
| 60 | + this.language = options.language; |
| 61 | +} |
| 62 | + |
| 63 | +Humanoid.prototype = Object.create(NPC.prototype); |
| 64 | +Humanoid.prototype.greet = function greet() { |
| 65 | + return (`${this.name} offers a greeting in ${this.language}.`); |
| 66 | +}; |
| 67 | + |
| 68 | +const hamsterHuey = new Humanoid({ |
| 69 | + createdAt: new Date(), |
| 70 | + dimensions: { |
| 71 | + length: 2, |
| 72 | + width: 1, |
| 73 | + height: 1, |
| 74 | + }, |
| 75 | + hp: 5, |
| 76 | + name: 'Hamster Huey', |
| 77 | + faction: 'Gooey Kablooie', |
| 78 | + weapons: [ |
| 79 | + 'bubblegum', |
| 80 | + ], |
| 81 | + language: 'Hamsterish', |
| 82 | +}); |
| 83 | + |
| 84 | +hamsterHuey.greet(); // returns 'Hamster Huey offers a greeting in Hamsterish' |
| 85 | +hamsterHuey.takeDamage(); // returns 'Hamster Huey took damage.' |
| 86 | +hamsterHuey.destroy(); // returns 'Game object was removed from the game.' |
51 | 87 |
|
52 | 88 | /* eslint-disable no-undef */ |
53 | 89 |
|
|
0 commit comments