|
1 | | -/* |
2 | | - Object oriented design is commonly used in video games. For this part of the assignment |
3 | | - you will be implementing several classes with their correct inheritance heirarchy. |
4 | | -
|
5 | | - In this file you will be creating three classes: |
6 | | - GameObject |
7 | | - createdAt |
8 | | - dimensions |
9 | | - destroy() // prototype method -> returns the string 'Game object was removed from the game.' |
10 | | -
|
11 | | - NPC |
12 | | - hp |
13 | | - name |
14 | | - takeDamage() // prototype method -> returns the string '<object name> took damage.' |
15 | | - // should inherit destroy() from GameObject's prototype |
16 | | -
|
17 | | - Humanoid |
18 | | - faction |
19 | | - weapons |
20 | | - language |
21 | | - greet() // prototype method -> returns the string '<object name> offers a greeting in <object language>.' |
22 | | - // should inherit destroy() from GameObject through NPC |
23 | | - // should inherit takeDamage() from NPC |
24 | | -
|
25 | | - Inheritance chain: Humanoid -> NPC -> GameObject |
26 | | - Instances of Humanoid should have all of the same properties as NPC and GameObject. |
27 | | - Instances of NPC should have all of the same properties as GameObject. |
28 | | -
|
29 | | - Example: |
30 | | -
|
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 | | - }); |
| 1 | +/* eslint-disable */ |
| 2 | + |
| 3 | +function GameObject(options) { |
| 4 | + this.createdAt = options.createdAt; |
| 5 | + this.dimensions = options.dimensions; |
| 6 | +} |
| 7 | +GameObject.prototype.destroy = function destroy() { |
| 8 | + return ('${this.name}was removed from the game'); |
| 9 | +}; |
46 | 10 |
|
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 | | -*/ |
| 11 | +function NPC(npcOptions) { |
| 12 | + GameObject.call(this, npcOptions); |
| 13 | + this.hp = npcOptions.hp; |
| 14 | + this.name = npcOptions.name; |
| 15 | +}; |
| 16 | +NPC.prototype = Object.create(GameObject.prototype); |
| 17 | +NPC.prototype.takedamage = function takeDamage() { |
| 18 | + return '${this.name}took damage.'; |
| 19 | +}; |
51 | 20 |
|
52 | | -/* eslint-disable no-undef */ |
| 21 | +function Humanoid(humOptions) { |
| 22 | + NPC.call(this, humOptions); |
| 23 | + this.faction = humOptions.faction; |
| 24 | + this.weapons = humOptions.weapons; |
| 25 | + this.language = humOptions.language; |
| 26 | +} |
| 27 | +Humanoid.prototype.greet = function greet() { |
| 28 | + return '${this.name}offers a greeting in`${this.language}'; |
| 29 | +} |
| 30 | + |
| 31 | +const Bill = 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 | +}); |
53 | 47 |
|
54 | 48 | module.exports = { |
55 | 49 | GameObject, |
|
0 commit comments