-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.js
141 lines (128 loc) · 4.24 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// create player object
var player = {
name: "",
items: [],
currLoc: map.locations[0],
health: 100
};
// add methods to player object
player.take = function(item) {
if(this.currLoc.has(item)){
this.currLoc.remove(item);
this.items.push(item);
displayFeedback("You took the "+item);
} else {
displayFeedback("There is no " + item + " here.");
}
};
//return an item to the location
player.drop = function (item){
if (player.has(item)){
this.items.splice(item, 1);
this.currLoc.put(item);
displayFeedback ("You dropped the "+item);
} else {
displayFeedback ("You don't have this item!");
}
};
//helper function for goto-------------------------------------------------
function indexOfLoc (locName) {
var locNum;
for (var i = 0; i < map.locations.length; i++){
if (map.locations[i].name === locName){
locNum = i;
}
}
if (locNum >=0) {
return locNum;
} else {
displayFeedback ("This location does not exist.");
}
};
//--------------------------------------------------
player.goto = function (locName) {
var nextLocNum = indexOfLoc(locName);
var currLoc = player.currLoc;
var currLocNum = indexOfLoc (player.currLoc.name);
if (player.currLoc.name === locName) {
displayFeedback ("You are already here.");
}
else if (map.connections[currLocNum][nextLocNum] === 0) {
displayFeedback ("These locations are not connected.");
}
else if(map.locations[nextLocNum].enemy){
// RANDOM EVENT --> DEATH
var r = Math.random ();
if (r<0.5) {
player.currLoc = map.locations[nextLocNum];
displayScene ();
clearContent(document.querySelector("label"));
healthPoints(100);
displayFeedback("The Madeline you saw was a ghost! The real one lies in the coffin! Oh no, the ghost reappears and kills you! You can start again, find a weapon, and hope the ghost will not be so fast.");
gameEnd();
}else{
player.currLoc = map.locations[nextLocNum];
displayScene ();
displayFeedback("The Madeline you saw was a ghost! The real one lies in the coffin! Oh no, the ghost reappears and start chasing you. Do you have a weapon to use?");
}
}
else if(map.locations[nextLocNum].locked){
displayFeedback("This room is locked. Find the key.");
}
else if(map.locations[nextLocNum].secret){
if (nextLocNum === 2) {
player.currLoc = map.locations[nextLocNum];
displayScene ();
displayFeedback("A raven stands on top of the fireplace, saying: 'I must conquer the Conqueror Worm!'");
}
else if (nextLocNum === 6) {
displayFeedback ("Oh my God... What is this terrible sound? Sounds like a beating heart.");
}
} else {
player.currLoc = map.locations[nextLocNum];
displayFeedback (" ");
}
};
//function to check if player has an item
player.has = function (item) {
var itemNum = this.items.indexOf(item);
for (i in this.items) {
if (itemNum >= 0) {
return true;
}
}
};
//function for the instances where items are used
player.use = function (item) {
for (i in this.items) {
if (this.items[i] === item) {
if (player.currLoc === map.locations[2] && player.has("worm")) {
displayFeedback ("A-ha! I am stronger than you all! Now go away and don't come back, " + player.name + ". Nevermore!");
}
else if (player.currLoc === map.locations[7] && player.has("sword")) {
healthPoints(20);
displayFeedback ("Yes! You defeated the ghost.You only lost 20 health points.");
}
} else {
displayFeedback ("You don't have this item!");
}
}
};
//unlocking rooms
player.unlock = function(locName) {
var locNum = indexOfLoc(locName);
for(var i = 0; i < this.items.length; i ++) {
token = this.items[i].split(" ");
if(token[0] === locName){
map.locations[locNum].locked = false;
displayFeedback ("You've unlocked the room, now you can go there.");
}
}
};
//function for taking player's health points
function healthPoints(n){
var newHealth = player.health - n;
var healthDisplay = document.querySelector("#health > ul");
clearContent(healthDisplay);
healthDisplay.textContent = newHealth;
}