-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.js
105 lines (97 loc) · 3.41 KB
/
player.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const { Room } = require('./engine');
class Player {
/**
* @description Creates a new player
* @param {Engine} game The game object
* @param {String} name Name of player
*/
constructor(game, name) {
this.game = game;
this.name = name;
this.health = 100;
this.energy = 100;
this.weapon;
this.inventory = [];
}
/**
* @param {Room} room Room to pickup item from
* @param {String} item Item to be picked up
* @returns {{'type': string, 'name': string} | null} Returns the
*/
pickup(room, item) {
let isPickedUp = false;
let itemIndex = -1;
for (let i = 0; i < room.collectables.length; i++) {
if (item === room.collectables[i].name) {
isPickedUp = true;
itemIndex = i;
}
}
if (isPickedUp) {
this.inventory.push(room.collectables[itemIndex]);
let item = room.collectables.splice(itemIndex, 1)[0];
return item;
} else {
return null;
}
}
/**
* @param {Room} room Room to look in
* @param {String} action What to look for
* @returns {String} Returns output text
*/
look(room, action) {
let text = "";
switch (action) {
case "exit":
text = "There seems to be an exit to the ";
if (room.exits.length > 0) {
for (let i = 0; i < room.exits.length; i++) {
if (i === 0) {
text += room.exits[i].direction;
} else if (i !== room.exits.length) {
text += ' and ' + room.exits[i].direction;
} else {
text += ', ' + room.exits[i].direction;
}
}
} else {
text = "There are no exits to be seen.";
}
return text;
case "items":
text = "There is a ";
if (room.collectables.length === 1) {
text += room.collectables[0].name;
return text;
} else if (room.collectables.length > 0) {
for (let i = 0; i < room.collectables.length; i++) {
if (i === 0) {
text += room.collectables[i].name;
} else if (i !== room.collectables.length) {
text += ' and a ' + room.collectables[i].name;
} else {
text += ', ' + room.collectables[i].name;
}
}
return text;
} else {
return "There are no items.";
}
case "around":
if (room.collectables.length >= 1) {
if (room.exits.length >= 1) {
text += "I can see some items and an exit";
} else {
text += "I can see some items laying around";
}
} else if (room.exits.length >= 1) {
text += "I can see an exit";
} else {
text += "I don't see antthing!";
}
return text;
}
}
}
module.exports = Player;