-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction-parser.js
154 lines (147 loc) · 7.77 KB
/
action-parser.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* Reads users action input and outputs accordingly
* @param {KeyboardEvent} e Listens for keypress
* @param {ClickEvent} e Listens for click
*/
function parseUserInput(e) {
if (e.charCode === 13 || e.type === 'click') {
let userAction = document.querySelector('input'),
userActionInput = userAction.value,
noSpaceString = removeSpaceFromString(userActionInput),
checkFirstFourLetters = noSpaceString.substring(0, 4),
checkStringEnding = noSpaceString.substring(4);
getInstructions = document.querySelector('.instructions-grid')
shouldBuildInventory = false
if (getLastPhaseGrid.style.display === 'grid') {
if (noSpaceString === 'alive') {
userActionInput = 'It clicked!'
activateEnding()
}
else {
userActionInput += " - No, that doesn't work..."
}
}
else {
if (noSpaceString === 'instructions') {
if (getInstructions.style.display === 'grid') {
userActionInput += " - Closing instructions..."
getInstructions.style.display = 'none'
}
else {
userActionInput += " - Showing instructions..."
getInstructions.style.display = 'grid'
}
}
else if (noSpaceString === 'location') {
userActionInput += " - You can find the available locations to the right."
}
else if (checkFirstFourLetters === 'look' || checkFirstFourLetters === 'take' || (checkFirstFourLetters === 'chop' && player.inventory.includes('axe'))) {
if (noSpaceString.length === 4) {
userActionInput += ' - Type "' + userActionInput + '" followed by an item in the room that you want to interact with.'
}
else {
let checkIfItem = false,
itemNameIndex
for (i = 0; i < currentLocation.items.length; i++) {
if (checkStringEnding === currentLocation.items[i]) {
itemNameIndex = i
checkIfItem = true
}
}
if (checkIfItem) {
const selectedItem = currentLocation[checkStringEnding]
if (checkFirstFourLetters === 'look') {
if (checkStringEnding === 'chest') {
if (player.inventory.length === 4) {
userActionInput += " - I think I have what I need to open this chest"
activateLastPhase()
}
else {
userActionInput += ' - ' + selectedItem['lookDescription']
}
}
else if ('contains' in selectedItem) {
let shouldDropItem = currentLocation.items.includes(selectedItem['contains']),
itemInInventory = player.inventory.includes(selectedItem['contains'])
if (shouldDropItem === false && itemInInventory === false) {
currentLocation.items.push(selectedItem['contains'])
userActionInput += ' - ' + selectedItem['lookDescription']
loadItems();
}
else if (itemInInventory === true) {
userActionInput += ' - I already did that'
}
else {
userActionInput += ' - ' + selectedItem['dropDescription']
}
}
else {
userActionInput += ' - ' + selectedItem['lookDescription']
}
}
else if (checkFirstFourLetters === 'take' || checkFirstFourLetters === 'chop') {
if ((checkFirstFourLetters === 'take' && selectedItem['canBeTake'] === false) || (checkFirstFourLetters === 'chop' && selectedItem['canBeChop'] === false) ) {
if (checkFirstFourLetters === 'take' && 'canBeTakeReason' in selectedItem) {
userActionInput +=' - ' + selectedItem['canBeTakeReason']
}
else if (checkFirstFourLetters === 'chop' && 'canBeChopReason' in selectedItem) {
userActionInput +=' - ' + selectedItem['canBeChopReason']
}
else {
userActionInput += " - Don't see why I should..."
}
}
else {
if (checkFirstFourLetters === 'take') {
userActionInput += ' - Took ' + checkStringEnding
shouldBuildInventory = true
if (checkStringEnding === 'axe') {
updateListElements()
}
}
else {
if (checkStringEnding === 'door') {
unlockNewLocations(checkStringEnding)
}
else if (checkStringEnding === 'boxes') {
currentLocation.items.push(selectedItem['contains'])
loadItems()
}
userActionInput += ' - Chopped ' + checkStringEnding + ' - ' + selectedItem['canBeChopReason']
}
loadItems(itemNameIndex, currentLocation['items'], checkStringEnding, shouldBuildInventory)
}
}
}
else {
userActionInput += " - Can't find that item in the room..."
}
}
}
else if (checkFirstFourLetters === 'move') {
if (noSpaceString.length === 4) {
userActionInput += ' - Type "move" and where you want to go. Locations are to the right.'
}
else if (checkStringEnding === player.location[1]) {
userActionInput += " - You're already there"
}
else {
if (player.acceptedLocations.includes(checkStringEnding)) {
updateLocationGraphic(checkStringEnding)
userActionInput += " - You went to the " + player.location[1]
}
else {
userActionInput += " - Invalid location..."
}
}
}
else {
userActionInput += ' - Invalid input...'
}
}
userAction.value = ""
userAction.focus()
newCommand = false
updateListElements(userActionInput)
}
}