|
6 | 6 | GameObject |
7 | 7 | createdAt |
8 | 8 | dimensions |
9 | | - destroy() // prototype method -> returns the string 'Game object was removed from the game.' |
| 9 | + destroy() // prototype method -> returns the string 'Game object was removed from the game.'*/ |
10 | 10 |
|
| 11 | + function GameObject (gameobj) { |
| 12 | + this.createdAt = gameobj.createdAt; |
| 13 | + this.dimensions = gameobj.dimensions; |
| 14 | + } |
| 15 | + |
| 16 | + GameObject.prototype.distroy = function(){ |
| 17 | + console.log('Game object was removed from the game'); |
| 18 | + } |
| 19 | +/* |
11 | 20 | NPC |
12 | 21 | hp |
13 | 22 | name |
14 | 23 | takeDamage() // prototype method -> returns the string '<object name> took damage.' |
15 | 24 | // should inherit destroy() from GameObject's prototype |
| 25 | +*/ |
| 26 | + function NPC(npcObj){ |
| 27 | + GameObject.call(this,npcObj); |
| 28 | + this. hp = npcObj.hp; |
| 29 | + this.name = npcObj.name; |
| 30 | + } |
16 | 31 |
|
| 32 | + NPC.prototype.takeDamage = function(){ |
| 33 | + console.log(`${this.name} took damage`); |
| 34 | + } |
| 35 | +/* |
17 | 36 | Humanoid |
18 | 37 | faction |
19 | 38 | weapons |
20 | 39 | language |
21 | 40 | greet() // prototype method -> returns the string '<object name> offers a greeting in <object language>.' |
22 | 41 | // should inherit destroy() from GameObject through NPC |
23 | 42 | // should inherit takeDamage() from NPC |
| 43 | +*/ |
| 44 | + function Humanoid (humanoidObj){ |
| 45 | + GameObject.call(this.humanoidObj); |
| 46 | + this.faction = humanoidObj.faction; |
| 47 | + this.weapons = humanoidObj.weapons; |
| 48 | + this.language = humanoidObj.language |
| 49 | + } |
24 | 50 |
|
| 51 | + Humanoid.prototype.greet = function(){ |
| 52 | + console.log(`${this.name} offers greetings in ${this.language}`); |
| 53 | + } |
| 54 | +/* |
25 | 55 | Inheritance chain: Humanoid -> NPC -> GameObject |
26 | 56 | Instances of Humanoid should have all of the same properties as NPC and GameObject. |
27 | 57 | Instances of NPC should have all of the same properties as GameObject. |
28 | 58 |
|
29 | 59 | Example: |
30 | | -
|
| 60 | +*/ |
31 | 61 | const hamsterHuey = new Humanoid({ |
32 | 62 | createdAt: new Date(), |
33 | 63 | dimensions: { |
|
47 | 77 | hamsterHuey.greet(); // returns 'Hamster Huey offers a greeting in Hamsterish' |
48 | 78 | hamsterHuey.takeDamage(); // returns 'Hamster Huey took damage.' |
49 | 79 | hamsterHuey.destroy(); // returns 'Game object was removed from the game.' |
50 | | -*/ |
| 80 | + |
| 81 | + |
51 | 82 |
|
52 | 83 | /* eslint-disable no-undef */ |
53 | 84 |
|
|
0 commit comments