|
1 | 1 | /* |
2 | 2 | Object oriented design is commonly used in video games. For this part of the assignment |
3 | 3 | you will be implementing several classes with their correct inheritance heirarchy. |
4 | | -
|
5 | 4 | In this file you will be creating three classes: |
6 | 5 | GameObject |
7 | 6 | createdAt |
8 | 7 | dimensions |
9 | 8 | destroy() // prototype method -> returns the string 'Game object was removed from the game.' |
| 9 | + */ |
| 10 | + function GameObject(object) { |
| 11 | + this.createdAt = object.createdAt; |
| 12 | + this.dimensions = object.dimensions; |
| 13 | + } |
| 14 | + GameObject.prototype.destroy = () => { |
| 15 | + return 'Game object was removed from the game.'; |
| 16 | + }; |
10 | 17 |
|
| 18 | + /* |
11 | 19 | NPC |
12 | 20 | hp |
13 | 21 | name |
14 | 22 | takeDamage() // prototype method -> returns the string '<object name> took damage.' |
15 | 23 | // should inherit destroy() from GameObject's prototype |
| 24 | + */ |
| 25 | + function NPC(npc) { |
| 26 | + GameObject.call(this, npc); |
| 27 | + this.hp = npc.hp; |
| 28 | + this.name = npc.name; |
| 29 | + // GameObject.call(this, options); |
| 30 | + } |
16 | 31 |
|
| 32 | + NPC.prototype = Object.create(GameObject.prototype); |
| 33 | + NPC.prototype.takeDamage = function () { |
| 34 | + return `${this.name} took damage.`; |
| 35 | + }; |
| 36 | + /* |
17 | 37 | Humanoid |
18 | 38 | faction |
19 | 39 | weapons |
20 | 40 | language |
21 | 41 | greet() // prototype method -> returns the string '<object name> offers a greeting in <object language>.' |
22 | 42 | // should inherit destroy() from GameObject through NPC |
23 | | - // should inherit takeDamage() from NPC |
| 43 | + // should inherit takeDamage() from NPC */ |
24 | 44 |
|
| 45 | + function Humanoid(player) { |
| 46 | + NPC.call(this, player); |
| 47 | + GameObject.call(this, player); |
| 48 | + this.faction = player.faction; |
| 49 | + this.weapons = player.weapons; |
| 50 | + this.language = player.language; |
| 51 | + } |
| 52 | + // Humanoid.prototype = Object.create(GameObject.prototype); |
| 53 | + Humanoid.prototype = Object.create(NPC.prototype); |
| 54 | + Humanoid.prototype.greet = function () { |
| 55 | + return `${this.name} offers a greeting in ${this.language}.`; |
| 56 | + }; |
| 57 | + /* |
25 | 58 | Inheritance chain: Humanoid -> NPC -> GameObject |
26 | 59 | Instances of Humanoid should have all of the same properties as NPC and GameObject. |
27 | 60 | Instances of NPC should have all of the same properties as GameObject. |
28 | | -
|
29 | 61 | Example: |
30 | 62 |
|
31 | 63 | const hamsterHuey = new Humanoid({ |
|
51 | 83 |
|
52 | 84 | /* eslint-disable no-undef */ |
53 | 85 |
|
54 | | -module.exports = { |
55 | | - GameObject, |
56 | | - NPC, |
57 | | - Humanoid, |
58 | | -}; |
| 86 | + module.exports = { |
| 87 | + GameObject, |
| 88 | + NPC, |
| 89 | + Humanoid, |
| 90 | + }; |
0 commit comments