-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayer.js
69 lines (60 loc) · 2.04 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
import readlineSync from "readline-sync";
import { Item } from "./item.js";
export class Player {
constructor(name = "Player", health = 10) {
this.name = name;
this.health = health;
this.inventory = [];
this.position = [1, 1];
}
move(direction, movement) {
this.position[direction] += movement;
if (!this.validatePosition()) {
console.log("You are at the edge of the map. Teleporting you to a random position!");
this.position = [...Array(2)].map(() => Math.floor(Math.random() * 4));
}
}
validatePosition() {
return (this.position[0] >= 0 && this.position[0] <= 3) && (this.position[1] >= 0 && this.position[1] <= 3);
}
addHealth(amount) {
if (typeof amount !== "undefined") {
this.health += amount
console.log(`You have added ${amount} health.`);
}
}
takeDamage(amount) {
if (typeof amount !== "undefined") {
this.health -= amount;
console.log(`You have taken ${amount} damage.`);
}
}
addToInventory(item) {
if (typeof item !== "undefined") {
this.inventory.push(item);
console.log(`${item.name} added to your inventory.`)
}
}
useItem(itemName, callback) {
let item = this.inventory.filter(i => {
return i.name.toLowerCase() === itemName.toLowerCase();
})[0] // only want the first item
if (item === undefined) {
console.log(`You do not have a ${itemName}.`);
} else {
this.inventory = this.inventory.filter(i => i !== item);
let outcome = { "health": item.healthBoost };
callback(outcome);
}
}
getInventoryString() {
let inventoryString = "";
if (this.inventory.length !== 0) {
inventoryString += "Inventory:\n";
for (let item of this.inventory) {
inventoryString += item.name + "\n";
}
}
return inventoryString;
}
}